From 69fe21a7ec45df526461c4bf308332344112169a Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Fri, 16 Aug 2019 21:21:12 +0100 Subject: [PATCH] Ensure invalid resize width/height as options throw #1817 --- docs/changelog.md | 3 +++ lib/resize.js | 16 ++++++++++++---- test/unit/resize.js | 12 ++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 09d9306c..8373df11 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -9,6 +9,9 @@ Requires libvips v8.8.1. * Ensure `sharp.format.vips` is present and correct (filesystem only). [#1813](https://github.com/lovell/sharp/issues/1813) +* Ensure invalid `width` and `height` provided as options to `resize` throw. + [#1817](https://github.com/lovell/sharp/issues/1817) + * Allow use of 'heic' and 'heif' identifiers with `toFormat`. [#1834](https://github.com/lovell/sharp/pull/1834) [@jaubourg](https://github.com/jaubourg) diff --git a/lib/resize.js b/lib/resize.js index 215dec29..1cc3fea9 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -210,12 +210,20 @@ function resize (width, height, options) { } if (is.object(options)) { // Width - if (is.integer(options.width) && options.width > 0) { - this.options.width = options.width; + if (is.defined(options.width)) { + if (is.integer(options.width) && options.width > 0) { + this.options.width = options.width; + } else { + throw is.invalidParameterError('width', 'positive integer', options.width); + } } // Height - if (is.integer(options.height) && options.height > 0) { - this.options.height = options.height; + if (is.defined(options.height)) { + if (is.integer(options.height) && options.height > 0) { + this.options.height = options.height; + } else { + throw is.invalidParameterError('height', 'positive integer', options.height); + } } // Fit if (is.defined(options.fit)) { diff --git a/test/unit/resize.js b/test/unit/resize.js index ee6ed639..f9146e7c 100644 --- a/test/unit/resize.js +++ b/test/unit/resize.js @@ -87,6 +87,18 @@ describe('Resize dimensions', function () { }, /Expected positive integer for height but received 1.5 of type number/); }); + it('Invalid width - via options', () => { + assert.throws(() => { + sharp().resize({ width: 1.5, height: 240 }); + }, /Expected positive integer for width but received 1.5 of type number/); + }); + + it('Invalid height - via options', () => { + assert.throws(() => { + sharp().resize({ width: 320, height: 1.5 }); + }, /Expected positive integer for height but received 1.5 of type number/); + }); + it('Invalid width - too large', function (done) { sharp(fixtures.inputJpg) .resize(0x4000, 1)