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

Add config limit checks for image optimizer #18217

Merged
merged 3 commits into from Oct 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/next/next-server/server/config.ts
Expand Up @@ -235,6 +235,13 @@ function assignDefaults(userConfig: { [key: string]: any }) {
`Specified images.domains should be an Array received ${typeof images.domains}`
)
}

if (images.domains.length > 50) {
throw new Error(
`Specified images.domains exceeds length of 50, received length (${images.domains.length}), please reduce the length of the array to continue`
)
}

const invalid = images.domains.filter(
(d: unknown) => typeof d !== 'string'
)
Expand All @@ -252,10 +259,20 @@ function assignDefaults(userConfig: { [key: string]: any }) {
`Specified images.sizes should be an Array received ${typeof images.sizes}`
)
}
const invalid = images.sizes.filter((d: unknown) => typeof d !== 'number')

if (images.sizes.length > 50) {
throw new Error(
`Specified images.sizes exceeds length of 50, received length (${images.sizes.length}), please reduce the length of the array to continue`
)
}

const invalid = images.sizes.filter((d: unknown) => {
return typeof d !== 'number' || d < 1 || d > 10000
})

if (invalid.length > 0) {
throw new Error(
`Specified images.sizes should be an Array of numbers received invalid values (${invalid.join(
`Specified images.sizes should be an Array of numbers that are between 1 and 10000, received invalid values (${invalid.join(
', '
)})`
)
Expand Down
78 changes: 78 additions & 0 deletions test/integration/image-optimizer/test/index.test.js
Expand Up @@ -10,6 +10,7 @@ import {
nextBuild,
nextStart,
File,
waitFor,
} from 'next-test-utils'
import sharp from 'sharp'

Expand Down Expand Up @@ -321,6 +322,83 @@ function runTests({ w, isDev, domains }) {
}

describe('Image Optimizer', () => {
describe('config checks', () => {
it('should error when domains length exceeds 50', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
domains: new Array(51).fill('google.com'),
},
})
)
let stderr = ''

app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg || ''
},
})
await waitFor(1000)
await killApp(app).catch(() => {})
await nextConfig.restore()

expect(stderr).toContain(
'Specified images.domains exceeds length of 50, received length (51), please reduce the length of the array to continue'
)
})

it('should error when sizes length exceeds 50', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
sizes: new Array(51).fill(1024),
},
})
)
let stderr = ''

app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg || ''
},
})
await waitFor(1000)
await killApp(app).catch(() => {})
await nextConfig.restore()

expect(stderr).toContain(
'Specified images.sizes exceeds length of 50, received length (51), please reduce the length of the array to continue'
)
})

it('should error when sizes contains invalid sizes', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
sizes: [0, 12000, 64, 128, 256],
},
})
)
let stderr = ''

app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg || ''
},
})
await waitFor(1000)
await killApp(app).catch(() => {})
await nextConfig.restore()

expect(stderr).toContain(
'Specified images.sizes should be an Array of numbers that are between 1 and 10000, received invalid values (0, 12000)'
)
})
})

// domains for testing
const domains = ['localhost', 'example.com']

Expand Down