Commit d11cbc9f authored by Jiachi Liu's avatar Jiachi Liu Committed by Zack Tanner

Reject next image urls in image optimizer (#68628)

Follow up on #67465, add more cases that should be rejected by image
optimizer when the url is tend to contain the generated next image with
`/_next/image` in url.

These should also get should get rejected:

* with encoded: `%2F_next%2Fimage%3`
* with asset prefix: `/assets/_next/image`
* with host: `https://<domain>/_next/image`

Closes NDX-101
x-ref:
[slack](https://vercel.slack.com/archives/C07AYREDX45/p1720111367743119)
parent 575385ef
...@@ -11,3 +11,11 @@ export function getPathname(url: string) { ...@@ -11,3 +11,11 @@ export function getPathname(url: string) {
export function isFullStringUrl(url: string) { export function isFullStringUrl(url: string) {
return /https?:\/\//.test(url) return /https?:\/\//.test(url)
} }
export function parseUrl(url: string): URL | undefined {
let parsed = undefined
try {
parsed = new URL(url, DUMMY_ORIGIN)
} catch {}
return parsed
}
...@@ -28,6 +28,7 @@ import type { ...@@ -28,6 +28,7 @@ import type {
import { sendEtagResponse } from './send-payload' import { sendEtagResponse } from './send-payload'
import { getContentType, getExtension } from './serve-static' import { getContentType, getExtension } from './serve-static'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { parseUrl } from '../lib/url'
type XCacheHeader = 'MISS' | 'HIT' | 'STALE' type XCacheHeader = 'MISS' | 'HIT' | 'STALE'
...@@ -197,9 +198,13 @@ export class ImageOptimizerCache { ...@@ -197,9 +198,13 @@ export class ImageOptimizerCache {
} }
} }
if (url.startsWith('/_next/image')) { const parsedUrl = parseUrl(url)
return { if (parsedUrl) {
errorMessage: '"url" parameter cannot be recursive', const decodedPathname = decodeURIComponent(parsedUrl.pathname)
if (/\/_next\/image($|\/)/.test(decodedPathname)) {
return {
errorMessage: '"url" parameter cannot be recursive',
}
} }
} }
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
fetchViaHTTP, fetchViaHTTP,
File, File,
findPort, findPort,
getFetchUrl,
killApp, killApp,
launchApp, launchApp,
nextBuild, nextBuild,
...@@ -879,11 +880,46 @@ export function runTests(ctx) { ...@@ -879,11 +880,46 @@ export function runTests(ctx) {
) )
}) })
it('should fail when url is recursive', async () => { describe('recursive url is not allowed', () => {
const query = { url: `/_next/image?url=test.pngw=1&q=1`, w: ctx.w, q: 1 } it('should fail with relative next image url', async () => {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {}) const query = { url: `/_next/image?url=test.pngw=1&q=1`, w: ctx.w, q: 1 }
expect(res.status).toBe(400) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(await res.text()).toBe(`"url" parameter cannot be recursive`) expect(res.status).toBe(400)
expect(await res.text()).toBe(`"url" parameter cannot be recursive`)
})
it('should fail with encoded relative image url', async () => {
const query = {
url: '%2F_next%2Fimage%3Furl%3Dtest.pngw%3D1%26q%3D1',
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 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`)
})
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 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`)
})
}) })
it('should fail when internal url is not an image', async () => { it('should fail when internal url is not an image', async () => {
......
...@@ -147,6 +147,15 @@ export function withQuery( ...@@ -147,6 +147,15 @@ export function withQuery(
return `${pathname}?${querystring}` return `${pathname}?${querystring}`
} }
export function getFetchUrl(
appPort: string | number,
pathname: string,
query?: Record<string, any> | string | null | undefined
) {
const url = query ? withQuery(pathname, query) : pathname
return getFullUrl(appPort, url)
}
export function fetchViaHTTP( export function fetchViaHTTP(
appPort: string | number, appPort: string | number,
pathname: string, pathname: string,
......
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