Files
notifications-admin/tests/app/main/views/test_headers.py

48 lines
1.9 KiB
Python
Raw Normal View History

from re import search
2025-04-29 15:55:14 -07:00
2025-04-29 15:41:06 -07:00
from flask import current_app
2025-04-29 15:55:14 -07:00
def test_owasp_useful_headers_set(
client_request,
mocker,
2023-07-12 12:09:44 -04:00
mock_get_service_and_organization_counts,
):
2024-07-11 09:38:32 -07:00
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
client_request.logout()
response = client_request.get_response(".index")
2017-07-24 15:20:40 +01:00
assert response.headers["X-Frame-Options"] == "deny"
assert response.headers["X-Content-Type-Options"] == "nosniff"
csp = response.headers["Content-Security-Policy"]
2025-04-10 15:58:26 -04:00
assert search(r"frame-src.*https://www\.googletagmanager\.com", csp)
assert search(r"frame-ancestors 'none';", csp)
assert search(r"form-action 'self';", csp)
assert search(
2023-10-26 12:05:26 -07:00
r"script-src 'self' static\.example\.com 'unsafe-eval' https:\/\/js-agent\.new"
r"relic\.com https:\/\/gov-bam\.nr-data\.net https:\/\/www\.googletagmanager\."
2025-04-10 16:13:34 -07:00
r"com https:\/\/www\.google-analytics\.com https:\/\/dap\.digitalgov\.gov "
r"https:\/\/cdn\.socket\.io",
csp,
)
2025-04-10 16:13:34 -07:00
assert search(r"'nonce-[^']+';", csp)
2023-12-13 08:50:25 -05:00
assert search(
2025-04-29 15:41:06 -07:00
r"connect-src 'self' https:\/\/gov-bam\.nr-data\.net https:\/\/www\.google-analytics\.",
2023-12-13 08:50:25 -05:00
csp,
)
assert search(r"style-src 'self' static\.example\.com 'nonce-.*';", csp)
assert search(r"img-src 'self' static\.example\.com static-logos\.test\.com", csp)
2025-04-29 15:41:06 -07:00
api_host_name = current_app.config.get("API_HOST_NAME")
assert api_host_name is not None, f"API_HOST_NAME: {api_host_name} — is missing"
assert api_host_name in csp
if api_host_name.startswith("http://"):
assert api_host_name.replace("http://", "ws://") in csp
elif api_host_name.startswith("https://"):
assert api_host_name.replace("https://", "wss://") in csp
else:
raise AssertionError(
f"Unexpected API_HOST_NAME format: {api_host_name} — must start with 'http://' or 'https://'"
)