Fix invalid `stdio` values being accepted
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 516a5e2 · Browse files · 5 files·+126 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+126 added−4 removed
- tests:+82 added−0 removed
- other:+44 added−4 removed
Provenance
- Commit
c6bd387- Tree
6d488cd- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
516a5e2
5 files changed+126 added−4 removed
modified:lib/stdio/direction.js+31 added-3 removed
18 unmodified lines19202122232425262728293031323334352526363738393 unmodified lines4344454647484950515253545556575859606162636465666768697044717273747576777879808182838433 unmodified lines18 unmodified lines
return directions.find(Boolean) ?? DEFAULT_DIRECTION;};
const getStdioItemDirection = (stdioItem, fdNumber) => { const knownDirection = KNOWN_DIRECTIONS[fdNumber]; const direction = getRequestedDirection(stdioItem); if (knownDirection === undefined) { return direction; }
validateKnownDirection(stdioItem, knownDirection); return knownDirection;};
// `direction` is set when the user explicitly requests it, e.g. `{value: 'pipe', input: true}`.// It takes precedence over guessing, but not over the fixed direction of `stdin`/`stdout`/`stderr`.const getStdioItemDirection = (stdioItem, fdNumber) => KNOWN_DIRECTIONS[fdNumber] ?? getRequestedDirection(stdioItem);
// The explicitly requested `direction` must not contradict a value that is intrinsically the other direction.// For example, `{value: writableStream, input: true}` is invalid since a writable value can only be an output.const getRequestedDirection = ({type, value, direction, optionName}) => { const guessedDirection = guessStreamDirection[type](value);3 unmodified lines
return direction ?? guessedDirection;};
// `stdin`/`stdout`/`stderr` have a fixed direction, which each value must not contradict.// Otherwise, asynchronous methods pipe in the wrong direction and crash, e.g. with `dest.end is not a function`.// Only values whose direction is intrinsic to a stream are checked: other values (e.g. `Uint8Array`) are already// rejected in the wrong direction when their stream properties are added.const validateKnownDirection = ({type, value, optionName}, knownDirection) => { if (!STREAM_TYPES.has(type)) { return; }
const guessedDirection = guessStreamDirection[type](value); if (guessedDirection !== undefined && guessedDirection !== knownDirection) { const valueName = guessedDirection === 'input' ? 'readable' : 'writable'; throw new TypeError(`The \`${optionName}\` option is invalid: a ${valueName} value is always an ${guessedDirection}, but \`${optionName}\` is an ${knownDirection}.`); }};
const STREAM_TYPES = new Set(['native', 'nodeStream', 'webStream']);
// `stdin`/`stdout`/`stderr` have a known directionconst KNOWN_DIRECTIONS = ['input', 'output', 'output'];
const anyDirection = () => undefined;const alwaysInput = () => 'input';
// `string` can only be added through the `input` option, i.e. does not need to be handled here// `string` can only be added through the `input` option, i.e. on `stdin`, so it is always an inputconst guessStreamDirection = { generator: anyDirection, asyncGenerator: anyDirection, fileUrl: anyDirection, filePath: anyDirection, iterable: alwaysInput, asyncIterable: alwaysInput, uint8Array: alwaysInput, string: alwaysInput, webStream: value => isWritableStream(value) ? 'output' : 'input', nodeStream(value) { if (!isNodeReadableStream(value, {checkOpen: false})) { return 'output';33 unmodified linesmodified:lib/stdio/handle.js+11 added
129 unmodified lines130 131 const validateStreams = stdioItems => {132 for (const stdioItem of stdioItems) {133 validateFileStdio(stdioItem);134+ validateFileDescriptorStdio(stdioItem);135 }136 };137 138+// A number is a file descriptor. Anything else is not a valid one, and passing it to `node:child_process` aborts the current process with a fatal assertion instead of throwing.139+const validateFileDescriptorStdio = ({type, value, optionName}) => {140+ if (type === 'native' && typeof value === 'number' && (!Number.isInteger(value) || value < 0 || value > MAX_FILE_DESCRIPTOR)) {141+ throw new TypeError(`The \`${optionName}\` option must be a non-negative 32-bit integer: got ${value}.`);142+ }143+};144+145+// `node:child_process` converts file descriptors to a 32-bit signed integer, and aborts the process when it cannot146+const MAX_FILE_DESCRIPTOR = 2 ** 31 - 1;147+148 const validateFileStdio = ({type, value, optionName}) => {149 if (isRegularUrl(value)) {150 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.151 For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);103 unmodified linesmodified:lib/stdio/stdio-option.js+2 added-1 removed
83 unmodified lines84 && isOutputPipeOnly(stdioOption, fdNumber)85 ? 'ignore'86 : stdioOption);87 88+// An empty array is not output pipe only: it must keep being rejected as an empty array below89 const isOutputPipeOnly = (stdioOption, fdNumber) => isOutputPipe(stdioOption, fdNumber)89- || (Array.isArray(stdioOption) && stdioOption.every(item => isOutputPipe(item, fdNumber)));90+ || (Array.isArray(stdioOption) && stdioOption.length > 0 && stdioOption.every(item => isOutputPipe(item, fdNumber)));91 92 const isOutputPipe = (stdioOption, fdNumber) => stdioOption === 'pipe'93 || isOutputPipeObject(stdioOption, fdNumber);94 8 unmodified linesmodified:test/stdio/direction.js+56 added
4 unmodified lines5 import {execa, execaSync} from '../../index.js';6 import {getStdio} from '../helpers/stdio.js';7 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8 import {foobarString} from '../helpers/input.js';9+import {noopReadable, noopWritable} from '../helpers/stream.js';10 11 setFixtureDirectory();12 13 const testInputOutput = (t, stdioOption, execaMethod) => {121 unmodified lines135 t.throws(() => {136 execaSync('empty.js', {stdout: {value: 'pipe', input: 'yes'}, buffer: false});137 }, {message: /`stdout\.input` option must use a boolean/});138 });139+140+/*141+`stdin`/`stdout`/`stderr` have a fixed direction, so values that are intrinsically the other direction142+are invalid, like with additional file descriptors.143+Otherwise, asynchronous methods pipe in the wrong direction and crash, e.g. with `dest.end is not a function`.144+*/145+const testFixedDirection = (t, options, expectedMessage, execaMethod) => {146+ t.throws(() => {147+ execaMethod('empty.js', options);148+ }, {message: expectedMessage});149+};150+151+const readableOutputMessage = /a readable value is always an input, but `std(?:out|err)` is an output/;152+153+test('stdout cannot use a readable Node.js stream', testFixedDirection, {stdout: [noopReadable(), 'pipe']}, readableOutputMessage, execa);154+test('stdout cannot use a readable Node.js stream - sync', testFixedDirection, {stdout: [noopReadable(), 'pipe']}, readableOutputMessage, execaSync);155+test('stderr cannot use a readable Node.js stream', testFixedDirection, {stderr: [noopReadable(), 'pipe']}, readableOutputMessage, execa);156+test('stdout cannot use a readable web stream', testFixedDirection, {stdout: new ReadableStream()}, readableOutputMessage, execa);157+test('stdout cannot use a readable web stream - sync', testFixedDirection, {stdout: new ReadableStream()}, readableOutputMessage, execaSync);158+test('stdout { value: readable, input: true } is invalid', testFixedDirection, {stdout: {value: noopReadable(), input: true}}, readableOutputMessage, execa);159+160+test('stdin cannot use a writable Node.js stream', testFixedDirection, {stdin: [noopWritable(), 'pipe']}, /a writable value is always an output/, execa);161+test('stdin cannot use a writable Node.js stream - sync', testFixedDirection, {stdin: [noopWritable(), 'pipe']}, /a writable value is always an output/, execaSync);162+test('stdin cannot use a writable web stream', testFixedDirection, {stdin: new WritableStream()}, /a writable value is always an output/, execa);163+test('stdin cannot use a writable web stream - sync', testFixedDirection, {stdin: new WritableStream()}, /a writable value is always an output/, execaSync);164+165+test('stdout { value: writable, input: true } is invalid', testFixedDirection, {stdout: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execa);166+test('stdout { value: writable, input: true } is invalid - sync', testFixedDirection, {stdout: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execaSync);167+test('stdout { value: readable, input: true } is invalid - sync', testFixedDirection, {stdout: {value: noopReadable(), input: true}}, readableOutputMessage, execaSync);168+test('stderr cannot use a readable Node.js stream - sync', testFixedDirection, {stderr: [noopReadable(), 'pipe']}, readableOutputMessage, execaSync);169+test('stderr cannot use a readable web stream', testFixedDirection, {stderr: new ReadableStream()}, readableOutputMessage, execa);170+test('stderr cannot use a readable web stream - sync', testFixedDirection, {stderr: new ReadableStream()}, readableOutputMessage, execaSync);171+test('stderr { value: readable, input: true } is invalid', testFixedDirection, {stderr: {value: noopReadable(), input: true}}, readableOutputMessage, execa);172+test('stderr { value: readable, input: true } is invalid - sync', testFixedDirection, {stderr: {value: noopReadable(), input: true}}, readableOutputMessage, execaSync);173+test('stderr { value: web readable, input: true } is invalid', testFixedDirection, {stderr: {value: new ReadableStream(), input: true}}, readableOutputMessage, execa);174+test('stderr { value: web readable, input: true } is invalid - sync', testFixedDirection, {stderr: {value: new ReadableStream(), input: true}}, readableOutputMessage, execaSync);175+176+test('stdin { value: writable, input: true } is invalid', testFixedDirection, {stdin: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execa);177+test('stdin { value: writable, input: true } is invalid - sync', testFixedDirection, {stdin: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execaSync);178+test('stdin { value: writable web stream, input: true } is invalid', testFixedDirection, {stdin: {value: new WritableStream(), input: true}}, /cannot be used with a writable value/, execa);179+test('stdin { value: writable web stream, input: true } is invalid - sync', testFixedDirection, {stdin: {value: new WritableStream(), input: true}}, /cannot be used with a writable value/, execaSync);180+181+const writableStdinMessage = /a writable value is always an output/;182+183+test('stdin cannot use a writable Node.js stream after another value', testFixedDirection, {stdin: ['pipe', noopWritable()]}, writableStdinMessage, execa);184+test('stdin cannot use a writable Node.js stream after another value - sync', testFixedDirection, {stdin: ['pipe', noopWritable()]}, writableStdinMessage, execaSync);185+test('stdin cannot use a writable web stream in an array', testFixedDirection, {stdin: [new WritableStream(), 'pipe']}, writableStdinMessage, execa);186+test('stdin cannot use a writable web stream in an array - sync', testFixedDirection, {stdin: [new WritableStream(), 'pipe']}, writableStdinMessage, execaSync);187+test('stdin cannot use both readable and writable values', testFixedDirection, {stdin: [noopReadable(), noopWritable()]}, writableStdinMessage, execa);188+test('stdin cannot use both readable and writable values - sync', testFixedDirection, {stdin: [noopReadable(), noopWritable()]}, writableStdinMessage, execaSync);189+190+test('stdout cannot use a readable Node.js stream after another value', testFixedDirection, {stdout: ['pipe', noopReadable()]}, readableOutputMessage, execa);191+test('stdout cannot use a readable Node.js stream after another value - sync', testFixedDirection, {stdout: ['pipe', noopReadable()]}, readableOutputMessage, execaSync);192+test('stdout cannot use a readable web stream in an array', testFixedDirection, {stdout: [new ReadableStream(), 'pipe']}, readableOutputMessage, execa);193+test('stdout cannot use a readable web stream in an array - sync', testFixedDirection, {stdout: [new ReadableStream(), 'pipe']}, readableOutputMessage, execaSync);modified:test/stdio/handle-invalid.js+26 added
18 unmodified lines1920212223242526272829303132333435363744 unmodified lines82838485868788899091929394959697989910018 unmodified linestest('Cannot pass an empty array to stdout - sync', testEmptyArray, 1, 'stdout', execaSync);test('Cannot pass an empty array to stderr - sync', testEmptyArray, 2, 'stderr', execaSync);test('Cannot pass an empty array to stdio[*] - sync', testEmptyArray, 3, 'stdio[3]', execaSync);
const testEmptyArrayBufferFalse = (t, fdNumber, optionName, execaMethod) => { t.throws(() => { execaMethod('empty.js', {...getStdio(fdNumber, []), buffer: false}); }, {message: `The \`${optionName}\` option must not be an empty array.`});};
test('Cannot pass an empty array to stdout with buffer: false', testEmptyArrayBufferFalse, 1, 'stdout', execa);test('Cannot pass an empty array to stderr with buffer: false', testEmptyArrayBufferFalse, 2, 'stderr', execa);test('Cannot pass an empty array to stdout with buffer: false - sync', testEmptyArrayBufferFalse, 1, 'stdout', execaSync);test('Cannot pass an empty array to stderr with buffer: false - sync', testEmptyArrayBufferFalse, 2, 'stderr', execaSync);
const testInvalidValueSync = (t, fdNumber, stdioOption) => { const {message} = t.throws(() => { execaSync('empty.js', getStdio(fdNumber, stdioOption)); });44 unmodified linestest('Cannot pass "ipc" and another value to stdio[*]', testIpcStdioOption, 3, ['pipe', 'ipc'], execa);test('Cannot pass "ipc" and another value to stdio[*] - sync', testIpcStdioOption, 3, ['pipe', 'ipc'], execaSync);test('Cannot pass {value: "ipc"} and another value to stdio[*]', testIpcStdioOption, 3, ['pipe', {value: 'ipc'}], execa);test('Cannot pass {value: "ipc"} and another value to stdio[*] - sync', testIpcStdioOption, 3, ['pipe', {value: 'ipc'}], execaSync);
// A file descriptor must be a non-negative integer. Without this, `node:child_process` aborts the current process with a fatal assertion when spawning.const testInvalidFileDescriptor = (t, fdNumber, optionName, stdioOption, execaMethod) => { t.throws(() => { execaMethod('empty.js', getStdio(fdNumber, stdioOption)); }, {message: `The \`${optionName}\` option must be a non-negative 32-bit integer: got ${stdioOption}.`});};
test('Cannot pass a fractional file descriptor to stdin', testInvalidFileDescriptor, 0, 'stdin', 1.5, execa);test('Cannot pass NaN as a file descriptor to stdin', testInvalidFileDescriptor, 0, 'stdin', Number.NaN, execa);test('Cannot pass Infinity as a file descriptor to stdout', testInvalidFileDescriptor, 1, 'stdout', Number.POSITIVE_INFINITY, execa);test('Cannot pass a too large file descriptor to stderr', testInvalidFileDescriptor, 2, 'stderr', 2 ** 32, execa);test('Cannot pass a negative file descriptor to stdio[*]', testInvalidFileDescriptor, 3, 'stdio[3]', -1, execa);test('Cannot pass a fractional file descriptor to stdout', testInvalidFileDescriptor, 1, 'stdout', 1.5, execaSync);test('Cannot pass a negative file descriptor to stdin', testInvalidFileDescriptor, 0, 'stdin', -1, execaSync);