Unverified Commit 5a501d37 authored by Steven's avatar Steven Committed by GitHub

chore(next/image): improve imgopt api bypass detection for unsupported images (#74569)

Backport https://github.com/vercel/next.js/pull/73909 to 15.1.x
parent 4cbaaa11
...@@ -37,11 +37,12 @@ const JPEG = 'image/jpeg' ...@@ -37,11 +37,12 @@ const JPEG = 'image/jpeg'
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 TIFF = 'image/tiff'
const BMP = 'image/bmp' const BMP = 'image/bmp'
const CACHE_VERSION = 4 const CACHE_VERSION = 4
const ANIMATABLE_TYPES = [WEBP, PNG, GIF] const ANIMATABLE_TYPES = [WEBP, PNG, GIF]
const VECTOR_TYPES = [SVG] const BYPASS_TYPES = [SVG, ICO, ICNS, BMP]
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`
...@@ -186,6 +187,9 @@ export function detectContentType(buffer: Buffer) { ...@@ -186,6 +187,9 @@ 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)) { if ([0x49, 0x49, 0x2a, 0x00].every((b, i) => buffer[i] === b)) {
return TIFF return TIFF
} }
...@@ -673,7 +677,10 @@ export async function imageOptimizer( ...@@ -673,7 +677,10 @@ export async function imageOptimizer(
}> { }> {
const { href, quality, width, mimeType } = paramsResult const { href, quality, width, mimeType } = paramsResult
const { buffer: upstreamBuffer, etag: upstreamEtag } = imageUpstream const { buffer: upstreamBuffer, etag: upstreamEtag } = imageUpstream
const maxAge = getMaxAge(imageUpstream.cacheControl) const maxAge = Math.max(
nextConfig.images.minimumCacheTTL,
getMaxAge(imageUpstream.cacheControl)
)
const upstreamType = const upstreamType =
detectContentType(upstreamBuffer) || detectContentType(upstreamBuffer) ||
...@@ -708,10 +715,7 @@ export async function imageOptimizer( ...@@ -708,10 +715,7 @@ export async function imageOptimizer(
upstreamEtag, upstreamEtag,
} }
} }
if (VECTOR_TYPES.includes(upstreamType)) { if (BYPASS_TYPES.includes(upstreamType)) {
// We don't warn here because we already know that "dangerouslyAllowSVG"
// was enabled above, therefore the user explicitly opted in.
// If we add more VECTOR_TYPES besides SVG, perhaps we could warn for those.
return { return {
buffer: upstreamBuffer, buffer: upstreamBuffer,
contentType: upstreamType, contentType: upstreamType,
...@@ -790,7 +794,7 @@ export async function imageOptimizer( ...@@ -790,7 +794,7 @@ export async function imageOptimizer(
return { return {
buffer: optimizedBuffer, buffer: optimizedBuffer,
contentType, contentType,
maxAge: Math.max(maxAge, nextConfig.images.minimumCacheTTL), maxAge,
etag: getImageEtag(optimizedBuffer), etag: getImageEtag(optimizedBuffer),
upstreamEtag, upstreamEtag,
} }
......
...@@ -241,7 +241,7 @@ export function runTests(ctx: RunTestsCtx) { ...@@ -241,7 +241,7 @@ export function runTests(ctx: RunTestsCtx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/gif') expect(res.headers.get('content-type')).toContain('image/gif')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate` `public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept') expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
...@@ -258,7 +258,7 @@ export function runTests(ctx: RunTestsCtx) { ...@@ -258,7 +258,7 @@ export function runTests(ctx: RunTestsCtx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/png') expect(res.headers.get('content-type')).toContain('image/png')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate` `public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept') expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
...@@ -275,7 +275,7 @@ export function runTests(ctx: RunTestsCtx) { ...@@ -275,7 +275,7 @@ export function runTests(ctx: RunTestsCtx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/png') expect(res.headers.get('content-type')).toContain('image/png')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate` `public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept') expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
...@@ -292,7 +292,7 @@ export function runTests(ctx: RunTestsCtx) { ...@@ -292,7 +292,7 @@ export function runTests(ctx: RunTestsCtx) {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('image/webp') expect(res.headers.get('content-type')).toContain('image/webp')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate` `public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate`
) )
expect(res.headers.get('Vary')).toBe('Accept') expect(res.headers.get('Vary')).toBe('Accept')
expect(res.headers.get('etag')).toBeTruthy() expect(res.headers.get('etag')).toBeTruthy()
...@@ -312,7 +312,7 @@ export function runTests(ctx: RunTestsCtx) { ...@@ -312,7 +312,7 @@ export function runTests(ctx: RunTestsCtx) {
expect(res.headers.get('Content-Length')).toBe('603') expect(res.headers.get('Content-Length')).toBe('603')
expect(res.headers.get('Content-Type')).toContain('image/svg+xml') expect(res.headers.get('Content-Type')).toContain('image/svg+xml')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate` `public, max-age=${isDev ? 0 : minimumCacheTTL}, must-revalidate`
) )
// SVG is compressible so will have accept-encoding set from // SVG is compressible so will have accept-encoding set from
// compression // compression
......
...@@ -34,6 +34,10 @@ describe('detectContentType', () => { ...@@ -34,6 +34,10 @@ describe('detectContentType', () => {
const buffer = await getImage('./images/test.ico') const buffer = await getImage('./images/test.ico')
expect(detectContentType(buffer)).toBe('image/x-icon') expect(detectContentType(buffer)).toBe('image/x-icon')
}) })
it('should return icns', async () => {
const buffer = await getImage('./images/test.icns')
expect(detectContentType(buffer)).toBe('image/x-icns')
})
it('should return tiff', async () => { it('should return tiff', async () => {
const buffer = await getImage('./images/test.tiff') const buffer = await getImage('./images/test.tiff')
expect(detectContentType(buffer)).toBe('image/tiff') expect(detectContentType(buffer)).toBe('image/tiff')
......
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