Skip to content
Merged
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
8 changes: 6 additions & 2 deletions clients/passkeys-browser/src/base64url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ export function encode(input: ArrayBuffer | Uint8Array | ArrayBufferView): Base6
for (let i = 0; i < bytes.length; i++) {
s += String.fromCharCode(bytes[i]!);
}
return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
// base64url per RFC 4648 §5: map +/ to -_ and strip padding. Using replaceAll with
// string literals (no regexes) keeps this linear — it avoids the ReDoS shape
// SonarQube flags on `/=+$/`. `=` only ever appears as trailing base64 padding, so
// removing all of it is equivalent to stripping the trailing run.
return btoa(s).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
}

export function decode(input: Base64Url): Uint8Array {
const pad = "=".repeat((4 - (input.length % 4)) % 4);
const normalized = input.replace(/-/g, "+").replace(/_/g, "/") + pad;
const normalized = input.replaceAll("-", "+").replaceAll("_", "/") + pad;
const binary = atob(normalized);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
Expand Down
Loading