Unverified Commit 5dd68a58 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[backport v14]: fix(next/image): improve and simplify detect-content-type (#82118) (#82179)

Backports:
- #82118

---------
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent bcc7c65c
...@@ -37,12 +37,19 @@ const AVIF = 'image/avif' ...@@ -37,12 +37,19 @@ const AVIF = 'image/avif'
const WEBP = 'image/webp' const WEBP = 'image/webp'
const PNG = 'image/png' const PNG = 'image/png'
const JPEG = 'image/jpeg' const JPEG = 'image/jpeg'
const JXL = 'image/jxl'
const JP2 = 'image/jp2'
const HEIC = 'image/heic'
const GIF = 'image/gif' const GIF = 'image/gif'
const SVG = 'image/svg+xml' const SVG = 'image/svg+xml'
const ICO = 'image/x-icon' const ICO = 'image/x-icon'
const ICNS = 'image/x-icns'
const TIFF = 'image/tiff'
const BMP = 'image/bmp'
const PDF = 'application/pdf'
const CACHE_VERSION = 3 const CACHE_VERSION = 3
const ANIMATABLE_TYPES = [WEBP, PNG, GIF] const ANIMATABLE_TYPES = [WEBP, PNG, GIF]
const VECTOR_TYPES = [SVG] const BYPASS_TYPES = [SVG, ICO, ICNS, BMP, JXL, HEIC]
const BLUR_IMG_SIZE = 8 // should match `next-image-loader` const BLUR_IMG_SIZE = 8 // should match `next-image-loader`
const BLUR_QUALITY = 70 // should match `next-image-loader` const BLUR_QUALITY = 70 // should match `next-image-loader`
...@@ -118,7 +125,9 @@ async function writeToCacheDir( ...@@ -118,7 +125,9 @@ async function writeToCacheDir(
* it matches the "magic number" of known file signatures. * it matches the "magic number" of known file signatures.
* https://en.wikipedia.org/wiki/List_of_file_signatures * https://en.wikipedia.org/wiki/List_of_file_signatures
*/ */
export function detectContentType(buffer: Buffer) { export async function detectContentType(
buffer: Buffer
): Promise<string | null> {
if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) { if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) {
return JPEG return JPEG
} }
...@@ -152,6 +161,72 @@ export function detectContentType(buffer: Buffer) { ...@@ -152,6 +161,72 @@ export function detectContentType(buffer: Buffer) {
if ([0x00, 0x00, 0x01, 0x00].every((b, i) => buffer[i] === b)) { if ([0x00, 0x00, 0x01, 0x00].every((b, i) => buffer[i] === b)) {
return ICO return ICO
} }
if ([0x69, 0x63, 0x6e, 0x73].every((b, i) => buffer[i] === b)) {
return ICNS
}
if ([0x49, 0x49, 0x2a, 0x00].every((b, i) => buffer[i] === b)) {
return TIFF
}
if ([0x42, 0x4d].every((b, i) => buffer[i] === b)) {
return BMP
}
if ([0xff, 0x0a].every((b, i) => buffer[i] === b)) {
return JXL
}
if (
[
0x00, 0x00, 0x00, 0x0c, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0x0a, 0x87, 0x0a,
].every((b, i) => buffer[i] === b)
) {
return JXL
}
if (
[0, 0, 0, 0, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63].every(
(b, i) => !b || buffer[i] === b
)
) {
return HEIC
}
if ([0x25, 0x50, 0x44, 0x46, 0x2d].every((b, i) => buffer[i] === b)) {
return PDF
}
if (
[
0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a,
].every((b, i) => buffer[i] === b)
) {
return JP2
}
// Fallback to sharp if available
if (sharp) {
const meta = await sharp(buffer)
.metadata()
.catch((_) => null)
switch (meta?.format) {
case 'avif':
return AVIF
case 'webp':
return WEBP
case 'png':
return PNG
case 'jpeg':
case 'jpg':
return JPEG
case 'gif':
return GIF
case 'svg':
return SVG
case 'tiff':
case 'tif':
return TIFF
case 'heif':
return HEIC
default:
return null
}
}
return null return null
} }
...@@ -639,45 +714,41 @@ export async function imageOptimizer( ...@@ -639,45 +714,41 @@ export async function imageOptimizer(
const { href, quality, width, mimeType } = paramsResult const { href, quality, width, mimeType } = paramsResult
const upstreamBuffer = imageUpstream.buffer const upstreamBuffer = imageUpstream.buffer
const maxAge = getMaxAge(imageUpstream.cacheControl) const maxAge = getMaxAge(imageUpstream.cacheControl)
const upstreamType = const upstreamType = await detectContentType(upstreamBuffer)
detectContentType(upstreamBuffer) ||
imageUpstream.contentType?.toLowerCase().trim()
if (upstreamType) {
if (
upstreamType.startsWith('image/svg') &&
!nextConfig.images.dangerouslyAllowSVG
) {
Log.error(
`The requested resource "${href}" has type "${upstreamType}" but dangerouslyAllowSVG is disabled`
)
throw new ImageError(
400,
'"url" parameter is valid but image type is not allowed'
)
}
if (ANIMATABLE_TYPES.includes(upstreamType) && isAnimated(upstreamBuffer)) { if (
Log.warnOnce( !upstreamType ||
`The requested resource "${href}" is an animated image so it will not be optimized. Consider adding the "unoptimized" property to the <Image>.` !upstreamType.startsWith('image/') ||
) upstreamType.includes(',')
return { buffer: upstreamBuffer, contentType: upstreamType, maxAge } ) {
} Log.error(
if (VECTOR_TYPES.includes(upstreamType)) { "The requested resource isn't a valid image for",
// We don't warn here because we already know that "dangerouslyAllowSVG" href,
// was enabled above, therefore the user explicitly opted in. 'received',
// If we add more VECTOR_TYPES besides SVG, perhaps we could warn for those. upstreamType
return { buffer: upstreamBuffer, contentType: upstreamType, maxAge } )
} throw new ImageError(400, "The requested resource isn't a valid image.")
if (!upstreamType.startsWith('image/') || upstreamType.includes(',')) { }
Log.error( if (
"The requested resource isn't a valid image for", upstreamType.startsWith('image/svg') &&
href, !nextConfig.images.dangerouslyAllowSVG
'received', ) {
upstreamType Log.error(
) `The requested resource "${href}" has type "${upstreamType}" but dangerouslyAllowSVG is disabled`
throw new ImageError(400, "The requested resource isn't a valid image.") )
} throw new ImageError(
400,
'"url" parameter is valid but image type is not allowed'
)
}
if (ANIMATABLE_TYPES.includes(upstreamType) && isAnimated(upstreamBuffer)) {
Log.warnOnce(
`The requested resource "${href}" is an animated image so it will not be optimized. Consider adding the "unoptimized" property to the <Image>.`
)
return { buffer: upstreamBuffer, contentType: upstreamType, maxAge }
}
if (BYPASS_TYPES.includes(upstreamType)) {
return { buffer: upstreamBuffer, contentType: upstreamType, maxAge }
} }
let contentType: string let contentType: string
...@@ -685,7 +756,6 @@ export async function imageOptimizer( ...@@ -685,7 +756,6 @@ export async function imageOptimizer(
if (mimeType) { if (mimeType) {
contentType = mimeType contentType = mimeType
} else if ( } else if (
upstreamType?.startsWith('image/') &&
getExtension(upstreamType) && getExtension(upstreamType) &&
upstreamType !== WEBP && upstreamType !== WEBP &&
upstreamType !== AVIF upstreamType !== AVIF
......
...@@ -5,6 +5,9 @@ import send from 'next/dist/compiled/send' ...@@ -5,6 +5,9 @@ import send from 'next/dist/compiled/send'
// Although "mime" has already add avif in version 2.4.7, "send" is still using mime@1.6.0 // Although "mime" has already add avif in version 2.4.7, "send" is still using mime@1.6.0
send.mime.define({ send.mime.define({
'image/avif': ['avif'], 'image/avif': ['avif'],
'image/x-icns': ['icns'],
'image/jxl': ['jxl'],
'image/heic': ['heic'],
}) })
export function serveStatic( export function serveStatic(
......
...@@ -196,6 +196,72 @@ export function runTests(ctx) { ...@@ -196,6 +196,72 @@ export function runTests(ctx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
}) })
it('should maintain icns', async () => {
const query = { w: ctx.w, q: 90, url: '/test.icns' }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toContain('image/x-icns')
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(res.headers.get('Content-Disposition')).toBe(
`${contentDispositionType}; filename="test.icns"`
)
await expectWidth(res, 256)
})
it('should maintain jxl', async () => {
const query = { w: ctx.w, q: 90, url: '/test.jxl' }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toContain('image/jxl')
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(res.headers.get('Content-Disposition')).toBe(
`${contentDispositionType}; filename="test.jxl"`
)
// JXL is a bypass type, served as-is without processing
// image-size@1.0.0 doesn't support JXL, so skip width check
})
it('should maintain heic', async () => {
const query = { w: ctx.w, q: 90, url: '/test.heic' }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toContain('image/heic')
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(res.headers.get('Content-Disposition')).toBe(
`${contentDispositionType}; filename="test.heic"`
)
// HEIC is a bypass type, served as-is without processing
// image-size@1.0.0 doesn't support HEIC, so skip width check
})
it('should maintain jp2', async () => {
const query = { w: ctx.w, q: 90, url: '/test.jp2' }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toContain('image/jp2')
expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate`
)
expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy()
expect(res.headers.get('Content-Disposition')).toBe(
`${contentDispositionType}; filename="test.jp2"`
)
await expectWidth(res, 1)
})
it('should maintain animated gif', async () => { it('should maintain animated gif', async () => {
const query = { w: ctx.w, q: 90, url: '/animated.gif' } const query = { w: ctx.w, q: 90, url: '/animated.gif' }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
...@@ -295,7 +361,6 @@ export function runTests(ctx) { ...@@ -295,7 +361,6 @@ export function runTests(ctx) {
'utf8' 'utf8'
) )
expect(actual).toMatch(expected) expect(actual).toMatch(expected)
expect(ctx.nextOutput).not.toContain('The requested resource')
}) })
} else { } else {
it('should not allow vector svg', async () => { it('should not allow vector svg', async () => {
...@@ -312,7 +377,7 @@ export function runTests(ctx) { ...@@ -312,7 +377,7 @@ export function runTests(ctx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(400) expect(res.status).toBe(400)
expect(await res.text()).toContain( expect(await res.text()).toContain(
"The requested resource isn't a valid image" '"url" parameter is valid but image type is not allowed'
) )
}) })
...@@ -322,7 +387,7 @@ export function runTests(ctx) { ...@@ -322,7 +387,7 @@ export function runTests(ctx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(400) expect(res.status).toBe(400)
expect(await res.text()).toContain( expect(await res.text()).toContain(
"The requested resource isn't a valid image" '"url" parameter is valid but image type is not allowed'
) )
}) })
...@@ -337,6 +402,16 @@ export function runTests(ctx) { ...@@ -337,6 +402,16 @@ export function runTests(ctx) {
}) })
} }
it('should not allow pdf format', async () => {
const query = { w: ctx.w, q: 90, url: '/test.pdf' }
const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(400)
expect(await res.text()).toContain(
"The requested resource isn't a valid image"
)
})
it('should maintain ico format', async () => { it('should maintain ico format', async () => {
const query = { w: ctx.w, q: 90, url: `/test.ico` } const query = { w: ctx.w, q: 90, url: `/test.ico` }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
...@@ -344,7 +419,7 @@ export function runTests(ctx) { ...@@ -344,7 +419,7 @@ export function runTests(ctx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toContain('image/x-icon') expect(res.headers.get('Content-Type')).toContain('image/x-icon')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate` `public, max-age=0, must-revalidate`
) )
expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/) expect(res.headers.get('Vary')).toMatch(/^Accept(,|$)/)
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
...@@ -940,8 +1015,8 @@ export function runTests(ctx) { ...@@ -940,8 +1015,8 @@ export function runTests(ctx) {
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(400) expect(res.status).toBe(400)
expect(await res.text()).toBe( expect(await res.text()).toContain(
`Unable to optimize image and unable to fallback to upstream image` "The requested resource isn't a valid image"
) )
}) })
...@@ -1186,7 +1261,7 @@ export function runTests(ctx) { ...@@ -1186,7 +1261,7 @@ export function runTests(ctx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('image/bmp') expect(res.headers.get('Content-Type')).toBe('image/bmp')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate` `public, max-age=0, must-revalidate`
) )
// bmp is compressible so will have accept-encoding set from // bmp is compressible so will have accept-encoding set from
// compression // compression
......
...@@ -330,7 +330,9 @@ export default (next: NextInstance) => { ...@@ -330,7 +330,9 @@ export default (next: NextInstance) => {
next.appPort, next.appPort,
'/_next/image?url=%2Fxss.svg&w=256&q=75' '/_next/image?url=%2Fxss.svg&w=256&q=75'
) )
expect(await browser.elementById('msg').text()).toBe('safe') expect(await browser.elementByCss('body').text()).toBe(
"The requested resource isn't a valid image."
)
} finally { } finally {
if (browser) await browser.close() if (browser) await browser.close()
} }
......
...@@ -8,26 +8,76 @@ const getImage = (filepath) => readFile(join(__dirname, filepath)) ...@@ -8,26 +8,76 @@ const getImage = (filepath) => readFile(join(__dirname, filepath))
describe('detectContentType', () => { describe('detectContentType', () => {
it('should return jpg', async () => { it('should return jpg', async () => {
const buffer = await getImage('./images/test.jpg') const buffer = await getImage('./images/test.jpg')
expect(detectContentType(buffer)).toBe('image/jpeg') expect(await detectContentType(buffer)).toBe('image/jpeg')
}) })
it('should return png', async () => { it('should return png', async () => {
const buffer = await getImage('./images/test.png') const buffer = await getImage('./images/test.png')
expect(detectContentType(buffer)).toBe('image/png') expect(await detectContentType(buffer)).toBe('image/png')
}) })
it('should return webp', async () => { it('should return webp', async () => {
const buffer = await getImage('./images/animated.webp') const buffer = await getImage('./images/animated.webp')
expect(detectContentType(buffer)).toBe('image/webp') expect(await detectContentType(buffer)).toBe('image/webp')
}) })
it('should return svg', async () => { it('should return svg', async () => {
const buffer = await getImage('./images/test.svg') const buffer = await getImage('./images/test.svg')
expect(detectContentType(buffer)).toBe('image/svg+xml') expect(await detectContentType(buffer)).toBe('image/svg+xml')
})
it('should return svg for inline svg', async () => {
const buffer = await getImage('./images/test-inline.svg')
expect(await detectContentType(buffer)).toBe('image/svg+xml')
})
it('should return svg when starts with space', async () => {
const buffer = Buffer.from(
' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>'
)
expect(await detectContentType(buffer)).toBe('image/svg+xml')
})
it('should return svg when starts with newline', async () => {
const buffer = Buffer.from(
'\n<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>'
)
expect(await detectContentType(buffer)).toBe('image/svg+xml')
})
it('should return svg when starts with tab', async () => {
const buffer = Buffer.from(
'\t<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>'
)
expect(await detectContentType(buffer)).toBe('image/svg+xml')
}) })
it('should return avif', async () => { it('should return avif', async () => {
const buffer = await getImage('./images/test.avif') const buffer = await getImage('./images/test.avif')
expect(detectContentType(buffer)).toBe('image/avif') expect(await detectContentType(buffer)).toBe('image/avif')
}) })
it('should return icon', async () => { it('should return icon', async () => {
const buffer = await getImage('./images/test.ico') const buffer = await getImage('./images/test.ico')
expect(detectContentType(buffer)).toBe('image/x-icon') expect(await detectContentType(buffer)).toBe('image/x-icon')
})
it('should return icns', async () => {
const buffer = await getImage('./images/test.icns')
expect(await detectContentType(buffer)).toBe('image/x-icns')
})
it('should return jxl', async () => {
const buffer = await getImage('./images/test.jxl')
expect(await detectContentType(buffer)).toBe('image/jxl')
})
it('should return jp2', async () => {
const buffer = await getImage('./images/test.jp2')
expect(await detectContentType(buffer)).toBe('image/jp2')
})
it('should return heic', async () => {
const buffer = await getImage('./images/test.heic')
expect(await detectContentType(buffer)).toBe('image/heic')
})
it('should return pdf', async () => {
const buffer = await getImage('./images/test.pdf')
expect(await detectContentType(buffer)).toBe('application/pdf')
})
it('should return tiff', async () => {
const buffer = await getImage('./images/test.tiff')
expect(await detectContentType(buffer)).toBe('image/tiff')
})
it('should return bmp', async () => {
const buffer = await getImage('./images/test.bmp')
expect(await detectContentType(buffer)).toBe('image/bmp')
}) })
}) })
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="400" height="400">
<path d="M0 200v200h400V0H0v200zm240.1-11.8 39.6 69.3-39.8.3c-22 .1-57.8.1-79.8 0l-39.8-.3 39.6-69.3c21.7-38 39.8-69.2 40.1-69.2.3 0 18.4 31.2 40.1 69.2z"/>
</svg>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment