forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorigin-validation.ts
More file actions
206 lines (187 loc) · 7.13 KB
/
origin-validation.ts
File metadata and controls
206 lines (187 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
export interface ParsedHostHeader {
hostname: string;
host: string;
port: string;
}
export interface RequestWithOriginHeaders {
headers?: {
host?: unknown;
origin?: unknown;
'sec-fetch-site'?: unknown;
};
}
export function configuredAllowedOrigins(env: NodeJS.ProcessEnv = process.env): string[] {
const raw = env.OD_ALLOWED_ORIGINS || '';
if (!raw.trim()) return [];
return raw
.split(',')
.map((origin) => origin.trim())
.filter(Boolean)
.map((origin) => {
const parsed = new URL(origin);
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
throw new Error('OD_ALLOWED_ORIGINS only supports http:// and https:// origins');
}
return parsed.origin;
});
}
export function configuredAllowedHosts(origins = configuredAllowedOrigins()): string[] {
return origins.map((origin) => new URL(origin).host);
}
export function allowedBrowserPorts(
port: number | string | null | undefined,
env: NodeJS.ProcessEnv = process.env,
): number[] {
const ports = [];
const primary = Number(port);
if (primary) ports.push(primary);
const webPort = Number(env.OD_WEB_PORT);
if (webPort && webPort !== primary) ports.push(webPort);
return ports;
}
export function parseHostHeader(value: unknown): ParsedHostHeader | null {
const raw = String(headerValue(value) || '').trim();
if (!raw) return null;
try {
const parsed = new URL(`http://${raw}`);
return { hostname: parsed.hostname, host: parsed.host, port: parsed.port || '80' };
} catch {
return null;
}
}
export function isPrivateIpv4(hostname: unknown): boolean {
const parts = String(hostname || '').split('.');
if (parts.length !== 4) return false;
if (!parts.every((part) => /^\d+$/.test(part))) return false;
const octets = parts.map((part) => Number(part));
if (!octets.every((n) => Number.isInteger(n) && n >= 0 && n <= 255)) return false;
const [a, b] = octets as [number, number, number, number];
return (
a === 10 ||
(a === 172 && b >= 16 && b <= 31) ||
(a === 192 && b === 168) ||
(a === 169 && b === 254)
);
}
export function isIpLiteralHostname(hostname: unknown): boolean {
const host = String(hostname || '').trim();
if (!host) return false;
if (host.startsWith('[') && host.endsWith(']')) return true;
const parts = host.split('.');
if (parts.length !== 4) return false;
if (!parts.every((part) => /^\d+$/.test(part))) return false;
return parts.map(Number).every((n) => Number.isInteger(n) && n >= 0 && n <= 255);
}
export function isLoopbackOrPrivateLanHost(hostname: unknown): boolean {
const host = String(hostname || '').toLowerCase();
return (
host === 'localhost' ||
host === '127.0.0.1' ||
host === '::1' ||
host === '[::1]' ||
host === '0.0.0.0' ||
host === '::' ||
isPrivateIpv4(host)
);
}
export function isAllowedBrowserHost(
hostHeader: unknown,
ports: number[],
bindHost: string,
extraAllowedOrigins: string[],
): boolean {
const requestHost = parseHostHeader(hostHeader);
if (!requestHost) return false;
const loopbackHosts = ['127.0.0.1', 'localhost', '[::1]'];
const explicitHosts = new Set([
...ports.flatMap((p) => [
...loopbackHosts.map((h) => `${h}:${p}`),
`${bindHost}:${p}`,
]),
...configuredAllowedHosts(extraAllowedOrigins),
]);
if (explicitHosts.has(requestHost.host)) return true;
if (!ports.map(String).includes(requestHost.port)) return false;
return isLoopbackOrPrivateLanHost(requestHost.hostname);
}
export function isAllowedBrowserOrigin(
origin: unknown,
hostHeader: unknown,
ports: number[],
bindHost: string,
extraAllowedOrigins: string[],
): boolean {
if (extraAllowedOrigins.includes(String(origin))) return true;
let parsedOrigin;
try {
parsedOrigin = new URL(String(origin));
} catch {
return false;
}
if (parsedOrigin.protocol !== 'http:' && parsedOrigin.protocol !== 'https:') return false;
const requestHost = parseHostHeader(hostHeader);
if (!requestHost) return false;
const schemes = ['http', 'https'];
const loopbackHosts = ['127.0.0.1', 'localhost', '[::1]'];
const explicitOrigins = new Set(
ports.flatMap((p) => [
...schemes.flatMap((s) => loopbackHosts.map((h) => `${s}://${h}:${p}`)),
...schemes.map((s) => `${s}://${bindHost}:${p}`),
]),
);
if (explicitOrigins.has(String(origin))) return true;
const originPort = parsedOrigin.port || (parsedOrigin.protocol === 'https:' ? '443' : '80');
if (!ports.map(String).includes(originPort)) return false;
if (parsedOrigin.hostname !== requestHost.hostname) return false;
return isLoopbackOrPrivateLanHost(parsedOrigin.hostname);
}
export function isLocalSameOrigin(
req: RequestWithOriginHeaders,
port: number | string | null | undefined,
env: NodeJS.ProcessEnv = process.env,
): boolean {
const host = String(headerValue(req.headers?.host) || '');
const origin = headerValue(req.headers?.origin);
const ports = allowedBrowserPorts(port, env);
const bindHost = env.OD_BIND_HOST || '127.0.0.1';
const extraAllowedOrigins = configuredAllowedOrigins(env);
const ipOnlyExtraOrigins = extraAllowedOrigins.filter((o) =>
isIpLiteralHostname(new URL(o).hostname),
);
const localHostAllowed = isAllowedBrowserHost(host, ports, bindHost, ipOnlyExtraOrigins);
if (origin == null || origin === '') {
if (localHostAllowed) return true;
// Browsers (Firefox, Chrome) omit Origin on same-origin GET subresource
// requests per the Fetch spec, which made hostname entries in
// OD_ALLOWED_ORIGINS unreachable for legitimate same-origin GETs
// through a reverse proxy. Sec-Fetch-Site is set by the user agent and
// cannot be modified by JavaScript, so a value of "same-origin"
// attests that the request originated from the same origin as the
// target — a cross-site `<img>`/`<script>` exploit would carry
// "cross-site" instead. Only consult the broader allow-list once that
// signal is present.
const fetchSite = headerValue(req.headers?.['sec-fetch-site']);
if (fetchSite === 'same-origin') {
return isAllowedBrowserHost(host, ports, bindHost, extraAllowedOrigins);
}
return false;
}
// Reverse-proxy deployments (e.g. Nginx in front of the daemon) terminate
// the browser connection at the proxy and open a fresh upstream
// connection to the daemon. The Host header the daemon sees is the
// proxy upstream's address, not the browser-visible origin, so the host
// check below fails even when the user explicitly listed their proxy
// origin in OD_ALLOWED_ORIGINS. Trust the Origin header in that case:
// a client-supplied origin that exactly matches an explicitly allow-
// listed entry is the documented escape hatch for these deployments.
if (extraAllowedOrigins.includes(origin)) return true;
if (!isAllowedBrowserHost(host, ports, bindHost, extraAllowedOrigins)) return false;
return isAllowedBrowserOrigin(origin, host, ports, bindHost, extraAllowedOrigins);
}
function headerValue(value: unknown): string | undefined {
if (Array.isArray(value)) {
const first = value[0];
return first == null ? undefined : String(first);
}
return value == null ? undefined : String(value);
}