When validating security-sensitive inputs (IDs, hostnames, tokens, IPs, payment-like numbers), ensure the implementation is strict, resilient, portable, and non-leaky:
typ checks should tolerate valid variants like JWT-at).Buffer) if the code must run in browsers; prefer atob/universal helpers.Example (portable JWT header decode + non-leaky error style):
function decodeHeader(headerB64: string) {
// Convert base64url to base64, then decode in browser-compatible way
const b64 = headerB64.replace(/-/g, "+").replace(/_/g, "/")
.padEnd(headerB64.length + ((4 - (headerB64.length % 4)) % 4), "=");
return JSON.parse(atob(b64));
}
function validateTyp(decoded: any) {
if ("typ" in decoded && !decoded.typ?.startsWith("JWT")) return false;
return true;
}
// Error message should not include the raw received discriminator/token/input.
const message = "Invalid discriminator value. Expected one of: ...";
Apply this checklist to every change involving validation/parsing logic, regexes, and security-related error reporting.
Enter the URL of a public GitHub repository