-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: Add documentation for experimentalCspAllowList
and CSP information
#5194
Merged
mschile
merged 4 commits into
cypress-io:release-12.15.0
from
pgoforth:cy-issue-1030/pgoforth/document-stripCspDirectives
Jun 14, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d92749
feat: Add documentation for `stripCspDirectives` and CSP information
pgoforth c9247ea
feat: Add documentation for `experimentalCspAllowList`
pgoforth 33f4b7b
chore: Add `child-src` and `frame-src` to optional CSP directive allo…
pgoforth 418592c
chore: Mark 'sandbox' and 'navigate-to' as always stripped
pgoforth 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: Content Security Policy | ||
e2eSpecific: true | ||
--- | ||
|
||
Content Security Policy (CSP) is a browser security feature that allows you to | ||
restrict the resources that can be loaded into your application. This can be | ||
problematic for Cypress, because it needs to inject JavaScript into your | ||
application in order to run tests and interact with the DOM. This page describes | ||
how Cypress handles CSP and how to configure it to work with your application. | ||
|
||
There are two ways to implement CSP: | ||
|
||
- [Meta tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#http-equiv) | ||
- [HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) | ||
|
||
The `<meta>` tag implementation is fully supported by Cypress without any | ||
configuration required. This is because Cypress loads the necessary `<script>` | ||
tags into your application before any `<meta>` tag is parsed. This prevents any | ||
CSP directives from being applied to the script loaded by Cypress. | ||
|
||
The second implementation requires you to configure Cypress to allow the headers | ||
to be sent to your application. By default, Cypress will remove any CSP headers | ||
from the response before it is sent to the browser. This is done to prevent | ||
Cypress from being blocked by the browser's CSP implementation. | ||
|
||
For most application tests, this should not cause any issues. However, if you | ||
are testing your application's CSP implementation, you will need to configure | ||
Cypress to allow the headers to be sent to the browser. You can do this by | ||
setting the | ||
[`experimentalCspAllowList`](/guides/references/experiments#Experimental-CSP-Allow-List) | ||
configuration option. | ||
|
||
For more information on CSP, see the | ||
[Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) | ||
documentation on MDN. |
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
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.
Can you add a note that this experiment is not compatible with the
experimentalSourceRewriting
experiment?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.
@emilyrohrbough This should still be compatible in certain scenarios. Can we say may not be compatible?
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.
@pgoforth @AtofStryker had flagged this as a possible risk / scenario where this configuration opt-in may fail.
experimentalModifyObstructiveThirdPartyCode
is enabled, integrity tags are stripped out of HTML documents as it is really difficult to inform the browser of the SHA to expect before the resource is rewritten and SHAed on the server (a bit of the chicken or the egg problem)Mind adding a blurb stating if there are issues and this may be an known issue
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.
@pgoforth I didn't see any code changes in the rewriting that would indicate this wouldn't cause issues with the nonce injection. Did you test any by chance?
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.
@emilyrohrbough Nonce injection is completely independent of SHA hashing. They are handled differently through CSP.
nonce
value is randomly generated at runtime and does not rely on the file hash at all.So even if Cypress made inline changes to a script, the
nonce
would still pass CSP validation. We are generating anonce
for inline script injections, but are currently not generating anonce
for scripts that have been modified by Cypress.experimentalSourceRewriting
does not trigger a CSP violation by default unless a SHA is used on the script node in question. Therefore, allowing CSP headers is unlikely to cause a the source rewrite code to fail CSP unless there are already specific directive values in place that validate against those scripts. That's why I said it may be an issue, because it's totally dependent on the original CSP headers that Cypress intercepts.Edit:
I think I was being unclear on re-reading what I've written. SRI
integrity
attributes do not function the same way CSPnonce
attributes do. They are mutually exclusive. Adding anonce
after already removing theintegrity
attribute would be moot. The CSP headers that affect rewrites are'trusted-types'
and'require-trusted-types-for'
, which we will ALWAYS strip.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 main issue with SHA inline scripts is if the script is rewritten and the sha doesnt match the actual hash. But I think it might be OK to say it may work with
experimentalSourceRewriting
because it is going to depend on the context of what is rewritten, similar to how things work now with the regex rewriterThere 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.
We can probably specify the scenario (using
'<hash-algorithm>-<base64-value>'
inside a CSP directive) as that's the only place it could trip againstexperimentalSourceRewriting
orexperimentalModifyObstructiveThirdPartyCode
/modifyObstructiveCode
.I also genuinely feel like that is a problem that can be solved given our ability to modify individual directive values. We would just need to match the original SHA in the CSP directive with the one we replaced, and swap the value for the replacement. Outside of the scope of this PR, but definitely doable in the near future.