Skip to content

Commit 26bee79

Browse files
committed
Always pass NODE_OPTIONS with max-http-header-size (#5452)
* cli: set NODE_OPTIONS=--max-http-header-size=1024*1024 on spawn * electron: remove redundant max-http-header-size * server: add useCli option to make e2e tests go thru cli * server: add test for XHR with body > 100kb via CLI * clean up conditional * cli: don't pass --max-http-header-size in dev w node < 11.10 * add original_node_options to restore o.g. user node_options * force no color
1 parent c656bfa commit 26bee79

File tree

9 files changed

+117
-43
lines changed

9 files changed

+117
-43
lines changed

cli/__snapshots__/spawn_spec.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
exports['lib/exec/spawn .start detects kill signal exits with error on SIGKILL 1'] = `
2-
The Test Runner unexpectedly exited via a exit event with signal SIGKILL
1+
exports['lib/exec/spawn .start forces colors and streams when supported 1'] = {
2+
"FORCE_COLOR": "1",
3+
"DEBUG_COLORS": "1",
4+
"MOCHA_COLORS": "1",
5+
"FORCE_STDIN_TTY": "1",
6+
"FORCE_STDOUT_TTY": "1",
7+
"FORCE_STDERR_TTY": "1",
8+
"NODE_OPTIONS": "--max-http-header-size=1048576"
9+
}
310

4-
Please search Cypress documentation for possible solutions:
5-
6-
https://on.cypress.io
7-
8-
Check if there is a GitHub issue describing this crash:
9-
10-
https://github.com/cypress-io/cypress/issues
11-
12-
Consider opening a new issue.
13-
14-
----------
15-
16-
Platform: darwin (Foo-OsVersion)
17-
Cypress Version: 0.0.0
18-
`
11+
exports['lib/exec/spawn .start does not force colors and streams when not supported 1'] = {
12+
"FORCE_COLOR": "0",
13+
"DEBUG_COLORS": "0",
14+
"FORCE_STDIN_TTY": "0",
15+
"FORCE_STDOUT_TTY": "0",
16+
"FORCE_STDERR_TTY": "0",
17+
"NODE_OPTIONS": "--max-http-header-size=1048576"
18+
}

cli/lib/exec/spawn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = {
109109
}
110110

111111
const { onStderrData, electronLogging } = overrides
112-
const envOverrides = util.getEnvOverrides()
112+
const envOverrides = util.getEnvOverrides(options)
113113
const electronArgs = _.clone(args)
114114
const node11WindowsFix = isPlatform('win32')
115115

cli/lib/util.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ const util = {
235235
return isCi
236236
},
237237

238-
getEnvOverrides () {
238+
getEnvOverrides (options = {}) {
239239
return _
240240
.chain({})
241241
.extend(util.getEnvColors())
@@ -244,9 +244,35 @@ const util = {
244244
.mapValues((value) => { // stringify to 1 or 0
245245
return value ? '1' : '0'
246246
})
247+
.extend(util.getNodeOptions(options))
247248
.value()
248249
},
249250

251+
getNodeOptions (options, nodeVersion) {
252+
if (!nodeVersion) {
253+
nodeVersion = Number(process.versions.node.split('.')[0])
254+
}
255+
256+
if (options.dev && nodeVersion < 12) {
257+
// `node` is used when --dev is passed, so this won't work if Node is too old
258+
logger.warn('(dev-mode warning only) NODE_OPTIONS=--max-http-header-size could not be set. See https://github.com/cypress-io/cypress/pull/5452')
259+
260+
return
261+
}
262+
263+
// https://github.com/cypress-io/cypress/issues/5431
264+
const NODE_OPTIONS = `--max-http-header-size=${1024 * 1024}`
265+
266+
if (_.isString(process.env.NODE_OPTIONS)) {
267+
return {
268+
NODE_OPTIONS: `${NODE_OPTIONS} ${process.env.NODE_OPTIONS}`,
269+
ORIGINAL_NODE_OPTIONS: process.env.NODE_OPTIONS || '',
270+
}
271+
}
272+
273+
return { NODE_OPTIONS }
274+
},
275+
250276
getForceTty () {
251277
return {
252278
FORCE_STDIN_TTY: util.isTty(process.stdin.fd),

cli/test/lib/exec/spawn_spec.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,7 @@ describe('lib/exec/spawn', function () {
329329

330330
return spawn.start([], { env: {} })
331331
.then(() => {
332-
expect(cp.spawn.firstCall.args[2].env).to.deep.eq({
333-
FORCE_COLOR: '1',
334-
DEBUG_COLORS: '1',
335-
MOCHA_COLORS: '1',
336-
FORCE_STDERR_TTY: '1',
337-
FORCE_STDIN_TTY: '1',
338-
FORCE_STDOUT_TTY: '1',
339-
})
332+
snapshot(cp.spawn.firstCall.args[2].env)
340333
})
341334
})
342335

@@ -368,13 +361,7 @@ describe('lib/exec/spawn', function () {
368361

369362
return spawn.start([], { env: {} })
370363
.then(() => {
371-
expect(cp.spawn.firstCall.args[2].env).to.deep.eq({
372-
FORCE_COLOR: '0',
373-
DEBUG_COLORS: '0',
374-
FORCE_STDERR_TTY: '0',
375-
FORCE_STDIN_TTY: '0',
376-
FORCE_STDOUT_TTY: '0',
377-
})
364+
snapshot(cp.spawn.firstCall.args[2].env)
378365
})
379366
})
380367

cli/test/lib/util_spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require('../spec_helper')
33
const os = require('os')
44
const tty = require('tty')
55
const snapshot = require('../support/snapshot')
6+
const mockedEnv = require('mocked-env')
67
const supportsColor = require('supports-color')
78
const proxyquire = require('proxyquire')
89
const hasha = require('hasha')
@@ -11,6 +12,9 @@ const la = require('lazy-ass')
1112
const util = require(`${lib}/util`)
1213
const logger = require(`${lib}/logger`)
1314

15+
// https://github.com/cypress-io/cypress/issues/5431
16+
const expectedNodeOptions = `--max-http-header-size=${1024 * 1024}`
17+
1418
describe('util', () => {
1519
beforeEach(() => {
1620
sinon.stub(process, 'exit')
@@ -213,6 +217,7 @@ describe('util', () => {
213217
FORCE_COLOR: '1',
214218
DEBUG_COLORS: '1',
215219
MOCHA_COLORS: '1',
220+
NODE_OPTIONS: expectedNodeOptions,
216221
})
217222

218223
util.supportsColor.returns(false)
@@ -224,7 +229,46 @@ describe('util', () => {
224229
FORCE_STDERR_TTY: '0',
225230
FORCE_COLOR: '0',
226231
DEBUG_COLORS: '0',
232+
NODE_OPTIONS: expectedNodeOptions,
233+
})
234+
})
235+
})
236+
237+
context('.getNodeOptions', () => {
238+
let restoreEnv
239+
240+
afterEach(() => {
241+
if (restoreEnv) {
242+
restoreEnv()
243+
restoreEnv = null
244+
}
245+
})
246+
247+
it('adds required NODE_OPTIONS', () => {
248+
restoreEnv = mockedEnv({
249+
NODE_OPTIONS: undefined,
227250
})
251+
252+
expect(util.getNodeOptions({})).to.deep.eq({
253+
NODE_OPTIONS: expectedNodeOptions,
254+
})
255+
})
256+
257+
it('includes existing NODE_OPTIONS', () => {
258+
restoreEnv = mockedEnv({
259+
NODE_OPTIONS: '--foo --bar',
260+
})
261+
262+
expect(util.getNodeOptions({})).to.deep.eq({
263+
NODE_OPTIONS: `${expectedNodeOptions} --foo --bar`,
264+
ORIGINAL_NODE_OPTIONS: '--foo --bar',
265+
})
266+
})
267+
268+
it('does not return if dev is set and version < 12', () => {
269+
expect(util.getNodeOptions({
270+
dev: true,
271+
}, 11)).to.be.undefined
228272
})
229273
})
230274

packages/electron/lib/electron.coffee

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ module.exports = {
7575
if opts.inspectBrk
7676
argv.unshift("--inspect-brk=5566")
7777

78-
## max HTTP header size 8kb -> 1mb
79-
## https://github.com/cypress-io/cypress/issues/76
80-
argv.unshift("--max-http-header-size=#{1024*1024}")
81-
8278
debug("spawning %s with args", execPath, argv)
8379

8480
if debug.enabled

packages/server/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// if running in production mode (CYPRESS_ENV)
2+
// all transpile should have been done already
3+
// and these calls should do nothing
4+
require('@packages/ts/register')
5+
require('@packages/coffee/register')
6+
7+
require('./lib/util/reset_node_options').reset()
8+
19
// override tty if we're being forced to
210
require('./lib/util/tty').override()
311

@@ -9,11 +17,6 @@ if (process.env.CY_NET_PROFILE && process.env.CYPRESS_ENV) {
917

1018
process.env.UV_THREADPOOL_SIZE = 128
1119
require('graceful-fs').gracefulify(require('fs'))
12-
// if running in production mode (CYPRESS_ENV)
13-
// all transpile should have been done already
14-
// and these calls should do nothing
15-
require('@packages/ts/register')
16-
require('@packages/coffee/register')
1720

1821
require && require.extensions && delete require.extensions['.litcoffee']
1922
require && require.extensions && delete require.extensions['.coffee.md']
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Once the Electron process is launched, restore the user's original NODE_OPTIONS
3+
* environment variables from before the CLI added extra NODE_OPTIONS.
4+
*
5+
* This way, any `node` processes launched by Cypress will retain the user's
6+
* `NODE_OPTIONS` without unexpected modificiations that could cause issues with
7+
* user code.
8+
*/
9+
10+
export function reset () {
11+
// @ts-ignore
12+
if (process.versions.electron && typeof process.env.ORIGINAL_NODE_OPTIONS === 'string') {
13+
process.env.NODE_OPTIONS = process.env.ORIGINAL_NODE_OPTIONS
14+
15+
delete process.env.ORIGINAL_NODE_OPTIONS
16+
}
17+
}

packages/server/test/e2e/4_xhr_spec.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ describe "e2e xhr", ->
3030
spec: "xhr_spec.coffee"
3131
snapshot: true
3232
expectedExitCode: 0
33+
useCli: true
3334
}

0 commit comments

Comments
 (0)