openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 8.31 kB
JavaScript
import{t as hasExactKeys}from"./strict-json-BAbyVgWi.mjs";import crypto from"node:crypto";
//!extensions/browser/chrome-extension/modules/relay-auth-v2-crypto.js
const RELAY_AUTH_LABEL=`openclaw.browser-relay.auth`;const RELAY_AUTH_VERSION=2;const textEncoder=new TextEncoder;function canonicalRelayAuthProofBytes(e,t,n){if(e!==`server`&&e!==`client`&&e!==`accept`)throw new Error(`invalid relay auth proof kind`);const r=[RELAY_AUTH_LABEL,2,e,t.keyId,t.instanceId,t.sessionId,t.clientNonce,t.serverNonce,t.issuedAtMs,t.expiresAtMs,t.role,t.transport,t.method,t.resource,t.flow];if(e===`accept`){if(typeof n!==`string`)throw new Error(`accept proof requires clientProof`);r.push(n)}return textEncoder.encode(JSON.stringify(r))}
//!extensions/browser/src/browser/extension-relay/auth-v2-crypto.ts
const KEY_HEX_PATTERN=/^[0-9a-f]{64}$/u;const BASE64URL_PATTERN=/^[A-Za-z0-9_-]+$/u;function decodeRelayKey(e){if(!KEY_HEX_PATTERN.test(e))throw new Error(`browser relay key must be 32 lowercase-hex bytes`);return Buffer.from(e,`hex`)}function isCanonicalBase64UrlBytes(e,t){if(typeof e!==`string`||!BASE64URL_PATTERN.test(e))return false;try{const n=Buffer.from(e,`base64url`);return n.length===t&&n.toString(`base64url`)===e}catch{return false}}function isBase64UrlText(e){return BASE64URL_PATTERN.test(e)}function relayKeyIdFromHex(e){return crypto.createHash(`sha256`).update(decodeRelayKey(e)).digest(`base64url`).slice(0,22)}function randomRelayNonce(){return crypto.randomBytes(32).toString(`base64url`)}function randomRelayId(){return crypto.randomBytes(16).toString(`base64url`)}function createRelayProof(e,t,n,r){const i=crypto.createHmac(`sha256`,decodeRelayKey(e));if(t===`accept`&&!isCanonicalBase64UrlBytes(r,32))throw new Error(`accept proof requires a 32-byte client proof`);return i.update(canonicalRelayAuthProofBytes(t,n,r)).digest(`base64url`)}function verifyRelayProof(e,t,n,r,i){if(!isCanonicalBase64UrlBytes(r,32))return false;const a=Buffer.from(createRelayProof(e,t,n,i),`base64url`);const o=Buffer.from(r,`base64url`);return o.length===a.length&&crypto.timingSafeEqual(o,a)}
//!extensions/browser/src/browser/extension-relay/auth-v2.ts
const BROWSER_RELAY_EXTENSION_SUBPROTOCOL=`openclaw-extension-relay.v2`;const BROWSER_RELAY_AUTH_CHALLENGE_PATH=`/_openclaw/relay/auth/v2/challenge`;const BROWSER_RELAY_AUTH_COMPLETE_PATH=`/_openclaw/relay/auth/v2/complete`;const BROWSER_RELAY_CHALLENGE_TTL_MS=1e4;const MAX_PENDING_AUTH_CONNECTIONS=128;const MAX_PENDING_AUTH_CONNECTIONS_PER_SOURCE=32;const MAX_AUTHENTICATED_CONNECTIONS=128;const MAX_REPLAY_ENTRIES=1024;const MAX_REPLAY_ENTRIES_PER_SOURCE=256;const LOOPBACK_AUTH_SOURCE=`loopback`;function normalizeAuthSource(e){const t=e.trim().toLowerCase();if(t===`::1`||/^127(?:\.\d{1,3}){3}$/.test(t)||/^::ffff:127(?:\.\d{1,3}){3}$/.test(t))return LOOPBACK_AUTH_SOURCE;return t||`unknown`}function parseRelayAuthHello(e){if(!hasExactKeys(e,[`type`,`v`,`keyId`,`clientNonce`])||e.type!==`auth.hello`||e.v!==2||typeof e.keyId!==`string`||e.keyId.length!==22||!isBase64UrlText(e.keyId)||!isCanonicalBase64UrlBytes(e.clientNonce,32))return null;return e}function parseRelayAuthResponse(e){if(!hasExactKeys(e,[`type`,`v`,`sessionId`,`clientProof`])||e.type!==`auth.response`||e.v!==2||!isCanonicalBase64UrlBytes(e.sessionId,16)||!isCanonicalBase64UrlBytes(e.clientProof,32))return null;return e}function parseRelayHttpChallengeRequest(e){if(!hasExactKeys(e,[`v`,`keyId`,`clientNonce`,`role`,`transport`,`method`,`resource`,`flow`])||e.v!==2||typeof e.keyId!==`string`||e.keyId.length!==22||!isBase64UrlText(e.keyId)||!isCanonicalBase64UrlBytes(e.clientNonce,32)||e.role!==`cdp`||e.transport!==`connection`||!(e.flow===`cdp`&&e.method===`SEQUENCE`&&e.resource===`/json/version -> /cdp`||e.flow===`json-list`&&e.method===`GET`&&e.resource===`/json/list`))return null;return e}function parseRelayHttpCompleteRequest(e){if(!hasExactKeys(e,[`v`,`sessionId`,`clientProof`])||e.v!==2||!isCanonicalBase64UrlBytes(e.sessionId,16)||!isCanonicalBase64UrlBytes(e.clientProof,32))return null;return e}function parseExtensionRelayResource(e,t){const n=URL.parse(e,`http://127.0.0.1`);if(!n||n.pathname!==t||n.hash)return null;if([...n.searchParams.entries()].some(([e])=>e!==`profile`)||n.searchParams.getAll(`profile`).length>1)return null;const r=n.searchParams.get(`profile`);if(r!==null&&!/^[a-z0-9-]+$/u.test(r))return null;return r===null?t:`${t}?profile=${encodeURIComponent(r)}`}var BrowserRelayAuthV2Authority=class{constructor(e){this.keyHex=e;this.instanceId=randomRelayId();this.challenges=/* @__PURE__ */ new Map;this.pendingConnections=/* @__PURE__ */ new Map;this.authenticatedConnections=/* @__PURE__ */ new Map;this.replay=/* @__PURE__ */ new Map;this.disposed=false;this.keyId=relayKeyIdFromHex(e)}registerPendingConnection(e,t,n){const r=normalizeAuthSource(n);let i=0;for(const e of this.pendingConnections.values())i+=e.source===r?1:0;if(this.disposed||this.pendingConnections.has(e)||this.authenticatedConnections.has(e)||i>=MAX_PENDING_AUTH_CONNECTIONS_PER_SOURCE||this.pendingConnections.size>=MAX_PENDING_AUTH_CONNECTIONS)return false;this.pendingConnections.set(e,{onInvalidate:t,source:r});return true}registerAuthenticatedConnection(e,t){if(this.disposed||this.pendingConnections.has(e)||this.authenticatedConnections.has(e)||this.authenticatedConnections.size>=MAX_AUTHENTICATED_CONNECTIONS)return false;this.authenticatedConnections.set(e,t);return true}releaseConnection(e){this.pendingConnections.delete(e);this.authenticatedConnections.delete(e);for(const[t,n]of this.challenges)if(n.binding===e)this.challenges.delete(t)}issueChallenge(e,t,n,r=Date.now()){const i=this.pendingConnections.get(e);if(this.disposed||!i||t.keyId!==this.keyId||this.challenges.size>=MAX_PENDING_AUTH_CONNECTIONS)return null;const a=r+BROWSER_RELAY_CHALLENGE_TTL_MS;let o=0;for(const[e,t]of this.replay)if(t.expiresAtMs<r)this.replay.delete(e);else o+=t.source===i.source?1:0;const s=`${this.keyId}:${t.clientNonce}`;if(this.replay.has(s)||o>=MAX_REPLAY_ENTRIES_PER_SOURCE||this.replay.size>=MAX_REPLAY_ENTRIES)return null;this.replay.set(s,{expiresAtMs:a,source:i.source});const c={keyId:this.keyId,instanceId:this.instanceId,sessionId:randomRelayId(),clientNonce:t.clientNonce,serverNonce:randomRelayNonce(),issuedAtMs:r,expiresAtMs:a,...n};this.challenges.set(c.sessionId,{binding:e,fields:c});return{type:`auth.challenge`,v:2,...c,serverProof:createRelayProof(this.keyHex,`server`,c)}}completeChallenge(e,t,n=Date.now()){const r=this.challenges.get(t.sessionId);if(!r||r.binding!==e||this.disposed)return null;this.challenges.delete(t.sessionId);if(n>r.fields.expiresAtMs||!verifyRelayProof(this.keyHex,`client`,r.fields,t.clientProof))return null;const i=this.pendingConnections.get(e);if(!i||this.authenticatedConnections.size>=MAX_AUTHENTICATED_CONNECTIONS)return null;this.pendingConnections.delete(e);this.authenticatedConnections.set(e,i.onInvalidate);return{fields:r.fields,ok:{type:`auth.ok`,v:2,sessionId:r.fields.sessionId,acceptProof:createRelayProof(this.keyHex,`accept`,r.fields,t.clientProof)}}}dispose(){if(this.disposed)return;this.disposed=true;const e=[...Array.from(this.pendingConnections.values(),({onInvalidate:e})=>e),...this.authenticatedConnections.values()];this.pendingConnections.clear();this.authenticatedConnections.clear();this.challenges.clear();this.replay.clear();for(const t of e)t()}};let activeAuthority=null;function getBrowserRelayAuthV2Authority(e){if(activeAuthority?.keyHex===e)return activeAuthority.authority;activeAuthority?.authority.dispose();const t=new BrowserRelayAuthV2Authority(e);activeAuthority={keyHex:e,authority:t};return t}function invalidateBrowserRelayAuthV2Authority(){activeAuthority?.authority.dispose();activeAuthority=null}export{verifyRelayProof as _,getBrowserRelayAuthV2Authority as a,parseRelayAuthHello as c,parseRelayHttpCompleteRequest as d,createRelayProof as f,relayKeyIdFromHex as g,randomRelayNonce as h,BROWSER_RELAY_EXTENSION_SUBPROTOCOL as i,parseRelayAuthResponse as l,randomRelayId as m,BROWSER_RELAY_AUTH_COMPLETE_PATH as n,invalidateBrowserRelayAuthV2Authority as o,isCanonicalBase64UrlBytes as p,BROWSER_RELAY_CHALLENGE_TTL_MS as r,parseExtensionRelayResource as s,BROWSER_RELAY_AUTH_CHALLENGE_PATH as t,parseRelayHttpChallengeRequest as u,RELAY_AUTH_LABEL as v};