openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
184 lines (183 loc) • 7.06 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { t as createDedupeCache } from "./dedupe-DxT0h2gR.mjs";
import { t as firstHeaderValue } from "./http-header-value-BTQlMYRm.mjs";
import { a as isLoopbackAddress, d as isTrustedProxyAddress, h as resolveClientIp, n as hasForwardedRequestHeaders, x as resolveRequestClientIpFromHeaders } from "./net-BgRayX-P.mjs";
//#region src/gateway/ingress-attribution.ts
const PROXY_ATTRIBUTION_REQUIRED_REASON = "proxy_attribution_required";
const PROXY_ATTRIBUTION_GUIDANCE = "Configure gateway.trustedProxies narrowly and make the proxy overwrite or safely rebuild forwarded client headers.";
const UNATTRIBUTABLE_PROXY_WARNING_WINDOW_MS = 3e5;
const UNATTRIBUTABLE_PROXY_WARNING_MAX_SOURCES = 16;
const requestTransport = /* @__PURE__ */ new WeakMap();
const preparedAttribution = /* @__PURE__ */ new WeakMap();
/** Records listener-owned provenance before any request-time policy runs. */
function markGatewayIngressTransport(e, t) {
const n = requestTransport.get(e);
if (n) {
if (n.kind !== t.kind || n.kind === "remote-forwarded" && t.kind === "remote-forwarded" && n.context !== t.context || n.kind === "managed-tailscale" && t.kind === "managed-tailscale" && n.mode !== t.mode) throw new Error("Gateway ingress transport was already assigned");
return;
}
if (preparedAttribution.has(e)) throw new Error("Gateway ingress policy already prepared before transport assignment");
requestTransport.set(e, Object.freeze({ ...t }));
}
function readGatewayIngressTransport(e) {
return requestTransport.get(e);
}
function getRemoteControlUiIngressContext(e) {
const t = readGatewayIngressTransport(e);
return t?.kind === "remote-forwarded" ? t.context : void 0;
}
function assertRemoteControlUiIngressCurrent(e) {
e?.signal.throwIfAborted();
e?.assertCurrent();
}
function hasCurrentRemoteControlUiIngress(e) {
try {
assertRemoteControlUiIngressCurrent(e);
return true;
} catch {
return false;
}
}
function unattributableProxy(e) {
return {
kind: "unattributable-proxy",
reason: PROXY_ATTRIBUTION_REQUIRED_REASON,
guidance: PROXY_ATTRIBUTION_GUIDANCE,
remoteAddress: e
};
}
function attributed(e, t) {
return {
kind: e,
clientIp: t,
rateLimit: {
subject: { key: t },
resetOnSuccess: true
}
};
}
function hasTailscaleProxyHeaders(e) {
const t = e.headers ?? {};
return Boolean(t["x-forwarded-for"] && t["x-forwarded-proto"] && t["x-forwarded-host"]);
}
function hasTailscaleOwnedHeaders(e) {
const t = e.headers ?? {};
return [
"tailscale-funnel-request",
"tailscale-headers-info",
"tailscale-user-login",
"tailscale-user-name",
"tailscale-user-profile-pic"
].some((e) => t[e] !== void 0);
}
function resolveTailscaleClientIp(e) {
return resolveClientIp({
remoteAddr: e.socket?.remoteAddress,
forwardedFor: firstHeaderValue(e.headers?.["x-forwarded-for"]),
trustedProxies: ["127.0.0.1", "::1"]
});
}
function resolveManagedTailscaleIngress(e) {
const { req: t, mode: n, remoteAddress: r, tailscaleWhois: i } = e;
if (!isLoopbackAddress(r) || !hasTailscaleProxyHeaders(t)) return unattributableProxy(r);
const a = resolveTailscaleClientIp(t);
if (!a || isLoopbackAddress(a)) return unattributableProxy(r);
const o = firstHeaderValue(t.headers?.["tailscale-funnel-request"]);
if (n === "funnel") return !o || o === "?1" ? attributed("tailscale-funnel", a) : unattributableProxy(r);
if (o) return unattributableProxy(r);
const s = normalizeOptionalString(t.headers?.["tailscale-user-login"]);
const c = normalizeOptionalString(t.headers?.["tailscale-user-name"]);
const l = normalizeOptionalString(t.headers?.["tailscale-user-profile-pic"]);
let u;
const verifyIdentity = () => {
if (!s) return Promise.resolve(void 0);
u ??= (async () => {
try {
const e = await i(a);
if (!e?.login || e.login.toLowerCase() !== s.toLowerCase()) return;
return {
login: e.login,
name: e.name ?? c ?? e.login,
...l ? { profilePic: l } : {}
};
} catch {
return;
}
})();
return u;
};
return {
...attributed("tailscale-serve", a),
verifyIdentity
};
}
function resolveGatewayIngressAttribution(e) {
const { req: t } = e;
const n = resolveClientIp({ remoteAddr: t.socket?.remoteAddress }) ?? t.socket?.remoteAddress ?? "unknown";
const r = requestTransport.get(t) ?? { kind: "ordinary" };
if (r.kind === "remote-forwarded") return Object.freeze({
kind: "remote-forwarded",
clientIp: n,
rateLimit: Object.freeze({
subject: Object.freeze({ key: `remote-control-ui:${r.context.pluginId}` }),
resetOnSuccess: true
})
});
if (r.kind === "managed-tailscale") return resolveManagedTailscaleIngress({
req: t,
mode: r.mode,
remoteAddress: n,
tailscaleWhois: e.tailscaleWhois ?? (async (e) => {
const { readTailscaleWhoisIdentity: t } = await import("./tailscale-B64A77jm.mjs");
return t(e);
})
});
const i = hasForwardedRequestHeaders(t);
const a = hasTailscaleOwnedHeaders(t);
if (isLoopbackAddress(n) && !i && !a) return attributed("direct-local", n);
if (isTrustedProxyAddress(n, e.trustedProxies)) {
const r = resolveRequestClientIpFromHeaders(t, e.trustedProxies, e.allowRealIpFallback === true);
if (!r || isLoopbackAddress(r)) return unattributableProxy(n);
return {
...attributed("trusted-proxy", r),
...firstHeaderValue(t.headers?.["tailscale-funnel-request"]) === "?1" ? { externalTailscaleExposure: "funnel" } : {}
};
}
if (i || a) return unattributableProxy(n);
return attributed("direct-remote", n);
}
function prepareGatewayIngressAttribution(e) {
const t = preparedAttribution.get(e.req);
if (t) return t;
const n = resolveGatewayIngressAttribution(e);
preparedAttribution.set(e.req, n);
return n;
}
function readPreparedGatewayIngressAttribution(e) {
return preparedAttribution.get(e);
}
/** Emits bounded, per-source warnings so one rejected peer cannot hide every later peer. */
function createGatewayUnattributableProxyReporter(e) {
const t = createDedupeCache({
ttlMs: UNATTRIBUTABLE_PROXY_WARNING_WINDOW_MS,
maxSize: 32
});
let n = Date.now();
let r = n;
let i = 0;
return (a) => {
const o = Date.now();
if (o < r || o - n >= UNATTRIBUTABLE_PROXY_WARNING_WINDOW_MS) {
n = o;
i = 0;
if (o < r) t.clear();
}
r = o;
if (i >= UNATTRIBUTABLE_PROXY_WARNING_MAX_SOURCES || t.peek(a.remoteAddress, o)) return;
t.check(a.remoteAddress, o);
i += 1;
e.warn(`gateway: observed unattributable proxy-shaped traffic from ${a.remoteAddress}; Gateway-authenticated routes reject it, while plugin-authenticated routes ignore forwarded claims. ${a.guidance}`);
};
}
//#endregion
export { getRemoteControlUiIngressContext as a, prepareGatewayIngressAttribution as c, createGatewayUnattributableProxyReporter as i, readGatewayIngressTransport as l, PROXY_ATTRIBUTION_REQUIRED_REASON as n, hasCurrentRemoteControlUiIngress as o, assertRemoteControlUiIngressCurrent as r, markGatewayIngressTransport as s, PROXY_ATTRIBUTION_GUIDANCE as t, readPreparedGatewayIngressAttribution as u };