Skip to content
Merged
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
18 changes: 18 additions & 0 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,24 @@ describe('CliRepl', function () {
await waitCompletion(cliRepl.bus);
expect(output).to.include('res.acknowledged');
});

it('completes only collection names that do not include control characters', async function () {
if (!hasCollectionNames) return;

input.write(
'db["actestcoll1"].insertOne({}); db["actestcoll2\\x1bfooobar"].insertOne({})\n'
);
await waitEval(cliRepl.bus);
input.write('db._getCollectionNames()\n'); // populate collection name cache
await waitEval(cliRepl.bus);

output = '';
input.write('db.actestc');
await tabtab();
await waitCompletion(cliRepl.bus);
expect(output).to.include('db.actestcoll1');
expect(output).to.not.include('db.actestcoll2');
});
});
}

Expand Down
10 changes: 9 additions & 1 deletion packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import { Script, createContext, runInContext } from 'vm';

declare const __non_webpack_require__: any;

// eslint-disable-next-line no-control-regex
const CONTROL_CHAR_REGEXP = /[\x00-\x1F\x7F-\x9F]/g;

/**
* All CLI flags that are useful for {@link MongoshNodeRepl}.
*/
Expand Down Expand Up @@ -434,7 +437,12 @@ class MongoshNodeRepl implements EvaluationListener {
async (text: string): Promise<[string[], string]> => {
this.insideAutoCompleteOrGetPrompt = true;
try {
return await innerCompleter(text);
// eslint-disable-next-line prefer-const
let [results, completeOn] = await innerCompleter(text);
results = results.filter(
(result) => !CONTROL_CHAR_REGEXP.test(result)
);
return [results, completeOn];
} finally {
this.insideAutoCompleteOrGetPrompt = false;
}
Expand Down
15 changes: 11 additions & 4 deletions packages/shell-api/src/shell-instance-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export interface ShellPlugin {
transformError?: (err: Error) => Error;
}

// eslint-disable-next-line no-control-regex
const CONTROL_CHAR_REGEXP = /[\x00-\x1F\x7F-\x9F]/g;

/**
* Anything to do with the state of the shell API and API objects is stored here.
*
Expand Down Expand Up @@ -452,8 +455,10 @@ export default class ShellInstanceState {
try {
const collectionNames =
await this.currentDb._getCollectionNamesForCompletion();
return collectionNames.filter((name) =>
name.toLowerCase().startsWith(collName.toLowerCase())
return collectionNames.filter(
(name) =>
name.toLowerCase().startsWith(collName.toLowerCase()) &&
!CONTROL_CHAR_REGEXP.test(name)
);
} catch (err: any) {
if (
Expand All @@ -469,8 +474,10 @@ export default class ShellInstanceState {
try {
const dbNames =
await this.currentDb._mongo._getDatabaseNamesForCompletion();
return dbNames.filter((name) =>
name.toLowerCase().startsWith(dbName.toLowerCase())
return dbNames.filter(
(name) =>
name.toLowerCase().startsWith(dbName.toLowerCase()) &&
!CONTROL_CHAR_REGEXP.test(name)
);
} catch (err: any) {
if (
Expand Down
Loading