035 — SecretStr Policy for Sensitive Settings¶
Status: ACCEPTED Date: 2026-06-18 Extends: 011-source-spec-auth-provider.md
Context¶
Seven Settings fields hold credentials as str | None:
telegram_api_hashtelegram_bot_tokentelegram_proxy_passwordopenai_api_keyqdrant_api_keylangfuse_secret_keyvector_pgvector_password
Plain str means any of the following leaks the value:
Settings().model_dump()writes the real value into JSON, breaking log capture and config dump tooling.repr(settings)orstr(settings)includes the value.pprint/loggingoutput that calls__str__exposes the value.- Error tracebacks that print
locals()include the value.
ADR-011 separated SourceSpec from AuthProvider so secrets live outside
declarative YAML. The runtime contract is correct; the type-level contract is
not. Pydantic ships SecretStr exactly for this: __repr__ / __str__ show
SecretStr('**********'), get_secret_value() returns the real value at the
point of use, and model_dump(exclude=secrets_set()) keeps them out of
serialised output.
Decision¶
- All seven fields above are typed
SecretStr | None(wasstr | None). - Settings adds
model_config = SettingsConfigDict(hide_secrets_in_model_dump=True)if the pydantic-settings version supports it; otherwise amodel_dump_safe()helper on Settings excludes the seven field names explicitly. - Code that previously read e.g.
settings.openai_api_keynow callssettings.openai_api_key.get_secret_value()(orNoneif absent). Grep audit: infrastructure/llm/openai_provider.pyinfrastructure/sources/telegram.pyinfrastructure/observability/otel_setup.pyinfrastructure/stores/postgres.py(if DSN embeds password, split it)infrastructure/embeddings/openai_provider.py.env.dev.exampleand.env.prod.examplekeep the same variable names so existing deployments work without env-file edits. Pydantic-settings parsesSecretStrfrom plain env values.- New
tests/test_settings_secrets.pyasserts that: repr(Settings())contains no real secret value.Settings().model_dump(exclude_secrets=True)(or our wrapper) contains no real secret value.get_secret_value()returns the real value when called explicitly.
Consequences¶
- (+) Defense in depth: secrets are masked in repr/str/JSON even by accident.
- (+) Aligns with Pydantic v2 idiom and ADR-011's auth isolation contract.
- (-) Every consumer call site changes from
fieldtofield.get_secret_value(). Acceptable — there are seven fields and ~7 call sites. - (-) Library users who introspect
Settings().openai_api_keydirectly will see aSecretStrobject instead ofstr. Documented in CHANGELOG. - (-)
model_dump()now returnsSecretStrobjects; callers that JSON-encode directly needmodel_dump(mode="json", exclude=...)orget_secret_value()expansion. Tests and CLI tooling updated accordingly.