fix(security): validate credentials and prevent weak defaults#793
Merged
fix(security): validate credentials and prevent weak defaults#793
Conversation
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
5cba662 to
f20797b
Compare
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.
2f208d1 to
a2e809c
Compare
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)
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: 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.
7aa6604 to
b254ba1
Compare
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace hardcoded default credentials with sentinel values and add comprehensive validation to prevent insecure deployments.
Changes
CHANGE_ME_IN_ENV_FILEsentinel valuesSECRET_KEY,DB_PASSWORD, andDB_USERSECRET_KEY.env.examplewith clear instructions and secure placeholdersMIN_SECRET_KEY_LENGTHconstant to avoid magic numbersSecurity Impact
🔒 Prevents accidental production deployment with default credentials
Testing
app/config/settings.pyExample Error Messages
Users will see helpful error messages like: