Unverified Commit 0816b775 authored by Steven's avatar Steven Committed by GitHub

Fix image optimization invalid internal upstream image (#34899)

This gracefully handles errors when the `url` query string param looks like an internal image because it starts with `/` but it is not pointing to an internal image.

Previously, this was printing an unnecessary stack trace when the upstream content-type was undefined.
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 6b37d6c3
...@@ -546,10 +546,18 @@ export async function imageOptimizer( ...@@ -546,10 +546,18 @@ export async function imageOptimizer(
throw new ImageError(500, 'Unable to optimize buffer') throw new ImageError(500, 'Unable to optimize buffer')
} }
} catch (error) { } catch (error) {
return { if (upstreamBuffer && upstreamType) {
buffer: upstreamBuffer, // If we fail to optimize, fallback to the original image
contentType: upstreamType!, return {
maxAge, buffer: upstreamBuffer,
contentType: upstreamType,
maxAge: nextConfig.images.minimumCacheTTL,
}
} else {
throw new ImageError(
500,
'Unable to optimize image and unable to fallback to upstream image'
)
} }
} }
} }
......
...@@ -691,6 +691,17 @@ export function runTests(ctx) { ...@@ -691,6 +691,17 @@ export function runTests(ctx) {
expect(await res.text()).toBe(`"url" parameter is invalid`) expect(await res.text()).toBe(`"url" parameter is invalid`)
}) })
it('should fail when internal url is not an image', async () => {
const url = `//<h1>not-an-image</h1>`
const query = { url, w: ctx.w, q: 39 }
const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(500)
expect(await res.text()).toBe(
`Unable to optimize image and unable to fallback to upstream image`
)
})
if (ctx.domains.includes('localhost')) { if (ctx.domains.includes('localhost')) {
it('should fail when url fails to load an image', async () => { it('should fail when url fails to load an image', async () => {
const url = `http://localhost:${ctx.appPort}/not-an-image` const url = `http://localhost:${ctx.appPort}/not-an-image`
......
...@@ -18,7 +18,7 @@ describe('should set-up next', () => { ...@@ -18,7 +18,7 @@ describe('should set-up next', () => {
'next.config.js': new FileRef(join(appDir, 'next.config.js')), 'next.config.js': new FileRef(join(appDir, 'next.config.js')),
}, },
dependencies: { dependencies: {
typescript: 'latest', typescript: '4.4.3',
'@types/node': 'latest', '@types/node': 'latest',
'@types/react': 'latest', '@types/react': 'latest',
'@types/react-dom': 'latest', '@types/react-dom': 'latest',
......
...@@ -4855,7 +4855,7 @@ ...@@ -4855,7 +4855,7 @@
version "4.1.5" version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd" resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
"@types/eslint-scope@^3.7.0", "@types/eslint-scope@^3.7.3": "@types/eslint-scope@^3.7.3":
version "3.7.3" version "3.7.3"
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.3.tgz#125b88504b61e3c8bc6f870882003253005c3224" resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.3.tgz#125b88504b61e3c8bc6f870882003253005c3224"
integrity sha512-PB3ldyrcnAicT35TWPs5IcwKD8S333HMaa2VVv4+wdvebJkjWuW/xESoB8IwRcog8HYVYamb1g/R31Qv5Bx03g== integrity sha512-PB3ldyrcnAicT35TWPs5IcwKD8S333HMaa2VVv4+wdvebJkjWuW/xESoB8IwRcog8HYVYamb1g/R31Qv5Bx03g==
...@@ -4880,11 +4880,6 @@ ...@@ -4880,11 +4880,6 @@
version "0.0.39" version "0.0.39"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
"@types/estree@^0.0.50":
version "0.0.50"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83"
integrity sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw==
"@types/estree@^0.0.51": "@types/estree@^0.0.51":
version "0.0.51" version "0.0.51"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.51.tgz#cfd70924a25a3fd32b218e5e420e6897e1ac4f40" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.51.tgz#cfd70924a25a3fd32b218e5e420e6897e1ac4f40"
......
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