Commit 5e59da1f authored by JJ Kasper's avatar JJ Kasper Committed by Jiachi Liu

Add dev warning for cross-origin and stabilize allowedDevOrigins (#77044)

Adding a warning when we block cross-origin requests in dev so users are
aware this is why easier and know how to allow the origin through as
custom setups can use different origins and it be valid.

x-ref:
https://github.com/vercel/next.js/pull/76880#issuecomment-2718487112

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 8151cb6c
---
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.
---
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. */}
......@@ -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(),
......
......@@ -673,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: {
......@@ -1134,6 +1136,7 @@ export const defaultConfig: NextConfig = {
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '',
allowedDevOrigins: [],
experimental: {
allowedDevOrigins: [],
nodeMiddleware: false,
......
......@@ -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,
......@@ -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
}
......@@ -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
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment