Skip to content

Commit

Permalink
fix sasl prep issue
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Jun 15, 2023
1 parent dc068ac commit d6899a3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 125 deletions.
12 changes: 9 additions & 3 deletions src/cmap/auth/scram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as crypto from 'crypto';
import { promisify } from 'util';

import { Binary, type Document } from '../../bson';
import { saslprep } from '../../deps';
import * as deps from '../../deps';
import {
MongoInvalidArgumentError,
MongoMissingCredentialsError,
Expand Down Expand Up @@ -34,7 +34,10 @@ class ScramSHA extends AuthProvider {
if (!credentials) {
throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
}
if (cryptoMethod === 'sha256' && saslprep == null) {
if (
cryptoMethod === 'sha256' &&
(!('kModuleError' in deps.saslprep) || typeof deps.saslprep !== 'function')
) {
emitWarning('Warning: no saslprep library specified. Passwords will not be sanitized');
}

Expand Down Expand Up @@ -140,7 +143,10 @@ async function continueScramConversation(

let processedPassword;
if (cryptoMethod === 'sha256') {
processedPassword = 'kModuleError' in saslprep ? password : saslprep(password);
processedPassword =
'kModuleError' in deps.saslprep || typeof deps.saslprep !== 'function'
? password
: deps.saslprep(password);
} else {
processedPassword = passwordDigest(username, password);
}
Expand Down
122 changes: 0 additions & 122 deletions test/integration/auth/scram_sha_256.test.js

This file was deleted.

41 changes: 41 additions & 0 deletions test/integration/auth/scram_sha_256.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import * as sinon from 'sinon';

import { deps, type MongoClient } from '../../mongodb';

describe('SCRAM_SHA_256', function () {
context('when saslprep is not a function', () => {
let client: MongoClient;
beforeEach(function () {
if (!this.configuration.parameters.authenticationMechanisms.includes('SCRAM-SHA-256')) {
this.currentTest!.skipReason = 'Test requires that SCRAM-SHA-256 be enabled on the server.';
this.currentTest!.skip();
}
});

beforeEach('setup mocks', function () {
sinon.stub(deps, 'saslprep').value({});
client = this.configuration.newClient({ authMechanism: 'SCRAM-SHA-256' });
});

afterEach(() => {
sinon.restore();
return client.close();
});

it('does not throw an error', { requires: { auth: 'enabled' } }, async function () {
await client.connect();
});

it('emits a warning', { requires: { auth: 'enabled' } }, async function () {
const warnings: Array<Error> = [];
process.once('warning', w => warnings.push(w));
await client.connect();
expect(warnings).to.have.lengthOf(1);
expect(warnings[0]).to.have.property(
'message',
'Warning: no saslprep library specified. Passwords will not be sanitized'
);
});
});
});
1 change: 1 addition & 0 deletions test/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export * from '../src/cursor/list_indexes_cursor';
export * from '../src/cursor/run_command_cursor';
export * from '../src/db';
export * from '../src/deps';
export * as deps from '../src/deps';
export * from '../src/encrypter';
export * from '../src/error';
export * from '../src/explain';
Expand Down

0 comments on commit d6899a3

Please sign in to comment.