UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

134 lines (133 loc) • 6.38 kB
import { o as registerSecretValueForRedaction } from "./secret-redaction-registry-Cy0acctn.mjs"; import { t as sha256Base64Url } from "./crypto-digest-B2m8wIBj.mjs"; import { i as generateSecureToken } from "./secure-random-CW5OZnK1.mjs"; import { t as safeEqualSecret } from "./secret-equal-BogABOFr.mjs"; import { y as serializeWorkerSessionTurnClaim } from "./placement-record-Hs-bCbCm.mjs"; import { _ as WORKER_RPC_SET_VERSION } from "./worker-admission-1P4EM7n_.mjs"; import { n as sameWorkerProtocolFeatures, r as supportsCurrentWorkerLaunch } from "./worker-build-identity-BrfBomn8.mjs"; import { a as isWorkerEnvironmentAttachedTo } from "./placement-target-pO1cgncb.mjs"; //#region src/gateway/worker-environments/credential.ts const WORKER_CREDENTIAL_TTL_MS = 6e5; const WORKER_CREDENTIAL_HASH_DOMAIN = "openclaw-worker-credential-v1\0"; const WORKER_TURN_CREDENTIAL_HASH_DOMAIN = "openclaw-worker-turn-credential-v1\0"; const WORKER_CREDENTIAL_BYTES = 32; /** Hash opaque worker credentials with their exact durable authority before persistence. */ function hashWorkerCredential(e, t) { if (!t) return sha256Base64Url(`${WORKER_CREDENTIAL_HASH_DOMAIN}${e}`); const n = serializeWorkerSessionTurnClaim(t); return sha256Base64Url(`${WORKER_TURN_CREDENTIAL_HASH_DOMAIN}${n}\0${e}`); } /** Generate one high-entropy credential. Plaintext is returned only to its delivery owner. */ function createWorkerCredentialMaterial(e = generateSecureToken, t) { const n = e(WORKER_CREDENTIAL_BYTES); registerSecretValueForRedaction(n); return { credential: n, credentialHash: hashWorkerCredential(n, t) }; } //#endregion //#region src/gateway/worker-environments/admission.ts const STALE_WORKER_BUILD_REASON = "Worker build does not match the current Gateway build; redispatch the session so its worker can bootstrap the current build before retrying."; var StaleWorkerBuildError = class extends Error { constructor() { super(STALE_WORKER_BUILD_REASON); this.code = "invalid_state"; } }; function requireCurrentWorkerTurnEnvironment(e) { const { placement: t } = e; const n = e.environments.get(t.environmentId); const r = n?.bootstrapReceipt; if (n?.error === "Worker build does not match the current Gateway build; redispatch the session so its worker can bootstrap the current build before retrying.") throw new StaleWorkerBuildError(); if (!isWorkerEnvironmentAttachedTo(n, t) || !r || r.bundleHash !== t.workerBundleHash) throw new Error("Active worker placement does not match its attached environment"); if (!supportsCurrentWorkerLaunch(r)) throw new Error("Active worker bundle lacks the current launch capability; reprovision the worker before launch"); return { environment: n, bootstrapReceipt: r }; } /** Validate an opaque credential and every server-owned worker admission binding. */ function admitWorkerConnection(e) { const { admission: t, store: n } = e; const r = e.turnClaim; const i = hashWorkerCredential(t.credential, r); const a = n.getCredential(t.environmentId); if (!a || !safeEqualSecret(i, a.credentialHash)) return { ok: false, reason: n.findCredentialByHash(i) ? "environment-mismatch" : "invalid-credential" }; if (a.environmentId !== t.environmentId) return { ok: false, reason: "environment-mismatch" }; if (t.sessionId !== null) { if (!r || r.owner.kind !== "worker" || r.sessionId !== t.sessionId || r.runId !== t.runId || r.owner.environmentId !== t.environmentId || r.owner.ownerEpoch !== t.ownerEpoch) return { ok: false, reason: "placement-mismatch" }; } else if (r || t.runId !== null) return { ok: false, reason: "session-mismatch" }; if (e.nowMs >= a.expiresAtMs && e.allowExpiredCredential !== true) return { ok: false, reason: "credential-expired" }; const o = n.get(t.environmentId); if (!o || o.state !== "ready" && o.state !== "idle" && o.state !== "attached" || o.destroyRequestedAtMs !== null || !o.bootstrapReceipt) return { ok: false, reason: "environment-unavailable" }; if (t.handshake.bundleHash !== a.bundleHash || t.handshake.bundleHash !== o.bootstrapReceipt.bundleHash || t.handshake.bundleHash !== e.expectedBuild.bundleHash) return { ok: false, reason: "bundle-mismatch" }; if (t.handshake.openclawVersion !== o.bootstrapReceipt.openclawVersion || t.handshake.openclawVersion !== e.expectedBuild.openclawVersion) return { ok: false, reason: "version-mismatch" }; if (t.sessionId !== a.sessionId) return { ok: false, reason: "session-mismatch" }; if (t.ownerEpoch !== a.ownerEpoch || t.ownerEpoch !== o.ownerEpoch) return { ok: false, reason: "owner-epoch-mismatch" }; if (t.rpcSetVersion !== a.rpcSetVersion || a.rpcSetVersion !== 1) return { ok: false, reason: "rpc-set-mismatch" }; if (!sameWorkerProtocolFeatures(t.handshake.protocolFeatures, o.bootstrapReceipt.protocolFeatures) || !sameWorkerProtocolFeatures(t.handshake.protocolFeatures, e.expectedBuild.protocolFeatures)) return { ok: false, reason: "protocol-features-mismatch" }; return { ok: true, identity: { environmentId: o.environmentId, credentialHash: a.credentialHash, bundleHash: a.bundleHash, sessionId: a.sessionId, runId: t.runId, turnClaim: r ?? null, ownerEpoch: a.ownerEpoch, rpcSetVersion: a.rpcSetVersion, protocolFeatures: [...o.bootstrapReceipt.protocolFeatures], credentialExpiresAtMs: a.expiresAtMs } }; } /** Revalidate live ownership on every worker RPC so rotation and expiry fence stale sockets. */ function validateWorkerConnectionIdentity(e) { const t = e.store.getCredential(e.identity.environmentId); if (!t || !safeEqualSecret(t.credentialHash, e.identity.credentialHash)) return "credential-replaced"; if (e.nowMs >= t.expiresAtMs) return "credential-expired"; const n = e.store.get(e.identity.environmentId); if (!n || n.state !== "ready" && n.state !== "idle" && n.state !== "attached" || n.destroyRequestedAtMs !== null) return "environment-unavailable"; if (n.ownerEpoch !== e.identity.ownerEpoch || t.ownerEpoch !== e.identity.ownerEpoch) return "owner-epoch-mismatch"; return null; } //#endregion export { validateWorkerConnectionIdentity as a, hashWorkerCredential as c, requireCurrentWorkerTurnEnvironment as i, StaleWorkerBuildError as n, WORKER_CREDENTIAL_TTL_MS as o, admitWorkerConnection as r, createWorkerCredentialMaterial as s, STALE_WORKER_BUILD_REASON as t };