Skip to content

Commit

Permalink
fix: decrypting empty ciphertext compact JWEs (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
bespokebob committed Mar 4, 2022
1 parent 8d016fd commit 95fe597
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/jwe/compact/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function compactDecrypt(

const decrypted = await flattenedDecrypt(
{
ciphertext: <string>(ciphertext || undefined),
ciphertext,
iv: <string>(iv || undefined),
protected: protectedHeader || undefined,
tag: <string>(tag || undefined),
Expand Down
13 changes: 12 additions & 1 deletion test/jwe/compact.decrypt.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'

const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto'
const { compactDecrypt } = await import(root)
const { CompactEncrypt, compactDecrypt } = await import(root)

test('JWE format validation', async (t) => {
await t.throwsAsync(compactDecrypt(null, new Uint8Array(0)), {
Expand All @@ -13,3 +13,14 @@ test('JWE format validation', async (t) => {
code: 'ERR_JWE_INVALID',
})
})

test('decrypt empty data', async (t) => {
const jwe = await new CompactEncrypt(new Uint8Array(0))
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.encrypt(new Uint8Array(16))

t.is(jwe.split('.')[3], '')

const { plaintext } = await compactDecrypt(jwe, new Uint8Array(16))
t.is(plaintext.byteLength, 0)
})
11 changes: 11 additions & 0 deletions test/jwe/flattened.decrypt.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,14 @@ test('AES CBC + HMAC', async (t) => {
})
}
})

test('decrypt empty data', async (t) => {
const jwe = await new FlattenedEncrypt(new Uint8Array(0))
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.encrypt(new Uint8Array(16))

t.is(jwe.ciphertext, '')

const { plaintext } = await flattenedDecrypt(jwe, new Uint8Array(16))
t.is(plaintext.byteLength, 0)
})
12 changes: 12 additions & 0 deletions test/jwe/general.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ test('General JWE format validation', async (t) => {
await t.notThrowsAsync(generalDecrypt(jwe, t.context.secret))
}
})

test('decrypt empty data', async (t) => {
const jwe = await new GeneralEncrypt(new Uint8Array(0))
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.addRecipient(new Uint8Array(16))
.encrypt()

t.is(jwe.ciphertext, '')

const { plaintext } = await generalDecrypt(jwe, new Uint8Array(16))
t.is(plaintext.byteLength, 0)
})

0 comments on commit 95fe597

Please sign in to comment.