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

feat: propagate contentSecurityPolicy.useDefaults through to helmet #184

Merged
merged 2 commits into from
May 23, 2022

Conversation

chrskrchr
Copy link
Contributor

The @fastify/helmet plugin was bumped to helmet@5 in #164. This release of helmet included a breaking change where useDefault now defaults to true, causing this plugin to now include a default set of directives on the response. We'd like to pass useDefault: false to @fastify/helmet and have that propgated through to helmet so we can disable this behavior.

https://github.com/helmetjs/helmet/tree/main/middlewares/content-security-policy

Checklist

@@ -262,7 +320,6 @@ test('It should access the correct options property', async (t) => {
enableCSPNonces: true,
contentSecurityPolicy: {
directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the exception of the test on L135 that explicitly tests this getDefaultDirectives() export, it seemed more appropriate in these tests to NOT explicitly provide these headers and instead rely on helmet's default behavior of injecting these default directives itself.

@@ -280,7 +337,7 @@ test('It should access the correct options property', async (t) => {
t.ok(cspCache.script)
t.ok(cspCache.style)
t.has(response.headers, {
'content-security-policy': `default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self' 'unsafe-eval' 'unsafe-inline' 'nonce-${cspCache.script}';script-src-attr 'none';style-src 'self' 'unsafe-inline' 'nonce-${cspCache.style}';upgrade-insecure-requests`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of these directives changed now that helmet's default directives are injected after those explicity provided on L266-L267, but the CSP content is otherwise identical.

@@ -85,7 +85,6 @@ test('It should add CSPNonce decorator and hooks when route `enableCSPNonces` op
enableCSPNonces: false,
contentSecurityPolicy: {
directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar explanation for this removal as in global.test.js on L265.

@chrskrchr chrskrchr marked this pull request as ready for review May 19, 2022 21:51
@chrskrchr
Copy link
Contributor Author

CC: @darkgl0w and @Fdawgs - looks like you've both touched some of this code recently and might be in the best position to provide feedback.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@chrskrchr
Copy link
Contributor Author

@mcollina - if/when the team merges this PR, would you consider making a branch on the latest 8.x release that I could use to target a backport of this change for fastify@3?

@climba03003
Copy link
Member

@mcollina - if/when the team merges this PR, would you consider making a branch on the latest 8.x release that I could use to target a backport of this change for fastify@3?

https://github.com/fastify/fastify-helmet/tree/v8.x Here you are.

@darkgl0w
Copy link
Member

I will review this afternoon 👍

README.md Outdated Show resolved Hide resolved
Co-authored-by: Frazer Smith <frazer.dev@outlook.com>
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@darkgl0w darkgl0w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change we will push an opinionated change (that may break existing code). < Not meant to be published for this PR nor this public account.

Can you add a note in the readme stating that while in helmet it is true by default when useDefaults is set to false it will disable the behavior. < right sentence.

LGTM with this README addition 👍

@Fdawgs
Copy link
Member

Fdawgs commented May 20, 2022

With this change we will push an opinionated change (that may break existing code).

Can you add a note in the readme stating that from now useDefaults is set to false by default while in helmet it is true by default.

LGTM with this README addition 👍

You've lost me there @darkgl0w, it's still enabled by default isn't it, so not a breaking change?

@darkgl0w
Copy link
Member

darkgl0w commented May 20, 2022

With this change we will push an opinionated change (that may break existing code).
Can you add a note in the readme stating that from now useDefaults is set to false by default while in helmet it is true by default.
LGTM with this README addition 👍

You've lost me there @darkgl0w, it's still enabled by default isn't it, so not a breaking change?

OMG 🤣 reviewed from my phone while I was on train and I mixed/misplaced my message editions while writing the comment @Fdawgs that was not the sentence I wanted to validate (I will edit the original message to avoid confusions xD).

Edit: And I have a piece of review that was not aimed at this PR nor this account too ... ~~
Things should be back to normal within the next week now that I'm done with my move and I should get my internet connection back on thursday ✌️

@chrskrchr
Copy link
Contributor Author

Can you add a note in the readme stating that while in helmet it is true by default when useDefaults is set to false it will disable the behavior. < right sentence.

LGTM with this README addition 👍

@darkgl0w - are you asking for the above in addition to the blurb below that's already been added to the README in this PR?

By default, helmet will add a default set of CSP directives to the response. This behavior can be disabled by setting useDefaults: false in the contentSecurityPolicy configuration.

If so, would you mind adding a suggestion of the specific changes you'd like to see in the README?

@darkgl0w
Copy link
Member

@chrskrchr > no the section you already added to the readme is good :)

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit b341c68 into fastify:master May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants