Skip to content

Commit 536e5ff

Browse files
authored
fix(NODE-3356): update redaction logic for command monitoring events (#2849)
1 parent abf01fc commit 536e5ff

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src/cmap/command_monitoring_events.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class CommandStartedEvent {
4141
this.requestId = command.requestId;
4242
this.databaseName = databaseName(command);
4343
this.commandName = commandName;
44-
this.command = cmd;
44+
this.command = maybeRedact(commandName, cmd, cmd);
4545
}
4646
}
4747

@@ -82,7 +82,7 @@ export class CommandSucceededEvent {
8282
this.requestId = command.requestId;
8383
this.commandName = commandName;
8484
this.duration = calculateDurationInMs(started);
85-
this.reply = maybeRedact(commandName, extractReply(command, reply));
85+
this.reply = maybeRedact(commandName, cmd, extractReply(command, reply));
8686
}
8787
}
8888

@@ -123,7 +123,7 @@ export class CommandFailedEvent {
123123
this.requestId = command.requestId;
124124
this.commandName = commandName;
125125
this.duration = calculateDurationInMs(started);
126-
this.failure = maybeRedact(commandName, error) as Error;
126+
this.failure = maybeRedact(commandName, cmd, error) as Error;
127127
}
128128
}
129129

@@ -140,13 +140,18 @@ const SENSITIVE_COMMANDS = new Set([
140140
'copydb'
141141
]);
142142

143+
const HELLO_COMMANDS = new Set(['hello', 'ismaster', 'isMaster']);
144+
143145
// helper methods
144146
const extractCommandName = (commandDoc: Document) => Object.keys(commandDoc)[0];
145147
const namespace = (command: WriteProtocolMessageType) => command.ns;
146148
const databaseName = (command: WriteProtocolMessageType) => command.ns.split('.')[0];
147149
const collectionName = (command: WriteProtocolMessageType) => command.ns.split('.')[1];
148-
const maybeRedact = (commandName: string, result?: Error | Document) =>
149-
SENSITIVE_COMMANDS.has(commandName) ? {} : result;
150+
const maybeRedact = (commandName: string, commandDoc: Document, result: Error | Document) =>
151+
SENSITIVE_COMMANDS.has(commandName) ||
152+
(HELLO_COMMANDS.has(commandName) && commandDoc.speculativeAuthenticate)
153+
? {}
154+
: result;
150155

151156
const LEGACY_FIND_QUERY_MAP: { [key: string]: string } = {
152157
$query: 'filter',

test/functional/apm.test.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ describe('APM', function () {
412412
expect(started).to.have.length(1);
413413
expect(succeeded).to.have.length(1);
414414
expect(failed).to.have.length(0);
415-
expect(started[0].commandObj).to.eql({ getnonce: true });
415+
expect(started[0].command).to.eql({});
416416
expect(succeeded[0].reply).to.eql({});
417417
return client.close();
418418
});
@@ -969,22 +969,12 @@ describe('APM', function () {
969969
describe('command monitoring unified spec tests', () => {
970970
for (const loadedSpec of loadSpecTests('command-monitoring/unified')) {
971971
expect(loadedSpec).to.include.all.keys(['description', 'tests']);
972-
// TODO: NODE-3356 unskip redaction tests
973-
const testsToSkip =
974-
loadedSpec.description === 'redacted-commands'
975-
? loadedSpec.tests
976-
.map(test => test.description)
977-
.filter(
978-
description =>
979-
description !== 'hello without speculative authenticate is not redacted'
980-
)
981-
: [];
982972
context(String(loadedSpec.description), function () {
983973
for (const test of loadedSpec.tests) {
984974
it(String(test.description), {
985975
metadata: { sessions: { skipLeakTests: true } },
986976
test: async function () {
987-
await runUnifiedTest(this, loadedSpec, test, testsToSkip);
977+
await runUnifiedTest(this, loadedSpec, test);
988978
}
989979
});
990980
}

0 commit comments

Comments
 (0)