Skip to content

Commit

Permalink
fix: unhandled exception or failing error body (#3137)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Apr 21, 2024
1 parent ad9b5bd commit ee2b386
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ async function getResolveErrorBodyCallback ({ callback, body, contentType, statu
let chunks = []
let length = 0

for await (const chunk of body) {
chunks.push(chunk)
length += chunk.length
if (length > CHUNK_LIMIT) {
chunks = null
break
try {
for await (const chunk of body) {
chunks.push(chunk)
length += chunk.length
if (length > CHUNK_LIMIT) {
chunks = []
length = 0
break
}
}
} catch {
chunks = []
length = 0
// Do nothing....
}

const message = `Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`

if (statusCode === 204 || !contentType || !chunks) {
if (statusCode === 204 || !contentType || !length) {
queueMicrotask(() => callback(new ResponseStatusCodeError(message, statusCode, headers)))
return
}
Expand Down
21 changes: 21 additions & 0 deletions test/issue-3136.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { request } = require('..')
const { test, after } = require('node:test')
const net = require('node:net')
const { once } = require('node:events')
const assert = require('node:assert')

test('https://github.com/mcollina/undici/issues/3136', async (t) => {
const server = net.createServer((socket) => {
socket.write('HTTP/1.1 404 Not Found\r\n')
socket.write('Transfer-Encoding: chunked\r\n\r\n')
socket.write('\r\n')
})
after(() => server.close())
server.listen(0)
await once(server, 'listening')
await assert.rejects(
request(`http://localhost:${server.address().port}`, {
throwOnError: true
})
)
})

0 comments on commit ee2b386

Please sign in to comment.