Skip to content
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

How to set nonce in helmet >4.0? #240

Closed
mercteil opened this issue Aug 6, 2020 · 6 comments
Closed

How to set nonce in helmet >4.0? #240

mercteil opened this issue Aug 6, 2020 · 6 comments

Comments

@mercteil
Copy link

mercteil commented Aug 6, 2020

In version 3 I could set nonce from res.locals like this:

...
        scriptSrc: [
          (req, res) => `'nonce-${res.locals.nonce}'`,
          "'strict-dynamic'",
...

In 4.0 I get an error when providing a function for setting nonce:

Error: Content-Security-Policy received an invalid directive value for "script-src"

So the question is how to set nonce in helmet 4.0 ?

@mercteil
Copy link
Author

mercteil commented Aug 6, 2020

.. apparently by picking and forwarding the context manually

server.use((req, res, next) =>
    helmet.contentSecurityPolicy({
      directives: {
        ...
        scriptSrc: [
          `'nonce-${res.locals.nonce}'`,
          "'strict-dynamic'",
         ...
        ],
      },
    })(req, res, next))

@einfallstoll
Copy link

I don't like this. Also, this should have been mentioned in the changelog.

@EvanHahn
Copy link
Member

EvanHahn commented Aug 6, 2020

@mercteil That's exactly right. Here's a wiki page describing how to do that in more detail.

@einfallstoll Good point. I've added it to the changelog in c01ad1e.

This has made me realize that I need to create a migration guide. See #241 to track this.

I'm going to close this issue because I think this has been resolved, but if folks really want this feature, we can discuss adding it back.

@EvanHahn EvanHahn closed this as completed Aug 6, 2020
@XhmikosR
Copy link

XhmikosR commented Aug 9, 2020

Agreed that this was pretty handy and simpler than the current solution. I haven't updated to helmet 4.0.0 due to this issue, so if it's not too much work, I'd like the ability to use functions back too 🙂

@EvanHahn
Copy link
Member

EvanHahn commented Aug 9, 2020

This issue has been troublesome for a lot of people and I'm going to re-add the behavior to Helmet, likely in version 4.1.0. See #243 to follow my progress.

@EvanHahn
Copy link
Member

This feature will be added to Helmet version 4.1.0. You can try it out today with npm install helmet@4.1.0-rc.1—let me know if it doesn't work for you, and follow along with the release at #245.

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

No branches or pull requests

4 participants