Skip to content

Commit

Permalink
Add icon and webp to secondGuessSourceContentType check
Browse files Browse the repository at this point in the history
  • Loading branch information
outlawtorn authored and alexander committed May 13, 2024
1 parent 4d63702 commit 48b24d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ module.exports = (options) => {
detectedContentType = 'image/png';
} else if (firstChunk[0] === 0x42 && firstChunk[1] === 0x4d) {
detectedContentType = 'image/bmp';
} else if (
firstChunk[0] === 0x0 &&
firstChunk[1] === 0x0 &&
firstChunk[2] === 0x1 &&
firstChunk[3] === 0x0
) {
detectedContentType = 'image/x-icon';
} else if (
firstChunk[8] === 0x57 &&
firstChunk[9] === 0x45 &&
firstChunk[10] === 0x42 &&
firstChunk[11] === 0x50
) {
detectedContentType = 'image/webp';
}
if (
detectedContentType &&
Expand Down
Binary file added reallyaico.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added reallyawebp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,36 @@ describe('express-processimage', () => {
beforeEach(() => {
config.secondGuessSourceContentType = true;
});
it('should recover gracefully when attempting to process a wrongly named webp', () => {
return expect('GET /reallyawebp.png?metadata', 'to yield response', {
body: {
format: 'webp',
size: 176972,
width: 1024,
height: 772,
space: 'srgb',
channels: 3,
depth: 'uchar',
isProgressive: false,
hasProfile: false,
hasAlpha: false,
contentType: 'image/webp',
filesize: 176972,
etag: 'W/"2b34c-18e564af60c"'
}
});
});
it('should return the correct contentType of a wrongly named ico', () => {
return expect('GET /reallyaico.gif?metadata', 'to yield response', {
body: {
error: 'Input buffer contains unsupported image format',
format: 'ico',
contentType: 'image/x-icon',
filesize: 67646,
etag: 'W/"1083e-18d82105007"'
}
});
});
it('should recover gracefully when attempting to process a wrongly named jpeg', () => {
config.debug = true;
return expect('GET /reallyajpeg.gif?resize=40,35', 'to yield response', {
Expand Down

0 comments on commit 48b24d3

Please sign in to comment.