Repository navigation
node:zlib: refuse reset() of a gzip member that is open and has given output #47364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
2
commits into
main
Choose a base branch
from
robobun/042207aa/zlib-reset-incomplete-frame
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+545
−7
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
test/js/node/test/parallel/test-zlib-reset-incomplete-output.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| 'use strict'; | ||
|
|
||
| // Tests that reset() refuses to run on gzip once an incomplete member has | ||
| // already emitted output. | ||
| // | ||
| // deflateReset() is equivalent to deflateEnd + deflateInit. gzip writes a | ||
| // header on the first write, so those bytes cannot be taken back. | ||
| // | ||
| // zlib-wrapped deflate still allows reset after flush: official | ||
| // test-zlib-dictionary.js discards the first member and reuses the | ||
| // compressor. Raw deflate has no wrapper header: a small write may emit | ||
| // nothing, and flush+reset still concatenates. | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { finished } = require('stream/promises'); | ||
| const test = require('node:test'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| async function writeHello(stream) { | ||
| await new Promise((resolve, reject) => { | ||
| stream.write(Buffer.from('hello'), (err) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| test('Gzip reset throws when write has emitted wrapper output', async () => { | ||
| const stream = zlib.createGzip(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| await writeHello(stream); | ||
| assert.ok(Buffer.concat(chunks).length > 0); | ||
|
|
||
| stream.reset(); | ||
| stream.end(Buffer.from('world')); | ||
|
|
||
| await assert.rejects(finished(stream), { | ||
| code: 'ERR_ZLIB_INCOMPLETE_FRAME', | ||
| }); | ||
| }); | ||
|
|
||
| test('Gzip reset throws when flush has emitted incomplete output', async () => { | ||
| const stream = zlib.createGzip(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| stream.write(Buffer.from('hello')); | ||
| await new Promise((resolve) => stream.flush(resolve)); | ||
| assert.ok(Buffer.concat(chunks).length > 0); | ||
|
|
||
| stream.reset(); | ||
| stream.end(Buffer.from('world')); | ||
|
|
||
| await assert.rejects(finished(stream), { | ||
| code: 'ERR_ZLIB_INCOMPLETE_FRAME', | ||
| }); | ||
| }); | ||
|
|
||
| test('Gzip flush followed by end still produces a valid stream', async () => { | ||
| const stream = zlib.createGzip(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| stream.write(Buffer.from('hello')); | ||
| await new Promise((resolve) => stream.flush(resolve)); | ||
| stream.end(Buffer.from('world')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.gunzipSync(Buffer.concat(chunks)).toString(), | ||
| 'helloworld', | ||
| ); | ||
| }); | ||
|
|
||
| test('Gzip reset before any write still works', async () => { | ||
| const stream = zlib.createGzip(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| stream.reset(); | ||
| stream.end(Buffer.from('hello')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.gunzipSync(Buffer.concat(chunks)).toString(), | ||
| 'hello', | ||
| ); | ||
| }); | ||
|
|
||
| test('Deflate reset after flush still works when first output is discarded', | ||
| async () => { | ||
| const stream = zlib.createDeflate(); | ||
| const chunks = []; | ||
| let take = false; | ||
| stream.on('data', (chunk) => { | ||
| if (take) { | ||
| chunks.push(chunk); | ||
| } | ||
| }); | ||
|
|
||
| stream.write(Buffer.from('hello')); | ||
| await new Promise((resolve) => stream.flush(resolve)); | ||
| stream.reset(); | ||
| take = true; | ||
| stream.end(Buffer.from('world')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.inflateSync(Buffer.concat(chunks)).toString(), | ||
| 'world', | ||
| ); | ||
| }); | ||
|
|
||
| test('DeflateRaw reset after write without emitted output still works', | ||
| async () => { | ||
| const stream = zlib.createDeflateRaw(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| await writeHello(stream); | ||
| assert.strictEqual(Buffer.concat(chunks).length, 0); | ||
|
|
||
| stream.reset(); | ||
| stream.end(Buffer.from('world')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.inflateRawSync(Buffer.concat(chunks)).toString(), | ||
| 'world', | ||
| ); | ||
| }); | ||
|
|
||
| test('DeflateRaw flush followed by reset still concatenates', async () => { | ||
| const stream = zlib.createDeflateRaw(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| stream.write(Buffer.from('hello')); | ||
| await new Promise((resolve) => stream.flush(resolve)); | ||
| stream.reset(); | ||
| stream.end(Buffer.from('world')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.inflateRawSync(Buffer.concat(chunks)).toString(), | ||
| 'helloworld', | ||
| ); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.