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: use custom base64 decoder to workaround deno bug #406

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/auth/scram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MongoDriverError } from "../error.ts";
import { b64, Binary, crypto as stdCrypto, Document, hex } from "../../deps.ts";
import { driverMetadata } from "../protocol/mod.ts";
import { pbkdf2 } from "./pbkdf2.ts";
import { decodeBase64 } from "../utils/decode_base64.ts";

type CryptoMethod = "sha1" | "sha256";

Expand Down Expand Up @@ -160,7 +161,7 @@ export async function continueScramConversation(
const withoutProof = `c=biws,r=${rnonce}`;
const saltedPassword = await HI(
processedPassword,
b64.decode(salt),
decodeBase64(salt),
iterations,
cryptoMethod,
);
Expand Down Expand Up @@ -193,7 +194,7 @@ export async function continueScramConversation(
);
if (
!compareDigest(
b64.decode(parsedResponse.v),
decodeBase64(parsedResponse.v),
new Uint8Array(serverSignature),
)
) {
Expand Down
25 changes: 25 additions & 0 deletions src/utils/decode_base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function decodeBase64(b64: string): Uint8Array {
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let base64 = b64.replace(/-/g, "+").replace(/_/g, "/");

while (base64.length % 4) {
base64 += "=";
}

let bitString = "";
for (let i = 0; i < base64.length; i++) {
const char = base64.charAt(i);
if (char !== "=") {
const charIndex = chars.indexOf(char);
bitString += charIndex.toString(2).padStart(6, "0");
}
}

const bytes = new Uint8Array(bitString.length / 8);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = parseInt(bitString.substring(8 * i, 8 * (i + 1)), 2);
}

return bytes;
}
41 changes: 41 additions & 0 deletions tests/cases/11_decode_base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { decodeBase64 } from "../../src/utils/decode_base64.ts";

import { assertEquals, describe, it } from "./../test.deps.ts";

describe("decodeBase64", () => {
it({
name: "should correctly decode a standard base64 encoded string",
fn() {
const encoded = "SGVsbG8gV29ybGQ="; // "Hello World" in base64
const decoded = decodeBase64(encoded);
assertEquals(new TextDecoder().decode(decoded), "Hello World");
},
});

it({
name: "should correctly decode a URL-safe base64 encoded string",
fn() {
const encoded = "SGVsbG8tV29ybGRf"; // URL-safe base64 variant
const decoded = decodeBase64(encoded);
assertEquals(new TextDecoder().decode(decoded), "Hello-World_");
},
});

it({
name: "should handle base64 strings with missing padding",
fn() {
const encoded = "SGVsbG8gV29ybGQ"; // Missing '=' at the end
const decoded = decodeBase64(encoded);
assertEquals(new TextDecoder().decode(decoded), "Hello World");
},
});

it({
name: "should return an empty array for an empty string",
fn() {
const encoded = "";
const decoded = decodeBase64(encoded);
assertEquals(decoded.length, 0);
},
});
});
Loading