From 2b80d19c06f11d7a299b08e1868dca6a95e9917f Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 8 Feb 2023 14:29:09 +0100 Subject: [PATCH] lib: tighten `AbortSignal.prototype.throwIfAborted` implementation PR-URL: https://github.com/nodejs/node/pull/46521 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- lib/internal/abort_controller.js | 5 +++-- test/parallel/test-abortcontroller.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index c66f2a8367f82d..0b4d0fe359f632 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -150,8 +150,9 @@ class AbortSignal extends EventTarget { } throwIfAborted() { - if (this.aborted) { - throw this.reason; + validateThisAbortSignal(this); + if (this[kAborted]) { + throw this[kReason]; } } diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index ed7af146e35967..d0329f290c2584 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -254,3 +254,20 @@ const { setTimeout: sleep } = require('timers/promises'); const ac = new AbortController(); ac.signal.throwIfAborted(); } + +{ + const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted'); + const actualReason = new Error(); + Reflect.defineProperty(AbortSignal.prototype, 'aborted', { value: false }); + throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason); + Reflect.defineProperty(AbortSignal.prototype, 'aborted', originalDesc); +} + +{ + const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'reason'); + const actualReason = new Error(); + const fakeExcuse = new Error(); + Reflect.defineProperty(AbortSignal.prototype, 'reason', { value: fakeExcuse }); + throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason); + Reflect.defineProperty(AbortSignal.prototype, 'reason', originalDesc); +}