Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icon and webp to secondGuessSourceContentType check #205

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
21 changes: 21 additions & 0 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,27 @@ describe('express-processimage', () => {
beforeEach(() => {
config.secondGuessSourceContentType = true;
});
it('should recover gracefully when attempting to process a wrongly named webp', () => {
config.debug = true;
return expect('GET /reallyawebp.png?resize=40,30', 'to yield response', {
headers: {
'X-Express-Processimage': 'sharp',
},
body: expect.it('to have metadata satisfying', {
format: 'WEBP',
size: { width: 40, height: 30 },
}),
});
});
it('should return the correct contentType of a wrongly named ico', () => {
config.debug = true;
return expect('GET /reallyaico.gif?metadata', 'to yield response', {
body: {
format: expect.it('to equal', 'ico'),
contentType: expect.it('to equal', 'image/x-icon'),
},
});
});
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
Binary file added testdata/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 testdata/reallyawebp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading