Skip to content

Commit 21693f4

Browse files
committed
fix(interceptor/dump): abort oversized chunked responses
Abort streamed responses whose received body exceeds the dump interceptor maxSize, wait for the real response end instead of invoking the terminal handler early, and propagate abort and network errors after reaching the size boundary. Fixes: GHSA-2gqq-gqf2-x968 CVE: CVE-2026-84947 Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit 0e76518f5e632084ce7f86222bd5b137f79d3bf8)
1 parent f690157 commit 21693f4

2 files changed

Lines changed: 47 additions & 24 deletions

File tree

‎lib/interceptor/dump.js‎

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class DumpHandler extends DecoratorHandler {
77
#maxSize = 1024 * 1024
88
#dumped = false
99
#size = 0
10-
#controller = null
1110
aborted = false
1211
reason = false
1312

@@ -29,7 +28,6 @@ class DumpHandler extends DecoratorHandler {
2928

3029
onRequestStart (controller, context) {
3130
controller.abort = this.#abort.bind(this)
32-
this.#controller = controller
3331

3432
return super.onRequestStart(controller, context)
3533
}
@@ -53,43 +51,32 @@ class DumpHandler extends DecoratorHandler {
5351
}
5452

5553
onResponseError (controller, err) {
56-
if (this.#dumped) {
57-
return
58-
}
59-
60-
// On network errors before connect, controller will be null
61-
err = this.#controller?.reason ?? err
62-
63-
super.onResponseError(controller, err)
54+
super.onResponseError(controller, this.aborted === true ? this.reason : err)
6455
}
6556

6657
onResponseData (controller, chunk) {
6758
this.#size = this.#size + chunk.length
6859

69-
if (this.#size >= this.#maxSize) {
70-
this.#dumped = true
60+
if (this.#size > this.#maxSize) {
61+
throw new RequestAbortedError(
62+
`Response size (${this.#size}) larger than maxSize (${this.#maxSize})`
63+
)
64+
}
7165

72-
if (this.aborted === true) {
73-
super.onResponseError(controller, this.reason)
74-
} else {
75-
super.onResponseEnd(controller, {})
76-
}
66+
if (this.#size === this.#maxSize) {
67+
this.#dumped = true
7768
}
7869

7970
return true
8071
}
8172

8273
onResponseEnd (controller, trailers) {
83-
if (this.#dumped) {
84-
return
85-
}
86-
87-
if (this.#controller.aborted === true) {
74+
if (this.aborted === true) {
8875
super.onResponseError(controller, this.reason)
8976
return
9077
}
9178

92-
super.onResponseEnd(controller, trailers)
79+
super.onResponseEnd(controller, this.#dumped ? {} : trailers)
9380
}
9481
}
9582

‎test/interceptors/dump-interceptor.js‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ test('Should dump on already aborted request', { skip }, async t => {
150150
})
151151

152152
res.once('close', () => {
153-
t.equal(offset, 1024)
153+
t.ok(offset >= 768 && offset <= 1024)
154154
})
155155

156156
const interval = setInterval(() => {
@@ -285,6 +285,42 @@ test('Should dump response body up to limit and ignore trailers', { skip }, asyn
285285
await t.completed
286286
})
287287

288+
test('Should abort if chunked response is larger than max size', { skip }, async t => {
289+
t = tspl(t, { plan: 2 })
290+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
291+
res.writeHead(200, {
292+
'Content-Type': 'application/octet-stream',
293+
'Transfer-Encoding': 'chunked'
294+
})
295+
res.write(Buffer.alloc(512))
296+
setImmediate(() => res.end(Buffer.alloc(512)))
297+
})
298+
299+
server.listen(0)
300+
await once(server, 'listening')
301+
302+
const client = new Client(
303+
`http://localhost:${server.address().port}`
304+
).compose(dump({ maxSize: 512 }))
305+
306+
after(async () => {
307+
await client.close()
308+
309+
server.close()
310+
await once(server, 'close')
311+
})
312+
313+
const response = await client.request({ method: 'GET', path: '/' })
314+
315+
t.equal(response.statusCode, 200)
316+
await t.rejects(response.body.text(), {
317+
name: 'AbortError',
318+
message: /^Response size \(\d+\) larger than maxSize \(512\)$/
319+
})
320+
321+
await t.completed
322+
})
323+
288324
test('Should forward common error', { skip }, async t => {
289325
t = tspl(t, { plan: 1 })
290326
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {

0 commit comments

Comments
 (0)