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

オープン
#33 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
express, nextjs, typescript

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
TypeScript
スター
2
フォーク
1
平均マージ
6分
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sumamakhan761/tryinstance のほかの issue

sumamakhan761/tryinstance の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。