Skip to content

Commit 3c67265

Browse files
committed
fix(retry): settle exposed body on terminal failure
Propagate a terminal retry error to the response body already exposed to the caller instead of forwarding a replacement response and orphaning the original stream. Preserve normal response forwarding when no earlier response has been exposed. Fixes: GHSA-pmjh-fq2x-6v4x CVE: CVE-2026-18149 Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit 0c63dde489bda28f2367bbaba082b24c10248d55)
1 parent cd8af90 commit 3c67265

2 files changed

Lines changed: 81 additions & 6 deletions

File tree

‎lib/handler/retry-handler.js‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ class RetryHandler {
9595
if (this.retryOpts.throwOnError) {
9696
// Preserve old behavior for status codes that are not eligible for retry
9797
if (this.retryOpts.statusCodes.includes(statusCode) === false) {
98-
this.headersSent = true
99-
this.checkpointResponseEnd(headers)
100-
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
98+
if (this.headersSent) {
99+
// The downstream handler already received the response from an
100+
// earlier attempt. Forwarding this response would replace the
101+
// downstream body and leave the original body pending forever.
102+
this.handler.onResponseError?.(controller, err)
103+
} else {
104+
this.headersSent = true
105+
this.checkpointResponseEnd(headers)
106+
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
107+
}
101108
} else {
102109
this.error = err
103110
}
@@ -114,9 +121,16 @@ class RetryHandler {
114121

115122
function shouldRetry (passedErr) {
116123
if (passedErr) {
117-
this.headersSent = true
118-
this.checkpointResponseEnd(headers)
119-
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
124+
if (this.headersSent) {
125+
// The downstream handler already received the response from an
126+
// earlier attempt. Forwarding this response would replace the
127+
// downstream body and leave the original body pending forever.
128+
this.handler.onResponseError?.(controller, passedErr)
129+
} else {
130+
this.headersSent = true
131+
this.checkpointResponseEnd(headers)
132+
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
133+
}
120134
controller.resume()
121135
return
122136
}

‎test/interceptors/retry.js‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,67 @@ test('should not error if request is not meant to be retried', async t => {
805805
t.equal(await response.body.text(), 'Bad request')
806806
})
807807

808+
test('should reject the original response body when a retry returns a non-retryable status', async t => {
809+
t = tspl(t, { plan: 3 })
810+
811+
let count = 0
812+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
813+
count++
814+
815+
if (count === 1) {
816+
res.setHeader('content-length', '5')
817+
res.write('123', () => res.destroy())
818+
return
819+
}
820+
821+
if (count === 2) {
822+
res.writeHead(400)
823+
res.end()
824+
return
825+
}
826+
827+
t.fail('unexpected request')
828+
})
829+
830+
server.listen(0)
831+
await once(server, 'listening')
832+
833+
const client = new Client(
834+
`http://localhost:${server.address().port}`,
835+
{ bodyTimeout: 100 }
836+
).compose(retry({ minTimeout: 10 }))
837+
838+
after(async () => {
839+
await client.close()
840+
server.close()
841+
await once(server, 'close')
842+
})
843+
844+
const response = await client.request({
845+
method: 'GET',
846+
path: '/'
847+
})
848+
849+
t.strictEqual(response.statusCode, 200)
850+
851+
let timer
852+
let bodyError
853+
try {
854+
bodyError = await Promise.race([
855+
response.body.text().then(() => null, err => err),
856+
new Promise(resolve => {
857+
timer = setTimeout(() => resolve(new Error('response body timed out')), 2e3)
858+
})
859+
])
860+
} finally {
861+
clearTimeout(timer)
862+
response.body.destroy()
863+
}
864+
865+
t.strictEqual(bodyError?.code, 'UND_ERR_REQ_RETRY')
866+
t.strictEqual(count, 2)
867+
})
868+
808869
test('#3975 - keep event loop ticking', async t => {
809870
const suite = tspl(t, { plan: 2 })
810871

0 commit comments

Comments
 (0)