-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(assertString): throw typeError on objects with constructor and name property #2692
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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}`) | ||||||
|
||||||
| if (typeof input !== 'string' && !(input instanceof String)) throw new TypeError(`Expected a string but received a ${input?.constructor?.name ?? typeof input}`) | |
| if (typeof input !== 'string' && !(input instanceof String)) throw new TypeError(`Expected a string but received a ${input?.constructor?.name ?? typeof input}`); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||||||||||||||
|
Comment on lines
+58
to
+62
|
||||||||||||||||||||||
| 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); | |
| 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); |
Copilot
AI
Mar 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This blank line contains trailing whitespace, which will fail no-trailing-spaces under airbnb-base ESLint. Remove the spaces so the line is truly empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instanceof Stringwill return false for boxed strings created in a different JS realm (e.g., iframe/window), which can cause valid String objects to be rejected. To avoid that cross-realm pitfall while still blocking{ constructor: { name: 'String' } }spoofing, consider checkingObject.prototype.toString.call(input) === '[object String]'(or reusing the existingtypeOfutil) alongside the primitivetypeof input === 'string'check.