UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

162 lines (161 loc) • 7.17 kB
import { t as isAbortError } from "./abort-signal-D4Q6SEw7.mjs"; import { o as getAsyncWorkSignal, t as AsyncWorkScope } from "./async-work-scope-BXagjUap.mjs"; import { o as emitInternalDiagnosticEvent } from "./diagnostic-events-BjAH1Kbx.mjs"; import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import { m as readUserProfileAliasRevision } from "./user-profile-events-BhLFevt9.mjs"; import "./io-4QsrsWTJ.mjs"; import { u as readOperatorRolePolicyRevision } from "./operator-role-policy-DhcqVpmG.mjs"; import { t as ToolAuthorizationError } from "./tool-input-error-B1hgavSw.mjs"; import { a as getRemoteControlUiIngressContext, r as assertRemoteControlUiIngressCurrent } from "./ingress-attribution-DAAQgd6y.mjs"; import { d as runHttpConnectionRequest, l as createHttpRequestAbortSignal, p as waitForHttpRequestRejection } from "./http-body-B9w7rzPz.mjs"; import { r as sourceRolePolicies } from "./operator-role-source-policy-CRYRV_Bv.mjs"; import { n as GatewayOperatorAccessDeniedError, r as hasCurrentGatewayOperatorAccess, t as GATEWAY_OPERATOR_ACCESS_DENIED_MESSAGE } from "./operator-access-policy-BWvd6twL.mjs"; import { r as isGatewayAuthPolicyCurrent, t as captureGatewayAuthPolicy } from "./auth-policy-CR6nKvhI.mjs"; import { t as resolveSharedGatewaySessionGeneration } from "./ws-shared-generation-BWsSp-Zl.mjs"; import { t as sendJson } from "./http-json-ddGJz5CA.mjs"; import { h as sendUnauthorized, l as sendGatewayAuthFailure, o as prepareGatewayHttpErrorResponse } from "./http-common-FfhIIwOW.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; import { isDeepStrictEqual } from "node:util"; //#region src/gateway/http-operator-access.ts function sendGatewayHttpAuthFailure(e, t) { if (t.reason !== "operator_access_denied") { sendGatewayAuthFailure(e, t); return; } if (!prepareGatewayHttpErrorResponse(e, "Forbidden")) return; sendJson(e, 403, { error: { message: GATEWAY_OPERATOR_ACCESS_DENIED_MESSAGE, type: "forbidden" } }); } /** The listener belongs to this response, never to a reusable keep-alive socket. */ function bindHttpOperatorAccessAuthority(e, t, n = true) { if (!t) return true; if (!hasCurrentGatewayOperatorAccess(t)) { if (n && !e.writableEnded && !e.destroyed) sendGatewayHttpAuthFailure(e, { ok: false, reason: "operator_access_denied" }); return false; } if (e.writableEnded || e.destroyed) return false; const release = () => { t.signal.removeEventListener("abort", revoke); e.off("finish", release); e.off("close", release); }; const revoke = () => { release(); if (!e.writableEnded && !e.destroyed) e.destroy(); }; t.signal.addEventListener("abort", revoke, { once: true }); e.once("finish", release); e.once("close", release); return true; } //#endregion //#region src/gateway/http-request-authority.ts /** Replaces startup custody with the live Gateway and one disconnect-aware request scope. */ function runGatewayHttpRequest(e, t, n, r) { if (n?.requestEntryLifetime?.signal.aborted) { if (!t.writableFinished && !t.destroyed) t.destroy(); return Promise.resolve(); } const run = async () => { const i = getRemoteControlUiIngressContext(e); assertRemoteControlUiIngressCurrent(i); const a = new AsyncWorkScope(); const o = a.run(() => AsyncLocalStorage.snapshot()); const s = createHttpRequestAbortSignal(e, t); const c = n?.requestEntryLifetime?.signal; const l = AbortSignal.any([ s.signal, ...c ? [c] : [], ...i ? [i.signal] : [] ]); let u; const cancel = () => o(() => { u = s.signal.aborted ? "client" : "shutdown"; a.beginClose(l.reason); if (u === "shutdown" && !t.writableFinished && !t.destroyed) t.destroy(); }); l.addEventListener("abort", cancel, { once: true }); if (l.aborted) cancel(); let d = false; try { await a.track(() => runHttpConnectionRequest(e, async () => { d = await r() === "failed"; }, t)); } catch (e) { d = true; throw e; } finally { l.removeEventListener("abort", cancel); s.cleanup(); if (u && !d && t.statusCode < 500 && !(u === "client" && waitForHttpRequestRejection(e))) emitInternalDiagnosticEvent({ type: "gateway.http.cancelled", source: u }); await o(() => a.drain()); } }; return n ? n.trackExecution(run) : run(); } /** Authority ended; its response has already been answered or closed. */ var GatewayHttpRequestAuthorityError = class extends Error {}; /** Request owners consume authority outcomes; unexpected failures keep their own diagnostics. */ function finishGatewayHttpAuthorityError(e, t) { const n = getAsyncWorkSignal(); if (n?.aborted && (t === n.reason || isAbortError(t))) { if (!e.writableEnded && !e.destroyed) e.destroy(); return true; } if (t instanceof GatewayOperatorAccessDeniedError) { sendGatewayHttpAuthFailure(e, { ok: false, reason: "operator_access_denied" }); return true; } return t instanceof GatewayHttpRequestAuthorityError; } function assertGatewayHttpRequestCurrent(e) { if (e.hasCurrentClientAuthority?.() === false) throw new ToolAuthorizationError("Gateway requester authority changed"); } function captureHttpRequestAuthority(e) { const t = e.cfg ?? readRuntimeConfig(); const n = captureGatewayAuthPolicy(t, null); const r = resolveSharedGatewaySessionGeneration(e.auth, e.trustedProxies ?? t.gateway?.trustedProxies); const i = t.gateway?.roles; const a = sourceRolePolicies(i); const o = i ? readOperatorRolePolicyRevision() : void 0; const s = readUserProfileAliasRevision(); const c = getRemoteControlUiIngressContext(e.req); return () => { if (c) try { assertRemoteControlUiIngressCurrent(c); } catch { return false; } const t = e.getRuntimeConfig?.() ?? readRuntimeConfig(); return !e.req.socket?.destroyed && isGatewayAuthPolicyCurrent(n, t) && (i === t.gateway?.roles || isDeepStrictEqual(a, sourceRolePolicies(t.gateway?.roles))) && (o === void 0 || o === readOperatorRolePolicyRevision()) && s === readUserProfileAliasRevision() && r === resolveSharedGatewaySessionGeneration(e.getResolvedAuth?.() ?? e.auth, e.trustedProxies ?? t.gateway?.trustedProxies); }; } function bindHttpResponseAuthority(e, t, n) { const assertCurrent = () => { if (t.writableEnded || t.destroyed) throw new GatewayHttpRequestAuthorityError("HTTP request authority expired"); if (!hasCurrentGatewayOperatorAccess(e.operatorAccessAuthority)) throw new GatewayOperatorAccessDeniedError(); if (!n() || e.hasCurrentProfileAuthority?.() === false) { sendUnauthorized(t); throw new GatewayHttpRequestAuthorityError("Unauthorized"); } }; return { ...e, hasCurrentClientAuthority: () => !t.writableEnded && !t.destroyed && n() && e.hasCurrentProfileAuthority?.() !== false && hasCurrentGatewayOperatorAccess(e.operatorAccessAuthority), assertCurrent, revalidate: async () => assertCurrent() }; } //#endregion export { finishGatewayHttpAuthorityError as a, sendGatewayHttpAuthFailure as c, captureHttpRequestAuthority as i, assertGatewayHttpRequestCurrent as n, runGatewayHttpRequest as o, bindHttpResponseAuthority as r, bindHttpOperatorAccessAuthority as s, GatewayHttpRequestAuthorityError as t };