-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:cisagov/XFD into CRASM_1073_fstr…
…ings_to_format
- Loading branch information
Showing
15 changed files
with
158 additions
and
84 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,60 +32,38 @@ | |
# Ensure apps are populated | ||
apps.populate(settings.INSTALLED_APPS) | ||
|
||
# Define the CSP policy | ||
CSP_POLICY = { | ||
"default-src": ["'self'"], | ||
"connect-src": [ | ||
"'self'", | ||
os.getenv("COGNITO_URL"), | ||
os.getenv("BACKEND_DOMAIN"), | ||
"https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js", | ||
], | ||
"frame-src": ["'self'", "https://www.dhs.gov/ntas/"], | ||
"img-src": [ | ||
"'self'", | ||
"data:", | ||
os.getenv("FRONTEND_DOMAIN"), | ||
"https://www.ssa.gov", | ||
"https://www.dhs.gov", | ||
"https://fastapi.tiangolo.com/img/favicon.png", | ||
], | ||
"object-src": ["'none'"], | ||
"script-src": [ | ||
"'self'", | ||
os.getenv("BACKEND_DOMAIN"), | ||
"https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js", | ||
"https://www.ssa.gov/accessibility/andi/fandi.js", | ||
"https://www.ssa.gov/accessibility/andi/andi.js", | ||
"https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js", | ||
"'sha256-QOOQu4W1oxGqd2nbXbxiA1Di6OHQOLQD+o+G9oWL8YY='", | ||
"https://www.dhs.gov", | ||
], | ||
"style-src": [ | ||
"'self'", | ||
"'unsafe-inline'", | ||
"https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css", | ||
], | ||
"frame-ancestors": ["'none'"], | ||
} | ||
|
||
|
||
def set_security_headers(response: Response): | ||
"""Apply security headers to the HTTP response.""" | ||
# Set Content Security Policy | ||
# Set Content Security Policy (CSP) | ||
csp_value = "; ".join( | ||
[ | ||
"{} {}".format(key, " ".join(map(str, value))) | ||
for key, value in CSP_POLICY.items() | ||
for key, value in settings.SECURE_CSP_POLICY.items() | ||
if isinstance(value, (list, tuple)) | ||
] | ||
) | ||
response.headers["Content-Security-Policy"] = csp_value | ||
response.headers["Strict-Transport-Security"] = "max-age=31536000" | ||
response.headers["X-XSS-Protection"] = "0" | ||
response.headers["X-Content-Type-Options"] = "nosniff" | ||
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" | ||
response.headers["Access-Control-Allow-Credentials"] = "true" | ||
|
||
# Set Strict-Transport-Security (HSTS) | ||
hsts_value = f"max-age={settings.SECURE_HSTS_SECONDS}" | ||
if settings.SECURE_HSTS_PRELOAD: | ||
hsts_value += "; preload" | ||
if settings.SECURE_HSTS_INCLUDE_SUBDOMAINS: | ||
hsts_value += "; includeSubDomains" | ||
response.headers["Strict-Transport-Security"] = hsts_value | ||
|
||
# Additional security headers | ||
response.headers["X-XSS-Protection"] = ( | ||
"1; mode=block" if settings.SECURE_BROWSER_XSS_FILTER else "0" | ||
) | ||
response.headers["X-Content-Type-Options"] = ( | ||
"nosniff" if settings.SECURE_CONTENT_TYPE_NOSNIFF else "" | ||
) | ||
response.headers["Cache-Control"] = settings.SECURE_CACHE_CONTROL | ||
response.headers["Access-Control-Allow-Credentials"] = ( | ||
"true" if settings.SECURE_ACCESS_CONTROL_ALLOW_CREDENTIALS else "false" | ||
) | ||
|
||
return response | ||
|
||
|
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 |
---|---|---|
|
@@ -136,3 +136,47 @@ | |
# SameSite policy to prevent CSRF via cross-origin requests | ||
SESSION_COOKIE_SAMESITE = "Lax" | ||
CSRF_COOKIE_SAMESITE = "Lax" | ||
|
||
# SECURITY CONFIGURATION | ||
SECURE_HSTS_SECONDS = 31536000 # Enable HSTS for 1 year | ||
SECURE_HSTS_PRELOAD = True | ||
SECURE_HSTS_INCLUDE_SUBDOMAINS = True | ||
SECURE_BROWSER_XSS_FILTER = True | ||
SECURE_CONTENT_TYPE_NOSNIFF = True | ||
SECURE_CACHE_CONTROL = "no-cache, no-store, must-revalidate" | ||
SECURE_CSP_POLICY = { | ||
"default-src": ["'self'"], | ||
"connect-src": [ | ||
"'self'", | ||
os.getenv("COGNITO_URL"), | ||
os.getenv("BACKEND_DOMAIN"), | ||
"https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js", | ||
], | ||
"frame-src": ["'self'", "https://www.dhs.gov/ntas/"], | ||
"img-src": [ | ||
"'self'", | ||
"data:", | ||
os.getenv("FRONTEND_DOMAIN"), | ||
"https://www.ssa.gov", | ||
"https://www.dhs.gov", | ||
"https://fastapi.tiangolo.com/img/favicon.png", | ||
], | ||
"object-src": ["'none'"], | ||
"script-src": [ | ||
"'self'", | ||
os.getenv("BACKEND_DOMAIN"), | ||
"https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js", | ||
"https://www.ssa.gov/accessibility/andi/fandi.js", | ||
"https://www.ssa.gov/accessibility/andi/andi.js", | ||
"https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js", | ||
"'sha256-QOOQu4W1oxGqd2nbXbxiA1Di6OHQOLQD+o+G9oWL8YY='", | ||
"https://www.dhs.gov", | ||
], | ||
"style-src": [ | ||
"'self'", | ||
"'unsafe-inline'", | ||
"https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css", | ||
], | ||
"frame-ancestors": ["'none'"], | ||
} | ||
SECURE_ACCESS_CONTROL_ALLOW_CREDENTIALS = True |
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
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
Oops, something went wrong.