Skip to content

feat(security): Standardize Security Headers and CORS Policy Restrictions for Production Deployments#657

Open
advikdivekar wants to merge 2 commits into
ritesh-1918:gssocfrom
advikdivekar:fix/issue-637-security-headers-cors
Open

feat(security): Standardize Security Headers and CORS Policy Restrictions for Production Deployments#657
advikdivekar wants to merge 2 commits into
ritesh-1918:gssocfrom
advikdivekar:fix/issue-637-security-headers-cors

Conversation

@advikdivekar
Copy link
Copy Markdown

Summary

Fixes #637

This PR hardens the web frontend and FastAPI backend against XSS, Clickjacking, and MIME-sniffing vulnerabilities by enforcing standard HTTP security headers and restricting CORS to authorized origins only.

Changes

  • Frontend/vite.config.js — CSP + Security Headers for Vite

    • Full Content-Security-Policy header: whitelists self, Supabase (REST/Auth/Realtime), Google Fonts, Gemini API, local backend — blocks everything else
    • X-Content-Type-Options: nosniff
    • X-Frame-Options: DENY (clickjacking protection)
    • X-XSS-Protection: 1; mode=block
    • Referrer-Policy: strict-origin-when-cross-origin
    • Permissions-Policy locks camera/mic/geolocation
    • Strict-Transport-Security in preview (production) mode
    • Added rollupOptions.manualChunks for vendor/ui bundle splitting
  • backend/middleware/security_headers.py — Helmet-equivalent FastAPI middleware

    • SecurityHeadersMiddleware (Starlette BaseHTTPMiddleware) injects all headers into every response
    • _parse_allowed_origins() reads ALLOWED_ORIGINS env var (comma-separated), falling back to existing defaults
    • add_security_middleware(app) convenience function replaces the inline CORSMiddleware block in main.py — CORS origins are now environment-driven
    • Allows methods explicitly (GET, POST, PUT, PATCH, DELETE, OPTIONS) instead of wildcard

Migration

In backend/main.py, replace the existing inline CORS block:

# Before:
app.add_middleware(CORSMiddleware, allow_origins=["https://..."], ...)

# After:
from backend.middleware.security_headers import add_security_middleware
add_security_middleware(app)

Set ALLOWED_ORIGINS=https://helpdeskaiv1.vercel.app,https://staging.helpdesk.ai in your .env.

Test Plan

  • Run vite dev and inspect response headers in browser DevTools — all security headers present
  • Run FastAPI backend and curl -v /health — all Helmet headers in response
  • Request from unlisted origin → CORS rejection (403/no Access-Control-Allow-Origin)
  • Request from ALLOWED_ORIGINS origin → CORS headers present
  • Verify CSP does not block Supabase auth calls or Gemini API

…production hardening

Closes ritesh-1918#637

- Update Frontend/vite.config.js: add CSP, X-Content-Type-Options, X-Frame-Options,
  X-XSS-Protection, Referrer-Policy, Permissions-Policy to Vite dev server and
  preview server headers; add rollupOptions chunk splitting for vendor/ui bundles
- Add backend/middleware/security_headers.py: Helmet-equivalent SecurityHeadersMiddleware
  that injects HSTS, CSP, X-Frame-Options, X-Content-Type-Options, and friends into
  every FastAPI response; CORS origins now read from ALLOWED_ORIGINS env var
  (comma-separated) so production/staging environments need no code changes;
  add_security_middleware() convenience function replaces the inline CORS block in main.py
@vercel
Copy link
Copy Markdown

vercel Bot commented May 29, 2026

@advikdivekar is attempting to deploy a commit to the ritesh Team on Vercel.

A member of the Team first needs to authorize it.

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d43808fe-6a60-41a9-aaa0-12f001864553

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@advikdivekar
Copy link
Copy Markdown
Author

@ritesh-1918 please review my PR, thank you

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant