Skip to content

Commit

Permalink
fix: throw JWSInvalid when jws protected header is invalid (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
trebler committed Aug 16, 2021
1 parent 35fc548 commit 1fc79aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/jws/flattened/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ async function flattenedVerify(
let parsedProt: JWSHeaderParameters = {}
if (jws.protected) {
const protectedHeader = base64url(jws.protected)
parsedProt = JSON.parse(decoder.decode(protectedHeader))
try {
parsedProt = JSON.parse(decoder.decode(protectedHeader))
} catch {
throw new JWSInvalid('JWS Protected Header is invalid')
}
}
if (!isDisjoint(parsedProt, jws.header)) {
throw new JWSInvalid(
Expand Down
10 changes: 10 additions & 0 deletions test/jws/flattened.verify.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ Promise.all([
await t.throwsAsync(flattenedVerify(jws, t.context.secret), assertion);
}

{
const jws = { ...fullJws };
const assertion = {
message: 'JWS Protected Header is invalid',
code: 'ERR_JWS_INVALID',
};
jws.protected = `1${jws.protected}`;
await t.throwsAsync(flattenedVerify(jws, t.context.secret), assertion);
}

{
const jws = { ...fullJws };
const assertion = {
Expand Down

0 comments on commit 1fc79aa

Please sign in to comment.