Skip to content

Commit

Permalink
fix: guard Sign payloads and Encrypt plaintext argument types
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Sep 8, 2021
1 parent 8b3cc98 commit 10a18f2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/jwe/flattened/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class FlattenedEncrypt {
* @param plaintext Binary representation of the plaintext to encrypt.
*/
constructor(plaintext: Uint8Array) {
if (!(plaintext instanceof Uint8Array)) {
throw new TypeError('plaintext must be an instance of Uint8Array')
}
this._plaintext = plaintext
}

Expand Down
3 changes: 3 additions & 0 deletions src/jws/flattened/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class FlattenedSign {
* @param payload Binary representation of the payload to sign.
*/
constructor(payload: Uint8Array) {
if (!(payload instanceof Uint8Array)) {
throw new TypeError('payload must be an instance of Uint8Array')
}
this._payload = payload
}

Expand Down
20 changes: 20 additions & 0 deletions test/jwe/flattened.encrypt.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ import(`${root}/jwe/flattened/encrypt`).then(
tag: 'gEwNlfPZ-O-dG7dTFkhMyQ',
});
}
{
for (const value of [
undefined,
null,
{},
'',
'foo',
1,
0,
true,
false,
[],
new FlattenedEncrypt(new Uint8Array()),
]) {
t.throws(() => new FlattenedEncrypt(value), {
instanceOf: TypeError,
message: 'plaintext must be an instance of Uint8Array',
});
}
}
});

test('FlattenedEncrypt.prototype.setProtectedHeader', (t) => {
Expand Down
20 changes: 20 additions & 0 deletions test/jws/flattened.sign.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ import(`${root}/jws/flattened/sign`).then(
signature: 'O7HdMZ_6_aEQWLGGItmCKN3pf8-nZ9mHnPfT7rrPCwk',
});
}
{
for (const value of [
undefined,
null,
{},
'',
'foo',
1,
0,
true,
false,
[],
new FlattenedSign(new Uint8Array()),
]) {
t.throws(() => new FlattenedSign(value), {
instanceOf: TypeError,
message: 'payload must be an instance of Uint8Array',
});
}
}
});

test('FlattenedSign.prototype.setProtectedHeader', (t) => {
Expand Down

0 comments on commit 10a18f2

Please sign in to comment.