Skip to main content
Lasita
Menu

Developer Utilities · 8 min read

Decode JWTs Safely, Encode URLs Correctly, and Fix Color Contrast

Practical frontend engineering guidance covering safe offline JWT claim inspection, UTF-8 Base64 pitfalls, encodeURI vs encodeURIComponent, alpha-composited WCAG 2.1 contrast, and perceptual OKLCH shade fixes.

By

Published

Key takeaways

  • Never paste bearer JWTs containing user emails, tenant IDs, or session claims into server-side web decoders — inspect them locally with connect-src none.
  • JavaScript atob()/btoa() throw on non-Latin UTF-8 characters and emojis; modern tools encode UTF-8 byte arrays via TextEncoder.
  • OKLCH lets design systems adjust lightness predictably to reach WCAG 4.5:1 or 7.0:1 contrast without muddying brand hue.
In this guide
  1. 1. Inspecting JWT Headers & Payloads Safely Offline
  2. 2. UTF-8 Base64 Pitfalls & encodeURI vs encodeURIComponent
  3. 3. HTML Entity Escaping for Safe Templates & Docs
  4. 4. WCAG 2.1 Relative Luminance & Alpha Compositing
  5. 5. Fixing Low-Contrast Brand Colors with OKLCH

1. Inspecting JWT Headers & Payloads Safely Offline

JSON Web Tokens (JWTs) power almost every modern single-page app and microservice API. A standard compact JWS token has three dot-separated segments: header.payload.signature, where both the header and payload are simply Base64URL-encoded JSON.

Because JWT payloads are not encrypted (unless wrapped in JWE), developers routinely decode them while debugging expired sessions, missing RBAC scopes, or mismatched aud/iss claims. The security mistake is pasting a live staging or production bearer token into a random online decoder that logs HTTP requests or executes third-party analytics on the same origin as the textarea!

Remember two security rules for JWTs: First, treat any live bearer token like a password — inspect it strictly locally inside a sandboxed origin enforcing connect-src 'none', such as Lasita DevKit Base64 & JWT Inspector. Second, decoding a JWT locally only displays unverified claims; signature verification must always happen on your backend with trusted signing keys.

A JWT split into header, payload and signature with the decoded header and payload JSON eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzQyIiwibmFtZSI6IkFkYSIs….sig… Header { "alg": "HS256", "typ": "JWT" } Payload (claims) { "sub": "user_42", "name": "Ada", "exp": 1796083200 } Signature HMAC / RSA / ECDSA over header.payload Needs the key to verify
Figure 1. A JSON Web Token is three Base64URL segments separated by dots. The header and payload are only encoded, not encrypted, so anyone holding the token can read them. Decoding locally avoids handing that data to a third-party website. Only the signature proves the token is genuine, and checking it needs the secret or public key.

2. UTF-8 Base64 Pitfalls & encodeURI vs encodeURIComponent

Browser JavaScript’s legacy btoa() and atob() functions operate on single-byte Latin-1 code units. Passing Japanese characters, accented European names, currency symbols like ₹, or emojis directly into btoa() throws a runtime DOMException. Encoding via TextEncoder UTF-8 bytes avoids data corruption cleanly:

// WRONG: btoa("Lasita 🚀") throws InvalidCharacterError (non-Latin1!)
// SAFE: Encode UTF-8 bytes explicitly with TextEncoder
function encodeBase64Utf8(input: string): string {
  const bytes = new TextEncoder().encode(input);
  let binary = '';
  for (const byte of bytes) binary += String.fromCharCode(byte);
  return btoa(binary);
}

// Convert standard Base64 to RFC 4648 §5 Base64URL (JWT-safe):
function toBase64Url(base64: string): string {
  return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}

A closely related bug appears in redirect flows and OAuth callbacks when developers confuse encodeURI() with encodeURIComponent():

const nextUrl = 'https://lasita.dev/tools?category=Images & Photos';

// WRONG: encodeURI leaves '&' and '=' unescaped, breaking the outer query string!
encodeURI(nextUrl);
// -> "https://lasita.dev/tools?category=Images%20&%20Photos"

// RIGHT: encodeURIComponent escapes every delimiter safely for a query parameter value
encodeURIComponent(nextUrl);
// -> "https%3A%2F%2Flasita.dev%2Ftools%3Fcategory%3DImages%20%26%20Photos"
  • Use encodeURIComponent() (or URLSearchParams) for any single query parameter value, path segment, or nested redirect URL.
  • Use encodeURI() only when cleaning spaces in an already-assembled full URL whose ://, ?, and & delimiters must remain intact.
  • Watch out for + in query strings: Under application/x-www-form-urlencoded rules (such as URLSearchParams), a literal + decodes into a space — which is why standard Base64 tokens break inside query parameters unless converted to Base64URL or percent-encoded as %2B.

3. HTML Entity Escaping for Safe Templates & Documentation

When displaying untrusted user strings, code snippets, or API error payloads inside HTML, five characters carry syntactic meaning and must be escaped into entities:

Character Escaped Entity Why It Matters
& & Prevents double-decoding and malformed entities
< and > &lt; / &gt; Stops raw text from opening accidental HTML or script tags
" and ' &quot; / &#39; Prevents breaking out of quoted HTML attributes

You can switch between Base64, Base64URL, URL parsing, HTML entity escaping, and local JWT inspection inside one isolated workspace on devkit.lasita.dev/base64-url-encoder.

4. WCAG 2.1 Relative Luminance & Alpha Compositing

Picking accessible colors by eye alone is notoriously unreliable — pure yellow (#ffff00) has high luminance (0.9278) while pure blue (#0000ff) has very low luminance (0.0722), even though both appear at 50% lightness in HSL!

WCAG 2.1 linearizes each sRGB channel (removing gamma encoding) and weights human eye sensitivity heavily toward green (0.7152), moderately toward red (0.2126), and lightly toward blue (0.0722). Then contrast ratio is computed as (L1 + 0.05) / (L2 + 0.05) where L1 ≥ L2.

One detail many simple contrast checkers miss is semi-transparent foreground colors (such as rgba(15, 23, 42, 0.55) or text-slate-900/60 in Tailwind). Before computing relative luminance, a translucent foreground must be alpha-composited onto its actual background color: C_out = α · C_fg + (1 − α) · C_bg.

5. Fixing Low-Contrast Brand Colors with OKLCH

When a brand color fails WCAG AA (for example, a bright teal or amber button achieving only 3.2:1 with white text instead of the required 4.5:1), nudging HSL lightness often shifts perceived hue — turning vibrant blues purplish or yellows muddy brown.

Modern CSS OKLCH (oklch(L C H)) separates perceptual lightness (L), chroma (C), and hue angle (H) accurately. Lowering L slightly while holding hue constant moves a brand color straight across the 4.5:1 AA or 7.0:1 AAA boundary while keeping its visual identity intact.

Test HEX, RGB/RGBA, HSL/HSLA, HWB, LAB/LCH, OKLAB/OKLCH, and named CSS colors — and generate the exact nearest WCAG-passing OKLCH shade automatically — on Lasita DevKit Color & WCAG Contrast Checker.

About the author

Abishek Verma

Software engineer & writer behind Lasita · India

Abishek Verma builds and maintains every Lasita tool and writes each guide on this site. Lasita began as a simple idea: everyday file, developer and date utilities should work entirely inside your browser, so nothing you open in them has to be uploaded to someone else’s server.

More about Lasita Email contact@lasita.dev

Spotted a mistake or have a question about this guide? Email contact@lasita.dev.

All 8 guides →