UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

128 lines (127 loc) • 4.59 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import "./utils-C8YECn2Q.mjs"; import "./official-external-plugin-catalog-DtkWz1Xo.mjs"; import { c as isOfficialExternalPluginCatalogFeed } from "./official-external-plugin-catalog-source-4o5oPhVN.mjs"; import { c as normalizeEd25519PublicKeyBase64Url, f as verifyEd25519SignatureBytes } from "./ed25519-signature-CUaH7IDK.mjs"; //#region src/plugins/official-external-plugin-catalog-envelope.ts const OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PAYLOAD_TYPE = "openclaw.official-external-plugin-catalog-feed.v1"; const OFFICIAL_EXTERNAL_PLUGIN_CATALOG_MAX_SIGNATURES = 16; function verifyOfficialExternalPluginCatalogSignedEnvelope(e, t) { const n = parseOfficialExternalPluginCatalogSignedEnvelope(e, { allowLegacyBetaEnvelope: t.allowLegacyBetaEnvelope === true }); if (!n) return { ok: false, error: "invalid-envelope", message: "hosted catalog signed envelope is malformed" }; if (n.payloadType !== OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PAYLOAD_TYPE) return { ok: false, error: "unsupported-payload", message: "hosted catalog signed envelope payload type is unsupported" }; const r = decodeOfficialExternalPluginCatalogEnvelopePayloadBytes(n.payload); if (!r) return { ok: false, error: "invalid-payload", message: "hosted catalog signed envelope payload is invalid" }; const i = dssePreAuthenticationEncoding(n.payloadType, r); const a = Math.max(1, Math.trunc(t.threshold ?? 1)); const o = []; const s = /* @__PURE__ */ new Set(); for (const e of n.signatures) { const n = e.keyid; const r = t.trustedKeys.find((e) => e.keyId === n); if (!r || o.includes(r.keyId)) continue; const c = normalizeEd25519PublicKeyBase64Url(r.publicKey); if (!c || s.has(c)) continue; if (verifyEd25519SignatureBytes({ publicKey: r.publicKey, payload: i, signatureBase64Url: e.sig })) { o.push(r.keyId); s.add(c); if (s.size >= a) break; } } if (s.size >= a) { const e = decodeOfficialExternalPluginCatalogEnvelopePayload(r); if (!e?.feed) return { ok: false, error: "invalid-payload", message: "hosted catalog signed envelope payload is invalid", ...e ? { authenticatedPayload: e.raw } : {} }; return { ok: true, feed: e.feed, signedBy: o[0] ?? "", ...a > 1 ? { signedByKeyIds: o, signatureCount: s.size, threshold: a } : {} }; } return n.signatures.some((e) => t.trustedKeys.some((t) => t.keyId === e.keyid)) ? { ok: false, error: "invalid-signature", message: o.length > 0 ? "hosted catalog signed envelope did not meet the configured signature threshold" : "hosted catalog signed envelope signature is invalid" } : { ok: false, error: "missing-trust-key", message: "hosted catalog signed envelope was not signed by a trusted key" }; } function parseOfficialExternalPluginCatalogSignedEnvelope(e, t) { if (!isRecord(e)) return null; const n = e.payloadType; const r = e.payload; const i = e.signatures; if (typeof n !== "string" || typeof r !== "string") return null; if (!Array.isArray(i) || i.length === 0) return null; if (i.length > OFFICIAL_EXTERNAL_PLUGIN_CATALOG_MAX_SIGNATURES) return null; const a = i.filter((e) => isRecord(e) && typeof e.keyid === "string" && e.keyid.trim().length > 0 && typeof e.sig === "string" && e.sig.trim().length > 0); const o = e.schemaVersion === 1 ? i.filter((e) => isRecord(e) && typeof e.keyId === "string" && e.keyId.trim().length > 0 && e.algorithm === "ed25519" && typeof e.signature === "string" && e.signature.trim().length > 0).map((e) => ({ keyid: e.keyId, sig: e.signature })) : []; if (a.length > 0 && o.length > 0) return null; const s = a.length > 0 ? a : t.allowLegacyBetaEnvelope ? o : []; if (s.length === 0) return null; const c = /* @__PURE__ */ new Set(); for (const e of s) { if (c.has(e.keyid)) return null; c.add(e.keyid); } return { payloadType: n, payload: r, signatures: s }; } function dssePreAuthenticationEncoding(e, t) { const n = Buffer.from(e, "utf8"); const r = Buffer.from(`DSSEv1 ${n.length} ${e} ${t.length} `, "utf8"); return Buffer.concat([r, t]); } function decodeOfficialExternalPluginCatalogEnvelopePayloadBytes(e) { try { return Buffer.from(e, "base64"); } catch { return null; } } function decodeOfficialExternalPluginCatalogEnvelopePayload(e) { try { const t = JSON.parse(e.toString("utf8")); return { raw: t, feed: isOfficialExternalPluginCatalogFeed(t) ? t : null }; } catch { return null; } } //#endregion export { verifyOfficialExternalPluginCatalogSignedEnvelope };