From 5d36f5f699f9b95c267ef1b1db1b382aa22777eb Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 4 Apr 2022 14:20:04 +0100 Subject: [PATCH] Improve error message for SVG render above limit #3167 --- docs/changelog.md | 3 +++ src/pipeline.cc | 8 +++++++- test/unit/svg.js | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 1516b9b4d..f75f40c94 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -8,6 +8,9 @@ Requires libvips v8.12.2 * Increase control over sensitivity to invalid images via `failOn`, deprecate `failOnError` (equivalent to `failOn: 'warning'`). +* Improve error message when attempting to render SVG input greater than 32767x32767. + [#3167](https://github.com/lovell/sharp/issues/3167) + * Ensure `create` input image has correct bit depth and colour space. [#3139](https://github.com/lovell/sharp/issues/3139) diff --git a/src/pipeline.cc b/src/pipeline.cc index 269d225b9..c0ada83a1 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -241,8 +241,10 @@ class PipelineWorker : public Napi::AsyncWorker { // Reload SVG file image = VImage::svgload(const_cast(baton->input->file.data()), option); } - sharp::SetDensity(image, baton->input->density); + if (image.width() > 32767 || image.height() > 32767) { + throw vips::VError("Input SVG image will exceed 32767x32767 pixel limit when scaled"); + } } else if (inputImageType == sharp::ImageType::PDF) { option->set("n", baton->input->pages); option->set("page", baton->input->page); @@ -260,6 +262,10 @@ class PipelineWorker : public Napi::AsyncWorker { sharp::SetDensity(image, baton->input->density); } + } else { + if (inputImageType == sharp::ImageType::SVG && (image.width() > 32767 || image.height() > 32767)) { + throw vips::VError("Input SVG image exceeds 32767x32767 pixel limit"); + } } // Any pre-shrinking may already have been done diff --git a/test/unit/svg.js b/test/unit/svg.js index abe2edc1f..4e11922e4 100644 --- a/test/unit/svg.js +++ b/test/unit/svg.js @@ -136,6 +136,20 @@ describe('SVG input', function () { assert.strictEqual(info.channels, 4); }); + it('Fails to render SVG larger than 32767x32767', () => + assert.rejects( + () => sharp(Buffer.from('')).toBuffer(), + /Input SVG image exceeds 32767x32767 pixel limit/ + ) + ); + + it('Fails to render scaled SVG larger than 32767x32767', () => + assert.rejects( + () => sharp(Buffer.from('')).resize(32768).toBuffer(), + /Input SVG image will exceed 32767x32767 pixel limit when scaled/ + ) + ); + it('Detects SVG passed as a string', () => assert.rejects( () => sharp('').toBuffer(),