-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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: Do not strip CSP headers from HTTPResponse #24760
Merged
flotwig
merged 3 commits into
cypress-io:develop
from
pgoforth:issue-1030/pgoforth/add-nonce-to-inline-script-injection
Jan 11, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { OutgoingHttpHeaders } from 'http' | ||
|
||
const cspRegExp = /[; ]*(.+?) +([^\n\r;]*)/g | ||
|
||
const caseInsensitiveGetAllHeaders = (headers: OutgoingHttpHeaders, lowercaseProperty: string): string[] => { | ||
return Object.entries(headers).reduce((acc: string[], [key, value]) => { | ||
if (key.toLowerCase() === lowercaseProperty) { | ||
// It's possible to set more than 1 CSP header, and in those instances CSP headers | ||
// are NOT merged by the browser. Instead, the most **restrictive** CSP header | ||
// that applies to the given resource will be used. | ||
// https://www.w3.org/TR/CSP2/#content-security-policy-header-field | ||
// | ||
// Therefore, we need to return each header as it's own value so we can apply | ||
// injection nonce values to each one, because we don't know which will be | ||
// the most restrictive. | ||
acc.push.apply( | ||
acc, | ||
`${value}`.split(',') | ||
.filter(Boolean) | ||
.map((policyString) => `${policyString}`.trim()), | ||
) | ||
} | ||
|
||
return acc | ||
}, []) | ||
} | ||
|
||
function getCspHeaders (headers: OutgoingHttpHeaders, headerName: string = 'content-security-policy'): string[] { | ||
return caseInsensitiveGetAllHeaders(headers, headerName.toLowerCase()) | ||
} | ||
|
||
export function hasCspHeader (headers: OutgoingHttpHeaders, headerName: string = 'content-security-policy') { | ||
return getCspHeaders(headers, headerName).length > 0 | ||
} | ||
|
||
export function parseCspHeaders (headers: OutgoingHttpHeaders, headerName: string = 'content-security-policy'): Map<string, string[]>[] { | ||
const cspHeaders = getCspHeaders(headers, headerName) | ||
|
||
// We must make an policy map for each CSP header individually | ||
return cspHeaders.reduce((acc: Map<string, string[]>[], cspHeader) => { | ||
const policies = new Map<string, string[]>() | ||
let policy = cspRegExp.exec(cspHeader) | ||
|
||
while (policy) { | ||
const [/* regExpMatch */, directive, values] = policy | ||
const currentDirective = policies.get(directive) || [] | ||
|
||
policies.set(directive, [...currentDirective, ...values.split(' ').filter(Boolean)]) | ||
policy = cspRegExp.exec(cspHeader) | ||
} | ||
|
||
return [...acc, policies] | ||
}, []) | ||
} | ||
|
||
export function generateCspDirectives (policies: Map<string, string[]>): string { | ||
return Array.from(policies.entries()).map(([directive, values]) => `${directive} ${values.join(' ')}`).join('; ') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For some reason I always thought that this was a hash of the script's source, this is way simpler than I thought.