UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

193 lines (192 loc) • 6.25 kB
import { l as resolveSecretSentinel, s as looksLikeSecretSentinel } from "./sentinel-BfPjneyJ.mjs"; import crypto from "node:crypto"; //#region src/agents/execution-auth-binding.ts const authBindingFingerprintKey = crypto.randomBytes(32); function hashAuthBinding(e) { return crypto.createHmac("sha256", authBindingFingerprintKey).update(JSON.stringify(e)).digest("hex"); } function normalizeIdentity(e, t = false) { const n = e?.trim(); return n ? t ? n.toLowerCase() : n : void 0; } /** * Project non-secret profile ownership for runtimes that keep rotating tokens * behind their own process boundary. An explicitly selected missing profile * has no owner shape and must never collapse to ambient runtime authority. */ function fingerprintAuthProfileOwnerShape(e) { const t = e.credential; if (!t) return; switch (t.type) { case "api_key": case "token": return hashAuthBinding([ "profile-owner-v1", e.profileId, t.type, t.provider, (t.type === "api_key" ? t.keyRef : t.tokenRef) ?? null, normalizeIdentity(t.email, true) ?? null, normalizeIdentity(t.displayName) ?? null, ...t.type === "api_key" ? [t.metadata ?? null] : [] ]); case "oauth": { const n = decodeJwtIdentity(t.idToken); return hashAuthBinding([ "profile-owner-v1", e.profileId, t.type, t.provider, normalizeIdentity(t.accountId) ?? n.subject ?? null, normalizeIdentity(t.email, true) ?? n.email ?? null, t.clientId ?? null, t.enterpriseUrl ?? null, t.projectId ?? null ]); } } } /** Fingerprint the stable owner boundary of a successful opaque runtime turn. */ function fingerprintOpaqueRuntimeOwner(e) { const t = e.runtimeArtifactFingerprint; const n = normalizeIdentity(e.authProfileId); if (n && !e.authProfileOwnerFingerprint) return; if (!n && e.skipLocalCredential) return; if ((e.kind === "cli-runtime" || e.kind === "plugin-harness") && !t) return; return hashAuthBinding([ e.kind === "aws-sdk" ? "opaque-runtime-owner-v1" : "opaque-runtime-owner-v2", e.kind, e.runner, e.provider.trim(), e.backendId, e.backendConfig ?? null, n ?? null, e.authProfileOwnerFingerprint ?? null, e.authSource ?? null, e.skipLocalCredential === true, t ?? null ]); } /** Fingerprint only AWS SDK owners whose exact credential is observable here. */ function fingerprintAwsSdkRuntimeOwner(e) { if (e.auth?.mode !== "aws-sdk" || e.auth.apiKey) return; const t = e.env ?? process.env; let n; if (t.AWS_BEARER_TOKEN_BEDROCK?.trim()) n = ["bearer", hashAuthBinding(t.AWS_BEARER_TOKEN_BEDROCK.trim())]; else if (t.AWS_PROFILE?.trim()) return; else if (t.AWS_ACCESS_KEY_ID?.trim() && t.AWS_SECRET_ACCESS_KEY?.trim()) n = [ "access-key", t.AWS_ACCESS_KEY_ID.trim(), hashAuthBinding([t.AWS_SECRET_ACCESS_KEY.trim(), t.AWS_SESSION_TOKEN?.trim() ?? null]) ]; else return; return fingerprintOpaqueRuntimeOwner({ kind: "aws-sdk", runner: "embedded", provider: e.provider, backendId: e.backendId, authSource: hashAuthBinding([e.auth.source, n]) }); } function decodeJwtIdentity(e) { const t = e?.split(".")[1]; if (!t) return {}; try { const e = JSON.parse(Buffer.from(t, "base64url").toString("utf8")); return { subject: typeof e.sub === "string" ? normalizeIdentity(e.sub) : void 0, email: typeof e.email === "string" ? normalizeIdentity(e.email, true) : void 0 }; } catch { return {}; } } /** Fingerprint the exact active credential owner used by one execution. */ function fingerprintAuthProfileCredential(e) { const t = e.credential; switch (t.type) { case "api_key": case "token": { const n = t.type === "api_key" ? t.key : t.token; if (!n) return; return hashAuthBinding([ t.type, e.profileId, t.provider, n, (t.type === "api_key" ? t.keyRef : t.tokenRef) ?? null, t.email ?? null, t.displayName ?? null, ...t.type === "api_key" ? [t.metadata ?? null] : [] ]); } case "oauth": { const n = decodeJwtIdentity(t.idToken); const r = normalizeIdentity(t.accountId) ?? n.subject; const i = normalizeIdentity(t.email, true) ?? n.email; const a = r ?? i; const o = a ? null : [ t.access, t.refresh, t.idToken ?? null ]; if (!a && !t.access && !t.refresh && !t.idToken) return; return hashAuthBinding([ "oauth", e.profileId, t.provider, t.clientId ?? null, r ?? null, i ?? null, t.enterpriseUrl ?? null, t.projectId ?? null, o ]); } } } /** Fingerprint a profile after materializing its selected SecretRef value. */ function fingerprintResolvedAuthProfileCredential(e) { const t = e.credential; if (t.type === "oauth") return fingerprintAuthProfileCredential({ profileId: e.profileId, credential: t }); if (e.resolvedAuth && e.resolvedAuth.profileId !== e.profileId) return; const n = t.type === "api_key" ? t.key : t.token; const r = e.resolvedAuth?.apiKey ?? n; if (!r) return; return fingerprintAuthProfileCredential({ profileId: e.profileId, credential: t.type === "api_key" ? { ...t, key: r } : { ...t, token: r } }); } /** * Fingerprint an ambient/config/env credential that was actually selected. * * The digest covers only the credential material and its transport mode. * Resolution-path facts (`source`, `profileId`) are deliberately excluded: * the successful-run capture and the later owner revalidation may resolve * the same key through different sources (env, models.json marker, profile * fallback), and the owner gate must treat that as the same authority. * A sentinel-sealed key is unwrapped first so both passes hash the same * plaintext. Route and provider identity are checked separately by the * binding, so a shared key remains one authority rather than two. */ function fingerprintResolvedProviderAuth(e) { if (!e?.apiKey) return; const t = looksLikeSecretSentinel(e.apiKey) ? resolveSecretSentinel(e.apiKey) : e.apiKey; if (!t) return; return hashAuthBinding([ "resolved-v2", e.mode, t ]); } //#endregion export { fingerprintResolvedAuthProfileCredential as a, fingerprintOpaqueRuntimeOwner as i, fingerprintAuthProfileOwnerShape as n, fingerprintResolvedProviderAuth as o, fingerprintAwsSdkRuntimeOwner as r, fingerprintAuthProfileCredential as t };