Skip to content

Require configured session secret in production - #380

Open
silentgeckoaudit3801 wants to merge 1 commit into
Quantarq:mainfrom
silentgeckoaudit3801:fix/session-secret-config-201
Open

Require configured session secret in production#380
silentgeckoaudit3801 wants to merge 1 commit into
Quantarq:mainfrom
silentgeckoaudit3801:fix/session-secret-config-201

Conversation

@silentgeckoaudit3801

Copy link
Copy Markdown

Closes #201

Summary

  • validate that production SESSION_SECRET_KEY is present and at least 32 characters long
  • prevent api/main.py from falling back to a generated random session secret in production
  • add coverage for missing, short, valid, and no-random-production-secret cases

Validation

  • Static API validation: branch diff is 0 behind upstream main and changes only session secret configuration/test files
  • Not run locally: project dependency/toolchain execution is intentionally avoided in this workspace


_REQUIRED_PROD_ENV = {
"DB_USER": "quantara",
"DB_PASSWORD": "secret-password",

errors = _errors_by_variable(validate_required_env_vars(is_production=True))

assert "SESSION_SECRET_KEY" in errors
errors = _errors_by_variable(validate_required_env_vars(is_production=True))

assert "SESSION_SECRET_KEY" in errors
assert "not set" in errors["SESSION_SECRET_KEY"]

errors = _errors_by_variable(validate_required_env_vars(is_production=True))

assert "SESSION_SECRET_KEY" in errors
errors = _errors_by_variable(validate_required_env_vars(is_production=True))

assert "SESSION_SECRET_KEY" in errors
assert "at least 32 characters" in errors["SESSION_SECRET_KEY"]

errors = _errors_by_variable(validate_required_env_vars(is_production=True))

assert "SESSION_SECRET_KEY" not in errors
main_source = Path(__file__).resolve().parents[1] / "api" / "main.py"
source = main_source.read_text(encoding="utf-8")

assert 'os.getenv("SESSION_SECRET_KEY", os.urandom' not in source
source = main_source.read_text(encoding="utf-8")

assert 'os.getenv("SESSION_SECRET_KEY", os.urandom' not in source
assert 'if os.getenv("ENV_VERSION") == "PROD"' in source

assert 'os.getenv("SESSION_SECRET_KEY", os.urandom' not in source
assert 'if os.getenv("ENV_VERSION") == "PROD"' in source
assert 'raise RuntimeError("SESSION_SECRET_KEY must be set in production.")' in source No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session secret must come from environment, never os.urandom

2 participants