-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Django check for the old settings and update docs.
- Loading branch information
Showing
8 changed files
with
251 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
.tox | ||
dist | ||
build | ||
docs/_build |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.apps import AppConfig | ||
from django.core import checks | ||
|
||
from csp.checks import check_django_csp_lt_4_0 | ||
|
||
|
||
class CspConfig(AppConfig): | ||
name = "csp" | ||
|
||
def ready(self): | ||
checks.register(check_django_csp_lt_4_0, checks.Tags.security) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import json | ||
|
||
from django.conf import settings | ||
from django.core.checks import Error | ||
|
||
|
||
OUTDATED_SETTINGS = [ | ||
"CSP_CHILD_SRC", | ||
"CSP_CONNECT_SRC", | ||
"CSP_DEFAULT_SRC", | ||
"CSP_SCRIPT_SRC", | ||
"CSP_SCRIPT_SRC_ATTR", | ||
"CSP_SCRIPT_SRC_ELEM", | ||
"CSP_OBJECT_SRC", | ||
"CSP_STYLE_SRC", | ||
"CSP_STYLE_SRC_ATTR", | ||
"CSP_STYLE_SRC_ELEM", | ||
"CSP_FONT_SRC", | ||
"CSP_FRAME_SRC", | ||
"CSP_IMG_SRC", | ||
"CSP_MANIFEST_SRC", | ||
"CSP_MEDIA_SRC", | ||
"CSP_PREFETCH_SRC", | ||
"CSP_WORKER_SRC", | ||
"CSP_BASE_URI", | ||
"CSP_PLUGIN_TYPES", | ||
"CSP_SANDBOX", | ||
"CSP_FORM_ACTION", | ||
"CSP_FRAME_ANCESTORS", | ||
"CSP_NAVIGATE_TO", | ||
"CSP_REQUIRE_SRI_FOR", | ||
"CSP_REQUIRE_TRUSTED_TYPES_FOR", | ||
"CSP_TRUSTED_TYPES", | ||
"CSP_UPGRADE_INSECURE_REQUESTS", | ||
"CSP_BLOCK_ALL_MIXED_CONTENT", | ||
"CSP_REPORT_URI", | ||
"CSP_REPORT_TO", | ||
"CSP_INCLUDE_NONCE_IN", | ||
] | ||
|
||
|
||
def migrate_settings(): | ||
# This function is used to migrate settings from the old format to the new format. | ||
config = { | ||
"DIRECTIVES": {}, | ||
} | ||
REPORT_ONLY = False | ||
|
||
if hasattr(settings, "CSP_REPORT_ONLY"): | ||
REPORT_ONLY = settings.CSP_REPORT_ONLY | ||
|
||
if hasattr(settings, "CSP_EXCLUDE_URL_PREFIXES"): | ||
config["EXCLUDE_URL_PREFIXES"] = settings.CSP_EXCLUDE_URL_PREFIXES | ||
|
||
if hasattr(settings, "CSP_REPORT_PERCENTAGE"): | ||
config["REPORT_PERCENTAGE"] = round(settings.CSP_REPORT_PERCENTAGE * 100) | ||
|
||
for setting in OUTDATED_SETTINGS: | ||
if hasattr(settings, setting): | ||
directive = setting[4:].replace("_", "-").lower() | ||
value = getattr(settings, setting) | ||
if value: | ||
config["DIRECTIVES"][directive] = value | ||
|
||
return config, REPORT_ONLY | ||
|
||
|
||
def check_django_csp_lt_4_0(app_configs, **kwargs): | ||
check_settings = OUTDATED_SETTINGS + ["CSP_REPORT_ONLY", "CSP_EXCLUDE_URL_PREFIXES", "CSP_REPORT_PERCENTAGE"] | ||
if any(hasattr(settings, setting) for setting in check_settings): | ||
# Try to build the new config. | ||
config, REPORT_ONLY = migrate_settings() | ||
warning = ( | ||
"You are using django-csp < 4.0 settings. Please update your settings to use the new format.\n" | ||
"See https://django-csp.readthedocs.io/en/latest/migration-guide.html for more information.\n\n" | ||
"We have attempted to build the new CSP config for you based on your current settings:\n\n" | ||
f"CONTENT_SECURITY_POLICY{'_REPORT_ONLY' if REPORT_ONLY else ''} = " + json.dumps(config, indent=4, default=repr) | ||
) | ||
return [Error(warning, id="csp.E001")] | ||
|
||
return [] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django.test.utils import override_settings | ||
|
||
from csp.checks import check_django_csp_lt_4_0, migrate_settings | ||
|
||
|
||
@override_settings( | ||
CSP_REPORT_PERCENTAGE=0.25, | ||
CSP_EXCLUDE_URL_PREFIXES=["/admin/"], | ||
CSP_REPORT_ONLY=False, | ||
CSP_DEFAULT_SRC=["'self'", "example.com"], | ||
) | ||
def test_migrate_settings(): | ||
config, report_only = migrate_settings() | ||
assert config == { | ||
"REPORT_PERCENTAGE": 25, | ||
"EXCLUDE_URL_PREFIXES": ["/admin/"], | ||
"DIRECTIVES": {"default-src": ["'self'", "example.com"]}, | ||
} | ||
assert report_only is False | ||
|
||
|
||
@override_settings( | ||
CSP_REPORT_ONLY=True, | ||
CSP_DEFAULT_SRC=["'self'", "example.com"], | ||
CSP_SCRIPT_SRC=["'self'", "example.com", "'unsafe-inline'"], | ||
CSP_INCLUDE_NONCE_IN=["script-src"], | ||
) | ||
def test_migrate_settings_report_only(): | ||
config, report_only = migrate_settings() | ||
assert config == { | ||
"DIRECTIVES": { | ||
"default-src": ["'self'", "example.com"], | ||
"script-src": ["'self'", "example.com", "'unsafe-inline'"], | ||
"include-nonce-in": ["script-src"], | ||
} | ||
} | ||
assert report_only is True | ||
|
||
|
||
@override_settings( | ||
CSP_DEFAULT_SRC=["'self'", "example.com"], | ||
) | ||
def test_check_django_csp_lt_4_0(): | ||
errors = check_django_csp_lt_4_0(None) | ||
assert len(errors) == 1 | ||
error = errors[0] | ||
assert error.id == "csp.E001" | ||
assert "update your settings to use the new format" in error.msg | ||
|
||
|
||
def test_check_django_csp_lt_4_0_no_config(): | ||
assert check_django_csp_lt_4_0(None) == [] |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ Contents: | |
|
||
installation | ||
configuration | ||
migration-guide | ||
decorators | ||
nonce | ||
trusted_types | ||
|
This file contains 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
This file contains 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