Skip to content

Commit dd928b0

Browse files
committed
zlib: be strict about what strategies are accepted
Currently, strategy constants are integers but Node.js will accept string versions of those integers. Users should be using the provided zlib constants and not hardcoding numbers, strings, or anything else. As such, Node.js should be strict about accepting only exactly those values that are in the provided zlib constants. PR-URL: #10934 Fixes: #10932 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 01b90ee commit dd928b0

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

lib/zlib.js

+10-16
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ function isValidFlushFlag(flag) {
277277
flag === constants.Z_BLOCK;
278278
}
279279

280+
const strategies = [constants.Z_FILTERED,
281+
constants.Z_HUFFMAN_ONLY,
282+
constants.Z_RLE,
283+
constants.Z_FIXED,
284+
constants.Z_DEFAULT_STRATEGY];
285+
280286
// the Zlib class they all inherit from
281287
// This thing manages the queue of requests, and returns
282288
// true or false if there is anything in the queue when
@@ -326,15 +332,8 @@ function Zlib(opts, mode) {
326332
}
327333
}
328334

329-
if (opts.strategy) {
330-
if (opts.strategy != constants.Z_FILTERED &&
331-
opts.strategy != constants.Z_HUFFMAN_ONLY &&
332-
opts.strategy != constants.Z_RLE &&
333-
opts.strategy != constants.Z_FIXED &&
334-
opts.strategy != constants.Z_DEFAULT_STRATEGY) {
335-
throw new Error('Invalid strategy: ' + opts.strategy);
336-
}
337-
}
335+
if (opts.strategy && !(strategies.includes(opts.strategy)))
336+
throw new Error('Invalid strategy: ' + opts.strategy);
338337

339338
if (opts.dictionary) {
340339
if (!(opts.dictionary instanceof Buffer)) {
@@ -378,7 +377,7 @@ function Zlib(opts, mode) {
378377
this.once('end', this.close);
379378

380379
Object.defineProperty(this, '_closed', {
381-
get: () => { return !this._handle; },
380+
get: () => !this._handle,
382381
configurable: true,
383382
enumerable: true
384383
});
@@ -391,13 +390,8 @@ Zlib.prototype.params = function(level, strategy, callback) {
391390
level > constants.Z_MAX_LEVEL) {
392391
throw new RangeError('Invalid compression level: ' + level);
393392
}
394-
if (strategy != constants.Z_FILTERED &&
395-
strategy != constants.Z_HUFFMAN_ONLY &&
396-
strategy != constants.Z_RLE &&
397-
strategy != constants.Z_FIXED &&
398-
strategy != constants.Z_DEFAULT_STRATEGY) {
393+
if (!(strategies.includes(strategy)))
399394
throw new TypeError('Invalid strategy: ' + strategy);
400-
}
401395

402396
if (this._level !== level || this._strategy !== strategy) {
403397
var self = this;

test/parallel/test-zlib-deflate-constructors.js

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ assert.doesNotThrow(
8686
() => { new zlib.Deflate({ strategy: zlib.constants.Z_DEFAULT_STRATEGY}); }
8787
);
8888

89+
// Throws if opt.strategy is the wrong type.
90+
assert.throws(
91+
() => { new zlib.Deflate({strategy: '' + zlib.constants.Z_RLE }); },
92+
/^Error: Invalid strategy: 3$/
93+
);
94+
8995
// Throws if opts.strategy is invalid
9096
assert.throws(
9197
() => { new zlib.Deflate({strategy: 'this is a bogus strategy'}); },

0 commit comments

Comments
 (0)