Skip to content

Commit e518d76

Browse files
committed
repl: do not cause side effects in tab completion
A number of recent changes to the REPL tab completion logic have introduced the ability for completion to cause side effects, specifically, calling arbitrary functions or variable assignments/updates. This was first introduced in 0722023 and the problem exacerbated in 8ba66c5. Our team noticed this because our tests started failing when attempting to update to Node.js 20.19.5. Some recent commits, such as 1093f38 or 6945337, have messages or PR descriptions that imply the intention to avoid side effects, which I can can generally be agreed upon is in line with the expectations that a user has of autocomplete functionality. However, some of the tests introduced in those commts specifically verify that side effects *can* happen under specific circunmstances. I am assuming here that this is unintentional, and the corresponding tests have been removed/replaced in this commit. Refs: #58709 Refs: #58775 Refs: #57909 Refs: #58891
1 parent 57bc177 commit e518d76

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

lib/repl.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,10 +1754,25 @@ function findExpressionCompleteTarget(code) {
17541754
return findExpressionCompleteTarget(argumentCode);
17551755
}
17561756

1757+
// Walk the AST for the current block of code, and check whether it contains any
1758+
// statement or expression type that would potentially have side effects if evaluated.
1759+
let isAllowed = true;
1760+
const disallow = () => isAllowed = false;
1761+
acornWalk.simple(lastBodyStatement, {
1762+
ForInStatement: disallow,
1763+
ForOfStatement: disallow,
1764+
CallExpression: disallow,
1765+
AssignmentExpression: disallow,
1766+
UpdateExpression: disallow,
1767+
});
1768+
if (!isAllowed) {
1769+
return null;
1770+
}
1771+
17571772
// If any of the above early returns haven't activated then it means that
17581773
// the potential complete target is the full code (e.g. the code represents
17591774
// a simple partial identifier, a member expression, etc...)
1760-
return code;
1775+
return code.slice(lastBodyStatement.start, lastBodyStatement.end);
17611776
}
17621777

17631778
/**

test/parallel/test-repl-completion-on-getters-disabled.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ describe('REPL completion in relation of getters', () => {
6161
test(`completions are generated for properties that don't trigger getters`, () => {
6262
runCompletionTests(
6363
`
64-
function getFooKey() {
65-
return "foo";
66-
}
67-
6864
const fooKey = "foo";
6965
7066
const keys = {
@@ -90,7 +86,6 @@ describe('REPL completion in relation of getters', () => {
9086
["objWithGetters[keys['foo key']].b", ["objWithGetters[keys['foo key']].bar"]],
9187
['objWithGetters[fooKey].b', ['objWithGetters[fooKey].bar']],
9288
["objWithGetters['f' + 'oo'].b", ["objWithGetters['f' + 'oo'].bar"]],
93-
['objWithGetters[getFooKey()].b', ['objWithGetters[getFooKey()].bar']],
9489
]);
9590
});
9691

test/parallel/test-repl-tab-complete-getter-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ async function runTest() {
2727

2828
await new Promise((resolve, reject) => {
2929
replServer.eval(`
30-
const getNameText = () => "name";
3130
const foo = { get name() { throw new Error(); } };
3231
`, replServer.context, '', (err) => {
3332
if (err) {
@@ -38,7 +37,7 @@ async function runTest() {
3837
});
3938
});
4039

41-
['foo.name.', 'foo["name"].', 'foo[getNameText()].'].forEach((test) => {
40+
['foo.name.', 'foo["name"].'].forEach((test) => {
4241
replServer.complete(
4342
test,
4443
common.mustCall((error, data) => {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
24+
const common = require('../common');
25+
const ArrayStream = require('../common/arraystream');
26+
const { describe, it } = require('node:test');
27+
const assert = require('assert');
28+
29+
const repl = require('repl');
30+
31+
function prepareREPL() {
32+
const input = new ArrayStream();
33+
const replServer = repl.start({
34+
prompt: '',
35+
input,
36+
output: process.stdout,
37+
allowBlockingCompletions: true,
38+
});
39+
40+
// Some errors are passed to the domain, but do not callback
41+
replServer._domain.on('error', assert.ifError);
42+
43+
return { replServer, input };
44+
}
45+
46+
function getNoResultsFunction() {
47+
return common.mustSucceed((data) => {
48+
assert.deepStrictEqual(data[0], []);
49+
});
50+
}
51+
52+
describe('REPL tab completion without side effects', () => {
53+
const setup = [
54+
'globalThis.counter = 0;',
55+
'function incCounter() { return counter++; }',
56+
'const arr = [{ bar: "baz" }];',
57+
];
58+
// None of these expressions should affect the value of `counter`
59+
for (const code of [
60+
'incCounter().',
61+
'a=(counter+=1).foo.',
62+
'a=(counter++).foo.',
63+
'for((counter)of[1])foo.',
64+
'for((counter)in{1:1})foo.',
65+
'arr[incCounter()].b',
66+
]) {
67+
it(`does not evaluate with side effects (${code})`, async () => {
68+
const { replServer, input } = prepareREPL();
69+
input.run(setup);
70+
71+
replServer.complete(code, getNoResultsFunction());
72+
73+
assert.strictEqual(replServer.context.counter, 0);
74+
replServer.close();
75+
});
76+
}
77+
});

0 commit comments

Comments
 (0)