🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!
Security: Content-Security-Policy and Referrer-Policy headers (CFG-07b, CFG-07d)
Omega (QA)51be54911a5b
diff --git a/backend/__init__.py b/backend/__init__.pyindex b6db874..f410f67 100644--- a/backend/__init__.py+++ b/backend/__init__.py@@ -170,6 +170,31 @@ def ensure_owner_exists():if conn:release_db_connection(conn)+# Content-Security-Policy for every response Flask serves (CFG-07b).+# The frontend uses inline scripts, inline event handlers, and inline+# styles throughout, so script-src and style-src keep 'unsafe-inline';+# the policy still pins the fetchable origins, blocks plugins and+# foreign framing, and keeps form submissions and base URIs+# same-origin. The two third-party allowances are load-bearing: the+# About page loads the Buy Me a Coffee button script, which fetches+# its font and images from the buymeacoffee and Google Fonts hosts,+# and version-checker.js fetches the latest release from api.github.com.+_CONTENT_SECURITY_POLICY = (+ "default-src 'self'; "+ "script-src 'self' 'unsafe-inline' https://cdnjs.buymeacoffee.com; "+ "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "+ "img-src 'self' data: blob: https://*.buymeacoffee.com; "+ "font-src 'self' data: https://fonts.gstatic.com; "+ "connect-src 'self' https://api.github.com; "+ "frame-src 'self' https://*.buymeacoffee.com; "+ "object-src 'none'; "+ "base-uri 'self'; "+ "form-action 'self'; "+ "frame-ancestors 'self'; "+ "upgrade-insecure-requests"+)++def create_app(config_name=None):"""Create and configure an instance of the Flask application."""# Configure logging FIRST@@ -190,11 +215,18 @@ def create_app(config_name=None):app.wsgi_app = _ForwardedHeaderGuard(app.wsgi_app)@app.after_request- def _set_framing_protection(response):+ def _set_security_headers(response):# CFG-07a: never render framed by another origin. SAMEORIGIN# keeps any same-origin framing working while blocking the# cross-origin clickjacking case.response.headers.setdefault('X-Frame-Options', 'SAMEORIGIN')+ # CFG-07b: Content-Security-Policy, see the module constant.+ response.headers.setdefault(+ 'Content-Security-Policy', _CONTENT_SECURITY_POLICY)+ # CFG-07d: send the full URL only to the same origin, the bare+ # origin cross-origin, and nothing on an HTTPS to HTTP drop.+ response.headers.setdefault(+ 'Referrer-Policy', 'strict-origin-when-cross-origin')return response# Load configurationdiff --git a/nginx.conf b/nginx.confindex 80dd386..5d59e24 100644--- a/nginx.conf+++ b/nginx.conf@@ -6,6 +6,16 @@ server {root /var/www/html;index index.html;+ # Security headers (CFG-07a/b/d). These apply to the pages nginx+ # serves directly; the Flask app sets the same headers on API+ # responses, which pass through the proxy untouched. Keep the CSP+ # value identical to _CONTENT_SECURITY_POLICY in backend/__init__.py.+ # Locations below that define their own add_header directives do+ # not inherit these, so the HTML location repeats them.+ add_header X-Frame-Options "SAMEORIGIN" always;+ add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdnjs.buymeacoffee.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https://*.buymeacoffee.com; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' https://api.github.com; frame-src 'self' https://*.buymeacoffee.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests" always;+ add_header Referrer-Policy "strict-origin-when-cross-origin" always;+# Suppress the nginx version number in error pages and the Server# header (CFG-08). Valid in the server context; this file is# included as the default server block.@@ -117,6 +127,11 @@ server {location ~ \.html$ {add_header Content-Type "text/html; charset=utf-8";add_header Cache-Control "no-cache, no-store, must-revalidate";+ # Repeated: locations with their own add_header do not inherit+ # the server-level security headers (CFG-07a/b/d).+ add_header X-Frame-Options "SAMEORIGIN" always;+ add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdnjs.buymeacoffee.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https://*.buymeacoffee.com; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' https://api.github.com; frame-src 'self' https://*.buymeacoffee.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests" always;+ add_header Referrer-Policy "strict-origin-when-cross-origin" always;}# Favicon - specific handlingdiff --git a/tests/test_security_headers.py b/tests/test_security_headers.pynew file mode 100644index 0000000..c2e38d2--- /dev/null+++ b/tests/test_security_headers.py@@ -0,0 +1,61 @@+"""CFG-07b / CFG-07d: every response Flask serves carries a+Content-Security-Policy and a Referrer-Policy header.++The policy must keep the app's own third-party calls working: the+Buy Me a Coffee button script on the About page and the version+checker's fetch of api.github.com. nginx.conf repeats the same+headers for the pages nginx serves directly, and the values must+stay identical to the Flask constant.+"""+import re+from pathlib import Path++from backend import _CONTENT_SECURITY_POLICY++NGINX_CONF = Path(__file__).parent.parent / "nginx.conf"+++def test_csp_and_referrer_policy_on_api_response(client):+ r = client.get("/api/health")+ assert r.status_code == 200+ assert r.headers.get("Content-Security-Policy") == _CONTENT_SECURITY_POLICY+ assert r.headers.get("Referrer-Policy") == "strict-origin-when-cross-origin"+++def test_csp_and_referrer_policy_on_error_response(client):+ r = client.get("/api/no-such-endpoint")+ assert r.status_code == 404+ assert r.headers.get("Content-Security-Policy") == _CONTENT_SECURITY_POLICY+ assert r.headers.get("Referrer-Policy") == "strict-origin-when-cross-origin"+++def test_nginx_conf_repeats_the_same_csp_and_referrer_policy():+ text = NGINX_CONF.read_text()+ csp_values = re.findall(+ r'add_header Content-Security-Policy "(.*)" always;', text)+ assert len(csp_values) == 2+ assert all(value == _CONTENT_SECURITY_POLICY for value in csp_values)+ referrer_values = re.findall(+ r'add_header Referrer-Policy "(.*)" always;', text)+ assert len(referrer_values) == 2+ assert all(+ value == "strict-origin-when-cross-origin"+ for value in referrer_values)+++def test_csp_keeps_inline_scripts_and_styles_working():+ assert "script-src 'self' 'unsafe-inline'" in _CONTENT_SECURITY_POLICY+ assert "style-src 'self' 'unsafe-inline'" in _CONTENT_SECURITY_POLICY+++def test_csp_allows_the_about_page_widget_and_version_check():+ assert "https://cdnjs.buymeacoffee.com" in _CONTENT_SECURITY_POLICY+ assert "https://*.buymeacoffee.com" in _CONTENT_SECURITY_POLICY+ assert "https://api.github.com" in _CONTENT_SECURITY_POLICY+++def test_csp_blocks_plugins_and_foreign_framing():+ assert "object-src 'none'" in _CONTENT_SECURITY_POLICY+ assert "frame-ancestors 'self'" in _CONTENT_SECURITY_POLICY+ assert "base-uri 'self'" in _CONTENT_SECURITY_POLICY+ assert "form-action 'self'" in _CONTENT_SECURITY_POLICY