Skip to content

Commit

Permalink
fix: Handle evaled privileged commands (#27267)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Jul 12, 2023
1 parent 32cfa50 commit 4c0371f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
10 changes: 9 additions & 1 deletion cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 12.17.2

_Released 07/18/2023 (PENDING)_

**Bugfixes:**

- Fixed an issue where `cy.writeFile()` would erroneously fail with the error `cy.writeFile() must only be invoked from the spec file or support file`. Fixes [#27097](https://github.com/cypress-io/cypress/issues/27097).

## 12.17.1

_Released 07/18/2023_
_Released 07/10/2023_

**Bugfixes:**

Expand Down
17 changes: 17 additions & 0 deletions packages/driver/cypress/e2e/e2e/privileged_commands.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ describe('privileged commands', () => {
cy.get('#basic').selectFile(Uint8Array.from([98, 97, 122]))
})

it('handles evaled code', () => {
window.eval(`
cy.task('return:arg', 'eval arg')
.then(() => {
cy.task('return:arg', 'then eval arg')
})
cy.get('body')
.each(() => {
cy.task('return:arg', 'each eval arg')
})
.within(() => {
cy.task('return:arg', 'within eval arg')
})
`)
})

it('passes in test body .then() callback', () => {
cy.then(() => {
cy.exec('echo "hello"')
Expand Down
32 changes: 28 additions & 4 deletions packages/server/lib/privileged-commands/privileged-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

const queryStringRegex = /\?.*$/

let hasValidCallbackContext = false

// since this function is eval'd, the scripts are included as stringified JSON
if (scripts) {
scripts = parse(scripts)
Expand Down Expand Up @@ -69,6 +71,18 @@
return filteredLines.length > 0
}

const isInCallback = (err) => {
return stringIncludes.call(err.stack, 'thenFn@') || stringIncludes.call(err.stack, 'withinFn@')
}

const hasCallbackInsideEval = (err) => {
if (browserFamily === 'webkit') {
return isInCallback(err) && hasValidCallbackContext
}

return isInCallback(err) && stringIncludes.call(err.stack, '> eval line')
}

// in non-chromium browsers, the stack will include either the spec file url
// or the support file
const hasStackLinesFromSpecOrSupportFile = (err) => {
Expand Down Expand Up @@ -96,16 +110,22 @@
'task',
]

const callbackCommands = [
'each',
'then',
'within',
]

function stackIsFromSpecFrame (err) {
if (isSpecBridge) {
return hasSpecBridgeInvocation(err)
}

if (browserFamily === 'chromium') {
return hasSpecFrameStackLines(err)
return hasStackLinesFromSpecOrSupportFile(err) || hasSpecFrameStackLines(err)
}

return hasStackLinesFromSpecOrSupportFile(err)
return hasCallbackInsideEval(err) || hasStackLinesFromSpecOrSupportFile(err)
}

// source: https://github.com/bryc/code/blob/d0dac1c607a005679799024ff66166e13601d397/jshash/experimental/cyrb53.js
Expand Down Expand Up @@ -141,8 +161,6 @@
}

async function onCommandInvocation (command) {
if (!arrayIncludes.call(privilegedCommands, command.name)) return

// message doesn't really matter since we're only interested in the stack
const err = new Err('command stack error')

Expand All @@ -152,6 +170,12 @@
captureStackTrace.call(Err, err, onCommandInvocation)
}

if (arrayIncludes.call(callbackCommands, command.name)) {
hasValidCallbackContext = stackIsFromSpecFrame(err)
}

if (!arrayIncludes.call(privilegedCommands, command.name)) return

// if stack is not validated as being from the spec frame, don't add
// it as a verified command
if (!stackIsFromSpecFrame(err)) return
Expand Down

4 comments on commit 4c0371f

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4c0371f Jul 12, 2023

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:

npm install https://cdn.cypress.io/beta/npm/12.17.2/linux-arm64/develop-4c0371f4b54f540393819b0d5db884163a16d0ad/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4c0371f Jul 12, 2023

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:

npm install https://cdn.cypress.io/beta/npm/12.17.2/linux-x64/develop-4c0371f4b54f540393819b0d5db884163a16d0ad/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4c0371f Jul 12, 2023

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:

npm install https://cdn.cypress.io/beta/npm/12.17.2/darwin-arm64/develop-4c0371f4b54f540393819b0d5db884163a16d0ad/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4c0371f Jul 12, 2023

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:

npm install https://cdn.cypress.io/beta/npm/12.17.2/win32-x64/develop-4c0371f4b54f540393819b0d5db884163a16d0ad/cypress.tgz

Please sign in to comment.