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

Open
#33 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active

Research direction

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.

Written by the indexing model from the issue text.

Description

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.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.