From e4cc9619867e532914d1c4832eeac56ae8ebee08 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Fri, 6 Dec 2024 18:07:31 +0000 Subject: [PATCH] fix: Resilient to prototype pollution of Intl --- src/fake-timers-src.js | 9 +++++++-- test/issue-516-test.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/issue-516-test.js diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js index 03ad8d5..11dab90 100644 --- a/src/fake-timers-src.js +++ b/src/fake-timers-src.js @@ -206,7 +206,12 @@ function withGlobal(_global) { } const NativeDate = _global.Date; - const NativeIntl = _global.Intl; + const NativeIntl = isPresent.Intl + ? Object.defineProperties( + Object.create(null), + Object.getOwnPropertyDescriptors(_global.Intl), + ) + : undefined; let uniqueTimerId = idCounterStart; if (NativeDate === undefined) { @@ -1107,7 +1112,7 @@ function withGlobal(_global) { } if (isPresent.Intl) { - timers.Intl = _global.Intl; + timers.Intl = NativeIntl; } const originalSetTimeout = _global.setImmediate || _global.setTimeout; diff --git a/test/issue-516-test.js b/test/issue-516-test.js new file mode 100644 index 0000000..b21ed3b --- /dev/null +++ b/test/issue-516-test.js @@ -0,0 +1,20 @@ +"use strict"; + +const { FakeTimers } = require("./helpers/setup-tests"); + +describe("issue #516 - not resilient to changes on Intl", function () { + it("should successfully install the timer", function () { + const originalIntlProperties = Object.getOwnPropertyDescriptors( + global.Intl, + ); + for (const key of Object.keys(originalIntlProperties)) { + delete global.Intl[key]; + } + try { + const clock = FakeTimers.createClock(); + clock.tick(16); + } finally { + Object.defineProperties(global.Intl, originalIntlProperties); + } + }); +});