-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
BUG(isJWT): validate decoded header and payload as JSON objects #2677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
657328d
3233fb7
d141406
f44c09e
018dfb4
32c48e3
6853ed2
eaeaeb0
81ac039
cdea4ec
6aae9bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import assertString from './util/assertString'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import isBase64 from './isBase64'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function tryDecodeJSON(segment) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isBase64(segment, { urlSafe: true })) return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Normalize base64url alphabet to base64, then restore stripped padding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let b64 = segment.replace(/-/g, '+').replace(/_/g, '/'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (b64.length % 4) b64 += '='; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const decoded = Buffer.from(b64, 'base64').toString('utf8'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function tryDecodeJSON(segment) { | |
| if (!isBase64(segment, { urlSafe: true })) return false; | |
| try { | |
| // Normalize base64url alphabet to base64, then restore stripped padding | |
| let b64 = segment.replace(/-/g, '+').replace(/_/g, '/'); | |
| while (b64.length % 4) b64 += '='; | |
| const decoded = Buffer.from(b64, 'base64').toString('utf8'); | |
| function decodeBase64UrlToUtf8(b64) { | |
| // Prefer Node.js Buffer when available (supports old Node via `new Buffer`). | |
| if (typeof Buffer !== 'undefined') { | |
| if (typeof Buffer.from === 'function') { | |
| return Buffer.from(b64, 'base64').toString('utf8'); | |
| } | |
| // Fallback for very old Node versions where Buffer.from is not available. | |
| // eslint-disable-next-line no-buffer-constructor | |
| return new Buffer(b64, 'base64').toString('utf8'); | |
| } | |
| // Browser / non-Node environment: use atob/TextDecoder if available. | |
| if (typeof atob === 'function') { | |
| const binary = atob(b64); | |
| // If TextDecoder is available, use it for proper UTF-8 decoding. | |
| if (typeof TextDecoder !== 'undefined') { | |
| const bytes = new Uint8Array(binary.length); | |
| for (let i = 0; i < binary.length; i += 1) { | |
| bytes[i] = binary.charCodeAt(i); | |
| } | |
| return new TextDecoder('utf-8').decode(bytes); | |
| } | |
| // Fallback UTF-8 decoding using percent-encoding. | |
| let encoded = ''; | |
| for (let i = 0; i < binary.length; i += 1) { | |
| const code = binary.charCodeAt(i).toString(16).padStart(2, '0'); | |
| encoded += '%' + code; | |
| } | |
| return decodeURIComponent(encoded); | |
| } | |
| // As a last resort, return the input unchanged. | |
| return b64; | |
| } | |
| function tryDecodeJSON(segment) { | |
| if (!isBase64(segment, { urlSafe: true })) return false; | |
| try { | |
| // Normalize base64url alphabet to base64, then restore stripped padding | |
| let b64 = segment.replace(/-/g, '+').replace(/_/g, '/'); | |
| while (b64.length % 4) b64 += '='; | |
| const decoded = decodeBase64UrlToUtf8(b64); |
Kartikeya-guthub marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5549,6 +5549,19 @@ describe('Validators', () => { | |||||||||
| 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTYxNjY1Mzg3Mn0.eyJpc3MiOiJodHRwczovL2V4YW1wbGUuY29tIiwiaWF0IjoxNjE2NjUzODcyLCJleHAiOjE2MTY2NTM4ODJ9.a1jLRQkO5TV5y5ERcaPAiM9Xm2gBdRjKrrCpHkGr_8M', | ||||||||||
| '$Zs.ewu.su84', | ||||||||||
| 'ks64$S/9.dy$§kz.3sd73b', | ||||||||||
| 'foo.bar.', | ||||||||||
| '..', | ||||||||||
| '.t.', | ||||||||||
| 'foo.bar.baz', | ||||||||||
| 'Zm9v.YmFy.', | ||||||||||
|
Comment on lines
+5553
to
+5557
|
||||||||||
| 'eyJmb28iOiJiYXIifQ.YmFy.', | ||||||||||
| 'Zm9v.eyJiYXIiOiJiYXoifQ.', | ||||||||||
| 'W10=.eyJiYXIiOiJiYXoifQ.', | ||||||||||
| 'eyJmb28iOiJiYXIifQ.W10=.', | ||||||||||
|
||||||||||
| 'W10=.eyJiYXIiOiJiYXoifQ.', | |
| 'eyJmb28iOiJiYXIifQ.W10=.', | |
| 'W10.eyJiYXIiOiJiYXoifQ.', | |
| 'eyJmb28iOiJiYXIifQ.W10.', |
Uh oh!
There was an error while loading. Please reload this page.