Skip to content

Commit 754c194

Browse files
committed
add test
1 parent b8f05e6 commit 754c194

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

packages/next/src/server/config.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -364,22 +364,19 @@ function assignDefaults(
364364
)
365365
}
366366

367-
// Convert URL to RemotePattern
367+
// We must convert URL to RemotePattern since URL has a colon in the protocol
368+
// and also has additional properties we want to filter out. Also, new URL()
369+
// accepts any protocol so we need manual validation here.
368370
images.remotePatterns = images.remotePatterns.map(
369371
({ protocol, hostname, port, pathname, search }) => {
370-
if (
371-
protocol &&
372-
!['http', 'https', 'http:', 'https:'].includes(protocol)
373-
) {
372+
const proto = protocol?.replace(/:$/, '')
373+
if (!['http', 'https', undefined].includes(proto)) {
374374
throw new Error(
375-
`Specified images.remotePatterns must have protocol "http" or "https" received "${protocol}".`
375+
`Specified images.remotePatterns must have protocol "http" or "https" received "${proto}".`
376376
)
377377
}
378378
return {
379-
protocol: protocol?.replace(/:$/, '') as
380-
| 'http'
381-
| 'https'
382-
| undefined,
379+
protocol: proto as 'http' | 'https' | undefined,
383380
hostname,
384381
port,
385382
pathname,

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

+21
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,27 @@ describe('Image Optimizer', () => {
567567
)
568568
})
569569

570+
it('should error when images.remotePatterns URL has invalid protocol', async () => {
571+
await nextConfig.replace(
572+
'{ /* replaceme */ }',
573+
`{ images: { remotePatterns: [new URL('file://example.com/**')] } }`
574+
)
575+
let stderr = ''
576+
577+
app = await launchApp(appDir, await findPort(), {
578+
onStderr(msg) {
579+
stderr += msg || ''
580+
},
581+
})
582+
await waitFor(1000)
583+
await killApp(app).catch(() => {})
584+
await nextConfig.restore()
585+
586+
expect(stderr).toContain(
587+
`Specified images.remotePatterns must have protocol "http" or "https" received "file"`
588+
)
589+
})
590+
570591
it('should error when images.contentDispositionType is not valid', async () => {
571592
await nextConfig.replace(
572593
'{ /* replaceme */ }',

0 commit comments

Comments
 (0)