mirror of
https://github.com/fosrl/pangolin.git
synced 2026-11-29 02:52:57 +00:00
AI provider upstreamUrl was only z.url() and the AI gateway streamed the upstream response back, so any org admin could read internal services, Pangolin's own internal API or the cloud metadata service (prod had a provider pointed at 169.254.169.254). Alert webhooks and HTTP log streaming fetched any URL and followed redirects. - server/lib/outboundAddress.ts (no config dependency, unit tested): always refuses loopback, unspecified, link-local, broadcast, multicast and reserved addresses (incl. IPv4-mapped forms), localhost and Pangolin's own service names; refuses RFC1918/ULA/CGNAT when private outbound is blocked. Provides a resolver check and a connect-time DNS lookup so rebinding cannot get around it. - server/lib/outboundUrlGuard.ts: policy from config. New flags.block_private_outbound_urls defaults to on for SaaS, off for self-hosted (local models). - aiGatewayUpstreamFetch checks the host and passes the guarded lookup to every upstream request; create/updateAiProvider reject blocked URLs. - sendAlertWebhook and HttpLogDestination check the URL before sending and no longer follow redirects. - Migration notes cover the flag and a query for providers that will stop working. PAN-1799 (3v9x, 4g5q)
104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
import http from "node:http";
|
|
import https from "node:https";
|
|
import { Readable } from "node:stream";
|
|
import {
|
|
getOutboundPolicy,
|
|
guardedLookup,
|
|
OutboundUrlError
|
|
} from "@server/lib/outboundUrlGuard";
|
|
import { outboundHostBlockReason } from "@server/lib/outboundAddress";
|
|
|
|
type UpstreamFetchInit = {
|
|
method: string;
|
|
headers: Record<string, string>;
|
|
body?: string;
|
|
skipTlsVerification?: boolean;
|
|
signal?: AbortSignal;
|
|
};
|
|
|
|
const insecureHttpsAgent = new https.Agent({
|
|
rejectUnauthorized: false,
|
|
keepAlive: true
|
|
});
|
|
|
|
export function aiGatewayUpstreamFetch(
|
|
url: string,
|
|
init: UpstreamFetchInit
|
|
): Promise<Response> {
|
|
const parsed = new URL(url);
|
|
// Provider URLs are tenant-supplied: never let them reach this host, the
|
|
// metadata service or (on SaaS) internal networks. The lookup re-checks
|
|
// the address actually connected to, so DNS changes cannot get around it.
|
|
const policy = getOutboundPolicy();
|
|
const hostReason = outboundHostBlockReason(parsed.hostname, policy);
|
|
if (hostReason) {
|
|
return Promise.reject(
|
|
new OutboundUrlError(`Upstream URL not allowed: ${hostReason}`)
|
|
);
|
|
}
|
|
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
return Promise.reject(
|
|
new OutboundUrlError("Upstream URL must be http or https")
|
|
);
|
|
}
|
|
const isHttps = parsed.protocol === "https:";
|
|
const lib = isHttps ? https : http;
|
|
const agent =
|
|
isHttps && init.skipTlsVerification ? insecureHttpsAgent : undefined;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (init.signal?.aborted) {
|
|
reject(init.signal.reason ?? new Error("Request aborted"));
|
|
return;
|
|
}
|
|
|
|
const req = lib.request(
|
|
url,
|
|
{
|
|
method: init.method,
|
|
headers: init.headers,
|
|
agent,
|
|
lookup: guardedLookup(policy)
|
|
},
|
|
(res) => {
|
|
const headers = new Headers();
|
|
for (const [key, value] of Object.entries(res.headers)) {
|
|
if (value === undefined) {
|
|
continue;
|
|
}
|
|
if (Array.isArray(value)) {
|
|
for (const entry of value) {
|
|
headers.append(key, entry);
|
|
}
|
|
} else {
|
|
headers.set(key, value);
|
|
}
|
|
}
|
|
|
|
const body = Readable.toWeb(res) as ReadableStream<Uint8Array>;
|
|
resolve(
|
|
new Response(body, {
|
|
status: res.statusCode ?? 502,
|
|
statusText: res.statusMessage,
|
|
headers
|
|
})
|
|
);
|
|
}
|
|
);
|
|
|
|
req.on("error", reject);
|
|
|
|
if (init.signal) {
|
|
const onAbort = () => req.destroy(init.signal!.reason);
|
|
init.signal.addEventListener("abort", onAbort, { once: true });
|
|
req.on("close", () =>
|
|
init.signal!.removeEventListener("abort", onAbort)
|
|
);
|
|
}
|
|
|
|
if (init.body !== undefined) {
|
|
req.write(init.body);
|
|
}
|
|
req.end();
|
|
});
|
|
}
|