-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
zlib: reject reset while a zstd frame is incomplete #66088
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
xia-chao
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
xia-chao:zlib-flush-reset-corrupt-fix
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.
+175
−8
Open
Changes from all commits
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
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,129 @@ | ||
| 'use strict'; | ||
|
|
||
| // Tests that reset() refuses to run only when an incomplete zstd frame has | ||
| // already emitted output. | ||
| // | ||
| // ZSTD_reset_session_only cancels unflushed internal data, so write()-then- | ||
| // reset() with no emitted bytes is safe. Once flush() (or a write that filled | ||
| // the output buffer) has written a fragment out, resetting would append the | ||
| // next frame to that fragment and produce an undecodable stream. | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { finished } = require('stream/promises'); | ||
| const test = require('node:test'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| test('ZstdCompress reset throws when an incomplete frame has emitted output', | ||
| async () => { | ||
| const stream = zlib.createZstdCompress(); | ||
| 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); | ||
|
|
||
| // A fragment of the frame is already outside the compressor, so reset() | ||
| // must refuse instead of silently producing a stream that cannot be | ||
| // decoded. | ||
| stream.reset(); | ||
| stream.end(Buffer.from('world')); | ||
|
|
||
| await assert.rejects(finished(stream), { | ||
| code: 'ERR_ZLIB_INCOMPLETE_FRAME', | ||
| }); | ||
| }); | ||
|
|
||
| test('ZstdCompress reset throws when write itself emitted frame output', | ||
| async () => { | ||
| const stream = zlib.createZstdCompress(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| // Fill a buffer that is large enough for zstd to emit compressed bytes | ||
| // during write(), without an explicit flush(). | ||
| const input = Buffer.allocUnsafe(512 * 1024); | ||
| for (let i = 0; i < input.length; i++) { | ||
| input[i] = i & 0xff; | ||
| } | ||
| await new Promise((resolve, reject) => { | ||
| stream.write(input, (err) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| 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('ZstdCompress reset after write without emitted output still works', | ||
| async () => { | ||
| const stream = zlib.createZstdCompress(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| // Small writes often stay buffered inside zstd until flush/end. | ||
| await new Promise((resolve, reject) => { | ||
| stream.write(Buffer.from('hello'), (err) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| assert.strictEqual(Buffer.concat(chunks).length, 0); | ||
|
|
||
| // No bytes have left the compressor, so session reset is allowed. | ||
| stream.reset(); | ||
| stream.end(Buffer.from('world')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.zstdDecompressSync(Buffer.concat(chunks)).toString(), | ||
| 'world', | ||
| ); | ||
| }); | ||
|
|
||
| test('ZstdCompress flush followed by end still produces a valid stream', | ||
| async () => { | ||
| const stream = zlib.createZstdCompress(); | ||
| 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.zstdDecompressSync(Buffer.concat(chunks)).toString(), | ||
| 'helloworld', | ||
| ); | ||
| }); | ||
|
|
||
| test('ZstdCompress reset before any write still works', async () => { | ||
| const stream = zlib.createZstdCompress(); | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| // No frame has been started yet, so reset() is allowed. | ||
| stream.reset(); | ||
| stream.end(Buffer.from('hello')); | ||
| await finished(stream); | ||
|
|
||
| assert.strictEqual( | ||
| zlib.zstdDecompressSync(Buffer.concat(chunks)).toString(), | ||
| 'hello', | ||
| ); | ||
| }); |
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.