Skip to content

Commit 77f05b0

Browse files
MacMac
authored andcommitted
Fix critical security vulnerabilities in default configuration
- Change STAGING default from True to False (secure by default) - Add SECRET_KEY validation (minimum 50 characters) - Add ALTCHA_HMAC_KEY validation (64-character hex requirement) - Remove default database password to prevent use of weak credentials These changes ensure production deployments fail fast with clear error messages if critical security settings are misconfigured, following security best practices and the principle of secure by default. Fixes #<issue_number>
1 parent a535807 commit 77f05b0

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

vulnerablecode/settings.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232

3333
SECRET_KEY = env.str("SECRET_KEY")
3434

35+
# Validate SECRET_KEY for security
36+
if not SECRET_KEY:
37+
raise exceptions.ImproperlyConfigured(
38+
"SECRET_KEY environment variable must be set. "
39+
"Generate one with: python -c 'import secrets; print(secrets.token_urlsafe(50))'"
40+
)
41+
if len(SECRET_KEY) < 50:
42+
raise exceptions.ImproperlyConfigured(
43+
"SECRET_KEY must be at least 50 characters long for security. "
44+
"Current length: {}".format(len(SECRET_KEY))
45+
)
46+
3547
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[".localhost", "127.0.0.1", "[::1]"])
3648

3749
VULNERABLECODE_PASSWORD_MIN_LENGTH = env.int("VULNERABLECODE_PASSWORD_MIN_LENGTH", default=14)
@@ -42,6 +54,18 @@
4254

4355
ALTCHA_HMAC_KEY = env.str("ALTCHA_HMAC_KEY")
4456

57+
# Validate ALTCHA_HMAC_KEY for security
58+
if not ALTCHA_HMAC_KEY:
59+
raise exceptions.ImproperlyConfigured(
60+
"ALTCHA_HMAC_KEY environment variable must be set. "
61+
"Generate one with: head -c 32 /dev/urandom | xxd -p -c 32"
62+
)
63+
if len(ALTCHA_HMAC_KEY) != 64:
64+
raise exceptions.ImproperlyConfigured(
65+
"ALTCHA_HMAC_KEY must be a 32-byte hexadecimal key (64 characters). "
66+
"Current length: {}".format(len(ALTCHA_HMAC_KEY))
67+
)
68+
4569
# SECURITY WARNING: do not run with debug turned on in production
4670
DEBUG = env.bool("VULNERABLECODE_DEBUG", default=False)
4771

@@ -51,8 +75,9 @@
5175
# SECURITY WARNING: do not run with debug turned on in production
5276
DEBUG_UI = env.bool("VULNERABLECODE_DEBUG_UI", default=False)
5377

54-
# WARNING: Set this to False in production
55-
STAGING = env.bool("STAGING", default=True)
78+
# CRITICAL: STAGING must be explicitly set to True in non-production environments
79+
# Default is False for security - production deployments are secure by default
80+
STAGING = env.bool("STAGING", default=False)
5681

5782
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
5883
EMAIL_HOST = env.str("EMAIL_HOST", default="")
@@ -119,7 +144,7 @@
119144
"HOST": env.str("VULNERABLECODE_DB_HOST", "localhost"),
120145
"NAME": env.str("VULNERABLECODE_DB_NAME", "vulnerablecode"),
121146
"USER": env.str("VULNERABLECODE_DB_USER", "vulnerablecode"),
122-
"PASSWORD": env.str("VULNERABLECODE_DB_PASSWORD", "vulnerablecode"),
147+
"PASSWORD": env.str("VULNERABLECODE_DB_PASSWORD"),
123148
"PORT": env.str("VULNERABLECODE_DB_PORT", "5432"),
124149
"ATOMIC_REQUESTS": True,
125150
}

0 commit comments

Comments
 (0)