-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Content Security Policy headers #805
Conversation
I don't know if it helps at all, but this is what I did in Outer Space. I can't remember how strict this was or if it was just better than nothing. const ContentSecurityPolicy = process.env.NODE_ENV === 'development'
? `
default-src 'self';
connect-src 'self' webpack://* wss://*;
script-src 'self' 'unsafe-eval';
worker-src 'self' blob:;
script-src-elem 'self';
style-src 'self' 'unsafe-inline';`
: `
default-src 'self';
script-src 'self' 'sha256-/6SBPqW+GW+//4nlXX6Y1nR9dWlh0gsQJ6KK71djH6A=';
worker-src 'self' blob:;
connect-src 'self' wss://*;
style-src 'self' 'unsafe-inline';`
const securityHeaders = [
{
key: 'X-DNS-Prefetch-Control',
value: 'on'
},
{
key: 'X-XSS-Protection',
value: '1; mode=block'
},
{
key: 'X-Frame-Options',
value: 'SAMEORIGIN'
},
{
key: 'X-Content-Type-Options',
value: 'nosniff'
},
{
key: 'Referrer-Policy',
value: 'origin-when-cross-origin'
},
{
key: 'Content-Security-Policy',
value: ContentSecurityPolicy.replace(/\s{2,}/g, ' ').trim()
}
] |
We could also put stuff into nginx if that makes more sense. Just another option! |
d5e3bf4
to
93d33f3
Compare
I think since you used hashes, it is a strict CSP ( edit: Oh, I was wrong.
-- https://web.dev/articles/strict-csp#what_is_a_strict_content_security_policy followed that link from here
I think I got it working with nonces now in 93d33f3. Will test more tomorrow |
One shouldn't use
Did set other nice headers like Did not add |
CSP looks good according to https://csp-evaluator.withgoogle.com/: Another scan from https://observatory.mozilla.org/: Would be nice if we can disable Going to add the HSTS header for extra points (even though we shouldn't care too much about these points haha) |
If you want to find it, let's remove it. |
Good call, Also allowed CDN, media domain where required and fixed blocking of YouTube and twitter embeds via New evaluation: Going to take a look into Subresource Integrity.
I am getting a lot of this errors in the server log in production mode now though:
But app seems to run fine. I think it's related to this: vercel/next.js#56368 |
Apparently, there is a bug for Chrome on iOS if connect-src does not allow 'self'. See known issues at https://caniuse.com/contentsecuritypolicy
For some reason, www.youtube.com is enough. It also works for youtube.com and youtube-nocookie.com. For twitter embeds from twitter.com or x.com, platform.twitter.com is enough.
adfa67f
to
b6a7e77
Compare
I noticed yesterday that Brave disabled the Reporting API because of its privacy implications: brave/brave-browser#7956 And wasn't able to get it to work on Chrome. Tried to test by removing the Will look more into this today. However, I also noticed that one needs to explicitly allow browser extensions ( Found this nice website related to problems with weird CSP reports: https://csper.io/blog/csp-report-filtering It linked to this summary of "WTF" reports: https://github.com/nico3333fr/CSP-useful/blob/master/csp-wtf/explained.md I would like to have CSP reports but if it's too much work (and because of the privacy implications), I think we can ship it enforced and rely on user reports etc. and I can look into this at some other point in the future (when it feels more important). I am quite confident that the current CSP policy is not interfering with most functionality on the site. I am also quite confident that the middleware works as expected. It's not interfering with referrals (I tested) and CSP is only setting headers so I don't think there is anything to worry about the middleware. Better middleware support from NextJS would still be nice though. |
Regarding This error only happened if the dev build in the browser was trying to connect to wss://sn.ekzyis.com/_next/webpack-hmr with the server built in prod mode running. If I refreshed the page the errors stopped showing up since the prod build does not use hot reload. Ignoring that route in 03969f4 now. |
c134c56
to
03969f4
Compare
close #770
Some notes regarding the approach of using a middleware:
We could use meta tags in
<Head>
in app.js but then we can't usereport-to
.We could add static CSP headers in next.config.js but then we can't enforce a strict CSP with dynamic nonces. We could use hashes but hashes are fragile.
So afaict, middleware is the only viable solution. Even though middlewares feel like third-class citizens in NextJS :/
TODO:
unsafe-inline
forscript-src
andstyle-src
(strict CSP)We could also uses hashes but this can break easily:
-- https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html#note
So I'll try to use nonces first.
usereport-to
to report errors to Slack channel before enforcing the policyif not, no problem, we can rely onreport-to
before enforcing policyTypeError: Cannot read properties of undefined (reading 'bind')
which might be related to Next.js TypeError: Cannot read properties of undefined (reading 'bind') - caused by middleware.ts vercel/next.js#56368