From 89fcae75efc4e40d64548bce49b646109b2f8248 Mon Sep 17 00:00:00 2001 From: Zhuyuyangyy <193758752+Zhuyuyangyy@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:35:37 +0800 Subject: [PATCH] Ignore directories in secret repositories --- decouple.py | 5 ++++- tests/test_secrets.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/decouple.py b/decouple.py index 9873fc9..c95adef 100644 --- a/decouple.py +++ b/decouple.py @@ -178,7 +178,10 @@ def __init__(self, source='/run/secrets/'): ls = os.listdir(source) for file in ls: - with open(os.path.join(source, file), 'r') as f: + path = os.path.join(source, file) + if not os.path.isfile(path): + continue + with open(path, 'r') as f: self.data[file] = f.read() def __contains__(self, key): diff --git a/tests/test_secrets.py b/tests/test_secrets.py index 7d6185b..e3d2d60 100644 --- a/tests/test_secrets.py +++ b/tests/test_secrets.py @@ -36,3 +36,14 @@ def test_secret_repo_keyerror(): with pytest.raises(KeyError): repo['UndefinedKey'] + + +def test_secret_repository_ignores_subdirectories(tmpdir): + secret_dir = tmpdir.mkdir('nested') + secret_dir.join('not_a_secret').write('ignored') + tmpdir.join('db_password').write('world') + + repository = RepositorySecret(str(tmpdir)) + + assert repository['db_password'] == 'world' + assert 'nested' not in repository