From 5d32501a2121f966228cdf9a44644d9942a1816d Mon Sep 17 00:00:00 2001 From: harsh0641 Date: Thu, 19 Nov 2026 19:23:13 -0500 Subject: [PATCH] fix: add security headers to the API (CSP, HSTS, nosniff, frame protection) --- src/lysis/api.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lysis/api.py b/src/lysis/api.py index e5c50eb..309de12 100644 --- a/src/lysis/api.py +++ b/src/lysis/api.py @@ -32,6 +32,21 @@ allow_methods=["GET", "HEAD", "POST"], allow_headers=["*"], ) + +@app.middleware("http") +async def security_headers(request: Request, call_next): + resp = await call_next(request) + resp.headers["X-Content-Type-Options"] = "nosniff" + resp.headers["X-Frame-Options"] = "DENY" + resp.headers["Referrer-Policy"] = "no-referrer" + resp.headers["Permissions-Policy"] = "camera=(), microphone=(), geolocation=()" + resp.headers["Strict-Transport-Security"] = "max-age=63072000; includeSubDomains" + if not request.url.path.startswith(("/docs", "/openapi", "/redoc")): + resp.headers["Content-Security-Policy"] = ( + "default-src 'none'; frame-ancestors 'none'" + ) + return resp + REPO = r"^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$" PER_IP = SlidingWindow(int(os.getenv("RATE_PER_IP", "30"))) GLOBAL = SlidingWindow(int(os.getenv("RATE_GLOBAL", "300")))