-
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.
- Loading branch information
1 parent
f196701
commit 9ecf75a
Showing
5 changed files
with
148 additions
and
126 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from . import defaults | ||
|
||
DIRECTIVES = set(defaults.POLICY_DEFINITIONS['default']) | ||
PSEUDO_DIRECTIVES = {d for d in DIRECTIVES if '_' in d} | ||
|
||
# used in setting_to_directive (enables deletion updates via None) | ||
no_value = object() | ||
|
||
|
||
def setting_to_directive(setting, prefix='CSP_', value=no_value): | ||
setting = setting[len(prefix):].lower() | ||
if setting not in PSEUDO_DIRECTIVES: | ||
setting = setting.replace('_', '-') | ||
assert setting in DIRECTIVES | ||
|
||
if value is not no_value: | ||
if isinstance(value, str): | ||
value = [value] | ||
return setting, value | ||
return setting | ||
|
||
|
||
def directive_to_setting(directive, prefix='CSP_'): | ||
setting = '{}{}'.format( | ||
prefix, | ||
directive.replace('-', '_').upper() | ||
) | ||
return setting |
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,46 @@ | ||
POLICIES = ['default'] | ||
|
||
UPDATE_TEMPLATE = 'default' | ||
|
||
EXCLUDE_URL_PREFIXES = () | ||
|
||
POLICY_DEFINITIONS = { | ||
'default': { | ||
# Fetch Directives | ||
'child-src': None, | ||
'connect-src': None, | ||
'default-src': ("'self'",), | ||
'script-src': None, | ||
'script-src-attr': None, | ||
'script-src-elem': None, | ||
'object-src': None, | ||
'style-src': None, | ||
'style-src-attr': None, | ||
'style-src-elem': None, | ||
'font-src': None, | ||
'frame-src': None, | ||
'img-src': None, | ||
'manifest-src': None, | ||
'media-src': None, | ||
'prefetch-src': None, | ||
'worker-src': None, | ||
# Document Directives | ||
'base-uri': None, | ||
'plugin-types': None, | ||
'sandbox': None, | ||
# Navigation Directives | ||
'form-action': None, | ||
'frame-ancestors': None, | ||
'navigate-to': None, | ||
# Reporting Directives | ||
'report-uri': None, | ||
'report-to': None, | ||
'require-sri-for': None, | ||
# Other Directives | ||
'upgrade-insecure-requests': False, | ||
'block-all-mixed-content': False, | ||
# Pseudo Directives | ||
'report_only': False, | ||
'include_nonce_in': ('default-src',), | ||
} | ||
} |
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,47 @@ | ||
import warnings | ||
|
||
from django.conf import settings | ||
|
||
from . import ( | ||
setting_to_directive, | ||
directive_to_setting, | ||
DIRECTIVES, | ||
) | ||
|
||
CHILD_SRC_DEPRECATION_WARNING = ( | ||
'child-src is deprecated in CSP v3. Use frame-src and worker-src.' | ||
) | ||
|
||
LEGACY_SETTINGS_NAMES_DEPRECATION_WARNING = ( | ||
'The following settings are deprecated: %s. ' | ||
'Use CSP_POLICY_DEFINITIONS and CSP_POLICIES instead.' | ||
) | ||
|
||
|
||
_LEGACY_SETTINGS = { | ||
directive_to_setting(directive) for directive in DIRECTIVES | ||
} | ||
|
||
|
||
def _handle_legacy_settings(definitions, defer_to_legacy=True): | ||
legacy_names = ( | ||
_LEGACY_SETTINGS | ||
& set(s for s in dir(settings) if s.startswith('CSP_')) | ||
) | ||
if not legacy_names: | ||
return | ||
|
||
warnings.warn( | ||
LEGACY_SETTINGS_NAMES_DEPRECATION_WARNING % ', '.join(legacy_names), | ||
DeprecationWarning, | ||
) | ||
|
||
csp = definitions['default'] | ||
legacy_csp = ( | ||
setting_to_directive(name, value=getattr(settings, name)) | ||
for name in legacy_names | ||
) | ||
if defer_to_legacy: | ||
csp.update(legacy_csp) | ||
else: | ||
csp.update((key, val) for key, val in legacy_csp if key not in csp) |
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