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

Allow external image urls with _next/image pathname to be rendered via Image component #69586

Merged
merged 3 commits into from
Sep 3, 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
19 changes: 9 additions & 10 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,20 @@ export class ImageOptimizerCache {
}
}

const parsedUrl = parseUrl(url)
if (parsedUrl) {
const decodedPathname = decodeURIComponent(parsedUrl.pathname)
if (/\/_next\/image($|\/)/.test(decodedPathname)) {
return {
errorMessage: '"url" parameter cannot be recursive',
}
}
}

let isAbsolute: boolean

if (url.startsWith('/')) {
href = url
isAbsolute = false
if (
/\/_next\/image($|\/)/.test(
decodeURIComponent(parseUrl(url)?.pathname ?? '')
)
) {
return {
errorMessage: '"url" parameter cannot be recursive',
}
}
} else {
let hrefParsed: URL

Expand Down
35 changes: 20 additions & 15 deletions test/integration/image-optimizer/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
fetchViaHTTP,
File,
findPort,
getFetchUrl,
killApp,
launchApp,
nextBuild,
Expand Down Expand Up @@ -1041,22 +1040,28 @@ export function runTests(ctx: RunTestsCtx) {
expect(await res.text()).toBe(`"url" parameter is invalid`)
})

it('should fail with absolute next image url', async () => {
const fullUrl = getFetchUrl(
ctx.appPort,
'/_next/image?url=test.pngw=1&q=1'
)
const query = { url: fullUrl, w: ctx.w, q: 1 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(`"url" parameter cannot be recursive`)
})
if (domains.length > 0) {
it('should pass with absolute next image url', async () => {
const fullUrl =
'https://image-optimization-test.vercel.app/_next/image?url=%2Ffrog.jpg&w=1024&q=75'
const query = { url: fullUrl, w: ctx.w, q: 1 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
await expectWidth(res, ctx.w)
})
} else {
it('should fail with absolute next image url', async () => {
const fullUrl =
'https://image-optimization-test.vercel.app/_next/image?url=%2Ffrog.jpg&w=1024&q=75'
const query = { url: fullUrl, w: ctx.w, q: 1 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(`"url" parameter is not allowed`)
})
}

it('should fail with relative image url with assetPrefix', async () => {
const fullUrl = getFetchUrl(
ctx.appPort,
`/assets/_next/image?url=test.pngw=1&q=1`
)
const fullUrl = '/assets/_next/image?url=%2Ftest.png&w=128&q=75'
const query = { url: fullUrl, w: ctx.w, q: 1 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
Expand Down
Loading