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

feat(next/image): add images.localPatterns config #70529

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ export default function Page() {

> **Warning:** Dynamic `await import()` or `require()` are _not_ supported. The `import` must be static so it can be analyzed at build time.

You can optionally configure `localPatterns` in your `next.config.js` file in order to allow specific images and block all others.

```js filename="next.config.js"
module.exports = {
images: {
localPatterns: [
{
pathname: '/assets/images/**',
search: '',
},
],
},
}
```

### Remote Images

To use a remote image, the `src` property should be a URL string.
Expand Down
19 changes: 19 additions & 0 deletions docs/02-app/02-api-reference/01-components/image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,25 @@ Other properties on the `<Image />` component will be passed to the underlying

In addition to props, you can configure the Image Component in `next.config.js`. The following options are available:

## `localPatterns`

You can optionally configure `localPatterns` in your `next.config.js` file in order to allow specific paths to be optimized and block all others paths.

```js filename="next.config.js"
module.exports = {
images: {
localPatterns: [
{
pathname: '/assets/images/**',
search: '',
},
],
},
}
```

> **Good to know**: The example above will ensure the `src` property of `next/image` must start with `/assets/images/` and must not have a query string. Attempting to optimize any other path will respond with 400 Bad Request.

### `remotePatterns`

To protect your application from malicious users, configuration is required in order to use external images. This ensures that only external images from your account can be served from the Next.js Image Optimization API. These external images can be configured with the `remotePatterns` property in your `next.config.js` file, as shown below:
Expand Down
2 changes: 2 additions & 0 deletions errors/invalid-images-config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module.exports = {
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
// sets the Content-Disposition header ('inline' or 'attachment')
contentDispositionType: 'attachment',
// limit of 25 objects
localPatterns: [],
// limit of 50 objects
remotePatterns: [],
// when true, every image will be unoptimized
Expand Down
29 changes: 29 additions & 0 deletions errors/next-image-unconfigured-localpatterns.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: '`next/image` Un-configured localPatterns'
---

## Why This Error Occurred

One of your pages that leverages the `next/image` component, passed a `src` value that uses a URL that isn't defined in the `images.localPatterns` property in `next.config.js`.

## Possible Ways to Fix It

Add an entry to `images.localPatterns` array in `next.config.js` with the expected URL pattern. For example:

```js filename="next.config.js"
module.exports = {
images: {
localPatterns: [
{
pathname: '/assets/**',
search: '',
},
],
},
}
```

## Useful Links

- [Image Optimization Documentation](/docs/pages/building-your-application/optimizing/images)
- [Local Patterns Documentation](/docs/pages/api-reference/components/image#localpatterns)
5 changes: 5 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ async function writeImagesManifest(
pathname: makeRe(p.pathname ?? '**', { dot: true }).source,
search: p.search,
}))
images.localPatterns = (config?.images?.localPatterns || []).map((p) => ({
// Modifying the manifest should also modify matchLocalPattern()
pathname: makeRe(p.pathname ?? '**', { dot: true }).source,
search: p.search,
}))

await writeManifest(path.join(distDir, IMAGES_MANIFEST), {
version: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function getImageConfig(
// pass domains in development to allow validating on the client
domains: config.images.domains,
remotePatterns: config.images?.remotePatterns,
localPatterns: config.images?.localPatterns,
output: config.output,
}
: {}),
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/client/legacy/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ function defaultLoader({
process.env.NEXT_RUNTIME !== 'edge'
) {
// We use dynamic require because this should only error in development
const { hasMatch } = require('../../shared/lib/match-remote-pattern')
if (!hasMatch(config.domains, config.remotePatterns, parsedSrc)) {
const {
hasRemoteMatch,
} = require('../../shared/lib/match-remote-pattern')
if (!hasRemoteMatch(config.domains, config.remotePatterns, parsedSrc)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this check be present for localPatterns as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I had it on the new image component but not the legacy image component.

Added in 246d0c2

throw new Error(
`Invalid src prop (${src}) on \`next/image\`, hostname "${parsedSrc.hostname}" is not configured under images in your \`next.config.js\`\n` +
`See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host`
Expand Down
9 changes: 9 additions & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,15 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
.optional(),
images: z
.strictObject({
localPatterns: z
.array(
z.strictObject({
pathname: z.string().optional(),
search: z.string().optional(),
})
)
.max(25)
.optional(),
remotePatterns: z
.array(
z.strictObject({
Expand Down
13 changes: 13 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,19 @@ function assignDefaults(
)
}

if (images.localPatterns) {
if (!Array.isArray(images.localPatterns)) {
throw new Error(
`Specified images.localPatterns should be an Array received ${typeof images.localPatterns}.\nSee more info here: https://nextjs.org/docs/messages/invalid-images-config`
)
}
// static import images are automatically allowed
images.localPatterns.push({
pathname: '/_next/static/media/**',
search: '',
})
}

if (images.remotePatterns) {
if (!Array.isArray(images.remotePatterns)) {
throw new Error(
Expand Down
9 changes: 7 additions & 2 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import nodeUrl, { type UrlWithParsedQuery } from 'url'

import { getImageBlurSvg } from '../shared/lib/image-blur-svg'
import type { ImageConfigComplete } from '../shared/lib/image-config'
import { hasMatch } from '../shared/lib/match-remote-pattern'
import { hasLocalMatch } from '../shared/lib/match-local-pattern'
import { hasRemoteMatch } from '../shared/lib/match-remote-pattern'
import type { NextConfigComplete } from './config-shared'
import { createRequestResponseMocks } from './lib/mock-request'
import type { NextUrlWithParsedQuery } from './request-meta'
Expand Down Expand Up @@ -213,6 +214,7 @@ export class ImageOptimizerCache {
formats = ['image/webp'],
} = imageData
const remotePatterns = nextConfig.images?.remotePatterns || []
const localPatterns = nextConfig.images?.localPatterns
const { url, w, q } = query
let href: string

Expand Down Expand Up @@ -252,6 +254,9 @@ export class ImageOptimizerCache {
errorMessage: '"url" parameter cannot be recursive',
}
}
if (!hasLocalMatch(localPatterns, url)) {
return { errorMessage: '"url" parameter is not allowed' }
}
} else {
let hrefParsed: URL

Expand All @@ -267,7 +272,7 @@ export class ImageOptimizerCache {
return { errorMessage: '"url" parameter is invalid' }
}

if (!hasMatch(domains, remotePatterns, hrefParsed)) {
if (!hasRemoteMatch(domains, remotePatterns, hrefParsed)) {
return { errorMessage: '"url" parameter is not allowed' }
}
}
Expand Down
21 changes: 20 additions & 1 deletion packages/next/src/shared/lib/image-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ export type ImageLoaderPropsWithConfig = ImageLoaderProps & {
config: Readonly<ImageConfig>
}

export type LocalPattern = {
/**
* Can be literal or wildcard.
* Single `*` matches a single path segment.
* Double `**` matches any number of path segments.
*/
pathname?: string

/**
* Can be literal query string such as `?v=1` or
* empty string meaning no query string.
*/
search?: string
}

export type RemotePattern = {
/**
* Must be `http` or `https`.
Expand Down Expand Up @@ -100,6 +115,9 @@ export type ImageConfigComplete = {
/** @see [Remote Patterns](https://nextjs.org/docs/api-reference/next/image#remotepatterns) */
remotePatterns: RemotePattern[]

/** @see [Remote Patterns](https://nextjs.org/docs/api-reference/next/image#localPatterns) */
localPatterns: LocalPattern[] | undefined

/** @see [Unoptimized](https://nextjs.org/docs/api-reference/next/image#unoptimized) */
unoptimized: boolean
}
Expand All @@ -119,6 +137,7 @@ export const imageConfigDefault: ImageConfigComplete = {
dangerouslyAllowSVG: false,
contentSecurityPolicy: `script-src 'none'; frame-src 'none'; sandbox;`,
contentDispositionType: 'attachment',
remotePatterns: [],
localPatterns: undefined, // default: allow all local images
remotePatterns: [], // default: allow no remote images
unoptimized: false,
}
21 changes: 19 additions & 2 deletions packages/next/src/shared/lib/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ function defaultLoader({
)
}

if (src.startsWith('/') && config.localPatterns) {
if (
process.env.NODE_ENV !== 'test' &&
// micromatch isn't compatible with edge runtime
process.env.NEXT_RUNTIME !== 'edge'
) {
// We use dynamic require because this should only error in development
const { hasLocalMatch } = require('./match-local-pattern')
if (!hasLocalMatch(config.localPatterns, src)) {
throw new Error(
`Invalid src prop (${src}) on \`next/image\` does not match \`images.localPatterns\` configured in your \`next.config.js\`\n` +
`See more info: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns`
)
}
}
}

if (!src.startsWith('/') && (config.domains || config.remotePatterns)) {
let parsedSrc: URL
try {
Expand All @@ -46,8 +63,8 @@ function defaultLoader({
process.env.NEXT_RUNTIME !== 'edge'
) {
// We use dynamic require because this should only error in development
const { hasMatch } = require('./match-remote-pattern')
if (!hasMatch(config.domains, config.remotePatterns, parsedSrc)) {
const { hasRemoteMatch } = require('./match-remote-pattern')
if (!hasRemoteMatch(config.domains, config.remotePatterns, parsedSrc)) {
throw new Error(
`Invalid src prop (${src}) on \`next/image\`, hostname "${parsedSrc.hostname}" is not configured under images in your \`next.config.js\`\n` +
`See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host`
Expand Down
29 changes: 29 additions & 0 deletions packages/next/src/shared/lib/match-local-pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { LocalPattern } from './image-config'
import { makeRe } from 'next/dist/compiled/picomatch'

// Modifying this function should also modify writeImagesManifest()
export function matchLocalPattern(pattern: LocalPattern, url: URL): boolean {
if (pattern.search !== undefined) {
if (pattern.search !== url.search) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does ordering on the search value here matter since the user prop can't control intermediaries like CDNs changing it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the order matters because search is a string literal.

I don't think CDN's changing it will be very common since this is going to be the value passed to the src prop on the image component.

In the future, we could add support for any order using an an object or array.

return false
}
}

if (!makeRe(pattern.pathname ?? '**', { dot: true }).test(url.pathname)) {
return false
}

return true
}

export function hasLocalMatch(
localPatterns: LocalPattern[] | undefined,
urlPathAndQuery: string
): boolean {
if (!localPatterns) {
// if the user didn't define "localPatterns", we allow all local images
return true
}
const url = new URL(urlPathAndQuery, 'http://n')
return localPatterns.some((p) => matchLocalPattern(p, url))
}
2 changes: 1 addition & 1 deletion packages/next/src/shared/lib/match-remote-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function matchRemotePattern(pattern: RemotePattern, url: URL): boolean {
return true
}

export function hasMatch(
export function hasRemoteMatch(
domains: string[],
remotePatterns: RemotePattern[],
url: URL
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/telemetry/events/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type EventCliSessionStarted = {
localeDetectionEnabled: boolean | null
imageDomainsCount: number | null
imageRemotePatternsCount: number | null
imageLocalPatternsCount: number | null
imageSizes: string | null
imageLoader: string | null
imageFormats: string | null
Expand Down Expand Up @@ -78,6 +79,7 @@ export function eventCliSession(
| 'localeDetectionEnabled'
| 'imageDomainsCount'
| 'imageRemotePatternsCount'
| 'imageLocalPatternsCount'
| 'imageSizes'
| 'imageLoader'
| 'imageFormats'
Expand Down Expand Up @@ -120,6 +122,9 @@ export function eventCliSession(
imageRemotePatternsCount: images?.remotePatterns
? images.remotePatterns.length
: null,
imageLocalPatternsCount: images?.localPatterns
? images.localPatterns.length
: null,
imageSizes: images?.imageSizes ? images.imageSizes.join(',') : null,
imageLoader: images?.loader,
imageFormats: images?.formats ? images.formats.join(',') : null,
Expand Down
52 changes: 52 additions & 0 deletions test/integration/image-optimizer/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,58 @@ describe('Image Optimizer', () => {
)
})

it('should error when localPatterns length exceeds 25', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
localPatterns: Array.from({ length: 26 }).map((_) => ({
pathname: '/foo/**',
})),
},
})
)
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(
'Array must contain at most 25 element(s) at "images.localPatterns"'
)
})

it('should error when localPatterns has invalid prop', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
localPatterns: [{ pathname: '/foo/**', foo: 'bar' }],
},
})
)
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(
`Unrecognized key(s) in object: 'foo' at "images.localPatterns[0]"`
)
})

it('should error when remotePatterns length exceeds 50', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
Expand Down
Loading
Loading