Skip to content

Commit

Permalink
zlib: add internal assertion on stream transform
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Dec 25, 2024
1 parent 3147ec2 commit 07f3de0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
codes: {
ERR_BROTLI_INVALID_PARAM,
ERR_BUFFER_TOO_LARGE,
ERR_INTERNAL_ASSERTION,
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
},
Expand Down Expand Up @@ -353,6 +354,9 @@ ZlibBase.prototype._destroy = function(err, callback) {
};

ZlibBase.prototype._transform = function(chunk, encoding, cb) {
if (!Buffer.isBuffer(chunk)) {
throw new ERR_INTERNAL_ASSERTION('Chunk must be a Buffer');
}
let flushFlag = this._defaultFlushFlag;
// We use a 'fake' zero-length chunk to carry information about flushes from
// the public API to the actual stream implementation.
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,15 @@ testKeys.forEach(common.mustCall((file) => {
const gzip = zlib.Gzip();
assert.ok(gzip instanceof zlib.Gzip);
}

{
// Misuse of stream internals
// Issue: https://github.com/nodejs/node/issues/37884
assert.throws(() => zlib.createGunzip()._transform('Hello'), {
code: 'ERR_INTERNAL_ASSERTION',
});

assert.throws(() => zlib.createGunzip()._write('Hello'), {
code: 'ERR_INTERNAL_ASSERTION',
});
}

0 comments on commit 07f3de0

Please sign in to comment.