Skip to content

Commit da31f7a

Browse files
authored
fix(encoding): decoding base64 with invalid bytes >= 128 (#6477)
1 parent 347926a commit da31f7a

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

encoding/_common64.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function decode(
114114
}
115115

116116
function getByte(char: number, alphabet: Uint8Array): number {
117-
const byte = alphabet[char]!;
117+
const byte = alphabet[char] ?? 64;
118118
if (byte === 64) { // alphabet.Base64.length
119119
throw new TypeError(`Invalid Character (${String.fromCharCode(char)})`);
120120
}

encoding/unstable_base32_test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,12 @@ Deno.test("decodeRawBase32() with invalid offsets", () => {
289289
}
290290
}
291291
});
292+
293+
Deno.test("decodeBase32() throws with invalid byte >= 128", () => {
294+
const input = new TextDecoder().decode(new Uint8Array(5).fill(200));
295+
assertThrows(
296+
() => decodeBase32(input),
297+
TypeError,
298+
"Invalid Character",
299+
);
300+
});

encoding/unstable_base64_test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,12 @@ Deno.test("decodeRawBase64() with invalid offsets", () => {
227227
}
228228
}
229229
});
230+
231+
Deno.test("decodeBase64() throws with invalid byte >= 128", () => {
232+
const input = new TextDecoder().decode(new Uint8Array(4).fill(200));
233+
assertThrows(
234+
() => decodeBase64(input),
235+
TypeError,
236+
"Invalid Character",
237+
);
238+
});

0 commit comments

Comments
 (0)