Skip to content

Commit

Permalink
fix: reject if image is not supported (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Feb 27, 2023
1 parent aa42fb1 commit e4f4930
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions __test__/fixtures/broken.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion __test__/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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')))
})
2 changes: 1 addition & 1 deletion load-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down
24 changes: 19 additions & 5 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e4f4930

Please sign in to comment.