Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5289): prevent scram auth from throwing TypeError if saslprep is not a function #3727

Merged
merged 4 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/cmap/auth/scram.ts
Original file line number Diff line number Diff line change
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 saslprep || typeof saslprep !== 'function')
) {
emitWarning('Warning: no saslprep library specified. Passwords will not be sanitized');
}

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

let processedPassword;
if (cryptoMethod === 'sha256') {
processedPassword = 'kModuleError' in saslprep ? password : saslprep(password);
processedPassword =
'kModuleError' in saslprep || typeof saslprep !== 'function' ? password : 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.

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

// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import * as deps from '../../../src/deps';
import { type MongoClient } from '../../mongodb';

describe('SCRAM_SHA_256', function () {
context('when saslprep is not a function', () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
let client: MongoClient;
beforeEach(function () {
if (!this.configuration.parameters.authenticationMechanisms.includes('SCRAM-SHA-256')) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.currentTest!.skipReason = 'Test requires that SCRAM-SHA-256 be enabled on the server.';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
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';
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
export * from '../src/encrypter';
export * from '../src/error';
export * from '../src/explain';
Expand Down