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 dev warning for cross-origin and stabilize allowedDevOrigins #77044

Merged
merged 8 commits into from
Mar 12, 2025
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.
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
@@ -0,0 +1,18 @@
---
title: allowedDevOrigins
description: Use `allowedDevOrigins` to configure additional origins that can request the dev server.
---

{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}

To configure a Next.js application to allow requests from origins other than the hostname the server was initialized with (`localhost` by default) you can use the `allowedDevOrigins` config option.

`allowedDevOrigins` allows you to set additional origins that can be used in development mode. For example, to use `local-origin.dev` instead of only `localhost`, open `next.config.js` and add the `allowedDevOrigins` config:

```js filename="next.config.js"
module.exports = {
allowedDevOrigins: ['local-origin.dev'],
}
```

Cross-origin requests are blocked by default to prevent unauthorized requesting of internal assets/endpoints which are available in development mode. This behavior is similar to other dev servers like `webpack-dev-middleware` to ensure the same protection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: allowedDevOrigins
description: Use `allowedDevOrigins` to configure additional origins that can request the dev server.
source: app/api-reference/config/next-config-js/allowedDevOrigins
---

{/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
2 changes: 1 addition & 1 deletion packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const zTurboRuleConfigItemOrShortcut: zod.ZodType<TurboRuleConfigItemOrShortcut>

export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
z.strictObject({
allowedDevOrigins: z.array(z.string()).optional(),
amp: z
.object({
canonicalBase: z.string().optional(),
Expand Down Expand Up @@ -262,7 +263,6 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
experimental: z
.strictObject({
generateOnlyEnv: z.boolean().optional(),
allowedDevOrigins: z.array(z.string()).optional(),
nodeMiddleware: z.boolean().optional(),
after: z.boolean().optional(),
appDocumentPreloading: z.boolean().optional(),
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ export interface LoggingConfig {

export interface ExperimentalConfig {
generateOnlyEnv?: boolean
allowedDevOrigins?: string[]
nodeMiddleware?: boolean
cacheHandlers?: {
default?: string
Expand Down Expand Up @@ -674,6 +673,8 @@ export type ExportPathMap = {
* Read more: [Next.js Docs: `next.config.js`](https://nextjs.org/docs/app/api-reference/config/next-config-js)
*/
export interface NextConfig extends Record<string, any> {
allowedDevOrigins?: string[]

exportPathMap?: (
defaultMap: ExportPathMap,
ctx: {
Expand Down Expand Up @@ -1135,9 +1136,9 @@ export const defaultConfig: NextConfig = {
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '',
allowedDevOrigins: [],
experimental: {
generateOnlyEnv: false,
allowedDevOrigins: [],
nodeMiddleware: false,
cacheLife: {
default: {
Expand Down
5 changes: 1 addition & 4 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ export async function initialize(opts: {
renderServer.instance =
require('./render-server') as typeof import('./render-server')

const allowedOrigins = [
'localhost',
...(config.experimental.allowedDevOrigins || []),
]
const allowedOrigins = ['localhost', ...(config.allowedDevOrigins || [])]
if (opts.hostname) {
allowedOrigins.push(opts.hostname)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/server/lib/router-utils/block-cross-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Duplex } from 'stream'
import type { IncomingMessage, ServerResponse } from 'webpack-dev-server'
import { parseUrl } from '../../../lib/url'
import net from 'net'
import { warnOnce } from '../../../build/output/log'

export const blockCrossSite = (
req: IncomingMessage,
Expand All @@ -23,6 +24,9 @@ export const blockCrossSite = (
res.statusCode = 403
}
res.end('Unauthorized')
warnOnce(
`Blocked cross-origin request to /_next/*. To allow this, configure "allowedDevOrigins" in next.config\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins`
)
return true
}

Expand Down Expand Up @@ -50,6 +54,9 @@ export const blockCrossSite = (
res.statusCode = 403
}
res.end('Unauthorized')
warnOnce(
`Blocked cross-origin request from ${originLowerCase}. To allow this, configure "allowedDevOrigins" in next.config\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins`
)
return true
}
}
Expand Down
Loading