Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: make validateAbortSignal stricter #40657

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,9 @@ function abortChildProcess(child, killSignal) {
function spawn(file, args, options) {
options = normalizeSpawnArguments(file, args, options);
validateTimeout(options.timeout);
validateAbortSignal(options.signal, 'options.signal');
if (options.signal !== undefined)
validateAbortSignal(options.signal, 'options.signal');

const killSignal = sanitizeKillSignal(options.killSignal);
const child = new ChildProcess();

Expand Down
6 changes: 4 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ function getEventListeners(emitterOrTarget, type) {
*/
async function once(emitter, name, options = {}) {
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal !== undefined)
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
throw new AbortError();
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -886,7 +887,8 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
*/
function on(emitter, event, options) {
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal !== undefined)
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
throw new AbortError();

Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,8 @@ async function writeFile(path, data, options) {
data = Buffer.from(data, options.encoding || 'utf8');
}

validateAbortSignal(options.signal);
if (options.signal !== undefined)
validateAbortSignal(options.signal);
if (path instanceof FileHandle)
return writeFileHandle(path, data, options.signal, options.encoding);

Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ async function* watch(filename, options = {}) {

validateBoolean(persistent, 'options.persistent');
validateBoolean(recursive, 'options.recursive');
validateAbortSignal(signal, 'options.signal');
if (signal !== undefined)
validateAbortSignal(signal, 'options.signal');

if (encoding && !isEncoding(encoding)) {
const reason = 'is invalid encoding';
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ function eos(stream, options, callback) {
validateObject(options, 'options');
}
validateFunction(callback, 'callback');
validateAbortSignal(options.signal, 'options.signal');
if (options.signal !== undefined)
validateAbortSignal(options.signal, 'options.signal');

callback = once(callback);

Expand Down
3 changes: 2 additions & 1 deletion lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ function pipelineImpl(streams, callback, opts) {
const signal = ac.signal;
const outerSignal = opts?.signal;

validateAbortSignal(outerSignal, 'options.signal');
if (outerSignal !== undefined)
validateAbortSignal(outerSignal, 'options.signal');

function abort() {
finishImpl(new AbortError());
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,8 @@ const validateCallback = hideStackFrames((callback) => {
});

const validateAbortSignal = hideStackFrames((signal, name) => {
if (signal !== undefined &&
(signal === null ||
typeof signal !== 'object' ||
!('aborted' in signal))) {
if (signal === null || typeof signal !== 'object' ||
!('aborted' in signal)) {
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal);
}
});
Expand Down
23 changes: 14 additions & 9 deletions lib/timers/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ function setTimeout(after, value, options = {}) {
options));
}
const { signal, ref = true } = options;
try {
validateAbortSignal(signal, 'options.signal');
} catch (err) {
return PromiseReject(err);
if (signal !== undefined) {
try {
validateAbortSignal(signal, 'options.signal');
} catch (err) {
return PromiseReject(err);
}
}
if (typeof ref !== 'boolean') {
return PromiseReject(
Expand Down Expand Up @@ -85,10 +87,12 @@ function setImmediate(value, options = {}) {
options));
}
const { signal, ref = true } = options;
try {
validateAbortSignal(signal, 'options.signal');
} catch (err) {
return PromiseReject(err);
if (signal !== undefined) {
try {
validateAbortSignal(signal, 'options.signal');
} catch (err) {
return PromiseReject(err);
}
}
if (typeof ref !== 'boolean') {
return PromiseReject(
Expand Down Expand Up @@ -123,7 +127,8 @@ function setImmediate(value, options = {}) {
async function* setInterval(after, value, options = {}) {
validateObject(options, 'options');
const { signal, ref = true } = options;
validateAbortSignal(signal, 'options.signal');
if (signal !== undefined)
validateAbortSignal(signal, 'options.signal');
validateBoolean(ref, 'options.ref');

if (signal?.aborted)
Expand Down