Commit cfeaa86f authored by Zack Tanner's avatar Zack Tanner Committed by JJ Kasper

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.
parent f8473026
...@@ -323,15 +323,7 @@ export async function initialize(opts: { ...@@ -323,15 +323,7 @@ export async function initialize(opts: {
// handle hot-reloader first // handle hot-reloader first
if (developmentBundler) { if (developmentBundler) {
if ( if (blockCrossSite(req, res, config.allowedDevOrigins, opts.hostname)) {
blockCrossSite(
req,
res,
config.allowedDevOrigins,
opts.hostname,
`${opts.port}`
)
) {
return return
} }
const origUrl = req.url || '/' const origUrl = req.url || '/'
...@@ -698,13 +690,7 @@ export async function initialize(opts: { ...@@ -698,13 +690,7 @@ export async function initialize(opts: {
if (opts.dev && developmentBundler && req.url) { if (opts.dev && developmentBundler && req.url) {
if ( if (
blockCrossSite( blockCrossSite(req, socket, config.allowedDevOrigins, opts.hostname)
req,
socket,
config.allowedDevOrigins,
opts.hostname,
`${opts.port}`
)
) { ) {
return return
} }
......
import type { Duplex } from 'stream' import type { Duplex } from 'stream'
import type { IncomingMessage, ServerResponse } from 'webpack-dev-server' import type { IncomingMessage, ServerResponse } from 'webpack-dev-server'
import { parseUrl } from '../../../lib/url' import { parseUrl } from '../../../lib/url'
import net from 'net'
import { warnOnce } from '../../../build/output/log' import { warnOnce } from '../../../build/output/log'
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection' import { isCsrfOriginAllowed } from '../../app-render/csrf-protection'
...@@ -36,8 +35,7 @@ export const blockCrossSite = ( ...@@ -36,8 +35,7 @@ export const blockCrossSite = (
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse | Duplex, res: ServerResponse | Duplex,
allowedDevOrigins: string[] | undefined, allowedDevOrigins: string[] | undefined,
hostname: string | undefined, hostname: string | undefined
activePort: string
): boolean => { ): boolean => {
// in the future, these will be blocked by default when allowed origins aren't configured. // in the future, these will be blocked by default when allowed origins aren't configured.
// for now, we warn when allowed origins aren't configured // for now, we warn when allowed origins aren't configured
...@@ -52,7 +50,7 @@ export const blockCrossSite = ( ...@@ -52,7 +50,7 @@ export const blockCrossSite = (
allowedOrigins.push(hostname) allowedOrigins.push(hostname)
} }
// only process _next URLs when // only process _next URLs
if (!req.url?.includes('/_next')) { if (!req.url?.includes('/_next')) {
return false return false
} }
...@@ -73,16 +71,8 @@ export const blockCrossSite = ( ...@@ -73,16 +71,8 @@ export const blockCrossSite = (
if (parsedOrigin) { if (parsedOrigin) {
const originLowerCase = parsedOrigin.hostname.toLowerCase() const originLowerCase = parsedOrigin.hostname.toLowerCase()
const isMatchingPort = parsedOrigin.port === activePort
const isIpRequest =
net.isIPv4(originLowerCase) || net.isIPv6(originLowerCase)
if ( if (!isCsrfOriginAllowed(originLowerCase, allowedOrigins)) {
// allow requests if direct IP and matching port and
// allow if any of the allowed origins match
!(isIpRequest && isMatchingPort) &&
!isCsrfOriginAllowed(originLowerCase, allowedOrigins)
) {
return warnOrBlockRequest(res, originLowerCase, mode) return warnOrBlockRequest(res, originLowerCase, mode)
} }
} }
......
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