Skip to content

Commit cff9e6a

Browse files
committed
util: safely inspect getter errors whose message throws
1 parent cdc3ca8 commit cff9e6a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/internal/util/inspect.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,14 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
25572557
}
25582558
ctx.indentationLvl -= 2;
25592559
} catch (err) {
2560-
const message = `<Inspection threw (${err.message})>`;
2560+
let messageSuffix;
2561+
try {
2562+
// Error message itself may be a getter
2563+
messageSuffix = ` (${err.message})`;
2564+
} catch {
2565+
messageSuffix = '';
2566+
}
2567+
const message = `<Inspection threw${messageSuffix}>`;
25612568
str = `${s(`[${label}:`, sp)} ${message}${s(']', sp)}`;
25622569
}
25632570
} else {

test/parallel/test-util-inspect.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,24 @@ assert.strictEqual(
25512551
"'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}");
25522552
}
25532553

2554+
// https://owasp.org/Top10/2025/A10_2025-Mishandling_of_Exceptional_Conditions/
2555+
// Test for property getter throwing an error with a bad message.
2556+
{
2557+
const error = {
2558+
// The message itself is a getter that throws
2559+
get message() { throw new Error('Oops'); }
2560+
};
2561+
2562+
const thrower = {
2563+
get foo() { throw error; }
2564+
};
2565+
2566+
assert.strictEqual(
2567+
inspect(thrower, { getters: true }),
2568+
'{ foo: [Getter: <Inspection threw>] }'
2569+
);
2570+
}
2571+
25542572
// Check compact number mode.
25552573
{
25562574
let obj = {

0 commit comments

Comments
 (0)