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(encoding): decoding base64 with invalid bytes >= 128 #6477

Merged
merged 2 commits into from
Mar 13, 2025
Merged
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
2 changes: 1 addition & 1 deletion encoding/_common64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function decode(
}

function getByte(char: number, alphabet: Uint8Array): number {
const byte = alphabet[char]!;
const byte = alphabet[char] ?? 64;
if (byte === 64) { // alphabet.Base64.length
throw new TypeError(`Invalid Character (${String.fromCharCode(char)})`);
Copy link
Member

Choose a reason for hiding this comment

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

Not directly related to this change, but can we change this error message to something like:

Cannot decode base64 string as it includes invalid character: "${String.fromCharCode(char)}"

to align this to our style guide? https://github.com/denoland/std/blob/main/.github/CONTRIBUTING.md#error-messages

}
Expand Down
9 changes: 9 additions & 0 deletions encoding/unstable_base32_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,12 @@ Deno.test("decodeRawBase32() with invalid offsets", () => {
}
}
});

Deno.test("decodeBase32() throws with invalid byte >= 128", () => {
const input = new TextDecoder().decode(new Uint8Array(5).fill(200));
assertThrows(
() => decodeBase32(input),
TypeError,
"Invalid Character",
);
});
9 changes: 9 additions & 0 deletions encoding/unstable_base64_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,12 @@ Deno.test("decodeRawBase64() with invalid offsets", () => {
}
}
});

Deno.test("decodeBase64() throws with invalid byte >= 128", () => {
const input = new TextDecoder().decode(new Uint8Array(4).fill(200));
assertThrows(
() => decodeBase64(input),
TypeError,
Copy link
Member

Choose a reason for hiding this comment

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

Note: This used to throw a DOMException of the form:

new DOMException(
  "Failed to decode base64: invalid character",
  "InvalidCharacterError",
);

"Invalid Character",
);
});
Loading