Skip to content

fix(security): validate credentials and prevent weak defaults#793

Merged
seapagan merged 5 commits intomainfrom
security/validate-credentials
Dec 29, 2025
Merged

fix(security): validate credentials and prevent weak defaults#793
seapagan merged 5 commits intomainfrom
security/validate-credentials

Conversation

@seapagan
Copy link
Owner

@seapagan seapagan commented Dec 29, 2025

Summary

Replace hardcoded default credentials with sentinel values and add comprehensive validation to prevent insecure deployments.

Changes

  • ✅ Replace weak defaults in settings.py with CHANGE_ME_IN_ENV_FILE sentinel values
  • ✅ Add validators for SECRET_KEY, DB_PASSWORD, and DB_USER
  • ✅ Enforce minimum 32-character length for SECRET_KEY
  • ✅ Detect and reject known weak passwords and default values
  • ✅ Update .env.example with clear instructions and secure placeholders
  • ✅ Add comprehensive test coverage for all validators (100% coverage)
  • ✅ Extract MIN_SECRET_KEY_LENGTH constant to avoid magic numbers
  • ✅ Combine test files for better organization

Security Impact

🔒 Prevents accidental production deployment with default credentials

  • Application will fail to start if weak/default credentials are detected
  • Forces proper configuration before application startup
  • Provides clear error messages with remediation steps

Testing

  • ✅ 22 new/updated tests for all validators
  • ✅ 100% code coverage for app/config/settings.py
  • ✅ All 504 tests passing
  • ✅ Ruff linting clean
  • ✅ MyPy type checking passing

Example Error Messages

Users will see helpful error messages like:

======================================================================
SECURITY ERROR: SECRET_KEY is using a weak/default value!
======================================================================
Generate a strong key with one of these commands:
  openssl rand -hex 32
  python -c 'import secrets; print(secrets.token_hex(32))'

Then add it to your .env file:
  SECRET_KEY=your_generated_key_here
======================================================================

Replace hardcoded default credentials with sentinel values and add
comprehensive validation to prevent insecure deployments.

Changes:
- Replace weak defaults in settings.py with CHANGE_ME_IN_ENV_FILE
- Add validators for SECRET_KEY, DB_PASSWORD, and DB_USER
- Enforce minimum 32-character length for SECRET_KEY
- Detect and reject known weak passwords and default values
- Update .env.example with clear instructions and secure placeholders
- Add comprehensive test coverage for all validators (100% coverage)
- Extract MIN_SECRET_KEY_LENGTH constant to avoid magic numbers

Security impact:
- Prevents accidental production deployment with default credentials
- Forces proper configuration before application startup
- Provides clear error messages with remediation steps
@seapagan seapagan force-pushed the security/validate-credentials branch from 5cba662 to f20797b Compare December 29, 2025 13:54
Add GITHUB_ACTIONS environment checks to DB credential validators
to allow tests to run in CI without requiring .env file. Also add
test-safe SECRET_KEY to pytest-env configuration.

Changes:
- Skip DB_USER and DB_PASSWORD validation when GITHUB_ACTIONS is set
  (CI uses hardcoded database URL that bypasses Settings)
- Add SECRET_KEY to pytest.ini_options.env for test validation
- Keep SECRET_KEY validator strict (no CI bypass) for security
- Add 3 new tests to verify GITHUB_ACTIONS bypass behavior:
  * test_weak_db_password_accepted_in_ci
  * test_default_db_user_accepted_in_ci
  * test_secret_key_still_validated_in_ci

This allows CI to pass while maintaining strict validation locally.
All 25 validation tests now pass with 100% coverage of settings.py.
@seapagan seapagan force-pushed the security/validate-credentials branch from 2f208d1 to a2e809c Compare December 29, 2025 14:35
Remove GITHUB_ACTIONS environment checks from credential validators
to ensure validation always runs in both local and CI environments.
This properly tests security features instead of bypassing them.

Changes:
- Remove GITHUB_ACTIONS bypass from DB validators in settings.py
- Remove unused `import os` from settings.py
- Add SECRET_KEY, DB_USER, DB_PASSWORD, MAIL_FROM to GitHub Actions env
- Remove SECRET_KEY and MAIL_FROM from pytest-env (local tests need .env
anyway)
- Delete TestCIEnvironmentValidation class (3 tests no longer needed)
@codacy-production
Copy link

codacy-production bot commented Dec 29, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.00% (target: -1.00%) 100.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (420aefc) 1709 1709 100.00%
Head commit (1982dbb) 1736 (+27) 1736 (+27) 100.00% (+0.00%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#793) 35 35 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Update documentation to explicitly state that a valid .env file is
required to run tests locally. This was always needed for database
access, but is now also enforced by credential validators.
@seapagan seapagan force-pushed the security/validate-credentials branch from 7aa6604 to b254ba1 Compare December 29, 2025 15:09
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
@seapagan seapagan self-assigned this Dec 29, 2025
@seapagan seapagan added the security Security Fixes label Dec 29, 2025
@seapagan seapagan merged commit f2daeee into main Dec 29, 2025
17 checks passed
@seapagan seapagan deleted the security/validate-credentials branch December 29, 2025 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Security Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant