Repository navigation
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
Conversation
… 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>
Owner
|
@Anshuu2004 CI/CD checks are currently failing on
This failure occurs because public profiles are now private by default with opt-in publication (from PR #381).
Please rebase/merge the latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves the helmet setup out of
index.tsintobackend/src/middleware/securityHeaders.ts, sends the same CSP in every environment, addsframe-ancestors 'none', and adds a header test.Closes #363
What changed
backend/src/middleware/securityHeaders.tsexportingsecurityHeaders({ isProd })and theCSP_DIRECTIVES. The directive list is the one that was inline inindex.tsfor production, plusframeAncestors: ["'none'"].index.tsnow just doesapp.use(securityHeaders({ isProd: env.IS_PROD })).contentSecurityPolicy: undefinedmakes 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-Optionsgoes from helmet's defaultSAMEORIGINtoDENYso it agrees withframe-ancestors 'none'for browsers that ignore CSP. Nothing in the repo frames a backend page (no iframes infrontend/srcorbackend/src).base-uri 'self',form-action 'self',script-src-attr 'none',upgrade-insecure-requests); the only prod CSP difference isframe-ancestors 'none'instead of'self'./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, whichscript-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.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.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.frameAncestors, CSP off outside prod, default frameguard) 7 of the 11 tests fail; they pass with this change.npx tsc --noEmit -p .inbackend/: only the 5 existing errors from the unbuilt local@devtrack/ats-corepackage, 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.