options.js
This file
Path
lib/arguments/options.js
144 lines · 4.9 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
The walk stopped after 200 commits: lines older than c6ce870 point at it.
Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 1why, one step back1import path from 'node:path';Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 2–4why, one step back2import process from 'node:process';3import {npmRunPathEnv} from 'npm-run-path';4import {normalizeForceKillAfterDelay} from '../terminate/kill.js';Improve validation and typing of signals (#1025)ehmickybefore adoption7213287
line 5why, one step back5import {normalizeKillSignal} from '../terminate/signal.js';Fix the "signal" option being silently ignored by synchronous methodsSindre Sorhusbefore adoption59de065
line 6why, one step back6import {validateCancelSignal, validateRenamedSignalOption} from '../terminate/cancel.js';Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 7why, one step back7import {validateGracefulCancel} from '../terminate/graceful.js';Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 8–9why, one step back8import {validateTimeout} from '../terminate/timeout.js';9import {handleNodeOption} from '../methods/node.js';Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 10why, one step back10import {validateIpcInputOption} from '../ipc/ipc-input.js';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 11why, one step back11import {checkBooleanOption} from '../utils/options.js';Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
line 12why, one step back12import {validateEncoding, BINARY_ENCODINGS} from './encoding-option.js';Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f
line 13why, one step back13import {parseCommandFile} from './command-file.js';Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 14–17why, one step back14import {normalizeCwd} from './cwd.js';15import {normalizeFileUrl} from './file-url.js';16import {normalizeFdSpecificOptions} from './specific.js';17Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733
lines 18–19why, one step back18const cmdExeRegExp = /^cmd(?:\.exe)?$/i;19Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 20–22why, one step back20// Normalize the options object, and sometimes also the file paths and arguments.21// Applies default values, validate allowed options, normalize them.22export const normalizeOptions = (filePath, rawArguments, rawOptions) => {Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 23–26why, one step back23 // Prevent prototype pollution by copying only own properties to a null-prototype object24 const sanitizedOptions = {__proto__: null, ...rawOptions};25 sanitizedOptions.cwd = normalizeCwd(sanitizedOptions.cwd);26 const [processedFile, processedArguments, processedOptions] = handleNodeOption(filePath, rawArguments, sanitizedOptions);Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733
lines 27–28why, one step back27 const fdOptions = normalizeFdSpecificOptions(processedOptions);28 const options = addDefaultOptions(fdOptions);Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 29–30why, one step back29 validateBooleanOptions(options);30 validateEnv(options);Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733
line 31why, one step back31 options.env = getEnv(options);Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
line 32why, one step back32Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733
line 33why, one step back33 const {file, commandArguments} = parseCommandFile(processedFile, processedArguments, options);Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 34–36why, one step back3435 validateTimeout(options);36 validateEncoding(options);Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 37why, one step back37 validateIpcInputOption(options);Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977
line 38why, one step back38 validateCancelSignal(options);Fix the "signal" option being silently ignored by synchronous methodsSindre Sorhusbefore adoption59de065
line 39why, one step back39 validateRenamedSignalOption(options);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 40why, one step back40 validateGracefulCancel(options);Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
line 41why, one step back41 options.shell = normalizeFileUrl(options.shell);Improve validation and typing of signals (#1025)ehmickybefore adoption7213287
line 42why, one step back42 options.killSignal = normalizeKillSignal(options.killSignal);Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 43–45why, one step back43 options.forceKillAfterDelay = normalizeForceKillAfterDelay(options.forceKillAfterDelay);44 options.lines = options.lines.map((lines, fdNumber) => lines && !BINARY_ENCODINGS.has(options.encoding) && options.buffer[fdNumber]);45Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733
lines 46–47why, one step back46 // The file is now an absolute path resolved via `PATHEXT`, so its extension might be uppercase (`cmd.EXE`)47 if (process.platform === 'win32' && cmdExeRegExp.test(path.basename(file))) {Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 48–52why, one step back48 // #11649 commandArguments.unshift('/q');50 }5152 return {file, commandArguments, options};Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 53–78why, one step back53};5455const BOOLEAN_OPTIONS = [56 'extendEnv',57 'preferLocal',58 'reject',59 'cleanup',60 'killDescendants',61 'all',62 'gracefulCancel',63 'ipc',64 'node',65];6667// Wrong types must be reported instead of being silently coerced, e.g. the string 'false' enabling an option68const validateBooleanOptions = options => {69 for (const optionName of BOOLEAN_OPTIONS) {70 checkBooleanOption(options[optionName], optionName);71 }72};7374// A non-object `env` would be spread into unrelated properties, e.g. its characters when a string75const validateEnv = ({env}) => {76 if (env !== undefined && (typeof env !== 'object' || env === null || Array.isArray(env))) {77 throw new TypeError(`The \`env\` option must be an object: ${String(env)}.`);78 }Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 79–80why, one step back79};80Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 81why, one step back81// Use null prototype to prevent prototype pollution from leaking throughRemove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 82–89why, one step back82const addDefaultOptions = ({83 extendEnv = true,84 preferLocal = false,85 cwd,86 localDir: localDirectory = cwd,87 encoding = 'utf8',88 reject = true,89 cleanup = true,Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec
line 90why, one step back90 killDescendants = false,Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 91–94why, one step back91 all = false,92 windowsHide = true,93 killSignal = 'SIGTERM',94 forceKillAfterDelay = true,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 95why, one step back95 gracefulCancel = false,Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 96why, one step back96 ipcInput,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 97why, one step back97 ipc = ipcInput !== undefined || gracefulCancel,Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 98–100why, one step back98 serialization = 'advanced',99 ...options100}) => ({Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 101why, one step back101 __proto__: null,Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 102–109why, one step back102 ...options,103 extendEnv,104 preferLocal,105 cwd,106 localDirectory,107 encoding,108 reject,109 cleanup,Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec
line 110why, one step back110 killDescendants,Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 111–114why, one step back111 all,112 windowsHide,113 killSignal,114 forceKillAfterDelay,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 115why, one step back115 gracefulCancel,Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 116why, one step back116 ipcInput,Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 117–120why, one step back117 ipc,118 serialization,119});120Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 121–128why, one step back121/*122Keep a null prototype, otherwise a polluted `Object.prototype` would inject environment variables.123Unlike the other options, `child_process` intentionally passes the inherited properties of `env` to the subprocess, so they are the ones which must not leak. This includes `PATH`, which decides which file the command resolves to.124`npmRunPathEnv()` returns a plain object, so this is applied to its result too.125*/126const getEnv = options => ({__proto__: null, ...computeEnv(options)});127128const computeEnv = ({env: envOption, extendEnv, preferLocal, node, localDirectory, nodePath}) => {Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60
line 129why, one step back129 // Spread `envOption` even when it is `undefined`, so `extendEnv: false` results in an empty environment instead of inheriting `process.env`Harden some thingsSindre Sorhusbefore adoption4a0a121
line 130why, one step back130 const env = {__proto__: null, ...(extendEnv && process.env), ...envOption};Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 131–143why, one step back131132 if (preferLocal || node) {133 return npmRunPathEnv({134 env,135 cwd: localDirectory,136 execPath: nodePath,137 preferLocal,138 addExecPath: node,139 });140 }141142 return env;143};TweaksSindre Sorhusbefore adoption698a1b8
line 144why, one step back144