openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 6.91 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{t as createDedupeCache}from"./dedupe-CsfmSvUH.mjs";import{a as getRuntimeConfigSnapshot}from"./runtime-snapshot-Bg5K4JNu.mjs";import{n as isOpenClawIdEnabled}from"./openclaw-id-policy-CE_0QXmB.mjs";import{t as firstHeaderValue}from"./http-header-value-D4bOEsPJ.mjs";import{a as isLoopbackAddress,d as isTrustedProxyAddress,h as resolveClientIp,n as hasForwardedRequestHeaders,x as resolveRequestClientIpFromHeaders}from"./net-Bu4cO5Pq.mjs";
//!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;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||n.kind===`openclaw-id-tunnel`&&t.kind===`openclaw-id-tunnel`&&n.peerKey!==t.peerKey)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 hasGatewayProxyIdentityHeaders(e,t){return[`cf-access-authenticated-user-email`,`cf-access-jwt-assertion`,`remote-user`,`x-remote-user`,t?.userHeader,...t?.requiredHeaders??[]].some(t=>t&&e.headers[t.trim().toLowerCase()]!==void 0)}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===`openclaw-id-tunnel`)return Object.freeze({kind:`openclaw-id-tunnel`,clientIp:r.clientIp,peerKey:r.peerKey,rateLimit:Object.freeze({subject:Object.freeze({key:`openclaw-id:${r.peerKey}`}),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-DYshR0NK.mjs`);return t(e)})});const i=hasForwardedRequestHeaders(t);const a=hasTailscaleOwnedHeaders(t);const o=isOpenClawIdEnabled(getRuntimeConfigSnapshot()?.gateway?.openclawId)&&hasGatewayProxyIdentityHeaders(t,e.trustedProxy);if(isLoopbackAddress(n)&&!i&&!a&&!o)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||o)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)}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}`)}}export{getRemoteControlUiIngressContext as a,markGatewayIngressTransport as c,readPreparedGatewayIngressAttribution as d,createGatewayUnattributableProxyReporter as i,prepareGatewayIngressAttribution as l,PROXY_ATTRIBUTION_REQUIRED_REASON as n,hasCurrentRemoteControlUiIngress as o,assertRemoteControlUiIngressCurrent as r,hasGatewayProxyIdentityHeaders as s,PROXY_ATTRIBUTION_GUIDANCE as t,readGatewayIngressTransport as u};