Skip to content

Commit d6899a3

Browse files
fix sasl prep issue
1 parent dc068ac commit d6899a3

File tree

4 files changed

+51
-125
lines changed

4 files changed

+51
-125
lines changed

src/cmap/auth/scram.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as crypto from 'crypto';
22
import { promisify } from 'util';
33

44
import { Binary, type Document } from '../../bson';
5-
import { saslprep } from '../../deps';
5+
import * as deps from '../../deps';
66
import {
77
MongoInvalidArgumentError,
88
MongoMissingCredentialsError,
@@ -34,7 +34,10 @@ class ScramSHA extends AuthProvider {
3434
if (!credentials) {
3535
throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
3636
}
37-
if (cryptoMethod === 'sha256' && saslprep == null) {
37+
if (
38+
cryptoMethod === 'sha256' &&
39+
(!('kModuleError' in deps.saslprep) || typeof deps.saslprep !== 'function')
40+
) {
3841
emitWarning('Warning: no saslprep library specified. Passwords will not be sanitized');
3942
}
4043

@@ -140,7 +143,10 @@ async function continueScramConversation(
140143

141144
let processedPassword;
142145
if (cryptoMethod === 'sha256') {
143-
processedPassword = 'kModuleError' in saslprep ? password : saslprep(password);
146+
processedPassword =
147+
'kModuleError' in deps.saslprep || typeof deps.saslprep !== 'function'
148+
? password
149+
: deps.saslprep(password);
144150
} else {
145151
processedPassword = passwordDigest(username, password);
146152
}

test/integration/auth/scram_sha_256.test.js

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
3+
4+
import { deps, type MongoClient } from '../../mongodb';
5+
6+
describe('SCRAM_SHA_256', function () {
7+
context('when saslprep is not a function', () => {
8+
let client: MongoClient;
9+
beforeEach(function () {
10+
if (!this.configuration.parameters.authenticationMechanisms.includes('SCRAM-SHA-256')) {
11+
this.currentTest!.skipReason = 'Test requires that SCRAM-SHA-256 be enabled on the server.';
12+
this.currentTest!.skip();
13+
}
14+
});
15+
16+
beforeEach('setup mocks', function () {
17+
sinon.stub(deps, 'saslprep').value({});
18+
client = this.configuration.newClient({ authMechanism: 'SCRAM-SHA-256' });
19+
});
20+
21+
afterEach(() => {
22+
sinon.restore();
23+
return client.close();
24+
});
25+
26+
it('does not throw an error', { requires: { auth: 'enabled' } }, async function () {
27+
await client.connect();
28+
});
29+
30+
it('emits a warning', { requires: { auth: 'enabled' } }, async function () {
31+
const warnings: Array<Error> = [];
32+
process.once('warning', w => warnings.push(w));
33+
await client.connect();
34+
expect(warnings).to.have.lengthOf(1);
35+
expect(warnings[0]).to.have.property(
36+
'message',
37+
'Warning: no saslprep library specified. Passwords will not be sanitized'
38+
);
39+
});
40+
});
41+
});

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export * from '../src/cursor/list_indexes_cursor';
143143
export * from '../src/cursor/run_command_cursor';
144144
export * from '../src/db';
145145
export * from '../src/deps';
146+
export * as deps from '../src/deps';
146147
export * from '../src/encrypter';
147148
export * from '../src/error';
148149
export * from '../src/explain';

0 commit comments

Comments
 (0)