From 35570e970eeb5b0f5e1dcd279914383180d76a18 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 c71e5d1274afcc..74217075798ced 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 097d632afa7d1c..ce6c72f4294043 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); +}