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: add experimentalModifyObstructiveThirdPartyCode flag for regex rewriter #22568

Merged
merged 60 commits into from
Jul 22, 2022

Conversation

AtofStryker
Copy link
Contributor

@AtofStryker AtofStryker commented Jun 28, 2022

with the completion of #22479

User facing changelog

Introduces a new experimental flag, called experimentalModifyObstructiveThirdPartyCode. When enabled, experimentalModifyObstructiveThirdPartyCode will turn on additional modifyObstructiveCode options to prevent frame busting, as well as strips integrity tags out of <link> and <script> elements. In the current state of this experimental flag, SRI is not supported.

Additional details

As a team, we felt it be best that changes to the regex-rewriter for modifyObstructiveCode be wrapped in its own experimental flag to reduce the potential impact of the rewriter modify code that breaks users who are not leveraging cy.origin or wish to preserve SRI within their application. The impact of this flag will change as we continue to modify the regex-rewritter to handle different websites/authentication platforms. Additionally, the team plans to support server-side integrity matching to verify <link> and <script> elements that have had their integrity stripped be revalidated on the server through hash comparison.

  • When the experimental flag is enabled:
    • third party html/scripts are run through the rewriter
    • regex rewriter will look for patterns similar to e.top === e.self
    • subresource integrity hashes removed from script and link tags
    • calling formElement.submit() correctly handles unsupported targets

Steps to test

  • additional unit tests to the regex-rewriter have been added to help guarantee the expected rewriting behavior for newly added modifications to the strip/stripStream function, as well as adding the integrity.cy.ts test inside the origin folder. For this test suite to run correctly, both the experimentalSessionAndOrigin and experimentalExpandedModifyObstructiveCode MUST be set to true. This has been modified in the package.json to pass in CI.

How has the user experience changed?

Users should now be able to log into Microsoft Online SSO and Login Live SSO (not pictured) via Cypress
microsoft-login

PR Tasks

  • Have tests been added/updated?
  • Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • Has a PR for user-facing changes been opened in cypress-documentation?
  • Have API changes been updated in the type definitions?

@AtofStryker AtofStryker added the topic: cy.origin Problems or enhancements related to cy.origin command label Jun 28, 2022
@AtofStryker AtofStryker self-assigned this Jun 28, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 28, 2022

Thanks for taking the time to open a PR!

@AtofStryker AtofStryker changed the title Feature/expanded modify obstructive code Feat: add experimentalExpandedModifyObstructiveCode flag for regex rewriter Jun 28, 2022
@AtofStryker AtofStryker changed the title Feat: add experimentalExpandedModifyObstructiveCode flag for regex rewriter feat: add experimentalExpandedModifyObstructiveCode flag for regex rewriter Jun 28, 2022
@cypress
Copy link

cypress bot commented Jun 28, 2022



Test summary

4379 0 49 0Flakiness 0


Run details

Project cypress
Status Passed
Commit 97bb6e0
Started Jul 22, 2022 5:18 AM
Ended Jul 22, 2022 5:30 AM
Duration 11:51 💡
OS Linux Debian - 11.3
Browser Electron 102

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

|| resContentTypeIsJavaScript(this.incomingRes)
)
this.res.wantsSecurityRemoved = this.config.modifyObstructiveCode &&
(this.res.wantsInjection === 'full' || this.res.wantsInjection === 'fullCrossOrigin' || (resContentTypeIsJavaScript(this.incomingRes) && this.config.experimentalExpandedModifyObstructiveCode))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modify the content if 1st party html full injection, or html that is being injected for cy.origin. Currently, we don't have a good mechanism for modifying general 3rd party javascript and has been a considered a bug in the past #8983, so for now we will make sure the experimentalExpandedModifyObstructiveCode is enabled before doing so. This is subject to change, and we might even be able to determine if we can modify the code based on the initiator/requestor in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am also wondering if we want to do the same thing for html with this flag. Right now we run into weird edge casesd where HTML is returned from a redirect after the cy.origin block has exited, which turns the rewriting off for that html and framebusts, which is going to make it difficult for users to test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added more aggressive behavior in 5597dfa to remove security for all html/javascript resources, regardless of context, if the flag is enabled. This should prevent the weird gap use case where remote state changes at the end of a cy.origin block and html is still being fed to page from that origin, even though we have left that context and the rewriter no longer attempts to rewrite that html.

@@ -29,7 +29,7 @@ export function install (url: string, rewriter: RewritingStream, deferSourceMapR
const sriAttr = find(startTag.attrs, { name: 'integrity' })

if (sriAttr) {
sriAttr.name = 'cypress:stripped-integrity'
sriAttr.name = 'cypress-stripped-integrity'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is used by the AST rewriter. For some reason, jQuery has a fit when an html attribute contains a : when doing a generic selector (guessing a collision on stand html attribute characters?) I am wondering if we want to standardize this attribute and go with data-cypress-stripped-integrity

@mschile mschile marked this pull request as ready for review July 15, 2022 20:45
@mschile mschile changed the title feat: add experimentalExpandedModifyObstructiveCode flag for regex rewriter feat: add experimentalModifyObstructiveThirdPartyCode flag for regex rewriter Jul 15, 2022
@@ -5,21 +5,44 @@ const invalidTargets = new Set(['_parent', '_top'])
export type GuardedEvent = Event & {target: HTMLFormElement | HTMLAnchorElement}
Copy link
Contributor

@mschile mschile Jul 18, 2022

Choose a reason for hiding this comment

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

There aren't really any changes to this file. I just created the handleInvalidTarget function and updated the handleInvalidEventTarget and handleInvalidAnchorTarget functions to call it.

@@ -15,7 +13,7 @@
// - On an interval, get the browser's cookies for the given domain, so that
// updates to the cookie jar (via http requests, cy.setCookie, etc) are
// reflected in the document.cookie value.
export const patchDocumentCookie = (Cypress) => {
export const patchDocumentCookie = (Cypress, window) => {
Copy link
Member

Choose a reason for hiding this comment

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

these patches are pretty straight forward and a good way to keep the injected code small 👍

@frnc07
Copy link

frnc07 commented Jul 27, 2022

Waiting anxiously for this feature to be released. Any timeline on when would this be released?

@mschile
Copy link
Contributor

mschile commented Jul 27, 2022

@frnc07, this change should go in our next release which is scheduled for next week.

@ChrystelCy
Copy link

@frnc07, this change should go in our next release which is scheduled for next week.

I can't see it on the 10.4 changelog. Do you have a release date for this feature please?

@emilyrohrbough
Copy link
Member

@ChrystelCy It's listed as the 3rd bullet under the features section: https://docs.cypress.io/guides/references/changelog#10-4-0

@ChrystelCy
Copy link

@ChrystelCy It's listed as the 3rd bullet under the features section: https://docs.cypress.io/guides/references/changelog#10-4-0

Thanks, I was looking for "22568" ...

@emilyrohrbough
Copy link
Member

@ChrystelCy no worries at all. Generally we link to the issue(s) associated to a change in the changelog unless there was not an original issue to link to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: cy.origin Problems or enhancements related to cy.origin command
Projects
None yet
9 participants