Skip to content

Commit c9eaf70

Browse files
committed
Add config limit checks for image optimizer
1 parent 9c65c99 commit c9eaf70

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

packages/next/next-server/server/config.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ function assignDefaults(userConfig: { [key: string]: any }) {
235235
`Specified images.domains should be an Array received ${typeof images.domains}`
236236
)
237237
}
238+
239+
if (images.domains.length > 50) {
240+
throw new Error(
241+
`Specified images.domains exceeds length of 50, received length (${images.domains.length}), please reduce the length of the array to continue`
242+
)
243+
}
244+
238245
const invalid = images.domains.filter(
239246
(d: unknown) => typeof d !== 'string'
240247
)
@@ -252,10 +259,20 @@ function assignDefaults(userConfig: { [key: string]: any }) {
252259
`Specified images.sizes should be an Array received ${typeof images.sizes}`
253260
)
254261
}
255-
const invalid = images.sizes.filter((d: unknown) => typeof d !== 'number')
262+
263+
if (images.sizes.length > 50) {
264+
throw new Error(
265+
`Specified images.sizes exceeds length of 50, received length (${images.sizes.length}), please reduce the length of the array to continue`
266+
)
267+
}
268+
269+
const invalid = images.sizes.filter((d: unknown) => {
270+
return typeof d !== 'number' || d < 0 || d > 10000
271+
})
272+
256273
if (invalid.length > 0) {
257274
throw new Error(
258-
`Specified images.sizes should be an Array of numbers received invalid values (${invalid.join(
275+
`Specified images.sizes should be an Array of numbers that are between 0 and 10,000, received invalid values (${invalid.join(
259276
', '
260277
)})`
261278
)

test/integration/image-optimizer/test/index.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
nextBuild,
1111
nextStart,
1212
File,
13+
waitFor,
1314
} from 'next-test-utils'
1415
import sharp from 'sharp'
1516

@@ -321,6 +322,83 @@ function runTests({ w, isDev, domains }) {
321322
}
322323

323324
describe('Image Optimizer', () => {
325+
describe('config checks', () => {
326+
it('should error when domains length exceeds 50', async () => {
327+
await nextConfig.replace(
328+
'{ /* replaceme */ }',
329+
JSON.stringify({
330+
images: {
331+
domains: new Array(51).fill('google.com'),
332+
},
333+
})
334+
)
335+
let stderr = ''
336+
337+
app = await launchApp(appDir, await findPort(), {
338+
onStderr(msg) {
339+
stderr += msg || ''
340+
},
341+
})
342+
await waitFor(1000)
343+
await killApp(app).catch(() => {})
344+
await nextConfig.restore()
345+
346+
expect(stderr).toContain(
347+
'Specified images.domains exceeds length of 50, received length (51), please reduce the length of the array to continue'
348+
)
349+
})
350+
351+
it('should error when sizes length exceeds 50', async () => {
352+
await nextConfig.replace(
353+
'{ /* replaceme */ }',
354+
JSON.stringify({
355+
images: {
356+
sizes: new Array(51).fill(1024),
357+
},
358+
})
359+
)
360+
let stderr = ''
361+
362+
app = await launchApp(appDir, await findPort(), {
363+
onStderr(msg) {
364+
stderr += msg || ''
365+
},
366+
})
367+
await waitFor(1000)
368+
await killApp(app).catch(() => {})
369+
await nextConfig.restore()
370+
371+
expect(stderr).toContain(
372+
'Specified images.sizes exceeds length of 50, received length (51), please reduce the length of the array to continue'
373+
)
374+
})
375+
376+
it('should error when sizes contains invalid sizes', async () => {
377+
await nextConfig.replace(
378+
'{ /* replaceme */ }',
379+
JSON.stringify({
380+
images: {
381+
sizes: [-1, 12000, 64, 128, 256],
382+
},
383+
})
384+
)
385+
let stderr = ''
386+
387+
app = await launchApp(appDir, await findPort(), {
388+
onStderr(msg) {
389+
stderr += msg || ''
390+
},
391+
})
392+
await waitFor(1000)
393+
await killApp(app).catch(() => {})
394+
await nextConfig.restore()
395+
396+
expect(stderr).toContain(
397+
'Specified images.sizes should be an Array of numbers that are between 0 and 10,000, received invalid values (-1, 12000)'
398+
)
399+
})
400+
})
401+
324402
// domains for testing
325403
const domains = ['localhost', 'example.com']
326404

0 commit comments

Comments
 (0)