Skip to content

Commit 2910dca

Browse files
authored
fix(NODE-5536): remove credentials from ConnectionPoolCreatedEvent options (#3812)
1 parent 0c1b654 commit 2910dca

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

src/cmap/connection_pool_events.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ export class ConnectionPoolMonitoringEvent {
2828
*/
2929
export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
3030
/** The options used to create this connection pool */
31-
options?: ConnectionPoolOptions;
31+
options: Omit<ConnectionPoolOptions, 'credentials'> & { credentials?: Record<never, never> };
3232

3333
/** @internal */
3434
constructor(pool: ConnectionPool) {
3535
super(pool);
36-
this.options = pool.options;
36+
if (pool.options.credentials != null) {
37+
// Intentionally remove credentials: NODE-5460
38+
this.options = { ...pool.options, credentials: {} };
39+
} else {
40+
this.options = pool.options;
41+
}
3742
}
3843
}
3944

src/operations/operation.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ export const Aspect = {
1919
/** @public */
2020
export type Hint = string | Document;
2121

22-
export interface OperationConstructor extends Function {
23-
aspects?: Set<symbol>;
24-
}
25-
2622
/** @public */
2723
export interface OperationOptions extends BSONSerializeOptions {
2824
/** Specify ClientSession for this command */
@@ -96,7 +92,7 @@ export abstract class AbstractOperation<TResult = any> {
9692
): void;
9793

9894
hasAspect(aspect: symbol): boolean {
99-
const ctor = this.constructor as OperationConstructor;
95+
const ctor = this.constructor as { aspects?: Set<symbol> };
10096
if (ctor.aspects == null) {
10197
return false;
10298
}
@@ -122,7 +118,7 @@ export abstract class AbstractOperation<TResult = any> {
122118
}
123119

124120
export function defineAspects(
125-
operation: OperationConstructor,
121+
operation: { new (...args: any[]): any },
126122
aspects: symbol | symbol[] | Set<symbol>
127123
): Set<symbol> {
128124
if (!Array.isArray(aspects) && !(aspects instanceof Set)) {

test/integration/connection-monitoring-and-pooling/connection_monitoring_and_pooling.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { expect } from 'chai';
2+
import { once } from 'events';
3+
4+
import { MongoClient } from '../../../src';
15
import { loadSpecTests } from '../../spec';
26
import { CmapTest, runCmapTestSuite } from '../../tools/cmap_spec_runner';
37

@@ -16,4 +20,49 @@ describe('Connection Monitoring and Pooling (Node Driver)', function () {
1620
}
1721
]
1822
});
23+
24+
describe('ConnectionPoolCreatedEvent', () => {
25+
let client: MongoClient;
26+
beforeEach(async function () {
27+
client = this.configuration.newClient();
28+
});
29+
30+
afterEach(async function () {
31+
await client.close();
32+
});
33+
34+
describe('constructor()', () => {
35+
it('when auth is enabled redacts credentials from options', {
36+
metadata: { requires: { auth: 'enabled' } },
37+
async test() {
38+
const poolCreated = once(client, 'connectionPoolCreated');
39+
await client.connect();
40+
const [event] = await poolCreated;
41+
expect(event).to.have.deep.nested.property('options.credentials', {});
42+
43+
const poolOptions = Array.from(client.topology?.s.servers.values() ?? []).map(
44+
s => s.s.pool.options
45+
);
46+
expect(poolOptions).to.have.length.of.at.least(1);
47+
48+
for (const { credentials = {} } of poolOptions) {
49+
expect(
50+
Object.keys(credentials),
51+
'pool.options.credentials must exist and have keys'
52+
).to.not.equal(0);
53+
}
54+
}
55+
});
56+
57+
it('when auth is disabled does not add a credentials property to options', {
58+
metadata: { requires: { auth: 'disabled' } },
59+
async test() {
60+
const poolCreated = once(client, 'connectionPoolCreated');
61+
await client.connect();
62+
const [event] = await poolCreated;
63+
expect(event).to.not.have.nested.property('options.credentials');
64+
}
65+
});
66+
});
67+
});
1968
});

0 commit comments

Comments
 (0)