data
encoding
ai_generated
true
Base64 decoding fails with 'Invalid character' for URL-transmitted tokens or JWT segments
ID: data/base64-urlsafe-vs-standard-mismatch
88%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 64 | active | — | — | — |
Root Cause
Standard base64 uses +/ characters which are special in URLs. URL-safe base64 uses -_ instead. JWT uses URL-safe base64 WITHOUT padding (no = signs). Mixing these three variants causes silent corruption or decode errors.
genericWorkarounds
-
95% success Use base64.urlsafe_b64decode() with padding restoration for JWT
import base64; padded = segment + '=' * (-len(segment) % 4); base64.urlsafe_b64decode(padded)
-
92% success Use the correct base64 variant for each protocol
Standard (RFC 4648): +/ with =padding; URL-safe (RFC 4648 §5): -_ with =padding; JWT (RFC 7515): -_ WITHOUT padding
-
88% success Use PyJWT or similar library that handles base64url internally
import jwt; decoded = jwt.decode(token, key, algorithms=['HS256']) # handles base64url internally
Dead Ends
Common approaches that don't work:
-
Use standard base64.b64decode() to decode JWT segments
90% fail
JWT uses URL-safe base64 WITHOUT padding. Standard b64decode fails on - and _ characters. Adding padding back is also needed.
-
URL-encode standard base64 before putting in URL parameters
72% fail
URL encoding turns + into %2B and / into %2F. The result is valid but 33% longer. URL-safe base64 is the correct solution.