Skip to content

Commit b4d7093

Browse files
authored
unify allowed origin detection handling (#77053)
This unifies the CSRF origin detection to mirror what we do for server action origins, which supports things like wildcard matches (below the domain level) Fixes #76999
1 parent 0aa0ef6 commit b4d7093

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

docs/01-app/04-api-reference/05-config/01-next-config-js/allowedDevOrigins.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To configure a Next.js application to allow requests from origins other than the
1111

1212
```js filename="next.config.js"
1313
module.exports = {
14-
allowedDevOrigins: ['local-origin.dev'],
14+
allowedDevOrigins: ['local-origin.dev', '*.local-origin.dev'],
1515
}
1616
```
1717

packages/next/src/server/lib/router-utils/block-cross-site.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from 'webpack-dev-server'
33
import { parseUrl } from '../../../lib/url'
44
import net from 'net'
55
import { warnOnce } from '../../../build/output/log'
6+
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection'
67

78
export const blockCrossSite = (
89
req: IncomingMessage,
@@ -25,7 +26,7 @@ export const blockCrossSite = (
2526
}
2627
res.end('Unauthorized')
2728
warnOnce(
28-
`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`
29+
`Blocked cross-origin request to /_next/*. Cross-site requests are blocked in "no-cors" mode.`
2930
)
3031
return true
3132
}
@@ -46,9 +47,7 @@ export const blockCrossSite = (
4647
// allow requests if direct IP and matching port and
4748
// allow if any of the allowed origins match
4849
!(isIpRequest && isMatchingPort) &&
49-
!allowedOrigins.some(
50-
(allowedOrigin) => allowedOrigin === originLowerCase
51-
)
50+
!isCsrfOriginAllowed(originLowerCase, allowedOrigins)
5251
) {
5352
if ('statusCode' in res) {
5453
res.statusCode = 403

0 commit comments

Comments
 (0)