openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 5.49 kB
JavaScript
import{o as registerSecretValueForRedaction}from"./secret-redaction-registry-DospXE4m.mjs";import{t as sha256Base64Url}from"./crypto-digest-T8X5EZni.mjs";import{i as generateSecureToken}from"./secure-random-CDFMDkJP.mjs";import{t as safeEqualSecret}from"./secret-equal-BtEZdDeQ.mjs";import{y as serializeWorkerSessionTurnClaim}from"./placement-record-DJqsOxnL.mjs";import{v as WORKER_RPC_SET_VERSION}from"./worker-admission-B_YnpbP9.mjs";import{n as sameWorkerProtocolFeatures,r as supportsCurrentWorkerLaunch}from"./worker-build-identity-K5xlOZic.mjs";import{t as isWorkerRetainedWindowOpen}from"./retained-work-window-Bnz1Bhmw.mjs";import{a as isWorkerEnvironmentAttachedTo}from"./placement-target-L09iynDq.mjs";
//!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;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}`)}function createWorkerCredentialMaterial(e=generateSecureToken,t){const n=e(WORKER_CREDENTIAL_BYTES);registerSecretValueForRedaction(n);return{credential:n,credentialHash:hashWorkerCredential(n,t)}}
//!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)||!isWorkerRetainedWindowOpen(n)||!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}}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||!isWorkerRetainedWindowOpen(o,e.nowMs))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}}}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}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};