Skip to content

Commit

Permalink
Correctly check if width is lte 0 in Image Optimization API (#38226)
Browse files Browse the repository at this point in the history
This PR corrects a mistake where a negative number could pass, as a number greater than 0.

This is due to negative numbers being a truthy value in JS.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
  • Loading branch information
andershagbard committed Jul 6, 2022
1 parent 58070c6 commit e5be344
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/next/server/image-optimizer.ts
Expand Up @@ -125,7 +125,7 @@ export class ImageOptimizerCache {

const width = parseInt(w, 10)

if (!width || isNaN(width)) {
if (width <= 0 || isNaN(width)) {
return {
errorMessage: '"w" parameter (width) must be a number greater than 0',
}
Expand Down
11 changes: 10 additions & 1 deletion test/integration/image-optimizer/test/util.js
Expand Up @@ -397,7 +397,7 @@ export function runTests(ctx) {
)
})

it('should fail when w is 0 or less', async () => {
it('should fail when w is 0', async () => {
const query = { url: '/test.png', w: 0, q: 100 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
expect(res.status).toBe(400)
Expand All @@ -406,6 +406,15 @@ export function runTests(ctx) {
)
})

it('should fail when w is less than 0', async () => {
const query = { url: '/test.png', w: -100, 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 a number greater than 0`
)
})

it('should fail when w is not a number', async () => {
const query = { url: '/test.png', w: 'foo', q: 100 }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
Expand Down

0 comments on commit e5be344

Please sign in to comment.