|
| 1 | +import type { Duplex } from 'stream' |
| 2 | +import type { IncomingMessage, ServerResponse } from 'node:http' |
| 3 | +import { parseUrl } from '../../../lib/url' |
| 4 | +import { warnOnce } from '../../../build/output/log' |
| 5 | +import { isCsrfOriginAllowed } from '../../app-render/csrf-protection' |
| 6 | + |
| 7 | +function warnOrBlockRequest( |
| 8 | + res: ServerResponse | Duplex, |
| 9 | + origin: string | undefined, |
| 10 | + mode: 'warn' | 'block' |
| 11 | +): boolean { |
| 12 | + const originString = origin ? `from ${origin}` : '' |
| 13 | + if (mode === 'warn') { |
| 14 | + warnOnce( |
| 15 | + `Cross origin request detected ${originString} to /_next/* resource. In a future major version of Next.js, you will need to explicitly configure "allowedDevOrigins" in next.config to allow this.\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins` |
| 16 | + ) |
| 17 | + |
| 18 | + return false |
| 19 | + } |
| 20 | + |
| 21 | + warnOnce( |
| 22 | + `Blocked cross-origin request ${originString} to /_next/* resource. To allow this, configure "allowedDevOrigins" in next.config\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins` |
| 23 | + ) |
| 24 | + |
| 25 | + if ('statusCode' in res) { |
| 26 | + res.statusCode = 403 |
| 27 | + } |
| 28 | + |
| 29 | + res.end('Unauthorized') |
| 30 | + |
| 31 | + return true |
| 32 | +} |
| 33 | + |
| 34 | +function isInternalDevEndpoint(req: IncomingMessage): boolean { |
| 35 | + if (!req.url) return false |
| 36 | + |
| 37 | + try { |
| 38 | + // TODO: We should standardize on a single prefix for this |
| 39 | + const isMiddlewareRequest = req.url.includes('/__nextjs') |
| 40 | + const isInternalAsset = req.url.includes('/_next') |
| 41 | + // Static media requests are excluded, as they might be loaded via CSS and would fail |
| 42 | + // CORS checks. |
| 43 | + const isIgnoredRequest = |
| 44 | + req.url.includes('/_next/image') || |
| 45 | + req.url.includes('/_next/static/media') |
| 46 | + |
| 47 | + return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest) |
| 48 | + } catch (err) { |
| 49 | + return false |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export const blockCrossSite = ( |
| 54 | + req: IncomingMessage, |
| 55 | + res: ServerResponse | Duplex, |
| 56 | + allowedDevOrigins: string[] | undefined, |
| 57 | + hostname: string | undefined |
| 58 | +): boolean => { |
| 59 | + // in the future, these will be blocked by default when allowed origins aren't configured. |
| 60 | + // for now, we warn when allowed origins aren't configured |
| 61 | + const mode = typeof allowedDevOrigins === 'undefined' ? 'warn' : 'block' |
| 62 | + |
| 63 | + const allowedOrigins = [ |
| 64 | + '*.localhost', |
| 65 | + 'localhost', |
| 66 | + ...(allowedDevOrigins || []), |
| 67 | + ] |
| 68 | + if (hostname) { |
| 69 | + allowedOrigins.push(hostname) |
| 70 | + } |
| 71 | + |
| 72 | + // only process internal URLs/middleware |
| 73 | + if (!isInternalDevEndpoint(req)) { |
| 74 | + return false |
| 75 | + } |
| 76 | + // block non-cors request from cross-site e.g. script tag on |
| 77 | + // different host |
| 78 | + if ( |
| 79 | + req.headers['sec-fetch-mode'] === 'no-cors' && |
| 80 | + req.headers['sec-fetch-site'] === 'cross-site' |
| 81 | + ) { |
| 82 | + return warnOrBlockRequest(res, undefined, mode) |
| 83 | + } |
| 84 | + |
| 85 | + // ensure websocket requests from allowed origin |
| 86 | + const rawOrigin = req.headers['origin'] |
| 87 | + |
| 88 | + if (rawOrigin) { |
| 89 | + const parsedOrigin = parseUrl(rawOrigin) |
| 90 | + |
| 91 | + if (parsedOrigin) { |
| 92 | + const originLowerCase = parsedOrigin.hostname.toLowerCase() |
| 93 | + |
| 94 | + if (!isCsrfOriginAllowed(originLowerCase, allowedOrigins)) { |
| 95 | + return warnOrBlockRequest(res, originLowerCase, mode) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return false |
| 101 | +} |
0 commit comments