Skip to content

Commit bd031fc

Browse files
feat(NODE-5396): add mongodb-js/saslprep as a required dependency (#3815)
1 parent fd9a467 commit bd031fc

File tree

6 files changed

+22
-106
lines changed

6 files changed

+22
-106
lines changed

Diff for: package-lock.json

+11-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
},
2727
"dependencies": {
2828
"bson": "^5.4.0",
29-
"mongodb-connection-string-url": "^2.6.0"
30-
},
31-
"optionalDependencies": {
32-
"saslprep": "^1.0.3"
29+
"mongodb-connection-string-url": "^2.6.0",
30+
"@mongodb-js/saslprep": "^1.1.0"
3331
},
3432
"peerDependencies": {
3533
"@aws-sdk/credential-providers": "^3.188.0",

Diff for: src/cmap/auth/scram.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { saslprep } from '@mongodb-js/saslprep';
12
import * as crypto from 'crypto';
23
import { promisify } from 'util';
34

45
import { Binary, type Document } from '../../bson';
5-
import { saslprep } from '../../deps';
66
import {
77
MongoInvalidArgumentError,
88
MongoMissingCredentialsError,
99
MongoRuntimeError
1010
} from '../../error';
11-
import { emitWarning, ns } from '../../utils';
11+
import { ns } from '../../utils';
1212
import type { HandshakeDocument } from '../connect';
1313
import { type AuthContext, AuthProvider } from './auth_provider';
1414
import type { MongoCredentials } from './mongo_credentials';
@@ -34,12 +34,6 @@ class ScramSHA extends AuthProvider {
3434
if (!credentials) {
3535
throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
3636
}
37-
if (
38-
cryptoMethod === 'sha256' &&
39-
('kModuleError' in saslprep || typeof saslprep !== 'function')
40-
) {
41-
emitWarning('Warning: no saslprep library specified. Passwords will not be sanitized');
42-
}
4337

4438
const nonce = await this.randomBytesAsync(24);
4539
// store the nonce for later use
@@ -141,13 +135,8 @@ async function continueScramConversation(
141135
const username = cleanUsername(credentials.username);
142136
const password = credentials.password;
143137

144-
let processedPassword;
145-
if (cryptoMethod === 'sha256') {
146-
processedPassword =
147-
'kModuleError' in saslprep || typeof saslprep !== 'function' ? password : saslprep(password);
148-
} else {
149-
processedPassword = passwordDigest(username, password);
150-
}
138+
const processedPassword =
139+
cryptoMethod === 'sha256' ? saslprep(password) : passwordDigest(username, password);
151140

152141
const payload = Buffer.isBuffer(response.payload)
153142
? new Binary(response.payload)

Diff for: src/deps.ts

-13
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,6 @@ export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyErr
189189
}
190190
}
191191

192-
export let saslprep: typeof import('saslprep') | { kModuleError: MongoMissingDependencyError } =
193-
makeErrorModule(
194-
new MongoMissingDependencyError(
195-
'Optional module `saslprep` not found.' +
196-
' Please install it to enable Stringprep Profile for User Names and Passwords'
197-
)
198-
);
199-
200-
try {
201-
// Ensure you always wrap an optional require in the try block NODE-3199
202-
saslprep = require('saslprep');
203-
} catch {} // eslint-disable-line
204-
205192
interface AWS4 {
206193
/**
207194
* Created these inline types to better assert future usage of this API

Diff for: test/action/dependency.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import * as path from 'node:path';
55
import { expect } from 'chai';
66

77
import { dependencies, peerDependencies, peerDependenciesMeta } from '../../package.json';
8+
import { setDifference } from '../mongodb';
89
import { itInNodeProcess } from '../tools/utils';
910

10-
const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url'];
11+
const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url', '@mongodb-js/saslprep'];
1112
const EXPECTED_PEER_DEPENDENCIES = [
1213
'@aws-sdk/credential-providers',
1314
'@mongodb-js/zstd',
@@ -21,7 +22,7 @@ const EXPECTED_PEER_DEPENDENCIES = [
2122
describe('package.json', function () {
2223
describe('dependencies', function () {
2324
it('only contains the expected dependencies', function () {
24-
expect(dependencies).to.have.keys(EXPECTED_DEPENDENCIES);
25+
expect(Object.keys(dependencies)).to.deep.equal(EXPECTED_DEPENDENCIES);
2526
});
2627
});
2728

@@ -118,7 +119,7 @@ describe('package.json', function () {
118119

119120
const EXPECTED_IMPORTS = [
120121
'bson',
121-
'saslprep',
122+
'@mongodb-js/saslprep',
122123
'sparse-bitfield',
123124
'memory-pager',
124125
'mongodb-connection-string-url',
@@ -150,7 +151,7 @@ describe('package.json', function () {
150151

151152
context('when importing mongodb', () => {
152153
it('only contains the expected imports', function () {
153-
expect(imports).to.deep.equal(EXPECTED_IMPORTS);
154+
expect(setDifference(imports, EXPECTED_IMPORTS)).to.deep.equal(new Set());
154155
});
155156

156157
it('does not import optional dependencies', () => {

Diff for: test/integration/auth/scram_sha_256.test.ts

-64
This file was deleted.

0 commit comments

Comments
 (0)