Skip to content

Commit

Permalink
fix(next/image): improve query string validation for integers
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Jun 29, 2024
1 parent b1eab7f commit 527c88d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
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

0 comments on commit 527c88d

Please sign in to comment.