errors: validate constructor name · nodejs/node@c65fafe · GitHub
Skip to content

Commit c65fafe

Browse files
christianaurichzmaduh95
authored andcommitted
errors: validate constructor name
determineSpecificType() checks for a usable constructor name with `'name' in value.constructor`. That accepts an empty name, and the `in` operator requires its right-hand side to be an object. Anonymous classes own a `name` that is the empty string, so they produce messages ending in a dangling "Received an instance of ". A truthy primitive `constructor` reaches the `in` operator and throws while the message is being built, so ERR_INVALID_ARG_TYPE is replaced by a TypeError carrying no `code`. The latter is reachable from untrusted input, since `constructor` is an ordinary JSON key. The check changed in #49696, while this function was rewritten as a switch. That pull request updated test/common's invalidArgTypeHelper to match its deliberate change to the `function` branch, but left the helper's `object` branch on the original truthy check, so the two have disagreed since. Read `constructor` and its `name` once and use the name only when it is a non-empty string. Requiring a string also stops meaningless names from being interpolated: 42 currently yields "an instance of 42", and a symbol name throws outright. The current check reads `constructor` three times, which an accessor can observe. Move the helper to the same check so the two cannot drift apart again. Signed-off-by: Christian Aurich <christian.aurichzm@gmail.com> PR-URL: #65607 Refs: #49696 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b23928a commit c65fafe

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎lib/internal/errors.js‎

Lines changed: 8 additions & 3 deletions

‎test/common/index.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,9 @@ function invalidArgTypeHelper(input) {
807807
return ` Received function ${input.name}`;
808808
}
809809
if (typeof input === 'object') {
810-
if (input.constructor?.name) {
811-
return ` Received an instance of ${input.constructor.name}`;
810+
const name = input.constructor?.name;
811+
if (typeof name === 'string' && name !== '') {
812+
return ` Received an instance of ${name}`;
812813
}
813814
return ` Received ${inspect(input, { depth: -1 })}`;
814815
}

‎test/parallel/test-error-value-type-detection.mjs‎

Lines changed: 55 additions & 0 deletions

0 commit comments

Comments
 (0)