-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
zlib: fix windowBits validation to allow 0 for decompression mode #19686
Conversation
From the zlib v1.2.11 manual: > ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, > int windowBits)); > > ... > windowBits can also be zero to request that inflate use the window > size in the zlib header of the compressed stream. The current validation of windowBits in zlib.js doesn't check for this case.
lib/zlib.js
Outdated
mode === GUNZIP || | ||
mode === INFLATERAW || | ||
mode === UNZIP)) { | ||
windowBits = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you run make lint
? It should complain about the lack of a semicolon here ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops. I'm spoiled by standardjs! Will fix.
lib/zlib.js
Outdated
if (opts.windowBits === 0 && | ||
(mode === INFLATE || | ||
mode === GUNZIP || | ||
mode === INFLATERAW || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
INFLATERAW
means that there is no header, so I think we'd want to drop that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I picked the list from
Lines 541 to 544 in 22f4a35
case INFLATE: | |
case GUNZIP: | |
case INFLATERAW: | |
case UNZIP: |
inflateInit2()
. Will remove it from the list.
lib/zlib.js
Outdated
// windowBits is special. On the compression side, 0 is an invalid value. | ||
// But on the decompression side, a value of 0 for windowBits tells zlib | ||
// to extract the value from the headers of the compressed stream. | ||
if (opts.windowBits === 0 && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may actually want to also capture undefined
here, so that when decompressing we always go with the header-provided value (instead of the safe maximum) for lower memory usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
excellent idea! How about opts.windowBits == null || opts.windowBits === 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anandsuresh Sounds reasonable to me!
Test(s)? |
0eacda7
to
10e9d16
Compare
SGTM with tests added. |
10e9d16
to
736be73
Compare
tests added. |
failed = true; | ||
} catch (e) { | ||
assert(e instanceof RangeError); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
common.expectsError()
instead of try-catch.
failed = true; | ||
} catch (e) { | ||
assert(e instanceof RangeError); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
common.expectsError()
instead of try-catch.
let failed = false; | ||
let deflate = null; | ||
try { | ||
deflate = zlib.createDeflate({windowBits: 0}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already tested in test-zlib-deflate-constructors.js
so it is probably redundant.
let failed = false; | ||
let gzip = null; | ||
try { | ||
gzip = zlib.createGzip({windowBits: 0}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simply by using common.expectsError()
:
common.expectsError(
() => zlib.createGzip({windowBits: 0}),
{
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError,
message: '...'
}
);
bcb88e0
to
1ba53f2
Compare
standby.... some tests are failing. Working to fix those. |
1ba53f2
to
fda4044
Compare
all tests passing again. |
Landed in 7bc5151 🎉 |
From the zlib v1.2.11 manual: > ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, > int windowBits)); > > ... > windowBits can also be zero to request that inflate use the window > size in the zlib header of the compressed stream. The current validation of windowBits in zlib.js doesn't check for this case. PR-URL: nodejs#19686 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
From the zlib v1.2.11 manual: > ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, > int windowBits)); > > ... > windowBits can also be zero to request that inflate use the window > size in the zlib header of the compressed stream. The current validation of windowBits in zlib.js doesn't check for this case. PR-URL: nodejs#19686 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
From the zlib v1.2.11 manual:
The current validation of windowBits in zlib.js doesn't check for this
case.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes