GeezerVison is showing github.com — watch: escape quotes and backslashes in NODE_OPTIONS · nodejs/node@41e9ab9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js +14-4Lines changed: 14 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | ArrayPrototypePushApply, | 8 | 8 | ArrayPrototypeSlice, | 9 | 9 | StringPrototypeIncludes, | | 10 | + StringPrototypeReplaceAll, | 10 | 11 | StringPrototypeStartsWith, | 11 | 12 | } = primordials; | 12 | 13 |
| @@ -101,11 +102,20 @@ if (kNodeOptions != null) { | 101 | 102 | i++; | 102 | 103 | continue; | 103 | 104 | } | 104 | | - // The C++ tokenizer strips quotes during parsing, so values that | 105 | | - // originally contained spaces (e.g. --require "./path with spaces/f.js") | | 105 | + // The C++ tokenizer strips quotes and escapes during parsing, so values | | 106 | + // that contain spaces or double quotes (e.g. --require "./a b/f.js") | 106 | 107 | // need to be re-quoted before rejoining into a single string, otherwise | 107 | | - // the child's C++ parser would split them into separate tokens. | 108 | | - ArrayPrototypePush(keep, StringPrototypeIncludes(part, ' ') ? `"${part}"` : part); | | 108 | + // the child's C++ parser would split or misparse them. Inside quotes, | | 109 | + // backslashes are escape characters, so `"` and `\` must be escaped. | | 110 | + // Backslashes are escaped first so the ones added for `"` aren't doubled. | | 111 | + // Outside quotes, backslashes are literal, so other values are kept as-is. | | 112 | + if (StringPrototypeIncludes(part, ' ') || StringPrototypeIncludes(part, '"')) { | | 113 | + const escaped = StringPrototypeReplaceAll( | | 114 | + StringPrototypeReplaceAll(part, '\\', '\\\\'), '"', '\\"'); | | 115 | + ArrayPrototypePush(keep, `"${escaped}"`); | | 116 | + } else { | | 117 | + ArrayPrototypePush(keep, part); | | 118 | + } | 109 | 119 | } | 110 | 120 | cleanNodeOptions = ArrayPrototypeJoin(keep, ' '); | 111 | 121 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-watch-mode.mjs +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1064,6 +1064,35 @@ process.on('message', (message) => { | 1064 | 1064 | } | 1065 | 1065 | }); | 1066 | 1066 |
| | 1067 | + for (const { name, nodeOptions, expected } of [ | | 1068 | + { name: 'a double quote', nodeOptions: '--title="a\\"b"', expected: 'a"b' }, | | 1069 | + { name: 'a backslash', nodeOptions: '--title="a \\\\b"', expected: 'a \\b' }, | | 1070 | + ]) { | | 1071 | + it(`should preserve NODE_OPTIONS values containing ${name} in child process`, { | | 1072 | + // Honoring --title from NODE_OPTIONS is required for this test. | | 1073 | + // process.title is always an empty string on SunOS, so --title | | 1074 | + // cannot be observed there. | | 1075 | + skip: !!process.config.variables.node_without_node_options || common.isSunOS, | | 1076 | + }, async () => { | | 1077 | + const file = createTmpFile('console.log(JSON.stringify(process.title));'); | | 1078 | + const { done, restart } = runInBackground({ | | 1079 | + args: ['--watch', file], | | 1080 | + options: { | | 1081 | + env: { ...process.env, NODE_OPTIONS: `--watch ${nodeOptions}` }, | | 1082 | + }, | | 1083 | + }); | | 1084 | + | | 1085 | + try { | | 1086 | + const { stdout, stderr } = await restart(); | | 1087 | + | | 1088 | + assert.strictEqual(stderr, ''); | | 1089 | + assert.ok(stdout.includes(JSON.stringify(expected)), stdout.join('\n')); | | 1090 | + } finally { | | 1091 | + await done(); | | 1092 | + } | | 1093 | + }); | | 1094 | + } | | 1095 | + | 1067 | 1096 | it('should handle NODE_OPTIONS containing only watch flags', async () => { | 1068 | 1097 | const file = createTmpFile('console.log(JSON.stringify(process.env.NODE_OPTIONS));'); | 1069 | 1098 | const { done, restart } = runInBackground({ | |
|