Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/workerd/api/node/tests/zlib-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,63 @@ export const maxOutputLength = {
},
};

// Test for maxOutputLength: 0 - should throw immediately instead of infinite loop
export const maxOutputLengthZero = {
async test() {
const expectedError = {
name: 'RangeError',
message:
/The value of "options\.maxOutputLength" is out of range\. It must be >= 1/,
};

// Sync zlib - deflateSync with maxOutputLength: 0 should throw
assert.throws(
() => zlib.deflateSync('data', { maxOutputLength: 0 }),
expectedError
);

// Sync zlib - inflateSync with maxOutputLength: 0 should throw
const compressed = zlib.deflateSync('data');
assert.throws(
() => zlib.inflateSync(compressed, { maxOutputLength: 0 }),
expectedError
);

// Sync brotli - brotliCompressSync with maxOutputLength: 0 should throw
assert.throws(
() => zlib.brotliCompressSync('data', { maxOutputLength: 0 }),
expectedError
);

// Sync brotli - brotliDecompressSync with maxOutputLength: 0 should throw
const brotliCompressed = zlib.brotliCompressSync('data');
assert.throws(
() => zlib.brotliDecompressSync(brotliCompressed, { maxOutputLength: 0 }),
expectedError
);

// Async zlib - deflate with maxOutputLength: 0 should error
{
const { promise, resolve } = Promise.withResolvers();
zlib.deflate('data', { maxOutputLength: 0 }, (err) => {
assert.match(err.message, expectedError.message);
resolve();
});
await promise;
}

// Async brotli - brotliCompress with maxOutputLength: 0 should error
{
const { promise, resolve } = Promise.withResolvers();
zlib.brotliCompress('data', { maxOutputLength: 0 }, (err) => {
assert.match(err.message, expectedError.message);
resolve();
});
await promise;
}
},
};

// Test taken from
// https://github.com/nodejs/node/blob/24302c9fe94e1dd755ac8a8cc1f6aa4444f75cb3/test/parallel/test-zlib-invalid-arg-value-brotli-compress.js
export const invalidArgValueBrotliCompress = {
Expand Down
8 changes: 6 additions & 2 deletions src/workerd/api/node/zlib-util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,9 @@ kj::Array<kj::byte> ZlibUtil::zlibSync(
JSG_REQUIRE(Z_MIN_CHUNK <= chunkSize && chunkSize <= Z_MAX_CHUNK, RangeError,
kj::str("The value of \"options.chunkSize\" is out of range. It must be >= ", Z_MIN_CHUNK,
" and <= ", Z_MAX_CHUNK, ". Received ", chunkSize));
JSG_REQUIRE(maxOutputLength <= Z_MAX_CHUNK, RangeError, "Invalid maxOutputLength"_kj);
JSG_REQUIRE(maxOutputLength >= 1 && maxOutputLength <= Z_MAX_CHUNK, RangeError,
kj::str("The value of \"options.maxOutputLength\" is out of range. It must be >= 1 and <= ",
Z_MAX_CHUNK, ". Received ", maxOutputLength));
GrowableBuffer result(ZLIB_PERFORMANT_CHUNK_SIZE, maxOutputLength);

ctx.initialize(opts.level.orDefault(Z_DEFAULT_LEVEL),
Expand Down Expand Up @@ -879,7 +881,9 @@ kj::Array<kj::byte> ZlibUtil::brotliSync(
JSG_REQUIRE(Z_MIN_CHUNK <= chunkSize && chunkSize <= Z_MAX_CHUNK, RangeError,
kj::str("The value of \"options.chunkSize\" is out of range. It must be >= ", Z_MIN_CHUNK,
" and <= ", Z_MAX_CHUNK, ". Received ", chunkSize));
JSG_REQUIRE(maxOutputLength <= Z_MAX_CHUNK, Error, "Invalid maxOutputLength"_kj);
JSG_REQUIRE(maxOutputLength >= 1 && maxOutputLength <= Z_MAX_CHUNK, RangeError,
kj::str("The value of \"options.maxOutputLength\" is out of range. It must be >= 1 and <= ",
Z_MAX_CHUNK, ". Received ", maxOutputLength));
GrowableBuffer result(ZLIB_PERFORMANT_CHUNK_SIZE, maxOutputLength);

KJ_IF_SOME(err,
Expand Down