Skip to content

Commit cfeaa86

Browse files
ztannerijjk
authored andcommitted
remove direct ip/port bypass in dev origin check (#77414)
It's potentially unsafe to allow any sort of origin bypass if `allowedDevOrigins` is configured as it's trivial to stand up a remote server on the same port. This removes the case that would bypass the origin check.
1 parent f847302 commit cfeaa86

File tree

2 files changed

+5
-29
lines changed

2 files changed

+5
-29
lines changed

packages/next/src/server/lib/router-server.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,7 @@ export async function initialize(opts: {
323323

324324
// handle hot-reloader first
325325
if (developmentBundler) {
326-
if (
327-
blockCrossSite(
328-
req,
329-
res,
330-
config.allowedDevOrigins,
331-
opts.hostname,
332-
`${opts.port}`
333-
)
334-
) {
326+
if (blockCrossSite(req, res, config.allowedDevOrigins, opts.hostname)) {
335327
return
336328
}
337329
const origUrl = req.url || '/'
@@ -698,13 +690,7 @@ export async function initialize(opts: {
698690

699691
if (opts.dev && developmentBundler && req.url) {
700692
if (
701-
blockCrossSite(
702-
req,
703-
socket,
704-
config.allowedDevOrigins,
705-
opts.hostname,
706-
`${opts.port}`
707-
)
693+
blockCrossSite(req, socket, config.allowedDevOrigins, opts.hostname)
708694
) {
709695
return
710696
}

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

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Duplex } from 'stream'
22
import type { IncomingMessage, ServerResponse } from 'webpack-dev-server'
33
import { parseUrl } from '../../../lib/url'
4-
import net from 'net'
54
import { warnOnce } from '../../../build/output/log'
65
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection'
76

@@ -36,8 +35,7 @@ export const blockCrossSite = (
3635
req: IncomingMessage,
3736
res: ServerResponse | Duplex,
3837
allowedDevOrigins: string[] | undefined,
39-
hostname: string | undefined,
40-
activePort: string
38+
hostname: string | undefined
4139
): boolean => {
4240
// in the future, these will be blocked by default when allowed origins aren't configured.
4341
// for now, we warn when allowed origins aren't configured
@@ -52,7 +50,7 @@ export const blockCrossSite = (
5250
allowedOrigins.push(hostname)
5351
}
5452

55-
// only process _next URLs when
53+
// only process _next URLs
5654
if (!req.url?.includes('/_next')) {
5755
return false
5856
}
@@ -73,16 +71,8 @@ export const blockCrossSite = (
7371

7472
if (parsedOrigin) {
7573
const originLowerCase = parsedOrigin.hostname.toLowerCase()
76-
const isMatchingPort = parsedOrigin.port === activePort
77-
const isIpRequest =
78-
net.isIPv4(originLowerCase) || net.isIPv6(originLowerCase)
7974

80-
if (
81-
// allow requests if direct IP and matching port and
82-
// allow if any of the allowed origins match
83-
!(isIpRequest && isMatchingPort) &&
84-
!isCsrfOriginAllowed(originLowerCase, allowedOrigins)
85-
) {
75+
if (!isCsrfOriginAllowed(originLowerCase, allowedOrigins)) {
8676
return warnOrBlockRequest(res, originLowerCase, mode)
8777
}
8878
}

0 commit comments

Comments
 (0)