Skip to content
Myr

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

5 files+126 added−4 removed

modified:lib/stdio/direction.js+31 added-3 removed
modified:lib/stdio/handle.js+11 added
129 unmodified lines
130131const validateStreams = stdioItems => {132 for (const stdioItem of stdioItems) {133 validateFileStdio(stdioItem);134 validateFileDescriptorStdio(stdioItem);135 }136};137138// 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.139const 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};144145// `node:child_process` converts file descriptors to a 32-bit signed integer, and aborts the process when it cannot146const MAX_FILE_DESCRIPTOR = 2 ** 31 - 1;147148const validateFileStdio = ({type, value, optionName}) => {149 if (isRegularUrl(value)) {150 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.151For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);
103 unmodified lines
modified:lib/stdio/stdio-option.js+2 added-1 removed
83 unmodified lines
84 && isOutputPipeOnly(stdioOption, fdNumber)85 ? 'ignore'86 : stdioOption);8788// An empty array is not output pipe only: it must keep being rejected as an empty array below89const 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)));9192const isOutputPipe = (stdioOption, fdNumber) => stdioOption === 'pipe'93 || isOutputPipeObject(stdioOption, fdNumber);94
8 unmodified lines
modified:test/stdio/direction.js+56 added
4 unmodified lines
5import {execa, execaSync} from '../../index.js';6import {getStdio} from '../helpers/stdio.js';7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {foobarString} from '../helpers/input.js';9import {noopReadable, noopWritable} from '../helpers/stream.js';1011setFixtureDirectory();1213const testInputOutput = (t, stdioOption, execaMethod) => {
121 unmodified lines
135 t.throws(() => {136 execaSync('empty.js', {stdout: {value: 'pipe', input: 'yes'}, buffer: false});137 }, {message: /`stdout\.input` option must use a boolean/});138});139140/*141`stdin`/`stdout`/`stderr` have a fixed direction, so values that are intrinsically the other direction142are invalid, like with additional file descriptors.143Otherwise, asynchronous methods pipe in the wrong direction and crash, e.g. with `dest.end is not a function`.144*/145const testFixedDirection = (t, options, expectedMessage, execaMethod) => {146 t.throws(() => {147 execaMethod('empty.js', options);148 }, {message: expectedMessage});149};150151const readableOutputMessage = /a readable value is always an input, but `std(?:out|err)` is an output/;152153test('stdout cannot use a readable Node.js stream', testFixedDirection, {stdout: [noopReadable(), 'pipe']}, readableOutputMessage, execa);154test('stdout cannot use a readable Node.js stream - sync', testFixedDirection, {stdout: [noopReadable(), 'pipe']}, readableOutputMessage, execaSync);155test('stderr cannot use a readable Node.js stream', testFixedDirection, {stderr: [noopReadable(), 'pipe']}, readableOutputMessage, execa);156test('stdout cannot use a readable web stream', testFixedDirection, {stdout: new ReadableStream()}, readableOutputMessage, execa);157test('stdout cannot use a readable web stream - sync', testFixedDirection, {stdout: new ReadableStream()}, readableOutputMessage, execaSync);158test('stdout { value: readable, input: true } is invalid', testFixedDirection, {stdout: {value: noopReadable(), input: true}}, readableOutputMessage, execa);159160test('stdin cannot use a writable Node.js stream', testFixedDirection, {stdin: [noopWritable(), 'pipe']}, /a writable value is always an output/, execa);161test('stdin cannot use a writable Node.js stream - sync', testFixedDirection, {stdin: [noopWritable(), 'pipe']}, /a writable value is always an output/, execaSync);162test('stdin cannot use a writable web stream', testFixedDirection, {stdin: new WritableStream()}, /a writable value is always an output/, execa);163test('stdin cannot use a writable web stream - sync', testFixedDirection, {stdin: new WritableStream()}, /a writable value is always an output/, execaSync);164165test('stdout { value: writable, input: true } is invalid', testFixedDirection, {stdout: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execa);166test('stdout { value: writable, input: true } is invalid - sync', testFixedDirection, {stdout: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execaSync);167test('stdout { value: readable, input: true } is invalid - sync', testFixedDirection, {stdout: {value: noopReadable(), input: true}}, readableOutputMessage, execaSync);168test('stderr cannot use a readable Node.js stream - sync', testFixedDirection, {stderr: [noopReadable(), 'pipe']}, readableOutputMessage, execaSync);169test('stderr cannot use a readable web stream', testFixedDirection, {stderr: new ReadableStream()}, readableOutputMessage, execa);170test('stderr cannot use a readable web stream - sync', testFixedDirection, {stderr: new ReadableStream()}, readableOutputMessage, execaSync);171test('stderr { value: readable, input: true } is invalid', testFixedDirection, {stderr: {value: noopReadable(), input: true}}, readableOutputMessage, execa);172test('stderr { value: readable, input: true } is invalid - sync', testFixedDirection, {stderr: {value: noopReadable(), input: true}}, readableOutputMessage, execaSync);173test('stderr { value: web readable, input: true } is invalid', testFixedDirection, {stderr: {value: new ReadableStream(), input: true}}, readableOutputMessage, execa);174test('stderr { value: web readable, input: true } is invalid - sync', testFixedDirection, {stderr: {value: new ReadableStream(), input: true}}, readableOutputMessage, execaSync);175176test('stdin { value: writable, input: true } is invalid', testFixedDirection, {stdin: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execa);177test('stdin { value: writable, input: true } is invalid - sync', testFixedDirection, {stdin: {value: noopWritable(), input: true}}, /cannot be used with a writable value/, execaSync);178test('stdin { value: writable web stream, input: true } is invalid', testFixedDirection, {stdin: {value: new WritableStream(), input: true}}, /cannot be used with a writable value/, execa);179test('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);180181const writableStdinMessage = /a writable value is always an output/;182183test('stdin cannot use a writable Node.js stream after another value', testFixedDirection, {stdin: ['pipe', noopWritable()]}, writableStdinMessage, execa);184test('stdin cannot use a writable Node.js stream after another value - sync', testFixedDirection, {stdin: ['pipe', noopWritable()]}, writableStdinMessage, execaSync);185test('stdin cannot use a writable web stream in an array', testFixedDirection, {stdin: [new WritableStream(), 'pipe']}, writableStdinMessage, execa);186test('stdin cannot use a writable web stream in an array - sync', testFixedDirection, {stdin: [new WritableStream(), 'pipe']}, writableStdinMessage, execaSync);187test('stdin cannot use both readable and writable values', testFixedDirection, {stdin: [noopReadable(), noopWritable()]}, writableStdinMessage, execa);188test('stdin cannot use both readable and writable values - sync', testFixedDirection, {stdin: [noopReadable(), noopWritable()]}, writableStdinMessage, execaSync);189190test('stdout cannot use a readable Node.js stream after another value', testFixedDirection, {stdout: ['pipe', noopReadable()]}, readableOutputMessage, execa);191test('stdout cannot use a readable Node.js stream after another value - sync', testFixedDirection, {stdout: ['pipe', noopReadable()]}, readableOutputMessage, execaSync);192test('stdout cannot use a readable web stream in an array', testFixedDirection, {stdout: [new ReadableStream(), 'pipe']}, readableOutputMessage, execa);193test('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