Skip to content

Commit 4e5b1e4

Browse files
committed
More forgiving JWT parsing
1 parent 78c2d01 commit 4e5b1e4

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

src/Pages/Home/JwtDecoder/JwtDecoder.cshtml

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,24 +355,27 @@
355355
signature: null
356356
};
357357
358+
// Clear previous state before attempting to parse a new JWT
358359
await showDecodedJwt(null, null, null, ' ');
360+
359361
const jwt = $(this).text();
360362
if (jwt) {
361363
try {
362-
const parts = jwt.split('.');
363-
if (parts.length === 3) {
364-
const header = JSON.parse(decodeBase64UrlSafe(parts[0]));
365-
const payload = JSON.parse(decodeBase64UrlSafe(parts[1]));
366-
const signature = parts[2];
364+
const parts = jwt.indexOf('.') === -1 ? [jwt] : jwt.split('.');
365+
const header = parseJwtPart(parts, 0);
366+
const payload = parseJwtPart(parts, 1);
367+
const signature = parts.length > 2 ? parts[2] : '';
367368
368-
decodedJwt = {
369-
header: header,
370-
payload: payload,
371-
signature: signature
372-
};
373-
await showDecodedJwt(parts, header, payload, signature);
374-
colorJwtInput($(this), parts);
375-
} else {
369+
decodedJwt = {
370+
header: header,
371+
payload: payload,
372+
signature: signature
373+
};
374+
375+
await showDecodedJwt(parts, header, payload, signature);
376+
colorJwtInput($(this), parts);
377+
378+
if (parts.length !== 3) {
376379
showError('Invalid JWT format. A JWT should have three parts separated by dots.');
377380
}
378381
} catch (e) {
@@ -383,6 +386,27 @@
383386
}
384387
}
385388
389+
function parseJwtPart(parts, index) {
390+
if (index < 0 || index >= parts.length) {
391+
return null;
392+
}
393+
394+
const part = parts[index];
395+
if (!part) {
396+
return null;
397+
}
398+
399+
try {
400+
const decodedPart = decodeBase64UrlSafe(part);
401+
return JSON.parse(decodedPart);
402+
}
403+
catch {
404+
405+
}
406+
407+
return null;
408+
}
409+
386410
function colorJwtInput(target, parts) {
387411
let html = '';
388412
if (parts.length > 0) {

src/wwwroot/css/site.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ a.navbar-brand .icon-banner {
127127
}
128128
.jwt-decoder-container .jwt-input-editable {
129129
word-wrap: anywhere;
130+
max-width: 100vw;
130131
}
131132
.jwt-decoder-container .jwt-input-editable .text-danger {
132133
color: #c586c0 !important;

src/wwwroot/css/site.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wwwroot/css/site.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ a.navbar-brand {
145145

146146
.jwt-input-editable {
147147
word-wrap: anywhere;
148+
max-width: 100vw;
148149

149150
.text-danger {
150151
color: #c586c0 !important;

0 commit comments

Comments
 (0)