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
295 lines (274 loc) · 11 KB
/
Copy pathorigin-validation.ts
File metadata and controls
295 lines (274 loc) · 11 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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);
}
// Issue #3225 — operator-declared allowlist of internal hosts that are exempt
// from the default-deny SSRF guard for USER-CONFIGURED provider endpoints (an
// internally-hosted LiteLLM/Ollama on an RFC1918 address reachable only over
// VPN). Comma- or whitespace-separated; each entry may be a bare host
// (`10.0.0.5`), `host:port`, or a full URL — only the hostname is retained,
// since the SSRF block is host-based. IPv6 literals must be bracketed
// (`[fd00::1]` or `[fd00::1]:4000`) so the port is unambiguous; the brackets
// are stripped here so the result compares directly against a resolved
// address. An empty/unset value yields `[]`, preserving the strict default
// for every deployment that does not opt in.
//
// A malformed entry — or CIDR notation, which the host-based matcher cannot
// honor — is dropped with a warning rather than silently trusted, so a typo
// can never quietly widen (or quietly fail to widen) the guard. This list is
// threaded ONLY into the user-configured-endpoint validators, never the
// attacker-controllable asset-download guard (`assertExternalAssetUrl`).
export function configuredAllowedInternalHosts(
env: NodeJS.ProcessEnv = process.env,
warn: (message: string) => void = (message) => console.warn(message),
): string[] {
const raw = env.OD_ALLOWED_INTERNAL_HOSTS || '';
if (!raw.trim()) return [];
const hosts: string[] = [];
for (const part of raw.split(/[,\s]+/)) {
const entry = part.trim();
if (!entry) continue;
// A bare `10.0.0.0/24` parses as host `10.0.0.0` + path `/24`, so keeping
// it would silently trust a single network address the operator never
// meant. Reject loudly. (A full URL whose path happens to look numeric
// still carries a scheme, so exclude those from the CIDR heuristic.)
if (!entry.includes('://') && /^[^/]+\/\d{1,3}$/.test(entry)) {
warn(
`[ssrf] ignoring CIDR entry in OD_ALLOWED_INTERNAL_HOSTS: ${JSON.stringify(entry)} — list individual hosts instead`,
);
continue;
}
let hostname = '';
try {
const url = new URL(entry.includes('://') ? entry : `http://${entry}`);
hostname = url.hostname.toLowerCase();
if (hostname.startsWith('[') && hostname.endsWith(']')) {
hostname = hostname.slice(1, -1);
}
} catch {
hostname = '';
}
if (!hostname) {
warn(
`[ssrf] ignoring malformed OD_ALLOWED_INTERNAL_HOSTS entry: ${JSON.stringify(entry)}`,
);
continue;
}
hosts.push(hostname);
}
return hosts;
}
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);
}
/**
* Zero-config OD Clipper bypass for the `/api` origin middleware.
*
* `apiRelativePath` MUST be `req.path` as observed INSIDE `app.use('/api', …)`.
* Express strips the mounted `/api` prefix there, so a request to
* `/api/library/ingest` arrives as `/library/ingest`. Matching `'/library/'`
* is therefore the correct (and only working) prefix — matching
* `'/api/library/'` here would never fire because the mount prefix is gone.
*
* Returns true only for a locally-installed browser extension (an origin a web
* page cannot forge) targeting the narrow OD Clipper bootstrap surface: the
* dedicated probe route and the ingest endpoint. Library reads still require
* normal same-origin / allow-list validation so an unrelated installed
* extension cannot enumerate or download the user's library.
*/
export function isZeroConfigClipperLibraryRequest(
method: string,
apiRelativePath: string,
origin: unknown,
): boolean {
const normalizedMethod = method.toUpperCase();
const isAllowedClipperPath =
(normalizedMethod === 'GET' && apiRelativePath === '/library/clipper-probe') ||
((normalizedMethod === 'OPTIONS' || normalizedMethod === 'POST') &&
apiRelativePath === '/library/ingest');
if (!isAllowedClipperPath) return false;
return (
typeof origin === 'string' &&
(origin.startsWith('chrome-extension://') || origin.startsWith('moz-extension://'))
);
}