-
Notifications
You must be signed in to change notification settings - Fork 3
CSP header support #271
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
base: master
Are you sure you want to change the base?
CSP header support #271
Conversation
HJSielcken
left a comment
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.
Let's talk tomorrow about this stuff
| } | ||
|
|
||
|
|
||
| async function sendHtml(req, res, template, location, { status, headers, data }) { |
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.
This does not have to be async right?
| app.use(helmet(Object.assign( | ||
| { | ||
| hsts: false, // hsts-headers are sent by our loadbalancer | ||
| contentSecurityPolicy: false, |
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.
It feels a bit strange that we have
const { contentSecurityPolicy, ...helmetOptionsToUse } = helmetOptions || {}
and
Object.assign( {
hsts: false, // hsts-headers are sent by our loadbalancer
contentSecurityPolicy: false,
referrerPolicy: { policy: 'strict-origin-when-cross-origin' },
},helmetOptionsToUse)
Does that mean that the resulting object has always contentSecurityPolicy: false?
| } | ||
|
|
||
| function addCspHeaders(req, res) { | ||
| return new Promise((resolve, reject) => { |
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.
What is the reason we need to wrap this in a Promise? And make 'sendHtmlWithCspHeaders' async?
For me it seems that the template rendering is just a bit more involved (namely collecting the extra script SHA's), but no extra async work?
This pull request adds Content Security Policy header support.
Blocking
unsafe-inlineis one of the most important directives to avoid. But this will make all inline script tags unusable. Two approaches to solve 'trusted' inline scripts:nonce, a value that is unique for each request and is both present in the header and in the script tagOption 2. is the easiest to implement. The problem with this approach is that each request will get a unique header, making caching layers outside of the application unusable.
So we are stuck with option 1. For script tags created with this library a hash could be determined without using code. In real applications we however also use the script tags to provide information the
dataLayer. While not the only use case, it's one of the more well known ones.What we do in this pull request:
<script>tags to our<SafeScript>tagSafeScripttag renders, it creates a hash of the content and adds it to the collectionNote: the code should still be tested in a real environment