-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1081 rh UI implement security middleware and security policies (#7)
* Security policy * Fixed security headers setup * Code review changes * Removed commented out code * Removded HTTPS toggle * Added docstring to headder setting method * Fixed integration test
- Loading branch information
1 parent
f0483f1
commit 4838420
Showing
10 changed files
with
115 additions
and
15 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from flask import Blueprint | ||
CSP = { | ||
'default-src': [ | ||
"'self'", | ||
'https://cdn.ons.gov.uk', | ||
], | ||
'font-src': [ | ||
"'self'", | ||
'data:', | ||
'https://cdn.ons.gov.uk', | ||
], | ||
'script-src': [ | ||
"'self'", | ||
'https://*.googletagmanager.com', | ||
'https://cdn.ons.gov.uk', | ||
], | ||
'connect-src': [ | ||
"'self'", | ||
'https://cdn.ons.gov.uk', | ||
'https://*.google-analytics.com', | ||
'https://*.analytics.google.com', | ||
'https://*.googletagmanager.com' | ||
], | ||
'img-src': [ | ||
"'self'", | ||
'data:', | ||
'https://*.google-analytics.com', | ||
'https://*.googletagmanager.com', | ||
'https://cdn.ons.gov.uk' | ||
], | ||
} | ||
|
||
PERMISSION_POLICY = ('accelerometer=(),autoplay=(),camera=(),display-capture=(),document-domain=(),' | ||
'encrypted-media=(),fullscreen=(),geolocation=(),gyroscope=(),magnetometer=(),' | ||
'microphone=(),midi=(),payment=(),picture-in-picture=(),publickey-credentials-get=(),' | ||
'screen-wake-lock=(),sync-xhr=(self),usb=(),xr-spatial-tracking=()') | ||
|
||
# These headers were in use on the old RH-UI but are currently not able to be set via Talisman | ||
# and so require a different method to be set | ||
DEFAULT_RESPONSE_HEADERS = { | ||
'X-Frame-Options': 'DENY', | ||
'X-Permitted-Cross-Domain-Policies': 'None', | ||
'clear-site-data': '"storage"', | ||
'Cross-Origin-Opener-Policy': 'same-origin', | ||
'Cross-Origin-Resource-Policy': 'same-site', | ||
'Cache-Control': ['no-store', 'max-age=0'], | ||
'Server': 'Office For National Statistics', | ||
} | ||
|
||
security = Blueprint("security", __name__) | ||
|
||
|
||
def build_response_headers(): | ||
headers = {} | ||
for header, value in DEFAULT_RESPONSE_HEADERS.items(): | ||
if isinstance(value, dict): | ||
value = '; '.join([ | ||
f"{section} {' '.join(content)}" | ||
for section, content in value.items() | ||
]) | ||
elif not isinstance(value, str): | ||
value = ' '.join(value) | ||
headers[header] = value | ||
return headers | ||
|
||
|
||
@security.after_app_request | ||
def add_security_headers(resp): | ||
'''This is required to set extra headers that Talisman doesn't support''' | ||
resp.headers.extend(build_response_headers()) | ||
return resp |
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
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
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