|
| 1 | +import net from 'node:net' |
| 2 | +import type { Connect } from 'dep-types/connect' |
| 3 | +import type { ResolvedConfig } from '../../config' |
| 4 | +import type { ResolvedPreviewOptions, ResolvedServerOptions } from '../..' |
| 5 | + |
| 6 | +const allowedHostsCache = new WeakMap<ResolvedConfig, Set<string>>() |
| 7 | + |
| 8 | +const isFileOrExtensionProtocolRE = /^(?:file|.+-extension):/i |
| 9 | + |
| 10 | +export function getAdditionalAllowedHosts( |
| 11 | + resolvedServerOptions: Pick<ResolvedServerOptions, 'host' | 'hmr' | 'origin'>, |
| 12 | + resolvedPreviewOptions: Pick<ResolvedPreviewOptions, 'host'>, |
| 13 | +): string[] { |
| 14 | + const list = [] |
| 15 | + |
| 16 | + // allow host option by default as that indicates that the user is |
| 17 | + // expecting Vite to respond on that host |
| 18 | + if ( |
| 19 | + typeof resolvedServerOptions.host === 'string' && |
| 20 | + resolvedServerOptions.host |
| 21 | + ) { |
| 22 | + list.push(resolvedServerOptions.host) |
| 23 | + } |
| 24 | + if ( |
| 25 | + typeof resolvedServerOptions.hmr === 'object' && |
| 26 | + resolvedServerOptions.hmr.host |
| 27 | + ) { |
| 28 | + list.push(resolvedServerOptions.hmr.host) |
| 29 | + } |
| 30 | + if ( |
| 31 | + typeof resolvedPreviewOptions.host === 'string' && |
| 32 | + resolvedPreviewOptions.host |
| 33 | + ) { |
| 34 | + list.push(resolvedPreviewOptions.host) |
| 35 | + } |
| 36 | + |
| 37 | + // allow server origin by default as that indicates that the user is |
| 38 | + // expecting Vite to respond on that host |
| 39 | + if (resolvedServerOptions.origin) { |
| 40 | + const serverOriginUrl = new URL(resolvedServerOptions.origin) |
| 41 | + list.push(serverOriginUrl.hostname) |
| 42 | + } |
| 43 | + |
| 44 | + return list |
| 45 | +} |
| 46 | + |
| 47 | +// Based on webpack-dev-server's `checkHeader` function: https://github.com/webpack/webpack-dev-server/blob/v5.2.0/lib/Server.js#L3086 |
| 48 | +// https://github.com/webpack/webpack-dev-server/blob/v5.2.0/LICENSE |
| 49 | +export function isHostAllowedWithoutCache( |
| 50 | + allowedHosts: string[], |
| 51 | + additionalAllowedHosts: string[], |
| 52 | + host: string, |
| 53 | +): boolean { |
| 54 | + if (isFileOrExtensionProtocolRE.test(host)) { |
| 55 | + return true |
| 56 | + } |
| 57 | + |
| 58 | + // We don't care about malformed Host headers, |
| 59 | + // because we only need to consider browser requests. |
| 60 | + // Non-browser clients can send any value they want anyway. |
| 61 | + // |
| 62 | + // `Host = uri-host [ ":" port ]` |
| 63 | + const trimmedHost = host.trim() |
| 64 | + |
| 65 | + // IPv6 |
| 66 | + if (trimmedHost[0] === '[') { |
| 67 | + const endIpv6 = trimmedHost.indexOf(']') |
| 68 | + if (endIpv6 < 0) { |
| 69 | + return false |
| 70 | + } |
| 71 | + // DNS rebinding attacks does not happen with IP addresses |
| 72 | + return net.isIP(trimmedHost.slice(1, endIpv6)) === 6 |
| 73 | + } |
| 74 | + |
| 75 | + // uri-host does not include ":" unless IPv6 address |
| 76 | + const colonPos = trimmedHost.indexOf(':') |
| 77 | + const hostname = |
| 78 | + colonPos === -1 ? trimmedHost : trimmedHost.slice(0, colonPos) |
| 79 | + |
| 80 | + // DNS rebinding attacks does not happen with IP addresses |
| 81 | + if (net.isIP(hostname) === 4) { |
| 82 | + return true |
| 83 | + } |
| 84 | + |
| 85 | + // allow localhost and .localhost by default as they always resolve to the loopback address |
| 86 | + // https://datatracker.ietf.org/doc/html/rfc6761#section-6.3 |
| 87 | + if (hostname === 'localhost' || hostname.endsWith('.localhost')) { |
| 88 | + return true |
| 89 | + } |
| 90 | + |
| 91 | + for (const additionalAllowedHost of additionalAllowedHosts) { |
| 92 | + if (additionalAllowedHost === hostname) { |
| 93 | + return true |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + for (const allowedHost of allowedHosts) { |
| 98 | + if (allowedHost === hostname) { |
| 99 | + return true |
| 100 | + } |
| 101 | + |
| 102 | + // allow all subdomains of it |
| 103 | + // e.g. `.foo.example` will allow `foo.example`, `*.foo.example`, `*.*.foo.example`, etc |
| 104 | + if ( |
| 105 | + allowedHost[0] === '.' && |
| 106 | + (allowedHost.slice(1) === hostname || hostname.endsWith(allowedHost)) |
| 107 | + ) { |
| 108 | + return true |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return false |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @param config resolved config |
| 117 | + * @param host the value of host header. See [RFC 9110 7.2](https://datatracker.ietf.org/doc/html/rfc9110#name-host-and-authority). |
| 118 | + */ |
| 119 | +export function isHostAllowed(config: ResolvedConfig, host: string): boolean { |
| 120 | + if (config.server.allowedHosts === true) { |
| 121 | + return true |
| 122 | + } |
| 123 | + |
| 124 | + if (!allowedHostsCache.has(config)) { |
| 125 | + allowedHostsCache.set(config, new Set()) |
| 126 | + } |
| 127 | + |
| 128 | + const allowedHosts = allowedHostsCache.get(config)! |
| 129 | + if (allowedHosts.has(host)) { |
| 130 | + return true |
| 131 | + } |
| 132 | + |
| 133 | + const result = isHostAllowedWithoutCache( |
| 134 | + config.server.allowedHosts, |
| 135 | + config.additionalAllowedHosts, |
| 136 | + host, |
| 137 | + ) |
| 138 | + if (result) { |
| 139 | + allowedHosts.add(host) |
| 140 | + } |
| 141 | + return result |
| 142 | +} |
| 143 | + |
| 144 | +export function hostCheckMiddleware( |
| 145 | + config: ResolvedConfig, |
| 146 | +): Connect.NextHandleFunction { |
| 147 | + // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` |
| 148 | + return function viteHostCheckMiddleware(req, res, next) { |
| 149 | + const hostHeader = req.headers.host |
| 150 | + if (!hostHeader || !isHostAllowed(config, hostHeader)) { |
| 151 | + const hostname = hostHeader?.replace(/:\d+$/, '') |
| 152 | + const hostnameWithQuotes = JSON.stringify(hostname) |
| 153 | + res.writeHead(403, { |
| 154 | + 'Content-Type': 'text/plain', |
| 155 | + }) |
| 156 | + res.end( |
| 157 | + `Blocked request. This host (${hostnameWithQuotes}) is not allowed.\n` + |
| 158 | + `To allow this host, add ${hostnameWithQuotes} to \`server.allowedHosts\` in vite.config.js.`, |
| 159 | + ) |
| 160 | + return |
| 161 | + } |
| 162 | + return next() |
| 163 | + } |
| 164 | +} |