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

fix: launch args with multiple values inside quotes #29077

Merged
merged 16 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ _Released 4/16/2024 (PENDING)_

- Updated the Chrome flags to not show the "Enhanced Ad Privacy" dialog. Addresses [#29199](https://github.com/cypress-io/cypress/issues/29199).

**Bugfixes:**

- Fixed and issue where cypress launch arguments were not being escaped correctly with multiple values inside quotes. Fixes [#27454](https://github.com/cypress-io/cypress/issues/27454).


## 13.7.2

_Released 4/2/2024_
Expand Down
24 changes: 19 additions & 5 deletions packages/server/lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,33 @@ try {
}

if (process.env.ELECTRON_EXTRA_LAUNCH_ARGS) {
const electronLaunchArguments = process.env.ELECTRON_EXTRA_LAUNCH_ARGS.split(' ')
// regex will be used to convert ELECTRON_EXTRA_LAUNCH_ARGS into an array, for example
// input: 'foo --ipsum=0 --bar=--baz=quux --lorem="--ipsum=dolor --sit=amet"'
// output: ['foo', '--ipsum=0', '--bar=--baz=quux', '--lorem="--ipsum=dolor --sit=amet"']
const regex = /(?:[^\s"']+|"[^"]*"|'[^']*')+/g
erwin-huang marked this conversation as resolved.
Show resolved Hide resolved
const electronLaunchArguments = process.env.ELECTRON_EXTRA_LAUNCH_ARGS.match(regex) || []

electronLaunchArguments.forEach((arg) => {
// arg can be just key --disable-http-cache
// or key value --remote-debugging-port=8315
// https://github.com/cypress-io/cypress/issues/7994
const [key, value] = arg.split('=')
// or key value with another value --foo=--bar=4196
// or key value with another multiple value --foo='--bar=4196 --baz=quux'
const [key, ...value] = arg.split('=')

// because this is an environment variable, everything is a string
// thus we don't have to worry about casting
// --foo=false for example will be "--foo", "false"
if (value) {
app.commandLine.appendSwitch(key, value)
if (value.length) {
let joinedValues = value.join('=')

// check if the arg is wrapped in " or ' (unicode)
const isWrappedInQuotes = !!['\u0022', '\u0027'].find(((charAsUnicode) => joinedValues.startsWith(charAsUnicode) && joinedValues.endsWith(charAsUnicode)))

if (isWrappedInQuotes) {
joinedValues = joinedValues.slice(1, -1)
}

app.commandLine.appendSwitch(key, joinedValues)
} else {
app.commandLine.appendSwitch(key)
}
Expand Down
14 changes: 14 additions & 0 deletions packages/server/test/unit/environment_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ describe('lib/environment', () => {
expect(app.commandLine.appendSwitch).to.have.been.calledWith('--quux', 'false')
})

it('sets launch args with multiple values inside quotes', () => {
restore = mockedEnv({
ELECTRON_EXTRA_LAUNCH_ARGS: `--foo --ipsum=0 --bar=--baz=quux --lorem='--ipsum=dolor --sit=amet'`,
})

sinon.stub(app.commandLine, 'appendSwitch')
require(`../../lib/environment`)

expect(app.commandLine.appendSwitch).to.have.been.calledWith('--foo')
expect(app.commandLine.appendSwitch).to.have.been.calledWith('--ipsum', '0')
expect(app.commandLine.appendSwitch).to.have.been.calledWith('--bar', '--baz=quux')
expect(app.commandLine.appendSwitch).to.have.been.calledWith('--lorem', '--ipsum=dolor --sit=amet')
})

return afterEach(() => {
if (restore) {
return restore()
Expand Down