Skip to content

Commit c0937cf

Browse files
authored
fix: validate next/image params (#1661)
* fix: validate next/image params * fix: pass localPrefix to ipx * chore(deps): update netlify/ipx * chore: update lockfile * chore: fix local image prefix * chore: update error message
1 parent 5131f51 commit c0937cf

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

package-lock.json

Lines changed: 36 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dependencies": {
1313
"@netlify/esbuild": "0.14.39",
1414
"@netlify/functions": "^1.3.0",
15-
"@netlify/ipx": "^1.2.5",
15+
"@netlify/ipx": "^1.3.0",
1616
"@vercel/node-bridge": "^2.1.0",
1717
"chalk": "^4.1.2",
1818
"destr": "^1.1.1",

packages/runtime/src/templates/edge/ipx.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ interface ImageConfig extends Record<string, unknown> {
99
formats?: string[]
1010
}
1111

12+
// Checks if a URL param is numeric
13+
const isNumeric = (value: string | null) => Number(value).toString() === value
14+
1215
/**
1316
* Implement content negotiation for images
1417
*/
@@ -28,10 +31,28 @@ const handler = async (req: Request, context: Context) => {
2831

2932
const source = searchParams.get('url')
3033
const width = searchParams.get('w')
31-
const quality = searchParams.get('q') ?? 75
34+
const quality = searchParams.get('q') ?? '75'
35+
36+
const errors: Array<string> = []
37+
38+
if (!source) {
39+
errors.push('Missing "url" parameter')
40+
} else if (!source.startsWith('http') && !source.startsWith('/')) {
41+
errors.push('The "url" parameter must be a valid URL or path')
42+
}
43+
44+
if (!width) {
45+
errors.push('Missing "w" parameter')
46+
} else if (!isNumeric(width)) {
47+
errors.push('Invalid "w" parameter')
48+
}
49+
50+
if (!isNumeric(quality)) {
51+
errors.push('Invalid "q" parameter')
52+
}
3253

33-
if (!source || !width) {
34-
return new Response('Invalid request', {
54+
if (!source || errors.length > 0) {
55+
return new Response(`Invalid request: \n${errors.join('\n')}`, {
3556
status: 400,
3657
})
3758
}

packages/runtime/src/templates/ipx.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const handler: Handler = createIPXHandler({
1010
domains,
1111
remotePatterns,
1212
responseHeaders,
13+
localPrefix: '/_next/static/media/',
1314
}) as Handler
1415
/* eslint-enable n/no-missing-import, import/no-unresolved, @typescript-eslint/ban-ts-comment */

0 commit comments

Comments
 (0)