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/image): improve w and q query string validation for integers #67293

Merged
merged 1 commit into from
Jul 1, 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
15 changes: 12 additions & 3 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,28 @@ export class ImageOptimizerCache {
return { errorMessage: '"w" parameter (width) is required' }
} else if (Array.isArray(w)) {
return { errorMessage: '"w" parameter (width) cannot be an array' }
} else if (!/^[0-9]+$/.test(w)) {
return {
errorMessage: '"w" parameter (width) must be an integer greater than 0',
}
}

if (!q) {
return { errorMessage: '"q" parameter (quality) is required' }
} else if (Array.isArray(q)) {
return { errorMessage: '"q" parameter (quality) cannot be an array' }
} else if (!/^[0-9]+$/.test(q)) {
return {
errorMessage:
'"q" parameter (quality) must be an integer between 1 and 100',
}
}

const width = parseInt(w, 10)

if (width <= 0 || isNaN(width)) {
return {
errorMessage: '"w" parameter (width) must be a number greater than 0',
errorMessage: '"w" parameter (width) must be an integer greater than 0',
}
}

Expand All @@ -273,12 +282,12 @@ export class ImageOptimizerCache {
}
}

const quality = parseInt(q)
const quality = parseInt(q, 10)

if (isNaN(quality) || quality < 1 || quality > 100) {
return {
errorMessage:
'"q" parameter (quality) must be a number between 1 and 100',
'"q" parameter (quality) must be an integer between 1 and 100',
}
}

Expand Down
30 changes: 24 additions & 6 deletions test/integration/image-optimizer/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ export function runTests(ctx: RunTestsCtx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"q" parameter (quality) must be a number between 1 and 100`
`"q" parameter (quality) must be an integer between 1 and 100`
)
})

Expand All @@ -515,7 +515,7 @@ export function runTests(ctx: RunTestsCtx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"q" parameter (quality) must be a number between 1 and 100`
`"q" parameter (quality) must be an integer between 1 and 100`
)
})

Expand All @@ -524,7 +524,7 @@ export function runTests(ctx: RunTestsCtx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"w" parameter (width) must be a number greater than 0`
`"w" parameter (width) must be an integer greater than 0`
)
})

Expand All @@ -533,7 +533,7 @@ export function runTests(ctx: RunTestsCtx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"w" parameter (width) must be a number greater than 0`
`"w" parameter (width) must be an integer greater than 0`
)
})

Expand All @@ -542,7 +542,16 @@ export function runTests(ctx: RunTestsCtx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"w" parameter (width) must be a number greater than 0`
`"w" parameter (width) must be an integer greater than 0`
)
})

it('should fail when w is not an integer', async () => {
const query = { url: '/test.png', w: 99.9, q: 100 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"w" parameter (width) must be an integer greater than 0`
)
})

Expand All @@ -551,7 +560,16 @@ export function runTests(ctx: RunTestsCtx) {
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"q" parameter (quality) must be a number between 1 and 100`
`"q" parameter (quality) must be an integer between 1 and 100`
)
})

it('should fail when q is not an integer', async () => {
const query = { url: '/test.png', w: ctx.w, q: 99.9 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
expect(await res.text()).toBe(
`"q" parameter (quality) must be an integer between 1 and 100`
)
})

Expand Down
Loading