From ba3c7ae1b2f72dbb02ed421e4574d8fe57165aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Newton/m=C2=B2?= <27069811+newtondividedbysqm@users.noreply.github.com> Date: Mon, 30 Mar 2026 19:48:50 +0200 Subject: [PATCH 1/2] fix assertString to throw TypeErrors on objects with custom constructor property --- src/lib/util/assertString.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/util/assertString.js b/src/lib/util/assertString.js index 3baa4452f..e62e1a624 100644 --- a/src/lib/util/assertString.js +++ b/src/lib/util/assertString.js @@ -1,4 +1,4 @@ export default function assertString(input) { if (input === undefined || input === null) throw new TypeError(`Expected a string but received a ${input}`); - if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`); + if (typeof input !== 'string' && !(input instanceof String)) throw new TypeError(`Expected a string but received a ${input?.constructor?.name ?? typeof input}`) } From 5dc7d99b3641194d25e957ec33df4fe438b9152f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Newton/m=C2=B2?= <27069811+newtondividedbysqm@users.noreply.github.com> Date: Mon, 30 Mar 2026 19:57:14 +0200 Subject: [PATCH 2/2] Add tests for boxed strings and Objects pretending to be a string --- test/util.test.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/util.test.js b/test/util.test.js index 0146a4e2c..5edbf0441 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -54,11 +54,23 @@ describe('assertString', () => { assert.throws(() => { assertString([]); }, TypeError); }); + it('Should throw an error if argument provided is an Object pretending to be a \'string\'', () => { + assert.throws(() => { assertString({constructor: {name: "string"}}); }, TypeError); + }); + + it('Should throw an error if argument provided is an Object pretending to be a \'String\'', () => { + assert.throws(() => { assertString({constructor: {name: "String"}}); }, TypeError); + }); + it('Should not throw an error if the argument is an empty string', () => { assert.doesNotThrow(() => { assertString(''); }); }); - + it('Should not throw an error if the argument is a String', () => { assert.doesNotThrow(() => { assertString('antidisestablishmentarianism'); }); }); + + it('Should not throw an error if the argument is a boxed string', () => { + assert.doesNotThrow(() => { assertString(new String('antidisestablishmentarianism')); }); + }); });