Skip to content

fix(NODE-7548): SCRAM authentication fails on non-Node runtimes#4932

Open
PavelSafronov wants to merge 8 commits intomainfrom
node-7548-scram-deno
Open

fix(NODE-7548): SCRAM authentication fails on non-Node runtimes#4932
PavelSafronov wants to merge 8 commits intomainfrom
node-7548-scram-deno

Conversation

@PavelSafronov
Copy link
Copy Markdown
Contributor

@PavelSafronov PavelSafronov commented Apr 30, 2026

Description

Summary of Changes

This fixes the deno-specific issue where SCRAM is broken. The bug happens because we are invoking toString implicitly, and this results in a behavior change. The fix is to convert the bytes to string explicitly.

Part of the reason that this was not caught in testing is because we were still targeting node when creating the bundle.

Switching bundler to web exposed a few issues:

  • removed last few Buffer.readInt32LE calls
  • skipped more tests in nodeless where sinon can cause test failures
  • supply globals that are not available in web (TextDecoder, TextEncoder, atob, btoa)
  • export the created sandbox so our tests can verify that we aren't exporting things like Buffer
  • update tests that verify error types
  • update the bundle barrow file with missing exports
Notes for Reviewers

What is the motivation for this change?

Customer-created ticket. Driver is broken infor deno.

Release Highlight

Release notes highlight

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

PavelSafronov and others added 5 commits April 29, 2026 14:56
… call

also updated bundling logic to correctly remove Buffer. 9 tests are currently failing, with failures that are not related to scram.

Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
@PavelSafronov PavelSafronov marked this pull request as ready for review May 5, 2026 22:24
@PavelSafronov PavelSafronov requested a review from a team as a code owner May 5, 2026 22:24
Copilot AI review requested due to automatic review settings May 5, 2026 22:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses SCRAM authentication and unit test failures when running the driver in “nodeless” (non-Node) runtimes by replacing Node-specific Buffer APIs with runtime-neutral utilities, adjusting the bundling configuration, and conditionally adapting/skipping tests that rely on Node-only behavior.

Changes:

  • Update SCRAM message parsing/encoding to use ByteUtils UTF-8 helpers instead of Binary#toString('utf8').
  • Adjust unit tests to run in nodeless environments (conditional assertions, avoiding Buffer APIs, skipping sinon-dependent tests).
  • Change the driver bundling setup to platform: 'browser' and tighten the VM sandbox globals used by bundled tests.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/unit/utils.test.ts Makes compareObjectId tests conditional for nodeless behavior differences.
test/unit/sessions.test.ts Avoids instanceof Long checks in nodeless by using ensureTypeByName.
test/unit/nodeless.test.ts Adds assertions about the VM sandbox not exposing Node globals.
test/unit/commands.test.ts Replaces Buffer read methods with runtime-neutral helpers.
test/unit/cmap/wire_protocol/on_demand/document.test.ts Adapts throw assertions for nodeless runtime differences (needs tightening to avoid false positives).
test/unit/cmap/connect.test.ts Skips sinon-based socket tests when running nodeless.
test/unit/cmap/commands.test.ts Removes Buffer-specific toString usage for document sequence name assertions.
test/unit/client-side-encryption/state_machine.test.ts Skips sinon-based tests for nodeless runs.
test/tools/runner/vm_context_helper.ts Updates VM sandbox globals and adds bundle debugging hooks; exports sandbox.
test/mongodb_bundled.ts Exposes additional exports needed by bundled/nodeless tests (makeSocket, keep-alive constant).
src/cmap/auth/scram.ts Switches SCRAM auth message decoding/parsing to ByteUtils.toUTF8.
src/bson.ts Adds readUint8 helper alongside existing buffer read helpers (needs bounds validation).
etc/bundle-driver.mjs Bundles with platform: 'browser'/chrome112 and bundles bson instead of externalizing it.
Comments suppressed due to low confidence (1)

src/cmap/auth/scram.ts:213

  • parsePayload currently decodes payload.buffer using payload.buffer.length, which can include unwritten bytes (see Binary.position). It also assumes payload is always a Binary, but other code already handles response.payload being a Uint8Arrayr.payload may be as well. Consider normalizing inside parsePayload (accept Binary | Uint8Array) and decode only the actual payload length.
function parsePayload(payload: Binary) {
  const payloadStr = ByteUtils.toUTF8(payload.buffer, 0, payload.buffer.length, true);
  const dict: Document = {};
  const parts = payloadStr.split(',');
  for (let i = 0; i < parts.length; i++) {
    const valueParts = (parts[i].match(/^([^=]*)=(.*)$/) ?? []).slice(1);
    dict[valueParts[0]] = valueParts[1];
  }

Comment thread src/cmap/auth/scram.ts Outdated
].join(',');
const firstMessageBytes = clientFirstMessageBare(username, nonce);
const firstMessage = ByteUtils.toUTF8(firstMessageBytes, 0, firstMessageBytes.length, true);
const payloadString = ByteUtils.toUTF8(payload.buffer, 0, payload.buffer.length, true);
Comment thread src/bson.ts
};

// readUint8, reads a single unsigned byte from buffer at given offset
export const readUint8 = (buffer: Uint8Array, offset: number): number => {
Comment on lines 127 to +134
it('throws if required is set to true and element name does not exist', () => {
expect(() => document.get('blah!', BSONType.bool, true)).to.throw(BSONError);
if (runNodelessTests) {
try {
document.get('blah!', BSONType.bool, true);
} catch (e) {
ensureTypeByName(e, 'BSONError');
}
} else {
Comment on lines 140 to +147
it('throws if requested type is unsupported', () => {
expect(() => {
// @ts-expect-error: checking a bad BSON type
document.get('unsupportedType', BSONType.regex);
}).to.throw(BSONError, /unsupported/i);
if (runNodelessTests) {
try {
// @ts-expect-error: checking a bad BSON type
document.get('unsupportedType', BSONType.regex, true);
} catch (e) {
ensureTypeByName(e, 'BSONError');
}
Comment on lines 265 to +272
it('throws if required is set to true and element name does not exist', () => {
expect(() => document.getNumber('blah!', true)).to.throw(BSONError);
if (runNodelessTests) {
try {
document.getNumber('blah!', true);
} catch (e) {
ensureTypeByName(e, 'BSONError');
}
} else {
} else {
expect(() => {
document.getNumber('string', true);
}).to.throw();
@PavelSafronov PavelSafronov marked this pull request as draft May 5, 2026 22:31
@PavelSafronov PavelSafronov marked this pull request as ready for review May 5, 2026 22:42
- use Binary.position instead of buffer.length
- add a validateBufferInputs call to readUint8
- add expect.fail in cases where we expect an exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants