Skip to content

Commit b24606e

Browse files
baileympearsonorgads
authored andcommitted
feat(NODE-5396): add mongodb-js/saslprep as a required dependency (mongodb#3815)
(cherry picked from commit bd031fc)
1 parent 43673fa commit b24606e

File tree

6 files changed

+20
-106
lines changed

6 files changed

+20
-106
lines changed

package-lock.json

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

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
"email": "[email protected]"
2626
},
2727
"dependencies": {
28+
"@mongodb-js/saslprep": "^1.1.0",
2829
"bson": "^5.4.0",
2930
"mongodb-connection-string-url": "^2.6.0",
3031
"socks": "^2.7.1"
3132
},
32-
"optionalDependencies": {
33-
"@mongodb-js/saslprep": "^1.1.0"
34-
},
3533
"peerDependencies": {
3634
"@aws-sdk/credential-providers": "^3.188.0",
3735
"@mongodb-js/zstd": "^1.0.0",

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)

src/deps.ts

-14
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,6 @@ export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyE
126126
}
127127
}
128128

129-
export let saslprep:
130-
| typeof import('@mongodb-js/saslprep')
131-
| { kModuleError: MongoMissingDependencyError } = makeErrorModule(
132-
new MongoMissingDependencyError(
133-
'Optional module `saslprep` not found.' +
134-
' Please install it to enable Stringprep Profile for User Names and Passwords'
135-
)
136-
);
137-
138-
try {
139-
// Ensure you always wrap an optional require in the try block NODE-3199
140-
saslprep = require('saslprep');
141-
} catch {} // eslint-disable-line
142-
143129
interface AWS4 {
144130
/**
145131
* Created these inline types to better assert future usage of this API

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', 'socks'];
11+
const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url', 'socks', '@mongodb-js/saslprep'];
1112
const EXPECTED_PEER_DEPENDENCIES = [
1213
'@aws-sdk/credential-providers',
1314
'@mongodb-js/zstd',
@@ -19,7 +20,7 @@ const EXPECTED_PEER_DEPENDENCIES = [
1920
describe('package.json', function () {
2021
describe('dependencies', function () {
2122
it('only contains the expected dependencies', function () {
22-
expect(dependencies).to.have.keys(EXPECTED_DEPENDENCIES);
23+
expect(Object.keys(dependencies)).to.deep.equal(EXPECTED_DEPENDENCIES);
2324
});
2425
});
2526

@@ -112,7 +113,7 @@ describe('package.json', function () {
112113

113114
const EXPECTED_IMPORTS = [
114115
'bson',
115-
'saslprep',
116+
'@mongodb-js/saslprep',
116117
'sparse-bitfield',
117118
'memory-pager',
118119
'mongodb-connection-string-url',
@@ -147,7 +148,7 @@ describe('package.json', function () {
147148

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

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

test/integration/auth/scram_sha_256.test.ts

-64
This file was deleted.

0 commit comments

Comments
 (0)