feat(config): wrap credentials in SecretStr (audit S-4)
database_url, migration_database_url, mcp_auth_token and the mathpix app id/key are now pydantic SecretStr, so they render as '**********' in any repr/log/traceback instead of leaking the plaintext credential. Consumers (db.get_conn, cli.migrate, mcp._require_http_token, mathpix.extract_formulas) call .get_secret_value() at the point of use. Tests updated to the SecretStr type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -45,7 +45,7 @@ def migrate() -> None:
|
||||
from codex.db import apply_schema
|
||||
|
||||
settings = get_settings()
|
||||
url = settings.migration_database_url or settings.database_url
|
||||
url = (settings.migration_database_url or settings.database_url).get_secret_value()
|
||||
try:
|
||||
with psycopg.connect(url, row_factory=psycopg.rows.dict_row) as conn:
|
||||
apply_schema(conn)
|
||||
|
||||
@@ -9,7 +9,7 @@ from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
|
||||
from pydantic import AliasChoices, Field
|
||||
from pydantic import AliasChoices, Field, SecretStr
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
@@ -26,15 +26,15 @@ class Settings(BaseSettings):
|
||||
# ------------------------------------------------------------------
|
||||
# Database
|
||||
# ------------------------------------------------------------------
|
||||
database_url: str = Field(
|
||||
default="postgresql://researcher:change_me@localhost:5432/papers",
|
||||
database_url: SecretStr = Field(
|
||||
default=SecretStr("postgresql://researcher:change_me@localhost:5432/papers"),
|
||||
description=(
|
||||
"libpq-compatible connection string consumed by psycopg. "
|
||||
"Example: postgresql://user:pass@host:5432/dbname"
|
||||
),
|
||||
)
|
||||
|
||||
migration_database_url: str | None = Field(
|
||||
migration_database_url: SecretStr | None = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"Optional privileged connection string used by `codex migrate` to apply "
|
||||
@@ -148,7 +148,7 @@ class Settings(BaseSettings):
|
||||
# ------------------------------------------------------------------
|
||||
# F-09 Rich Parsing
|
||||
# ------------------------------------------------------------------
|
||||
mathpix_app_id: str | None = Field(
|
||||
mathpix_app_id: SecretStr | None = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"MathPix App ID for cloud formula extraction. "
|
||||
@@ -156,7 +156,7 @@ class Settings(BaseSettings):
|
||||
),
|
||||
)
|
||||
|
||||
mathpix_app_key: str | None = Field(
|
||||
mathpix_app_key: SecretStr | None = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"MathPix App Key for cloud formula extraction. "
|
||||
@@ -257,7 +257,7 @@ class Settings(BaseSettings):
|
||||
description="Bind port for HTTP transport (ignored for stdio).",
|
||||
)
|
||||
|
||||
mcp_auth_token: str | None = Field(
|
||||
mcp_auth_token: SecretStr | None = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"Bearer token required when mcp_transport='http'. "
|
||||
|
||||
@@ -36,7 +36,7 @@ def get_conn() -> Generator[psycopg.Connection[psycopg.rows.DictRow], None, None
|
||||
"""
|
||||
settings = get_settings()
|
||||
with psycopg.connect(
|
||||
settings.database_url,
|
||||
settings.database_url.get_secret_value(),
|
||||
row_factory=psycopg.rows.dict_row,
|
||||
) as conn:
|
||||
yield conn
|
||||
|
||||
@@ -294,12 +294,13 @@ def _require_http_token() -> str:
|
||||
from codex.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
if not settings.mcp_auth_token:
|
||||
token = settings.mcp_auth_token.get_secret_value() if settings.mcp_auth_token else ""
|
||||
if not token:
|
||||
raise RuntimeError(
|
||||
"HTTP transport requires MCP_AUTH_TOKEN to be set. "
|
||||
"Set the environment variable or add it to .env."
|
||||
)
|
||||
return settings.mcp_auth_token
|
||||
return token
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -302,8 +302,10 @@ def extract_formulas(
|
||||
"""
|
||||
settings = get_settings()
|
||||
|
||||
app_id = mathpix_app_id or settings.mathpix_app_id or ""
|
||||
app_key = mathpix_app_key or settings.mathpix_app_key or ""
|
||||
settings_id = settings.mathpix_app_id.get_secret_value() if settings.mathpix_app_id else ""
|
||||
settings_key = settings.mathpix_app_key.get_secret_value() if settings.mathpix_app_key else ""
|
||||
app_id = mathpix_app_id or settings_id
|
||||
app_key = mathpix_app_key or settings_key
|
||||
|
||||
if app_id and app_key:
|
||||
logger.info("Using MathPix backend for %s", pdf_path)
|
||||
|
||||
Reference in New Issue
Block a user