Skip to content

Commit 5e55085

Browse files
jasnelladuh95
authored andcommitted
zlib: reject invalid zstd dictionaries
Signed-off-by: James M Snell <jsnell@cloudflare.com> PR-URL: #65867 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 89aa2bd commit 5e55085

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

lib/zlib.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,11 @@ class Zstd extends ZlibBase {
950950
if (isAnyArrayBuffer(dictionary)) {
951951
dictionary = new Uint8Array(dictionary);
952952
} else {
953-
dictionary = undefined;
953+
throw new ERR_INVALID_ARG_TYPE(
954+
'options.dictionary',
955+
['Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'],
956+
dictionary,
957+
);
954958
}
955959
}
956960

src/node_zlib.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,8 +1820,7 @@ CompressionError ZstdDecompressContext::Init(uint64_t pledged_src_size,
18201820
}
18211821

18221822
CompressionError ZstdDecompressContext::ResetStream() {
1823-
const size_t result =
1824-
ZSTD_DCtx_reset(dctx_.get(), ZSTD_reset_session_only);
1823+
const size_t result = ZSTD_DCtx_reset(dctx_.get(), ZSTD_reset_session_only);
18251824
if (ZSTD_isError(result)) {
18261825
const ZSTD_ErrorCode error = ZSTD_getErrorCode(result);
18271826
return CompressionError(

test/parallel/test-zlib-zstd-dictionary.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,24 @@ for (const dict of [arrayBuffer, uint8, dataView]) {
4040
const decompressed = zlib.zstdDecompressSync(compressed, { dictionary: dict });
4141
assert.strictEqual(decompressed.toString(), input.toString());
4242
}
43+
44+
for (const dictionary of [null, 'string', 123, true, {}, [1, 2, 3]]) {
45+
const options = { dictionary };
46+
const expected = {
47+
code: 'ERR_INVALID_ARG_TYPE',
48+
name: 'TypeError',
49+
};
50+
51+
assert.throws(() => zlib.createZstdCompress(options), expected);
52+
assert.throws(() => zlib.createZstdDecompress(options), expected);
53+
assert.throws(() => zlib.zstdCompressSync(input, options), expected);
54+
assert.throws(() => zlib.zstdDecompressSync(input, options), expected);
55+
assert.throws(
56+
() => zlib.zstdCompress(input, options, common.mustNotCall()),
57+
expected,
58+
);
59+
assert.throws(
60+
() => zlib.zstdDecompress(input, options, common.mustNotCall()),
61+
expected,
62+
);
63+
}

0 commit comments

Comments
 (0)