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

fix(next): reject protocol-relative URLs in image optimization #65752

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
6 changes: 6 additions & 0 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ export class ImageOptimizerCache {
return { errorMessage: '"url" parameter is too long' }
}

if (url.startsWith('//')) {
return {
errorMessage: '"url" parameter cannot be a protocol-relative URL (//)',
}
}

let isAbsolute: boolean

if (url.startsWith('/')) {
Expand Down
5 changes: 5 additions & 0 deletions test/integration/image-optimizer/app/pages/api/no-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function handler(_req, res) {
// Intentionally missing Content-Type header so that
// the fallback is not served when optimization fails
res.end('foo')
}
11 changes: 10 additions & 1 deletion test/integration/image-optimizer/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,17 @@ export function runTests(ctx: RunTestsCtx) {
expect(await res.text()).toBe(`"url" parameter is too long`)
})

it('should fail when url is protocol relative', async () => {
const query = { url: `//example.com`, 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 a protocol-relative URL (//)`
)
})

it('should fail when internal url is not an image', async () => {
const url = `//<h1>not-an-image</h1>`
const url = `/api/no-header`
const query = { url, w: ctx.w, q: 39 }
const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
Expand Down
Loading