Skip to content

feat(security): move helmet config to securityHeaders middleware, CSP in all envs with frame-ancestors none - #391

Open
Anshuu2004 wants to merge 1 commit into
VarshithReddy2006:mainfrom
Anshuu2004:feat/issue-363-security-headers
Open

Anshuu2004 wants to merge 1 commit into
VarshithReddy2006:mainfrom
Anshuu2004:feat/issue-363-security-headers

Conversation

@Anshuu2004

Copy link
Copy Markdown
Contributor

Summary

Moves the helmet setup out of index.ts into backend/src/middleware/securityHeaders.ts, sends the same CSP in every environment, adds frame-ancestors 'none', and adds a header test.

Closes #363

What changed

  • New backend/src/middleware/securityHeaders.ts exporting securityHeaders({ isProd }) and the CSP_DIRECTIVES. The directive list is the one that was inline in index.ts for production, plus frameAncestors: ["'none'"].
  • index.ts now just does app.use(securityHeaders({ isProd: env.IS_PROD })).
  • CSP was only passed to helmet in production. Outside prod contentSecurityPolicy: undefined makes helmet fall back to its default policy (frame-ancestors 'self', style-src 'self' https: 'unsafe-inline', font-src 'self' https: data:, img-src 'self' data:), so dev and test saw a different policy than prod. Now all environments get the production one.
  • X-Frame-Options goes from helmet's default SAMEORIGIN to DENY so it agrees with frame-ancestors 'none' for browsers that ignore CSP. Nothing in the repo frames a backend page (no iframes in frontend/src or backend/src).
  • HSTS is unchanged (prod pins includeSubDomains + preload, other envs keep helmet's default).
  • Everything helmet merges in by default is unchanged in prod (base-uri 'self', form-action 'self', script-src-attr 'none', upgrade-insecure-requests); the only prod CSP difference is frame-ancestors 'none' instead of 'self'.
  • The OG shell (/u/:username) is registered after the helmet middleware, so it gets the headers from the same middleware; its only script is the same-origin /u/.well-known/portfolio-redirect.js, which script-src 'self' allows, and there is no inline script.

Not in this PR

  • frontend/vercel.json: a strict CSP for the SPA needs the Clerk origins and a hash for the inline theme script, and I could not check that without a browser, so I left it out rather than risk breaking the app.
  • Script nonces: the backend does not render any page with inline scripts.

How I tested

From backend/, using a vitest config without the global Mongo setup (the new test does not need a database):

  • npx vitest run src/__tests__/middleware/securityHeaders.test.ts -> 1 file, 11 tests passed.
  • The test starts a real express app with the middleware and reads the headers over HTTP: prod and non-prod CSP directives (frame-ancestors 'none', object-src 'none', base-uri 'self', script-src 'self', ...), identical policy in both envs, X-Frame-Options: DENY, HSTS prod vs non-prod, and the public profile shell (service and cache mocked) carrying the CSP with no inline script.
  • Against the old behaviour (no frameAncestors, CSP off outside prod, default frameguard) 7 of the 11 tests fail; they pass with this change.
  • npx tsc --noEmit -p . in backend/: only the 5 existing errors from the unbuilt local @devtrack/ats-core package, none in these files.

Not run: the full backend suite, and I did not load the app in a browser, so I have not looked at the console for CSP violations on the SPA (the SPA is served by Vercel, not by this backend).

Notes

Only the backend API and its HTML shell are affected; JSON responses just carry the extra header. CSP only applies to documents, so the Vite dev frontend calling the API cross-origin or via its proxy is not restricted by this header.

… in all envs with frame-ancestors none

The helmet options lived inline in index.ts and the CSP was only passed in
production, so dev and test got helmet's default policy instead. Move them to
middleware/securityHeaders.ts, send the same policy everywhere, set
frame-ancestors 'none' (and X-Frame-Options DENY to match) and cover the
headers with a test.

Signed-off-by: Abhishek <abhishekcse2004@gmail.com>
@VarshithReddy2006 VarshithReddy2006 added enhancement New feature or request help wanted Extra attention is needed nsoc26 level2 5 points - UI, frontend components, layouts, styling, and page-level changes. security Security-related improvements labels Nov 16, 2026
@VarshithReddy2006

Copy link
Copy Markdown
Owner

@Anshuu2004 CI/CD checks are currently failing on Unit Tests and Integration Tests:

  1. Unit Tests:
AssertionError: expected undefined to match object { achievements: [], trust: { underReview: true } }
 ❯ src/__tests__/modules/profile/antiGaming.worker.test.ts:138:18

This failure occurs because public profiles are now private by default with opt-in publication (from PR #381).

  1. Integration Tests:
FAIL src/__tests__/integration/revision-review.test.ts > Revision Radar Integration & Optimistic Concurrency > dsaProblemSolved.consumer > creates exactly one card with SM-2 defaults and is idempotent on repeat events
AssertionError: expected true to be false
 ❯ src/__tests__/integration/revision-review.test.ts:53:28
   53| expect(res2.created).toBe(false);

Please rebase/merge the latest main into your branch and fix these CI failures immediately so the PR can be merged.

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

Labels

enhancement New feature or request help wanted Extra attention is needed level2 5 points - UI, frontend components, layouts, styling, and page-level changes. nsoc26 security Security-related improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(security,m10): implement Content-Security-Policy (CSP) headers for SPA and strict script-src nonce generation

2 participants