fix(security): configure Express trust proxy and add Content-Security-Policy headers

Aperta
#33 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
express, nextjs, typescript

Direzione di ricerca

Start with apps/api/src/main.ts and apps/web/next.config.ts, then check the deployment proxy setup and the web app’s external script, analytics, and embed origins. Configure proxy trust and API Helmet CSP, and add a report-only web CSP that covers the required origins. Done means client IP handling works for deployed and local setups, API responses have the stated restrictive CSP, and browser reports can be reviewed before web enforcement.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

backend bug web
Problem Description
  1. Missing trust proxy in apps/api/src/main.ts:
    NestFactory.create(AppModule, { rawBody: true }) does not configure Express trust proxy. Behind a reverse proxy or load balancer (Cloudflare, Nginx, etc.), req.ip resolves to the proxy's IP rather than the real client IP, causing users across workspaces to share a single rate-limit bucket or breaking IP-based throttling.
  2. Missing Content-Security-Policy (CSP):
    • apps/api/src/main.ts explicitly disables Helmet's CSP (contentSecurityPolicy: false).
    • apps/web/next.config.ts (headers() for /:path*) sets X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and Permissions-Policy, but sets no Content-Security-Policy header.

Expected Behavior
  1. apps/api/src/main.ts configures Express trust proxy via an environment variable (e.g., TRUST_PROXY, defaulting to 1 in production and false locally, or 2 when behind Cloudflare + Nginx) so req.ip reflects the real client IP without breaking local/direct setups.
  2. apps/api/src/main.ts enables a restrictive Helmet CSP appropriate for API responses (defaultSrc: ["'none'"], frameAncestors: ["'none'"]).
  3. apps/web/next.config.ts rolls out a Content-Security-Policy-Report-Only header first so existing scripts, pixels, and embeds are never accidentally blocked for users, and promotes it to Content-Security-Policy once verified.

Where to Look
  • apps/api/src/main.ts
  • apps/web/next.config.ts

Solution / Implementation Guide
  1. API trust proxy & Helmet CSP (apps/api/src/main.ts):
    • Configure Express trust proxy from process.env.TRUST_PROXY (e.g. app.getHttpAdapter().getInstance().set('trust proxy', ...)), matching the deployment hop count (1 behind a single proxy, 2 behind CDN + proxy, false when no proxy is used).
    • Enable a strict contentSecurityPolicy in helmet(...) instead of false.
  2. Next.js CSP (apps/web/next.config.ts):
    • Add a Content-Security-Policy-Report-Only header under source: "/:path*" listing the app's required origins (API URL, analytics/pixel domains, YouTube/Spotify/Instagram embed & CDN hosts, frame-ancestors 'none').
    • Monitor browser violation reports in staging/production before switching the header to enforcing Content-Security-Policy.
Lingua principale
TypeScript
Stelle
2
Fork
1
Merge medio
6m
PR unite (30g)
27

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sumamakhan761/tryinstance

Tutte le issue di sumamakhan761/tryinstance

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.