|
| 1 | +import type { MiddlewareHandler } from 'hono'; |
| 2 | + |
| 3 | +export interface ExternalRequestUrlOptions { |
| 4 | + publicBaseUrl?: string; |
| 5 | + trustProxy?: boolean; |
| 6 | +} |
| 7 | + |
| 8 | +interface MutableRequestUrl { |
| 9 | + raw: Request; |
| 10 | + url: string; |
| 11 | +} |
| 12 | + |
| 13 | +function getFirstHeaderValue(value: string | null): string | null { |
| 14 | + if (!value) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + const first = value.split(',')[0]?.trim(); |
| 19 | + return first && first.length > 0 ? first : null; |
| 20 | +} |
| 21 | + |
| 22 | +function stripQuotes(value: string): string { |
| 23 | + if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) { |
| 24 | + return value.slice(1, -1); |
| 25 | + } |
| 26 | + |
| 27 | + return value; |
| 28 | +} |
| 29 | + |
| 30 | +function isTrustedProtocol(value: string | null): value is 'http' | 'https' { |
| 31 | + return value === 'http' || value === 'https'; |
| 32 | +} |
| 33 | + |
| 34 | +function parseForwardedHeader(value: string | null): { host: string | null; proto: 'http' | 'https' | null } { |
| 35 | + const first = getFirstHeaderValue(value); |
| 36 | + if (!first) { |
| 37 | + return { host: null, proto: null }; |
| 38 | + } |
| 39 | + |
| 40 | + let host: string | null = null; |
| 41 | + let proto: 'http' | 'https' | null = null; |
| 42 | + |
| 43 | + for (const part of first.split(';')) { |
| 44 | + const [rawKey, rawValue] = part.split('=', 2); |
| 45 | + if (!rawKey || !rawValue) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + const key = rawKey.trim().toLowerCase(); |
| 50 | + const parsedValue = stripQuotes(rawValue.trim()); |
| 51 | + |
| 52 | + if (key === 'host' && parsedValue.length > 0) { |
| 53 | + host = parsedValue; |
| 54 | + } |
| 55 | + |
| 56 | + if (key === 'proto') { |
| 57 | + const normalized = parsedValue.toLowerCase(); |
| 58 | + if (isTrustedProtocol(normalized)) { |
| 59 | + proto = normalized; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return { host, proto }; |
| 65 | +} |
| 66 | + |
| 67 | +function parseForwardedPort(value: string | null): string | null { |
| 68 | + const port = getFirstHeaderValue(value); |
| 69 | + return port && /^[0-9]+$/.test(port) ? port : null; |
| 70 | +} |
| 71 | + |
| 72 | +function applyOrigin(url: URL, origin: URL): URL { |
| 73 | + url.protocol = origin.protocol; |
| 74 | + url.host = origin.host; |
| 75 | + return url; |
| 76 | +} |
| 77 | + |
| 78 | +function applyForwardedHost(url: URL, forwardedHost: string): boolean { |
| 79 | + try { |
| 80 | + const parsed = new URL(`${url.protocol}//${forwardedHost}`); |
| 81 | + if ( |
| 82 | + parsed.username.length > 0 || |
| 83 | + parsed.password.length > 0 || |
| 84 | + parsed.pathname !== '/' || |
| 85 | + parsed.search.length > 0 || |
| 86 | + parsed.hash.length > 0 |
| 87 | + ) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + url.hostname = parsed.hostname; |
| 92 | + url.port = parsed.port; |
| 93 | + return true; |
| 94 | + } catch { |
| 95 | + return false; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export function getExternalRequestUrl(requestUrl: string, headers: Headers, options?: ExternalRequestUrlOptions): URL { |
| 100 | + const url = new URL(requestUrl); |
| 101 | + |
| 102 | + if (options?.publicBaseUrl) { |
| 103 | + return applyOrigin(url, new URL(options.publicBaseUrl)); |
| 104 | + } |
| 105 | + |
| 106 | + if (!options?.trustProxy) { |
| 107 | + return url; |
| 108 | + } |
| 109 | + |
| 110 | + const forwarded = parseForwardedHeader(headers.get('forwarded')); |
| 111 | + const forwardedHost = forwarded.host ?? getFirstHeaderValue(headers.get('x-forwarded-host')); |
| 112 | + const forwardedPort = parseForwardedPort(headers.get('x-forwarded-port')); |
| 113 | + const forwardedProto = forwarded.proto ?? (() => { |
| 114 | + const proto = getFirstHeaderValue(headers.get('x-forwarded-proto'))?.toLowerCase() ?? null; |
| 115 | + return isTrustedProtocol(proto) ? proto : null; |
| 116 | + })(); |
| 117 | + |
| 118 | + if (forwardedHost) { |
| 119 | + const appliedHost = applyForwardedHost(url, forwardedHost); |
| 120 | + if (appliedHost && forwardedPort && url.port.length === 0) { |
| 121 | + url.port = forwardedPort; |
| 122 | + } |
| 123 | + } else if (forwardedPort) { |
| 124 | + url.port = forwardedPort; |
| 125 | + } |
| 126 | + |
| 127 | + if (forwardedProto) { |
| 128 | + url.protocol = `${forwardedProto}:`; |
| 129 | + } |
| 130 | + |
| 131 | + return url; |
| 132 | +} |
| 133 | + |
| 134 | +export function normalizeExternalRequestUrl(request: MutableRequestUrl, headers: Headers, options?: ExternalRequestUrlOptions): string { |
| 135 | + const externalUrl = getExternalRequestUrl(request.url, headers, options).toString(); |
| 136 | + |
| 137 | + if (externalUrl !== request.url) { |
| 138 | + Object.defineProperty(request.raw, 'url', { |
| 139 | + value: externalUrl, |
| 140 | + configurable: true, |
| 141 | + writable: true |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + return externalUrl; |
| 146 | +} |
| 147 | + |
| 148 | +export function createExternalRequestUrlMiddleware(options?: ExternalRequestUrlOptions): MiddlewareHandler { |
| 149 | + return async (c, next) => { |
| 150 | + normalizeExternalRequestUrl(c.req, c.req.raw.headers, options); |
| 151 | + await next(); |
| 152 | + }; |
| 153 | +} |
0 commit comments