-
-
Notifications
You must be signed in to change notification settings - Fork 288
compression: enforce maximum uncompressed packet size #1498
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
VasilisDragon
wants to merge
3
commits into
PrismarineJS:master
Choose a base branch
from
VasilisDragon:vasilis-decompression-cap
base: master
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.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* eslint-env mocha */ | ||
| // Tests the compression transforms, in particular that the decompressor enforces the | ||
| // protocol's maximum uncompressed packet size so a malicious peer cannot send a | ||
| // "decompression bomb" that inflates to an unbounded synchronous allocation. See #664. | ||
| const assert = require('assert') | ||
| const zlib = require('zlib') | ||
| const [, writeVarInt, sizeOfVarInt] = require('protodef').types.varint | ||
| const { supportedVersions } = require('../src/version') | ||
| const { createCompressor, createDecompressor } = require('../src/transforms/compression') | ||
|
|
||
| const MAX_UNCOMPRESSED_LENGTH = 8388608 // 2 ** 23 | ||
|
|
||
| // Push a single chunk through a transform and resolve with the array of emitted chunks. | ||
| function pump (transform, chunk) { | ||
| return new Promise((resolve, reject) => { | ||
| const out = [] | ||
| transform.on('data', (d) => out.push(d)) | ||
| transform.on('error', reject) | ||
| transform.write(chunk, (err) => { | ||
| if (err) return reject(err) | ||
| setImmediate(() => resolve(out)) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| // Build a compressed-packet frame: a VarInt declared uncompressed length followed by `payload`. | ||
| function framePacket (declaredLength, payload) { | ||
| const header = Buffer.alloc(sizeOfVarInt(declaredLength)) | ||
| writeVarInt(declaredLength, header, 0) | ||
| return Buffer.concat([header, payload]) | ||
| } | ||
|
|
||
| // The transforms are version-independent, but the test job filters by `-g <version>v`, | ||
| // so the suite is registered per supported version to run under CI. | ||
| for (const supportedVersion of supportedVersions) { | ||
| describe('compression ' + supportedVersion + 'v', () => { | ||
| it('round-trips a compressed packet', async () => { | ||
| const original = Buffer.from('a payload long enough to be worth compressing'.repeat(8)) | ||
| const [compressed] = await pump(createCompressor(0), original) | ||
| const [restored] = await pump(createDecompressor(0), compressed) | ||
| assert.ok(restored.equals(original), 'decompressed output should equal the original') | ||
| }) | ||
|
|
||
| it('passes through an uncompressed packet (declared length 0)', async () => { | ||
| const original = Buffer.from('small') | ||
| // threshold above the payload size -> compressor emits it uncompressed (VarInt 0 prefix) | ||
| const [framed] = await pump(createCompressor(256), original) | ||
| const [restored] = await pump(createDecompressor(256), framed) | ||
| assert.ok(restored.equals(original)) | ||
| }) | ||
|
|
||
| it('drops a decompression bomb instead of allocating past the maximum', async () => { | ||
| // Payload inflates to more than the protocol maximum, but declares a small length. | ||
| const bomb = zlib.deflateSync(Buffer.alloc(MAX_UNCOMPRESSED_LENGTH + 1024)) | ||
| const packet = framePacket(100, bomb) | ||
| const out = await pump(createDecompressor(0, true), packet) | ||
| assert.strictEqual(out.length, 0, 'oversized inflate should be dropped, not emitted') | ||
| }) | ||
|
|
||
| it('rejects a packet whose declared uncompressed length exceeds the maximum', async () => { | ||
| const packet = framePacket(MAX_UNCOMPRESSED_LENGTH + 1, zlib.deflateSync(Buffer.from('x'))) | ||
| const out = await pump(createDecompressor(0, true), packet) | ||
| assert.strictEqual(out.length, 0, 'over-cap declared length should be dropped before inflating') | ||
| }) | ||
| }) | ||
| } | ||
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.