Skip to content

Add HSTS header #381

Open
Open

Description

@gkorland

The missing HSTS header exposes the organization to man-in-the-middle attacks.

• Verify the absence of the Strict-Transport-Security header in the output.

Activity

  1. overcut-ai commented on Jan 24, 2026

    @overcut-ai

    Completed Working on "Automated Ticket Triage "

    ✅ Workflow completed successfully.


    👉 View complete log

  2. overcut-ai commented on Jan 24, 2026

    @overcut-ai

    Completed Working on "Auto Root Cause Analysis"

    ✅ Workflow completed successfully.


    👉 View complete log

  3. overcut-ai commented on Jan 24, 2026

    @overcut-ai

    🛠 Root Cause Analysis & Fix Proposal

    📌 Summary of Root Cause

    The FastAPI application created in QueryWeaver/api/app_factory.py installs several middlewares (ProxyHeadersMiddleware, SessionMiddleware, custom SecurityMiddleware) but never sets an HTTP Strict-Transport-Security (HSTS) response header. Consequently, every HTTPS response is delivered without Strict-Transport-Security, leaving clients vulnerable to downgrade and man-in-the-middle attacks.

    🔍 Evidence

    • Repo-wide search shows zero occurrences of "Strict-Transport-Security" → no code path sets the header.
    • api/app_factory.py (around lines 70-170)
      • The create_app() function builds the FastAPI instance and attaches middleware, but none inject an HSTS header.
      • Example excerpt:
        app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
        app.add_middleware(
            SessionMiddleware,
            secret_key=SECRET_KEY,
            same_site="lax",
            https_only=False,
            max_age=60 * 60 * 24 * 14,
        )
        # Missing: Strict-Transport-Security header middleware

    All API routes (/graphs, /database, /tokens, OAuth flows) and the React SPA fall through this middleware stack, so the omission affects the entire service.

    💡 Suggested Minimal Fix

    Add a lightweight middleware that injects the HSTS header for HTTPS requests (detected via request.url.scheme or X-Forwarded-Proto) and allow disabling via env-var for local HTTP development.

    diff --git a/QueryWeaver/api/app_factory.py b/QueryWeaver/api/app_factory.py
    @@
    -from starlette.middleware.base import BaseHTTPMiddleware
    +from starlette.middleware.base import BaseHTTPMiddleware
    @@
     class SecurityMiddleware(BaseHTTPMiddleware):
    @@
    +class HSTSMiddleware(BaseHTTPMiddleware):
    +    """Add Strict-Transport-Security header to HTTPS responses."""
    +
    +    def __init__(self, app, enabled: bool = True, header_value: str | None = None):
    +        super().__init__(app)
    +        self.enabled = enabled
    +        self.header_value = (
    +            header_value or "max-age=31536000; includeSubDomains; preload"
    +        )
    +
    +    async def dispatch(self, request: Request, call_next):
    +        response = await call_next(request)
    +
    +        if not self.enabled:
    +            return response
    +
    +        scheme = request.headers.get("x-forwarded-proto", request.url.scheme)
    +        if scheme == "https" and "strict-transport-security" not in response.headers:
    +            response.headers["Strict-Transport-Security"] = self.header_value
    +
    +        return response
    @@
    -    app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
    +    app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
    +
    +    # --- Security: Enforce HSTS on HTTPS deployments ---
    +    hsts_enabled = os.getenv("ENABLE_HSTS", "true").lower() in ("1", "true", "yes")
    +    app.add_middleware(HSTSMiddleware, enabled=hsts_enabled)

    ⚠️ Risk Assessment

    • Low risk: Adds only response headers; no changes to request processing or core business logic.
    • Requires proxy/load-balancer to forward X-Forwarded-Proto for correct HTTPS detection. Document this requirement.
    • Local/plain-HTTP development can disable via ENABLE_HSTS=false.

    ✅ Test Recommendations

    1. Unit/ASGI Test: Using TestClient, send a request with header x-forwarded-proto: https; assert Strict-Transport-Security equals max-age=31536000; includeSubDomains; preload.
    2. Repeat with x-forwarded-proto: http (or no header) – header should be absent when not HTTPS or when ENABLE_HSTS=false.
    3. Integration/Staging: Deploy behind the real proxy, curl any endpoint over HTTPS and verify header presence.

    🎯 Confidence

    High (0.86) – Direct inspection confirmed no HSTS header in codebase, and fix is straightforward.


    Labels applied: rca-proposed, conf-high

  4. Anchel123 commented on Aug 24, 2026

    @Anchel123
    Contributor

    HSTS is already live on staging — api/app_factory.py sets Strict-Transport-Security: max-age=31536000; includeSubDomains; preload on every response, and tests/test_hsts_header.py pins it. This issue is done and can be closed.

    The remaining security headers (X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy, Content-Security-Policy) are still outstanding and are covered by #431.

    ⚠️ Note for whoever lands #431: app/src/pages/Index.tsx fetches https://api.github.com/repos/FalkorDB/QueryWeaver for the star count, so a bare connect-src 'self' will break the UI in production only. It needs connect-src 'self' https://api.github.com.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions