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

Đang mở
#33 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
backend, frontend, security

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
TypeScript
Star
2
Fork
1
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
27

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của sumamakhan761/tryinstance

Tất cả issue của sumamakhan761/tryinstance

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.