-
-
Notifications
You must be signed in to change notification settings - Fork 232
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
Set even more strict CSP header in redirect response #188
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for prepare this PR and for the context @ljeda!
I think that impose unnecessary restrictions that might conflict with how the consuming applications are designed to operate. For example:
- If a consuming app relies on
iframe
embedding (e.g., for previewing content),frame-ancestors 'none'
could break their functionality. - If forms are a core part of their app,
form-action 'none'
will prevent all form submissions, potentially breaking workflows.
It is possible to use setHeaders
(docs) to add this limits when using the library.
const express = require('express');
const serveStatic = require('serve-static');
const app = express();
// Serve static files with custom headers
app.use(
serveStatic('public', {
setHeaders: (res, path, stat) => {
// Set custom headers
res.setHeader(
'Content-Security-Policy',
"default-src 'self'; frame-ancestors 'none'; form-action 'none';"
);
},
})
);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
I agree, and think this is what folks should use |
Thanks @UlisesGascon and @wesleytodd for the comments! I have one remark though. Please notice that the part of code I'm referring to is related with the behavior of the Lines 181 to 207 in e2bf828
As far as I understand when serving redirect we shouldn't worry about any form actions or iframes (happy to be wrong) as we're just serving the simple redirect page that takes us to the final page. I've already tried to play around with |
I am honestly not sure about this, but I think it is reasonable for someone to serve a static asset in an iframe. And while I have not played around with this behavior at all to see what this new restriction might break I think unless someone comes with a clear and present risk around this default I believe we are better off recommending folks tighten their security posture with |
@wesleytodd I see your point. I was assuming the CSP headers will not prevent the redirect, but seems like I was wrong: I was hoping that maybe alternatively we could just add the same ability to customize the headers by using the mentioned I've tried using |
Ah, interesting. Yes this would set the header in a way that overrides helmet. As it stands with this code there is no way to tighten the security in user-land code without fully overriding this behavior. That is not good and is something I would love to see a PR for. It might be tricky from a design perspective as we would want to have similar handling for all of the headers this is setting. I could see this going two ways:
If you are interested in this work, please open a new issue or PR and we can discuss further in that. |
suggested fix for the too broad (wildcard) CSP directives (
frame-ancestors
andform-action
) in redirect response.fixes #187