File size: 2,716 Bytes
62cfde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
diff --git a/lib/jwe/decrypt.js b/lib/jwe/decrypt.js
index 9da8aead..358d084b 100644
--- a/lib/jwe/decrypt.js
+++ b/lib/jwe/decrypt.js
@@ -52,7 +52,7 @@ const validateAlgorithms = (algorithms, option) => {
 /*
  * @public
  */
-const jweDecrypt = (skipValidateHeaders, serialization, jwe, key, { crit = [], complete = false, keyManagementAlgorithms, contentEncryptionAlgorithms, maxPBES2Count = 10000 } = {}) => {
+const jweDecrypt = (skipValidateHeaders, serialization, jwe, key, { crit = [], complete = false, keyManagementAlgorithms, contentEncryptionAlgorithms, maxPBES2Count = 10000, inflateRawSyncLimit = 250000 } = {}) => {
   key = getKey(key, true)
 
   keyManagementAlgorithms = validateAlgorithms(keyManagementAlgorithms, 'keyManagementAlgorithms')
@@ -189,7 +189,7 @@ const jweDecrypt = (skipValidateHeaders, serialization, jwe, key, { crit = [], c
     let cleartext = decrypt(enc, cek, base64url.decodeToBuffer(ciphertext), { iv, tag, aad: adata })
 
     if (opts.zip) {
-      cleartext = inflateRawSync(cleartext)
+      cleartext = inflateRawSync(cleartext, { maxOutputLength: inflateRawSyncLimit })
     }
 
     if (complete) {
diff --git a/test/jwe/sanity.test.js b/test/jwe/sanity.test.js
index fd8a0837..e83d8c78 100644
--- a/test/jwe/sanity.test.js
+++ b/test/jwe/sanity.test.js
@@ -1,4 +1,5 @@
 const test = require('ava')
+const crypto = require('crypto')
 
 const base64url = require('../../lib/help/base64url')
 const { JWKS, JWK: { generateSync }, JWE, errors } = require('../..')
@@ -616,3 +617,25 @@ if (!('electron' in process.versions)) {
     }, { instanceOf: errors.JWEInvalid, message: 'JOSE Header "p2c" (PBES2 Count) out is of acceptable bounds' })
   })
 }
+
+test('Compressed JWE output length limit', t => {
+  const k = generateSync('oct', 256)
+  {
+    const jwe = JWE.encrypt(crypto.randomBytes(250000), k, { alg: 'dir', enc: 'A128CBC-HS256', zip: 'DEF' })
+    t.notThrows(() => {
+      JWE.decrypt(jwe, k)
+    })
+  }
+  {
+    const jwe = JWE.encrypt(crypto.randomBytes(250000 + 1), k, { alg: 'dir', enc: 'A128CBC-HS256', zip: 'DEF' })
+    t.throws(() => {
+      JWE.decrypt(jwe, k)
+    })
+  }
+  {
+    const jwe = JWE.encrypt(crypto.randomBytes(1001), k, { alg: 'dir', enc: 'A128CBC-HS256', zip: 'DEF' })
+    t.throws(() => {
+      JWE.decrypt(jwe, k, { inflateRawSyncLimit: 1000 })
+    })
+  }
+})
diff --git a/types/index.d.ts b/types/index.d.ts
index 30e146a0..ec330822 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -402,6 +402,7 @@ export namespace JWE {
     contentEncryptionAlgorithms?: string[];
     keyManagementAlgorithms?: string[];
     maxPBES2Count?: number;
+    inflateRawSyncLimit?: number;
   }
 
   interface completeDecrypt {