Skip to content

Commit 900b354

Browse files
committed
WIP - chore: use redact package
1 parent ed0f87c commit 900b354

File tree

6 files changed

+93
-18
lines changed

6 files changed

+93
-18
lines changed

packages/history/src/history.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import redactSensitiveData from 'mongodb-redact';
2-
3-
export const HIDDEN_COMMANDS = String.raw`\b(createUser|auth|updateUser|changeUserPassword|connect|Mongo)\b`;
1+
import { shouldRedactCommand } from './redact';
2+
import { redact } from 'mongodb-redact';
43

54
/**
65
* Modifies the most recent command in history based on sensitive information.
@@ -11,16 +10,13 @@ export const HIDDEN_COMMANDS = String.raw`\b(createUser|auth|updateUser|changeUs
1110
*/
1211
export function changeHistory(
1312
history: string[],
14-
redact: 'redact-sensitive-data' | 'keep-sensitive-data'
13+
redactMode: 'redact-sensitive-data' | 'keep-sensitive-data'
1514
): void {
1615
if (history.length === 0) return;
17-
const hiddenCommands = new RegExp(HIDDEN_COMMANDS, 'g');
1816

19-
if (hiddenCommands.test(history[0])) {
17+
if (shouldRedactCommand(history[0])) {
2018
history.shift();
21-
} else if (redact === 'redact-sensitive-data') {
22-
history[0] = redactSensitiveData(history[0]);
19+
} else if (redactMode === 'redact-sensitive-data') {
20+
history[0] = redact(history[0]);
2321
}
2422
}
25-
26-
export { redactSensitiveData };

packages/history/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
export { changeHistory, redactSensitiveData, HIDDEN_COMMANDS } from './history';
2-
export { redactConnectionString as redactURICredentials } from 'mongodb-connection-string-url';
1+
export { changeHistory } from './history';
2+
export {
3+
shouldRedactCommand,
4+
redactUriCredentials as redactURICredentials,
5+
} from './redact';

packages/history/src/redact.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// TODO: This will completely be replaced by mongodb-redact once it is released
2+
3+
/**
4+
* Regex pattern for commands that contain sensitive information and should be
5+
* completely removed from history rather than redacted.
6+
*
7+
* These commands typically involve authentication or connection strings with credentials.
8+
*/
9+
const HIDDEN_COMMANDS = String.raw`\b(createUser|auth|updateUser|changeUserPassword|connect|Mongo)\b`;
10+
11+
/**
12+
* Checks if a mongosh command should be redacted because it often contains sensitive information like credentials.
13+
*
14+
* @param input - The command string to check
15+
* @returns true if the command should be hidden/redacted, false otherwise
16+
*
17+
* @example
18+
* ```typescript
19+
* shouldRedactCommand('db.createUser({user: "admin", pwd: "secret"})')
20+
* // Returns: true
21+
*
22+
* shouldRedactCommand('db.getUsers()')
23+
* // Returns: false
24+
* ```
25+
*/
26+
export function shouldRedactCommand(input: string): boolean {
27+
const hiddenCommands = new RegExp(HIDDEN_COMMANDS, 'g');
28+
return hiddenCommands.test(input);
29+
}
30+
31+
import { redactConnectionString } from 'mongodb-connection-string-url';
32+
33+
export function redactUriCredentials(uri: string): string {
34+
return redactConnectionString(uri);
35+
}

packages/logging/src/logging-and-telemetry.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,42 @@ describe('MongoshLoggingAndTelemetry', function () {
11321132
expect(analyticsOutput).to.be.empty;
11331133
});
11341134

1135+
it('does not log sensitive commands', async function () {
1136+
loggingAndTelemetry.attachLogger(logger);
1137+
await (loggingAndTelemetry as LoggingAndTelemetry).setupTelemetryPromise;
1138+
1139+
expect(logOutput).to.have.lengthOf(0);
1140+
1141+
// Test that sensitive commands are not logged
1142+
bus.emit('mongosh:evaluate-input', {
1143+
input: 'db.createUser({user: "admin", pwd: "password", roles: []})',
1144+
});
1145+
bus.emit('mongosh:evaluate-input', { input: 'db.auth("user", "pass")' });
1146+
bus.emit('mongosh:evaluate-input', {
1147+
input: 'db.updateUser("user", {pwd: "newpass"})',
1148+
});
1149+
bus.emit('mongosh:evaluate-input', {
1150+
input: 'db.changeUserPassword("user", "newpass")',
1151+
});
1152+
bus.emit('mongosh:evaluate-input', {
1153+
input: 'connect("mongodb://user:pass@localhost/")',
1154+
});
1155+
bus.emit('mongosh:evaluate-input', {
1156+
input: 'new Mongo("mongodb://user:pass@localhost/")',
1157+
});
1158+
1159+
// Test that non-sensitive commands are still logged
1160+
bus.emit('mongosh:evaluate-input', { input: 'db.getUsers()' });
1161+
bus.emit('mongosh:evaluate-input', { input: 'show dbs' });
1162+
1163+
// Should only have logged the non-sensitive commands
1164+
expect(logOutput).to.have.lengthOf(2);
1165+
expect(logOutput[0].msg).to.equal('Evaluating input');
1166+
expect(logOutput[0].attr.input).to.equal('db.getUsers()');
1167+
expect(logOutput[1].msg).to.equal('Evaluating input');
1168+
expect(logOutput[1].attr.input).to.equal('show dbs');
1169+
});
1170+
11351171
it('tracks custom logging events', async function () {
11361172
expect(logOutput).to.have.lengthOf(0);
11371173
expect(analyticsOutput).to.be.empty;

packages/logging/src/logging-and-telemetry.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import redactInfo from 'mongodb-redact';
2-
import { redactURICredentials } from '@mongosh/history';
1+
import { redact } from 'mongodb-redact';
2+
import { shouldRedactCommand } from '@mongosh/history';
33
import type {
44
MongoshBus,
55
ApiEventWithArguments,
@@ -460,6 +460,11 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
460460
});
461461

462462
onBus('mongosh:evaluate-input', (args: EvaluateInputEvent) => {
463+
// Skip logging sensitive commands
464+
if (shouldRedactCommand(args.input)) {
465+
return;
466+
}
467+
463468
this.log.info(
464469
'MONGOSH',
465470
mongoLogId(1_000_000_007),
@@ -523,7 +528,7 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
523528
mongoLogId(1_000_000_011),
524529
'shell-api',
525530
'Performed API call',
526-
redactInfo(arg)
531+
redact(arg)
527532
);
528533
});
529534

@@ -848,7 +853,7 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
848853
mongoLogId(1_000_000_047),
849854
'editor',
850855
'Open external editor',
851-
redactInfo(ev)
856+
redact(ev)
852857
);
853858
}
854859
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
ShellUserConfig,
1818
} from '@mongosh/types';
1919
import { EventEmitter } from 'events';
20-
import redactInfo from 'mongodb-redact';
20+
import { redactURICredentials } from '@mongosh/history';
2121
import { toIgnore } from './decorators';
2222
import {
2323
ALL_PLATFORMS,
@@ -304,7 +304,7 @@ export class ShellInstanceState {
304304
api_version: apiVersionInfo?.version,
305305
api_strict: apiVersionInfo?.strict,
306306
api_deprecation_errors: apiVersionInfo?.deprecationErrors,
307-
uri: redactInfo(connectionInfo?.extraInfo?.uri),
307+
uri: redactURICredentials(connectionInfo?.extraInfo?.uri ?? ''),
308308
});
309309
return connectionInfo;
310310
}

0 commit comments

Comments
 (0)