← Back to Pull requests

test(backend): pin rate-limit identity and HSTS/origin rules #247

Closed
unknown closed this pull request without merging on Nov 25, 2026 09:45 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 09:16 UTC
Author

Two security-relevant backend modules had no tests.

  • app/client_identity.py decides the rate-limit bucket for every request. The new tests check that:

  • - bearer tokens are hashed and never appear in the bucket id;
    - an empty Bearer falls back to the IP;
    - API keys go through the slow KDF, not plain sha256, and map to a stable bucket;
    - X-Forwarded-For is ignored unless trust_proxy is set, and then the rightmost non-blank hop is used, so prepended fake hops can't dodge the limit;
    - the ip:unknown fallback works.
  • app/origins.py: HSTS is sent only when the effective scheme is https, X-Forwarded-Proto (first hop) takes precedence, and blank schemes are a no-op. Origin normalisation strips trailing slashes, qualifies bare hosts, and dedupes env origins.
  • These are tests only, with no behaviour change: 14 new tests, all passing locally. A plausible "simplification" such as parts[0], dropping the proxy gate, or sending HSTS on http would now fail CI.
    <!-- devin-review-badge-begin -->

    ---

    <a href="https://app.devin.ai/review/mangeshraut712/assistme-virtualassistant/pull/247" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
    <!-- devin-review-badge-end -->

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Low Risk

    Test-only PR with no runtime changes; it documents and guards existing security behavior in CI.

    > Overview

    Adds tests-only coverage for two security-sensitive backend helpers—no production behavior changes.

    > New tests in test_client_identity.py pin resolve_client_id rate-limit bucket rules: hashed bearer tokens (no token leakage), IP fallback for empty bearer, API keys via slow KDF (not plain SHA-256) with stable buckets, X-Forwarded-For gated on trust_proxy with rightmost hop selection, and ip:unknown fallback.

    > New tests in test_origins_hsts.py lock in HSTS only on effective HTTPS (X-Forwarded-Proto first hop wins), origin normalisation (scheme, trailing slashes), and deduplicated env-based CORS origins via allowed_origins_list().

    > 14 new tests; regressions like using leftmost X-Forwarded-For, skipping the proxy trust gate, or emitting HSTS on HTTP should fail CI.

    > <sup>Reviewed by Cursor Bugbot for commit f3b96e83fad9cc2f09519628852f5e90072bcd3f. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 331 lines (+248 / -83) across 4 files Reviewer: mangeshraut712 Review duration: 29m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 331 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.347
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #247"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.1% (MEDIUM) · Higher = larger blast radius
    42.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.6% · Safe to merge
    16.6%