-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NODE-5289): prevent scram auth from throwing TypeError if saslpre…
…p is not a function (#3727)
- Loading branch information
1 parent
0012460
commit 89b391c
Showing
3 changed files
with
70 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 () { | ||
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(); | ||
} | ||
}); | ||
|
||
context('when saslprep is not a function', () => { | ||
let client: MongoClient; | ||
|
||
beforeEach(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' | ||
); | ||
}); | ||
}); | ||
|
||
context('when saslprep is a function', () => { | ||
let client: MongoClient; | ||
|
||
beforeEach(function () { | ||
client = this.configuration.newClient({ authMechanism: 'SCRAM-SHA-256' }); | ||
}); | ||
|
||
afterEach(() => client.close()); | ||
|
||
it('calls saslprep', { requires: { auth: 'enabled' } }, async function () { | ||
const spy = sinon.spy(deps, 'saslprep'); | ||
|
||
await client.connect(); | ||
|
||
expect(spy).to.have.been.called; | ||
}); | ||
}); | ||
}); |