Skip to content

Commit 7f9ec4e

Browse files
committed
fix(jwt): handle full Authorization: Bearer header line in decodeJwt
The page detected Authorization: Bearer input as JWT but decodeJwt only stripped the Bearer scheme prefix, leaving the Authorization: field name in the string — causing atob to throw on the first dot-segment. Strip the header field name before the scheme prefix so the full header line round-trips correctly.
1 parent 325347b commit 7f9ec4e

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

src/lib/jwt.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ describe('decodeJwt — Bearer prefix', () => {
105105
const result = decodeJwt('bearer ' + RS256_FULL);
106106
expect(result.raw).toBe(RS256_FULL);
107107
});
108+
109+
it('strips full "Authorization: Bearer " HTTP header line', () => {
110+
const result = decodeJwt('Authorization: Bearer ' + RS256_FULL);
111+
expect(result.raw).toBe(RS256_FULL);
112+
expect(result.header.alg).toBe('RS256');
113+
});
108114
});
109115

110116
describe('decodeJwt — alg:none', () => {

src/lib/jwt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function parseScopes(payload: Record<string, unknown>): string[] | null {
5050
}
5151

5252
export function decodeJwt(input: string): JwtDecodeResult {
53-
const trimmed = input.trim().replace(/^Bearer\s+/i, '');
53+
const trimmed = input.trim().replace(/^Authorization:\s+/i, '').replace(/^Bearer\s+/i, '');
5454
const parts = trimmed.split('.');
5555
if (parts.length !== 3)
5656
throw new Error('Invalid JWT: expected 3 dot-separated parts');

0 commit comments

Comments
 (0)