-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Do not strip CSP headers from HTTPResponse (#24760)
Co-authored-by: Zach Bloomquist <[email protected]> Closes #1030
- Loading branch information
Showing
10 changed files
with
559 additions
and
50 deletions.
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
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.
0472bb9
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.
Circle has built the
linux arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
0472bb9
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.
Circle has built the
linux x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
0472bb9
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.
Circle has built the
darwin arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
0472bb9
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.
Circle has built the
darwin x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
0472bb9
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.
Circle has built the
win32 x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally: