Skip to content
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

repl: handle unexpected error objects #12400

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,25 @@ function REPLServer(prompt,
self._domain.on('error', function debugDomainError(e) {
debug('domain error');
const top = replMap.get(self);
var out;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't moved over to let completely yet.

internalUtil.decorateErrorStack(e);
if (e instanceof SyntaxError && e.stack) {

if (!((e instanceof Error) && typeof e.stack === 'string' &&
e.stack.length > 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how robust you want this code to be, but the typeof e.stack will crash with

function FakeError() {}
FakeError.prototype = Object.create(Error.prototype, {
  stack: { get() { throw new Error(); } }
});
var e = new FakeError();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how others feel, but I'm OK with it, at least in the context of this PR. It seems like any Node application can be crashed with a well crafted getter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that I would (personally) prefer is either using V8’s IsNativeError over instanceof Error or going full 🦆 here. Both would increase robustness in different ways :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that whatever is done here would need to factor in compatibility with internal/errors.js

out = util.inspect(e);
} else if (e instanceof SyntaxError) {
// remove repl:line-number and stack trace
e.stack = e.stack
.replace(/^repl:\d+\r?\n/, '')
.replace(/^\s+at\s.*\n?/gm, '');
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
(_, pre, line) => pre + (line - 1));
out = e.stack
.replace(/^repl:\d+\r?\n/, '')
.replace(/^\s+at\s.*\n?/gm, '');
} else if (self.replMode === exports.REPL_MODE_STRICT) {
out = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
(_, pre, line) => pre + (line - 1));
} else {
out = e.stack;
}
top.outputStream.write((e.stack || e) + '\n');

top.outputStream.write(out + '\n');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fail if e is a Symbol, won't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about if delete e.toString?

top.bufferedCommand = '';
top.lines.level = [];
top.displayPrompt();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-sigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ child.stdout.once('data', common.mustCall(() => {
child.on('close', function(code) {
assert.strictEqual(code, 0);
assert.ok(
stdout.includes('Script execution interrupted.\n'),
stdout.includes('Script execution interrupted.'),
`Expected stdout to contain "Script execution interrupted.", got ${stdout}`
);
assert.ok(
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ function error_test() {
// Uncaught error throws and prints out
{ client: client_unix, send: 'throw new Error(\'test error\');',
expect: /^Error: test error/ },
// Throws primitive and prints out
{ client: client_unix, send: 'throw null;',
expect: 'null' },
// Throws Object without toString() method and prints out
{ client: client_unix, send: 'throw Object.create(null);',
expect: '{}' },
// Throws Object with bad toString() method and prints out
{
client: client_unix,
send: 'var e = { toString() { throw new Error(\'test\'); } }; throw e;',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even smarter! 👍

expect: /{ toString: \[Function: toString\] }/
},
// Throws Symbol and prints out
{ client: client_unix, send: 'throw Symbol(\'test\');',
expect: /^Symbol\(test\)/ },
// Throws Error without stack and prints out
{
client: client_unix,
send: 'var e = new Error(\'test error\'); delete e.stack; throw e;',
expect: /^\[Error: test error\]/
},
// Common syntax error is treated as multiline command
{ client: client_unix, send: 'function test_func() {',
expect: prompt_multiline },
Expand Down