Skip to content

Commit 1b91d88

Browse files
committed
fix: add a maxOutputLength option to zlib inflate
1 parent 9ca2b24 commit 1b91d88

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Class: JWEDecompressionFailed
2+
3+
## [💗 Help the project](https://github.com/sponsors/panva)
4+
5+
Support from the community to continue maintaining and improving this module is welcome. If you find the module useful, please consider supporting the project by [becoming a sponsor](https://github.com/sponsors/panva).
6+
7+
---
8+
9+
An error subclass thrown when a JWE ciphertext decompression fails.
10+
11+
**`Example`**
12+
13+
Checking thrown error is this one using a stable error code
14+
15+
```js
16+
if (err.code === 'ERR_JWE_DECOMPRESSION_FAILED') {
17+
// ...
18+
}
19+
```
20+
21+
**`Example`**
22+
23+
Checking thrown error is this one using `instanceof`
24+
25+
```js
26+
if (err instanceof jose.errors.JWEDecompressionFailed) {
27+
// ...
28+
}
29+
```
30+
31+
## Table of contents
32+
33+
### Constructors
34+
35+
- [constructor](util_errors.JWEDecompressionFailed.md#constructor)
36+
37+
### Properties
38+
39+
- [code](util_errors.JWEDecompressionFailed.md#code)
40+
- [message](util_errors.JWEDecompressionFailed.md#message)
41+
42+
### Accessors
43+
44+
- [code](util_errors.JWEDecompressionFailed.md#code-1)
45+
46+
## Constructors
47+
48+
### constructor
49+
50+
**new JWEDecompressionFailed**(`message?`)
51+
52+
#### Parameters
53+
54+
| Name | Type |
55+
| :------ | :------ |
56+
| `message?` | `string` |
57+
58+
## Properties
59+
60+
### code
61+
62+
**code**: `string` = `'ERR_JWE_DECOMPRESSION_FAILED'`
63+
64+
A unique error code for the particular error subclass.
65+
66+
___
67+
68+
### message
69+
70+
**message**: `string` = `'decompression operation failed'`
71+
72+
## Accessors
73+
74+
### code
75+
76+
`Static` `get` **code**(): ``"ERR_JWE_DECOMPRESSION_FAILED"``
77+
78+
A unique error code for the particular error subclass.
79+
80+
#### Returns
81+
82+
``"ERR_JWE_DECOMPRESSION_FAILED"``

docs/modules/util_errors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Support from the community to continue maintaining and improving this module is
1313
- [JOSEAlgNotAllowed](../classes/util_errors.JOSEAlgNotAllowed.md)
1414
- [JOSEError](../classes/util_errors.JOSEError.md)
1515
- [JOSENotSupported](../classes/util_errors.JOSENotSupported.md)
16+
- [JWEDecompressionFailed](../classes/util_errors.JWEDecompressionFailed.md)
1617
- [JWEDecryptionFailed](../classes/util_errors.JWEDecryptionFailed.md)
1718
- [JWEInvalid](../classes/util_errors.JWEInvalid.md)
1819
- [JWKInvalid](../classes/util_errors.JWKInvalid.md)

src/runtime/node/zlib.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import type { InflateFunction, DeflateFunction } from '../../types.d'
66
const inflateRaw = promisify(inflateRawCb)
77
const deflateRaw = promisify(deflateRawCb)
88

9-
export const inflate: InflateFunction = (input: Uint8Array) => inflateRaw(input)
9+
export const inflate: InflateFunction = (input: Uint8Array) =>
10+
inflateRaw(input, { maxOutputLength: 250_000 })
1011
export const deflate: DeflateFunction = (input: Uint8Array) => deflateRaw(input)

test/jwe/flattened.decrypt.test.mjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import test from 'ava'
22
import * as crypto from 'crypto'
3+
import { promisify } from 'node:util'
4+
import { inflateRaw as inflateRawCb } from 'node:zlib'
35

46
const { FlattenedEncrypt, flattenedDecrypt } = await import('#dist')
57

@@ -228,3 +230,37 @@ test('decrypt PBES2 p2c limit', async (t) => {
228230
code: 'ERR_JWE_INVALID',
229231
})
230232
})
233+
234+
test('decrypt inflate output length limit', async (t) => {
235+
{
236+
const jwe = await new FlattenedEncrypt(new Uint8Array(250000))
237+
.setProtectedHeader({ alg: 'dir', enc: 'A128CBC-HS256', zip: 'DEF' })
238+
.encrypt(new Uint8Array(32))
239+
240+
await flattenedDecrypt(jwe, new Uint8Array(32))
241+
}
242+
243+
{
244+
const jwe = await new FlattenedEncrypt(new Uint8Array(250000 + 1))
245+
.setProtectedHeader({ alg: 'dir', enc: 'A128CBC-HS256', zip: 'DEF' })
246+
.encrypt(new Uint8Array(32))
247+
248+
await t.throwsAsync(flattenedDecrypt(jwe, new Uint8Array(32)), {
249+
message: 'decompression operation failed',
250+
code: 'ERR_JWE_DECOMPRESSION_FAILED',
251+
})
252+
}
253+
254+
{
255+
const jwe = await new FlattenedEncrypt(new Uint8Array(1000 + 1))
256+
.setProtectedHeader({ alg: 'dir', enc: 'A128CBC-HS256', zip: 'DEF' })
257+
.encrypt(new Uint8Array(32))
258+
259+
const inflateRawPromise = promisify(inflateRawCb)
260+
const inflateRaw = async (buffer) => inflateRawPromise(buffer, { maxOutputLength: 1000 })
261+
262+
await t.throwsAsync(flattenedDecrypt(jwe, new Uint8Array(32), { inflateRaw }), {
263+
code: 'ERR_BUFFER_TOO_LARGE',
264+
})
265+
}
266+
})

0 commit comments

Comments
 (0)