From 4fb265ca63422d211439b8e29a32cd3975c93c6e Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 27 Feb 2023 00:55:35 +0800 Subject: [PATCH] fix: reject if image is not supported --- Cargo.toml | 1 + __test__/fixtures/broken.png | 1 + __test__/image.spec.ts | 6 +++++- load-image.js | 2 +- src/image.rs | 24 +++++++++++++++++++----- 5 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 __test__/fixtures/broken.png diff --git a/Cargo.toml b/Cargo.toml index 54db05af..e69ec639 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["cdylib"] anyhow = "1" base64 = "0.21" cssparser = "0.29" +infer = "0.12" napi = { version = "2", default-features = false, features = [ "napi3", "serde-json", diff --git a/__test__/fixtures/broken.png b/__test__/fixtures/broken.png new file mode 100644 index 00000000..5b037f32 --- /dev/null +++ b/__test__/fixtures/broken.png @@ -0,0 +1 @@ +BROKEN PNG \ No newline at end of file diff --git a/__test__/image.spec.ts b/__test__/image.spec.ts index fbca93df..27e2983a 100644 --- a/__test__/image.spec.ts +++ b/__test__/image.spec.ts @@ -2,7 +2,7 @@ import { promises as fs } from 'fs' import { join } from 'path' import test from 'ava' -import { createCanvas, Image } from '../index' +import { createCanvas, Image, loadImage } from '../index' import { snapshotImage } from './image-snapshot' @@ -88,3 +88,7 @@ test('svg-transparent-background', async (t) => { await snapshotImage(t, { canvas }) }) + +test('load invalid image should throw error', async (t) => { + await t.throwsAsync(() => loadImage(join(__dirname, 'fixtures', 'broken.png'))) +}) diff --git a/load-image.js b/load-image.js index ca79d9d8..30ec726a 100644 --- a/load-image.js +++ b/load-image.js @@ -51,7 +51,7 @@ module.exports = async function loadImage(source, options = {}) { } } - // throw error as dont support that source + // throw error as don't support that source throw new TypeError('unsupported image source') } diff --git a/src/image.rs b/src/image.rs index 0ad41aa9..019dd330 100644 --- a/src/image.rs +++ b/src/image.rs @@ -262,15 +262,29 @@ impl Image { let image_binary = STANDARD .decode(base64_str) .map_err(|e| Error::new(Status::InvalidArg, format!("Decode data url failed {e}")))?; - Some(Bitmap::from_buffer( - image_binary.as_ptr() as *mut u8, - image_binary.len(), - )) + if let Some(kind) = infer::get(&image_binary) { + if kind.matcher_type() == infer::MatcherType::Image { + Some(Bitmap::from_buffer( + image_binary.as_ptr() as *mut u8, + image_binary.len(), + )) + } else { + return Err(Error::new(Status::InvalidArg, "Unsupported image type")); + } + } else { + return Err(Error::new(Status::InvalidArg, "Unsupported image type")); + } } else { None } + } else if let Some(kind) = infer::get(&data) { + if kind.matcher_type() == infer::MatcherType::Image { + Some(Bitmap::from_buffer(data.as_ptr() as *mut u8, length)) + } else { + return Err(Error::new(Status::InvalidArg, "Unsupported image type")); + } } else { - Some(Bitmap::from_buffer(data.as_ptr() as *mut u8, length)) + return Err(Error::new(Status::InvalidArg, "Unsupported image type")); }; if let Some(ref b) = bitmap { if (self.width - -1.0).abs() < f64::EPSILON {