Skip to content

Commit

Permalink
fix: dont crash on invalid brotli payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Sep 18, 2024
1 parent 0ad6007 commit f951577
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,10 @@ async function httpNetworkFetch (
} else if (coding === 'deflate') {
decoders.push(createInflate())
} else if (coding === 'br') {
decoders.push(zlib.createBrotliDecompress())
decoders.push(zlib.createBrotliDecompress({
flush: zlib.constants.BROTLI_OPERATION_FLUSH,
finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
}))
} else {
decoders.length = 0
break
Expand Down
48 changes: 48 additions & 0 deletions test/issue-3616.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const { createServer } = require('node:http')
const { tspl } = require('@matteo.collina/tspl')
const { describe, test, after } = require('node:test')
const { fetch } = require('..')
const { once } = require('node:events')

describe('https://github.com/nodejs/undici/issues/3616', () => {
const cases = [
'x-gzip',
'gzip',
'deflate',
'br'
]

for (const encoding of cases) {
test(encoding, async t => {
t = tspl(t, { plan: 2 })
const server = createServer((req, res) => {
res.writeHead(200, {
'Content-Length': '0',
Connection: 'close',
'Content-Encoding': encoding
})
res.end()
})

after(() => {
server.close()
})

server.listen(0)

await once(server, 'listening')
const result = await fetch(`http://localhost:${server.address().port}/`)

t.ok(result.body.getReader())

process.on('uncaughtException', (reason) => {
t.fail('Uncaught Exception:', reason, encoding)
})

await new Promise(resolve => setTimeout(resolve, 100))
t.ok(true)
})
}
})

0 comments on commit f951577

Please sign in to comment.