-
-
Notifications
You must be signed in to change notification settings - Fork 47
Support function-based customisation in reply.helmet options #287
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -88,9 +88,13 @@ async function replyDecorators (request, reply, configuration, enableCSP) { | |||||
| } | ||||||
|
|
||||||
| reply.helmet = function (opts) { | ||||||
| const helmetConfiguration = opts | ||||||
| ? Object.assign(Object.create(null), configuration, opts) | ||||||
| : configuration | ||||||
| let helmetConfiguration = configuration | ||||||
|
|
||||||
| if (typeof opts === 'function') { | ||||||
| helmetConfiguration = opts(helmetConfiguration) | ||||||
|
||||||
| helmetConfiguration = opts(helmetConfiguration) | |
| helmetConfiguration = opts(Object.assign(Object.create(null), configuration)) |
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.
Can you plz document why this is necessary, we should have very good reasons to ignore code coverage.
I see this package has a few ignore comments without any explanation, so it's not a feedback specific to you.
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.
@jean-michelet For whatever reason this condition isn't covered by the existing tests. I added the ignore as a work-around because I saw it was used elsewhere in the code (and it was the only way I could commit without --force). I've tried adding a test to cover it but was unsuccessful, so any help or advice is appreciated!
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.
FYI: I’ve opened a PR to add comments:
#292
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ declare module 'fastify' { | |
| script: string; | ||
| style: string; | ||
| }, | ||
| helmet: (opts?: HelmetOptions) => typeof helmet | ||
| helmet: (opts?: HelmetOptions | ((opts: HelmetOptions) => HelmetOptions)) => typeof helmet | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a test for the type? We use tsd. |
||
| } | ||
|
|
||
| export interface RouteOptions extends fastifyHelmet.FastifyHelmetRouteOptions { } | ||
|
|
||
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.
The example assumes
opts.contentSecurityPolicyand its nested properties exist, but users may receive configuration where these are undefined. Consider adding a comment noting that this is a simplified example and proper validation should be performed in production code.