Skip to content

Commit 1c54630

Browse files
committed
chore(cli-repl,shell-api): filter autocompletions for CCs MONGOSH-1908
Collection and database names, as well as other completion items, can mostly contain arbitrary characters with only a few exceptions. Since some characters may mess with our REPL input, we should filter those autocompletions out.
1 parent 0eb682a commit 1c54630

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,24 @@ describe('CliRepl', function () {
24492449
await waitCompletion(cliRepl.bus);
24502450
expect(output).to.include('res.acknowledged');
24512451
});
2452+
2453+
it('completes only collection names that do not include control characters', async function () {
2454+
if (!hasCollectionNames) return;
2455+
2456+
input.write(
2457+
'db["actestcoll1"].insertOne({}); db["actestcoll2\\x1bfooobar"].insertOne({})\n'
2458+
);
2459+
await waitEval(cliRepl.bus);
2460+
input.write('db._getCollectionNames()\n'); // populate collection name cache
2461+
await waitEval(cliRepl.bus);
2462+
2463+
output = '';
2464+
input.write('db.actestc');
2465+
await tabtab();
2466+
await waitCompletion(cliRepl.bus);
2467+
expect(output).to.include('db.actestcoll1');
2468+
expect(output).to.not.include('db.actestcoll2');
2469+
});
24522470
});
24532471
}
24542472

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ import { Script, createContext, runInContext } from 'vm';
4545

4646
declare const __non_webpack_require__: any;
4747

48+
// eslint-disable-next-line no-control-regex
49+
const CONTROL_CHAR_REGEXP = /[\x00-\x1F\x7F-\x9F]/g;
50+
4851
/**
4952
* All CLI flags that are useful for {@link MongoshNodeRepl}.
5053
*/
@@ -434,7 +437,12 @@ class MongoshNodeRepl implements EvaluationListener {
434437
async (text: string): Promise<[string[], string]> => {
435438
this.insideAutoCompleteOrGetPrompt = true;
436439
try {
437-
return await innerCompleter(text);
440+
// eslint-disable-next-line prefer-const
441+
let [results, completeOn] = await innerCompleter(text);
442+
results = results.filter(
443+
(result) => !CONTROL_CHAR_REGEXP.test(result)
444+
);
445+
return [results, completeOn];
438446
} finally {
439447
this.insideAutoCompleteOrGetPrompt = false;
440448
}

packages/shell-api/src/shell-instance-state.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export interface ShellPlugin {
126126
transformError?: (err: Error) => Error;
127127
}
128128

129+
// eslint-disable-next-line no-control-regex
130+
const CONTROL_CHAR_REGEXP = /[\x00-\x1F\x7F-\x9F]/g;
131+
129132
/**
130133
* Anything to do with the state of the shell API and API objects is stored here.
131134
*
@@ -452,8 +455,10 @@ export default class ShellInstanceState {
452455
try {
453456
const collectionNames =
454457
await this.currentDb._getCollectionNamesForCompletion();
455-
return collectionNames.filter((name) =>
456-
name.toLowerCase().startsWith(collName.toLowerCase())
458+
return collectionNames.filter(
459+
(name) =>
460+
name.toLowerCase().startsWith(collName.toLowerCase()) &&
461+
!CONTROL_CHAR_REGEXP.test(name)
457462
);
458463
} catch (err: any) {
459464
if (
@@ -469,8 +474,10 @@ export default class ShellInstanceState {
469474
try {
470475
const dbNames =
471476
await this.currentDb._mongo._getDatabaseNamesForCompletion();
472-
return dbNames.filter((name) =>
473-
name.toLowerCase().startsWith(dbName.toLowerCase())
477+
return dbNames.filter(
478+
(name) =>
479+
name.toLowerCase().startsWith(dbName.toLowerCase()) &&
480+
!CONTROL_CHAR_REGEXP.test(name)
474481
);
475482
} catch (err: any) {
476483
if (

0 commit comments

Comments
 (0)