Fix deployment security checks
cover Fix deployment security checks
The SecurityMiddleware was commented out/optional. We really should be enabling it all the time.
Security settings should come from the environment and we should always be using a proxy like nginx in production.
Tim Orling (2):
Enable SecurityMiddleware and XFrameOptionsMiddleware. Read security settings from the environment and expect a proxy.
Post/test.py | 11 +++++++++++ README | 5 ++++- docker/run-tests.sh | 7 ++++--- project/settings.py | 27 +++++++++++++++++++++------ project/settings.py.docker | 27 +++++++++++++++++++++------ 5 files changed, 61 insertions(+), 16 deletions(-)
base-commit: 752b7188d04c2fd0e2f32f0cd2dd437c43058d82
prerequisite-patch-id: 9c9d012e1163dd1d335531d0451657823f17eb64
prerequisite-patch-id: ddb4df3e54736ed25846f91d4918299e601f4071
prerequisite-patch-id: ab69f4aa494291611cbe0e916f18b07f4aeebe57
prerequisite-patch-id: ac2b802ea065536df3854c48e265f5508d201cff
prerequisite-patch-id: 81a7de0e9568848efc8c66d397abb5f56218db95
prerequisite-patch-id: cf32dcaef20903890a020211a35f46b2384207da
prerequisite-patch-id: 6920bc5c70071bbd42b55b08bfc2bff04133c3ab
prerequisite-patch-id: a72bee4356db80ccb45321967d18081961f60860
prerequisite-patch-id: a2285b682329ba20d838085588b7c6681a86d273
prerequisite-patch-id: c3980e46d49b00b419cf4a7c2bc32c346478a453
prerequisite-patch-id: 627f58daa68852066f56558f08b07ad4af7bef9f
Signature · 1 line
2.56.0
PATCH 1/2 Enable SecurityMiddleware and XFrameOptionsMiddleware.
Fixes check --deploy W001 and W002: adds nosniff, Referrer-Policy, Cross-Origin-Opener-Policy and X-Frame-Options: DENY. Use Django's middleware order, which drops a duplicate SessionMiddleware.
AI-Generated: Claude Opus 5.5
--- Post/test.py | 6 ++++++ project/settings.py | 7 +++---- project/settings.py.docker | 7 +++---- 3 files changed, 12 insertions(+), 8 deletions(-)
Post/test.py +6 -0
@@ -267,6 +267,12 @@ class CSPTest(TestCase): self.assertNotIn("unsafe-inline", csp) self.assertNotIn("unsafe-eval", csp) + def test_security_headers(self): + response = self.client.get('/Statistics/') + self.assertEqual(response['X-Frame-Options'], 'DENY') + self.assertEqual(response['X-Content-Type-Options'], 'nosniff') + self.assertEqual(response['Referrer-Policy'], 'same-origin') + def test_csp_report_only(self): with override_settings(CONTENT_SECURITY_POLICY_REPORT_ONLY=True): # New client so the middleware picks up the setting
project/settings.py +3 -4
@@ -132,15 +132,14 @@ STATICFILES_FINDERS = ( SECRET_KEY = 'changeme' MIDDLEWARE = [ - 'django.middleware.common.CommonMiddleware', + 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'Post.middleware.ContentSecurityPolicyMiddleware', - # Uncomment the next line for simple clickjacking protection: - # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'project.urls'
project/settings.py.docker +3 -4
@@ -126,15 +126,14 @@ STATICFILES_FINDERS = ( SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme') MIDDLEWARE = [ - 'django.middleware.common.CommonMiddleware', + 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'Post.middleware.ContentSecurityPolicyMiddleware', - # Uncomment the next line for simple clickjacking protection: - # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'project.urls'
Signature · 1 line
2.56.0
PATCH 2/2 Read security settings from the environment and expect a proxy.
In reply to Tim OrlingFixes the remaining check --deploy warnings, in both settings files. DEBUG, ALLOWED_HOSTS and SECRET_KEY (now required) come from the environment. Behind a TLS proxy that sets X-Forwarded-Proto, cookies are secure and HSTS is sent unless DEBUG. W008 (the proxy redirects) and W005/W021 (the domain owner's call) are silenced. run-tests.sh runs check --deploy so new warnings fail CI.
AI-Generated: Claude Opus 5.5
--- Post/test.py | 5 +++++ README | 5 ++++- docker/run-tests.sh | 7 ++++--- project/settings.py | 20 ++++++++++++++++++-- project/settings.py.docker | 20 ++++++++++++++++++-- 5 files changed, 49 insertions(+), 8 deletions(-)
Post/test.py +5 -0
@@ -273,6 +273,11 @@ class CSPTest(TestCase): self.assertEqual(response['X-Content-Type-Options'], 'nosniff') self.assertEqual(response['Referrer-Policy'], 'same-origin') + def test_hsts_only_over_https_from_proxy(self): + self.assertNotIn('Strict-Transport-Security', self.client.get('/Statistics/')) + response = self.client.get('/Statistics/', HTTP_X_FORWARDED_PROTO='https') + self.assertEqual(response['Strict-Transport-Security'], 'max-age=31536000') + def test_csp_report_only(self): with override_settings(CONTENT_SECURITY_POLICY_REPORT_ONLY=True): # New client so the middleware picks up the setting
README +4 -1
@@ -23,7 +23,10 @@ Setup instructions: 1. Initialise and activate a python virtual environment. `virtualenv venv && source ./venv/bin/activate` 2. Install the dependencies via `pip install -r requirements.txt` 3. Set DATABASES.default.ENGINE in settings.py to use the database engine of your choice. -4. Set a SECRET_KEY in settings.py +4. Set the SECRET_KEY environment variable, for example + `export SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")`. + Set DEBUG=1 for development, and ALLOWED_HOSTS to a comma-separated list of + the host names the site is served as (default: localhost,127.0.0.1). 5. Setup the database schema with the Django model. Run `python manage.py migrate`.
docker/run-tests.sh +4 -3
@@ -25,8 +25,9 @@ until docker exec $DB mysql -h127.0.0.1 -uroot -ptestingpw -e 'SELECT 1' >/dev/n sleep 1 done -# -Wa shows deprecation warnings, i.e. what breaks on the next Django. +# Fail on deployment warnings (hence the random key); -Wa shows deprecations. docker run --rm --network $NET \ - -e SECRET_KEY=ci-only \ + -e SECRET_KEY="$(head -c 48 /dev/urandom | base64)" \ -e MYSQL_DATABASE=errorreport -e MYSQL_USER=root -e MYSQL_PASSWORD=testingpw \ - "$IMAGE" python3 -Wa manage.py test --noinput Post + "$IMAGE" sh -c 'python3 manage.py check --deploy --fail-level WARNING && + python3 -Wa manage.py test --noinput Post'
project/settings.py +18 -2
@@ -6,7 +6,11 @@ import os -DEBUG = True +# DEBUG=1 for development; off by default. +DEBUG = os.environ.get('DEBUG', '').lower() in ('1', 'true', 'yes') + +# Comma-separated. +ALLOWED_HOSTS = [h for h in os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',') if h] ADMINS = ( # ('Your Name', '[email protected]'),
@@ -129,7 +133,7 @@ STATICFILES_FINDERS = ( ) # Make this unique, and don't share it with anybody. -SECRET_KEY = 'changeme' +SECRET_KEY = os.environ['SECRET_KEY'] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware',
@@ -209,6 +213,18 @@ LOGGING = { BUGZILLA_URL = 'https://bugzilla.yoctoproject.org' +# Expects a proxy that terminates TLS, redirects to HTTPS and sets +# X-Forwarded-Proto, overwriting the client's. +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +SESSION_COOKIE_SECURE = not DEBUG +CSRF_COOKIE_SECURE = not DEBUG +SECURE_HSTS_SECONDS = 0 if DEBUG else 31536000 +SILENCED_SYSTEM_CHECKS = [ + 'security.W008', # the proxy redirects to HTTPS + 'security.W005', # HSTS includeSubDomains and preload are + 'security.W021', # the domain owner's call +] + EMAIL_HOST = 'localhost' DEFAULT_FROM_EMAIL = '[email protected]'
project/settings.py.docker +18 -2
@@ -6,7 +6,11 @@ import os -DEBUG = True +# DEBUG=1 for development; off by default. +DEBUG = os.environ.get('DEBUG', '').lower() in ('1', 'true', 'yes') + +# Comma-separated. +ALLOWED_HOSTS = [h for h in os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',') if h] ADMINS = ( # ('Your Name', '[email protected]'),
@@ -123,7 +127,7 @@ STATICFILES_FINDERS = ( ) # Make this unique, and don't share it with anybody. -SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme') +SECRET_KEY = os.environ['SECRET_KEY'] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware',
@@ -203,6 +207,18 @@ LOGGING = { BUGZILLA_URL = 'https://bugzilla.yoctoproject.org' +# Expects a proxy that terminates TLS, redirects to HTTPS and sets +# X-Forwarded-Proto, overwriting the client's. +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +SESSION_COOKIE_SECURE = not DEBUG +CSRF_COOKIE_SECURE = not DEBUG +SECURE_HSTS_SECONDS = 0 if DEBUG else 31536000 +SILENCED_SYSTEM_CHECKS = [ + 'security.W008', # the proxy redirects to HTTPS + 'security.W005', # HSTS includeSubDomains and preload are + 'security.W021', # the domain owner's call +] + EMAIL_HOST = 'localhost' DEFAULT_FROM_EMAIL = '[email protected]'
Signature · 1 line
2.56.0