From 40e07fdf04a36fedd9d0d212053816100d1acc46 Mon Sep 17 00:00:00 2001 From: Barnaby Mercer Date: Tue, 4 Jul 2023 12:31:09 +0100 Subject: [PATCH 1/3] Update image-request.ts Extend JPG magic numbers to include FFD8FFE2 --- source/image-handler/image-request.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/source/image-handler/image-request.ts b/source/image-handler/image-request.ts index ad4fae161..1329b8ded 100644 --- a/source/image-handler/image-request.ts +++ b/source/image-handler/image-request.ts @@ -453,6 +453,7 @@ export class ImageRequest { case "FFD8FFED": case "FFD8FFEE": case "FFD8FFE1": + case "FFD8FFE2": return ContentTypes.JPEG; case "52494646": return ContentTypes.WEBP; From e4529ed04ff1a94a3ffc1e9fdf8461bb37b48ddc Mon Sep 17 00:00:00 2001 From: Barnaby Mercer Date: Tue, 4 Jul 2023 12:49:26 +0100 Subject: [PATCH 2/3] Update infer-image-type.spec.ts Generalise tests for other magic numbers --- .../test/image-request/infer-image-type.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source/image-handler/test/image-request/infer-image-type.spec.ts b/source/image-handler/test/image-request/infer-image-type.spec.ts index 301a746bf..04873551c 100644 --- a/source/image-handler/test/image-request/infer-image-type.spec.ts +++ b/source/image-handler/test/image-request/infer-image-type.spec.ts @@ -12,6 +12,20 @@ describe("inferImageType", () => { const secretsManager = new SecretsManager(); const secretProvider = new SecretProvider(secretsManager); + test.each([ + { value: [0xff, 0xd8, 0xff, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], label: "FFD8FFEE" }, + { value: [0xff, 0xd8, 0xff, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], label: "FFD8FFE2" }, + ])('Should pass if it returns "image/jpeg" for a magic number of $label', ({ value, label }) => { + const imageBuffer = Buffer.from(value); + + // Act + const imageRequest = new ImageRequest(s3Client, secretProvider); + const result = imageRequest.inferImageType(imageBuffer); + + // Assert + expect(result).toEqual("image/jpeg"); + }); + it('Should pass if it returns "image/jpeg"', () => { // Arrange const imageBuffer = Buffer.from([0xff, 0xd8, 0xff, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); From 69d874a6f53743ae09648d84dd78c8d0ab83b2a4 Mon Sep 17 00:00:00 2001 From: Barnaby Mercer Date: Tue, 4 Jul 2023 12:49:26 +0100 Subject: [PATCH 3/3] Update infer-image-type.spec.ts Generalise tests for other magic numbers --- .../image-request/infer-image-type.spec.ts | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/source/image-handler/test/image-request/infer-image-type.spec.ts b/source/image-handler/test/image-request/infer-image-type.spec.ts index 04873551c..abb2057e7 100644 --- a/source/image-handler/test/image-request/infer-image-type.spec.ts +++ b/source/image-handler/test/image-request/infer-image-type.spec.ts @@ -13,34 +13,15 @@ describe("inferImageType", () => { const secretProvider = new SecretProvider(secretsManager); test.each([ - { value: [0xff, 0xd8, 0xff, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], label: "FFD8FFEE" }, - { value: [0xff, 0xd8, 0xff, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], label: "FFD8FFE2" }, - ])('Should pass if it returns "image/jpeg" for a magic number of $label', ({ value, label }) => { - const imageBuffer = Buffer.from(value); - - // Act - const imageRequest = new ImageRequest(s3Client, secretProvider); - const result = imageRequest.inferImageType(imageBuffer); - - // Assert - expect(result).toEqual("image/jpeg"); - }); - - it('Should pass if it returns "image/jpeg"', () => { - // Arrange - const imageBuffer = Buffer.from([0xff, 0xd8, 0xff, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); - - // Act - const imageRequest = new ImageRequest(s3Client, secretProvider); - const result = imageRequest.inferImageType(imageBuffer); - - // Assert - expect(result).toEqual("image/jpeg"); - }); - - it('Should pass if it returns "image/jpeg for a magic number of FFD8FFED"', () => { - // Arrange - const imageBuffer = Buffer.from([0xff, 0xd8, 0xff, 0xed, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + { value: "FFD8FFDB" }, + { value: "FFD8FFE0" }, + { value: "FFD8FFED" }, + { value: "FFD8FFEE" }, + { value: "FFD8FFE1" }, + { value: "FFD8FFE2" }, + ])('Should pass if it returns "image/jpeg" for a magic number of $value', ({ value }) => { + const byteValues = value.match(/.{1,2}/g).map((x) => parseInt(x, 16)); + const imageBuffer = Buffer.from(byteValues.concat(new Array(8).fill(0x00))); // Act const imageRequest = new ImageRequest(s3Client, secretProvider);