public inbox for git-commits@fedoraproject.org
help / color / mirror / Atom feed
From: Benjamin A. Beasley <code@musicinmybrain.net>
To: git-commits@fedoraproject.org
Subject: [rpms/python-pydantic-settings] f45: Backport a security fix
Date: Fri, 13 Nov 2026 14:36:48 GMT	[thread overview]
Message-ID: <179458060824.1.2933627871367851205.rpms-python-pydantic-settings-4738d02ca27f@fedoraproject.org> (raw)

            A new commit has been pushed.

            Repo   : rpms/python-pydantic-settings
            Branch : f45
            Commit : 4738d02ca27f0644d02fbd21c76883b4e8e9540c
            Author : Benjamin A. Beasley <code@musicinmybrain.net>
            Date   : 2026-11-13T14:35:15+00:00
            Stats  : +188/-0 in 2 file(s)
            URL    : https://src.fedoraproject.org/rpms/python-pydantic-settings/c/4738d02ca27f0644d02fbd21c76883b4e8e9540c?branch=f45

            Log:
            Backport a security fix

- Prevent SecretsSettingsSource from following symlinks outside secrets_dir

---
diff --git a/0001-Prevent-SecretsSettingsSource-from-following-symlink.patch b/0001-Prevent-SecretsSettingsSource-from-following-symlink.patch
new file mode 100644
index 0000000..250d4e3
--- /dev/null
+++ b/0001-Prevent-SecretsSettingsSource-from-following-symlink.patch
@@ -0,0 +1,175 @@
+From 1b0a3df016736fe0a33d906eb06e65f118f3d393 Mon Sep 17 00:00:00 2001
+From: Twot <nguyenngoctinh011258@gmail.com>
+Date: Fri, 13 Nov 2026 17:58:02 +0700
+Subject: [PATCH] Prevent SecretsSettingsSource from following symlinks outside
+ secrets_dir (#1138)
+
+Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
+Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
+---
+ docs/index.md                                 |  4 +
+ .../sources/providers/secrets.py              | 21 ++++-
+ tests/test_settings.py                        | 91 +++++++++++++++++++
+ 3 files changed, 115 insertions(+), 1 deletion(-)
+
+diff --git a/docs/index.md b/docs/index.md
+index d008f26..16b58bb 100644
+--- a/docs/index.md
++++ b/docs/index.md
+@@ -2124,6 +2124,10 @@ class Settings(BaseSettings):
+ 
+ If any of `secrets_dir` is missing, it is ignored, and warning is shown. If any of `secrets_dir` is a file, error is raised.
+ 
++A secret file that is a symbolic link is read only if it resolves to a file inside its `secrets_dir`, as in the
++Kubernetes secret volume layout (`passwd` linking to `..data/passwd`). A link that resolves outside `secrets_dir` is
++skipped with a warning.
++
+ 
+ ### Use Case: Docker Secrets
+ 
+diff --git a/pydantic_settings/sources/providers/secrets.py b/pydantic_settings/sources/providers/secrets.py
+index df22acf..3288159 100644
+--- a/pydantic_settings/sources/providers/secrets.py
++++ b/pydantic_settings/sources/providers/secrets.py
+@@ -79,6 +79,9 @@ class SecretsSettingsSource(PydanticBaseEnvSettingsSource):
+             if not path.is_dir():
+                 raise SettingsError(f'secrets_dir must reference a directory, not a {path_type_label(path)}')
+ 
++        # resolved once here, used to check that every secret file stays inside its directory
++        self._resolved_secrets_paths = {path: path.resolve() for path in self.secrets_paths}
++
+         return super().__call__()
+ 
+     @classmethod
+@@ -124,9 +127,25 @@ class SecretsSettingsSource(PydanticBaseEnvSettingsSource):
+                         logger.debug('Secret file not found, skipping: %s', (secrets_path / env_name).resolve())
+                     continue
+ 
++                try:
++                    resolved_path = path.resolve()
++                except (OSError, RuntimeError) as e:
++                    # e.g. a symlink loop, which raises `RuntimeError` on Python < 3.13
++                    warnings.warn(
++                        f'attempted to load secret file "{path}" but it could not be resolved: {e}',
++                        stacklevel=4,
++                    )
++                    continue
++
+                 if path.is_file():
++                    if self._resolved_secrets_paths[secrets_path] not in resolved_path.parents:
++                        warnings.warn(
++                            f'attempted to load secret file "{path}" but it resolves outside of "{secrets_path}".',
++                            stacklevel=4,
++                        )
++                        continue
+                     if debug:
+-                        logger.debug('Loading secret file: %s', path.resolve())
++                        logger.debug('Loading secret file: %s', resolved_path)
+                     # Explicit encoding: `read_text()` would otherwise decode with the
+                     # locale encoding, so a UTF-8 secret is corrupted on a non-UTF-8
+                     # Windows code page (silently, when every byte happens to map).
+diff --git a/tests/test_settings.py b/tests/test_settings.py
+index b66fb34..933bfa0 100644
+--- a/tests/test_settings.py
++++ b/tests/test_settings.py
+@@ -2162,6 +2162,97 @@ def test_secrets_file_is_a_directory_multiple_one(tmp_path):
+     assert conf.foo == 'secret_value'  # value obtained from first directory
+ 
+ 
++def test_secrets_symlink_file_escaping_secrets_dir_is_ignored(tmp_path):
++    secrets_dir = tmp_path / 'secrets'
++    secrets_dir.mkdir()
++    outside = tmp_path / 'outside'
++    outside.mkdir()
++    outside.joinpath('passwd').write_text('outside-value')
++
++    link = secrets_dir / 'passwd'
++    try:
++        link.symlink_to(outside / 'passwd')
++    except OSError:
++        pytest.skip('symlinks not supported on this platform')
++
++    class Settings(BaseSettings):
++        model_config = SettingsConfigDict(secrets_dir=secrets_dir)
++
++        passwd: str | None = None
++
++    with pytest.warns(UserWarning, match='attempted to load secret file .* but it resolves outside of'):
++        assert Settings().model_dump() == {'passwd': None}
++
++
++def test_secrets_in_tree_symlink_is_followed(tmp_path):
++    secrets_dir = tmp_path / 'secrets'
++    secrets_dir.mkdir()
++    data_dir = secrets_dir / '..data'
++    data_dir.mkdir()
++    (data_dir / 'passwd').write_text('in-tree-value')
++
++    link = secrets_dir / 'passwd'
++    try:
++        link.symlink_to(data_dir / 'passwd')
++    except OSError:
++        pytest.skip('symlinks not supported on this platform')
++
++    class Settings(BaseSettings):
++        model_config = SettingsConfigDict(secrets_dir=secrets_dir)
++
++        passwd: str | None = None
++
++    assert Settings().model_dump() == {'passwd': 'in-tree-value'}
++
++
++def test_secrets_escaping_symlink_mocked_is_ignored(tmp_path, monkeypatch):
++    secrets_dir = tmp_path / 'secrets'
++    secrets_dir.mkdir()
++    file_path = secrets_dir / 'passwd'
++    file_path.write_text('outside-value')
++    outside_path = tmp_path / 'outside' / 'passwd'
++
++    orig_resolve = Path.resolve
++
++    def mock_resolve(self, *args, **kwargs):
++        if self == file_path:
++            return outside_path
++        return orig_resolve(self, *args, **kwargs)
++
++    monkeypatch.setattr(Path, 'resolve', mock_resolve)
++
++    class Settings(BaseSettings):
++        model_config = SettingsConfigDict(secrets_dir=secrets_dir)
++
++        passwd: str | None = None
++
++    with pytest.warns(UserWarning, match='attempted to load secret file .* but it resolves outside of'):
++        assert Settings().model_dump() == {'passwd': None}
++
++
++def test_secrets_resolve_error_is_skipped(tmp_path, monkeypatch):
++    secrets_dir = tmp_path / 'secrets'
++    secrets_dir.mkdir()
++    file_path = secrets_dir / 'passwd'
++    file_path.write_text('val')
++
++    orig_resolve = Path.resolve
++
++    def mock_resolve(self, *args, **kwargs):
++        if self == file_path:
++            raise OSError('mocked resolve error')
++        return orig_resolve(self, *args, **kwargs)
++
++    monkeypatch.setattr(Path, 'resolve', mock_resolve)
++
++    class Settings(BaseSettings):
++        model_config = SettingsConfigDict(secrets_dir=secrets_dir)
++        passwd: str | None = None
++
++    with pytest.warns(UserWarning, match='attempted to load secret file .* but it could not be resolved'):
++        assert Settings().passwd is None
++
++
+ def test_secrets_dotenv_precedence(tmp_path):
+     s = tmp_path / 'foo'
+     s.write_text('foo_secret_value_str')
+-- 
+2.56.1
+

diff --git a/python-pydantic-settings.spec b/python-pydantic-settings.spec
index 2b1e5b2..dd16883 100644
--- a/python-pydantic-settings.spec
+++ b/python-pydantic-settings.spec
@@ -17,6 +17,19 @@ Version:        2.15.0
 Release:        %autorelease
 Summary:        Settings management using pydantic
 
+# Prevent SecretsSettingsSource from following symlinks outside secrets_dir - #1138
+# https://github.com/pydantic/pydantic-settings/commit/e394a889d4f3b1332e26287325236fcfceee701e
+#
+# Fixes:
+#
+# SecretsSettingsSource follows a file symlink that resolves outside secrets_dir
+# https://github.com/pydantic/pydantic-settings/issues/1137
+#
+# See also https://github.com/pydantic/pydantic-settings/releases/tag/v2.16.3.
+#
+# Cherry-picked to v2.15.0.
+Patch:          0001-Prevent-SecretsSettingsSource-from-following-symlink.patch
+
 License:        MIT
 URL:            %{forgeurl}
 Source:         %{forgesource}

                 reply	other threads:[~2026-11-13 14:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179458060824.1.2933627871367851205.rpms-python-pydantic-settings-4738d02ca27f@fedoraproject.org \
    --to=code@musicinmybrain.net \
    --cc=git-commits@fedoraproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox