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: Mitigate vm #548 function redefinition issue #7624

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 21 additions & 13 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,19 +472,7 @@ function REPLServer(prompt,
}

var evalCmd = self.bufferedCommand + cmd;
if (/^\s*\{/.test(evalCmd) && /\}\s*$/.test(evalCmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
evalCmd = '(' + evalCmd + ')\n';
self.wrappedCmd = true;
} else {
// otherwise we just append a \n so that it will be either
// terminated, or continued onto the next expression if it's an
// unexpected end of input.
evalCmd = evalCmd + '\n';
}
evalCmd = preprocess(evalCmd);

debug('eval %j', evalCmd);
self.eval(evalCmd, self.context, 'repl', finish);
Expand Down Expand Up @@ -539,6 +527,26 @@ function REPLServer(prompt,
// Display prompt again
self.displayPrompt();
}

function preprocess(code) {
let cmd = code;
if (/^\s*\{/.test(cmd) && /\}\s*$/.test(cmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
cmd = `(${cmd})`;
self.wrappedCmd = true;
} else {
// Mitigate vm #548 issue
cmd = cmd.replace(/^\s*function\s+([^(]+)/,
(_, name) => `var ${name} = function ${name}`);
}
// Append a \n so that it will be either
// terminated, or continued onto the next expression if it's an
// unexpected end of input.
return `${cmd}\n`;
}
});

self.on('SIGCONT', function() {
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ function error_test() {
// or block comment. https://github.com/nodejs/node/issues/3611
{ client: client_unix, send: 'a = 3.5e',
expect: /^SyntaxError: Invalid or unexpected token/ },
// Mitigate #548 issue
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'd prefer this be the full URL to the issue rather than just the issue number. (Some issues scattered throughout the code are in nodejs/node-archive (formerly joyent/node, I believe) rather than nodejs/node and using the full URL eliminates guesswork. (Not the comment 3 lines above contains a full issue URL.)

{ client: client_unix, send: 'function name(){ return "node"; };name()',
expect: "'node'\n" + prompt_unix },
{ client: client_unix, send: 'function name(){ return "nodejs"; };name()',
expect: "'nodejs'\n" + prompt_unix },
]);
}

Expand Down