Skip to content

Commit e1bfa93

Browse files
Kashkovskyquanglam2807hipstersmoothie
authored
Update file-type from ^16 to ^21.3.3 in @jimp/core (#1400)
* Update file-type from ^16 to ^21.3.1 in @jimp/core Addresses the security vulnerability in file-type <16.5.4 and <18.7.0 (GHSA-5v7r-6r5c-r473 / CVE-2024-4367) by upgrading to v21. Changes: - Update file-type dependency from ^16.0.0 to ^21.3.1 - Remove deprecated @types/file-type (types are now bundled) - Update import from default export to named export (fileTypeFromBuffer) Fixes #1399 * Bump file-type to address CVE-2026-32630 * Update packages/core/package.json Co-authored-by: Quang Lam <1548835+quanglam2807@users.noreply.github.com> * fix build --------- Co-authored-by: Quang Lam <1548835+quanglam2807@users.noreply.github.com> Co-authored-by: Andrew Lisowski <lisowski54@gmail.com>
1 parent b6b0e41 commit e1bfa93

8 files changed

Lines changed: 84 additions & 76 deletions

File tree

packages/core/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
"@jimp/utils": "workspace:*",
2121
"await-to-js": "^3.0.0",
2222
"exif-parser": "^0.1.12",
23-
"file-type": "^16.0.0",
23+
"file-type": "^21.3.3",
2424
"mime": "3"
2525
},
2626
"devDependencies": {
2727
"@jimp/config-eslint": "workspace:*",
2828
"@jimp/config-typescript": "workspace:*",
2929
"@jimp/test-utils": "workspace:*",
30-
"@types/file-type": "^10.9.1",
3130
"@types/mime": "^3.0.4",
3231
"@types/node": "^18.19.48",
3332
"eslint": "^9.9.1",

packages/core/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Bitmap, Format, JimpClass, Edge } from "@jimp/types";
22
import { cssColorToHex, scan, scanIterator } from "@jimp/utils";
3-
import fileType from "file-type/core.js";
43
import { to } from "await-to-js";
54
import { existsSync, readFile, writeFile } from "@jimp/file-ops";
65
import mime from "mime/lite.js";
@@ -29,6 +28,15 @@ function bufferFromArrayBuffer(arrayBuffer: ArrayBuffer) {
2928
return buffer;
3029
}
3130

31+
async function detectFileTypeFromBuffer(buffer: Buffer | ArrayBuffer) {
32+
const { fileTypeFromBuffer } = await import("file-type");
33+
return fileTypeFromBuffer(
34+
buffer instanceof ArrayBuffer
35+
? buffer
36+
: new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
37+
);
38+
}
39+
3240
export { getExifOrientation } from "./utils/image-bitmap.js";
3341
export { composite } from "./utils/composite.js";
3442
export * from "./utils/constants.js";
@@ -334,7 +342,7 @@ export function createJimp<
334342
const actualBuffer =
335343
buffer instanceof ArrayBuffer ? bufferFromArrayBuffer(buffer) : buffer;
336344

337-
const mime = await fileType.fromBuffer(actualBuffer);
345+
const mime = await detectFileTypeFromBuffer(actualBuffer);
338346

339347
if (!mime || !mime.mime) {
340348
throw new Error("Could not find MIME for Buffer");

plugins/plugin-quantize/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const methods = {
5959
} = QuantizeOptionsSchema.parse(options);
6060

6161
const inPointContainer = utils.PointContainer.fromUint8Array(
62-
image.bitmap.data,
62+
new Uint8Array(image.bitmap.data.buffer),
6363
image.bitmap.width,
6464
image.bitmap.height
6565
);

plugins/wasm-avif/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ export default function avif() {
6565
},
6666
decode: async (data) => {
6767
await initDecoder();
68-
const result = await decode(data);
68+
const result = await decode(new Uint8Array(data).buffer);
6969

7070
return {
71-
data: Buffer.from(result.data),
71+
data: Buffer.from(new Uint8Array(result.data.buffer, result.data.byteOffset, result.data.byteLength)),
7272
width: result.width,
7373
height: result.height,
7474
};

plugins/wasm-jpeg/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ export default function jpeg() {
9797
},
9898
decode: async (data) => {
9999
await initDecoder();
100-
const result = await decode(data);
100+
const result = await decode(new Uint8Array(data).buffer);
101101

102102
return {
103-
data: Buffer.from(result.data),
103+
data: Buffer.from(new Uint8Array(result.data.buffer, result.data.byteOffset, result.data.byteLength)),
104104
width: result.width,
105105
height: result.height,
106106
};

plugins/wasm-png/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export default function png() {
4141
},
4242
decode: async (data) => {
4343
await initDecoder();
44-
const result = await decode(data);
44+
const result = await decode(new Uint8Array(data).buffer);
4545

4646
return {
47-
data: Buffer.from(result.data),
47+
data: Buffer.from(new Uint8Array(result.data.buffer, result.data.byteOffset, result.data.byteLength)),
4848
width: result.width,
4949
height: result.height,
5050
};

plugins/wasm-webp/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ export default function png() {
176176
},
177177
decode: async (data) => {
178178
await initDecoder();
179-
const result = await decode(data);
179+
const result = await decode(new Uint8Array(data).buffer);
180180

181181
return {
182-
data: Buffer.from(result.data),
182+
data: Buffer.from(new Uint8Array(result.data.buffer, result.data.byteOffset, result.data.byteLength)),
183183
width: result.width,
184184
height: result.height,
185185
};

0 commit comments

Comments
 (0)