Skip to content

Commit 5c17be7

Browse files
committed
zlib: reject invalid zstd dictionaries
Signed-off-by: James M Snell <jsnell@cloudflare.com>
1 parent 4491d9d commit 5c17be7

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
@@ -907,7 +907,11 @@ class Zstd extends ZlibBase {
907907
if (isAnyArrayBuffer(dictionary)) {
908908
dictionary = new Uint8Array(dictionary);
909909
} else {
910-
dictionary = undefined;
910+
throw new ERR_INVALID_ARG_TYPE(
911+
'options.dictionary',
912+
['Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'],
913+
dictionary,
914+
);
911915
}
912916
}
913917

‎src/node_zlib.cc‎

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

18011801
CompressionError ZstdDecompressContext::ResetStream() {
1802-
const size_t result =
1803-
ZSTD_DCtx_reset(dctx_.get(), ZSTD_reset_session_only);
1802+
const size_t result = ZSTD_DCtx_reset(dctx_.get(), ZSTD_reset_session_only);
18041803
if (ZSTD_isError(result)) {
18051804
const ZSTD_ErrorCode error = ZSTD_getErrorCode(result);
18061805
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)