Correctly check if width is lte 0 in Image Optimization API (#38226)
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)
This commit is contained in:
parent
58070c654a
commit
e5be344932
2 changed files with 11 additions and 2 deletions
|
@ -125,7 +125,7 @@ export class ImageOptimizerCache {
|
||||||
|
|
||||||
const width = parseInt(w, 10)
|
const width = parseInt(w, 10)
|
||||||
|
|
||||||
if (!width || isNaN(width)) {
|
if (width <= 0 || isNaN(width)) {
|
||||||
return {
|
return {
|
||||||
errorMessage: '"w" parameter (width) must be a number greater than 0',
|
errorMessage: '"w" parameter (width) must be a number greater than 0',
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 query = { url: '/test.png', w: 0, q: 100 }
|
||||||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
|
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
|
||||||
expect(res.status).toBe(400)
|
expect(res.status).toBe(400)
|
||||||
|
@ -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 () => {
|
it('should fail when w is not a number', async () => {
|
||||||
const query = { url: '/test.png', w: 'foo', q: 100 }
|
const query = { url: '/test.png', w: 'foo', q: 100 }
|
||||||
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
|
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, {})
|
||||||
|
|
Loading…
Reference in a new issue