Skip to content

Commit 31e4baf

Browse files
committed
fix: typ checking edge-cases when it contains a slash (/) character
1 parent bb9f7be commit 31e4baf

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/lib/jwt_claims_set.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ function validateInput(label: string, input: number) {
1414
return input
1515
}
1616

17-
const normalizeTyp = (value: string) => value.toLowerCase().replace(/^application\//, '')
17+
const normalizeTyp = (value: string) => {
18+
if (value.includes('/')) {
19+
return value.toLowerCase()
20+
}
21+
22+
return `application/${value.toLowerCase()}`
23+
}
1824

1925
const checkAudiencePresence = (audPayload: unknown, audOption: unknown[]) => {
2026
if (typeof audPayload === 'string') {

test/jwt/verify.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ test('typ verification', async (t) => {
176176
{ code: 'ERR_JWT_CLAIM_VALIDATION_FAILED', message: 'unexpected "typ" JWT header value' },
177177
)
178178
}
179+
{
180+
const typ = 'text/plain'
181+
const jwt = await new SignJWT(t.context.payload)
182+
.setProtectedHeader({ alg: 'HS256', typ })
183+
.sign(t.context.secret)
184+
185+
await t.notThrowsAsync(
186+
jwtVerify(jwt, t.context.secret, {
187+
typ: 'text/plain',
188+
}),
189+
)
190+
191+
await t.throwsAsync(
192+
jwtVerify(jwt, t.context.secret, {
193+
typ: 'application/text/plain',
194+
}),
195+
{ code: 'ERR_JWT_CLAIM_VALIDATION_FAILED', message: 'unexpected "typ" JWT header value' },
196+
)
197+
}
179198
})
180199

181200
test('Issuer[] verification', async (t) => {

0 commit comments

Comments
 (0)