UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

50 lines (49 loc) • 2.69 kB
//#region extensions/onepassword/onepassword-secret-id.js const ENCODED_SECRET_ID_PREFIX = "opb64:"; const MAX_NATIVE_SECRET_ID_BYTES = 2048; const EXEC_SECRET_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/#-]{0,255}$/; function invalidSecretId(e) { return /* @__PURE__ */ new Error(`Invalid 1Password SecretRef id: ${e}`); } function assertSafeNativeSecretId(e) { if (typeof e !== "string" || e.length === 0) throw invalidSecretId("the value is empty."); if (Buffer.byteLength(e, "utf8") > MAX_NATIVE_SECRET_ID_BYTES) throw invalidSecretId(`the value exceeds ${MAX_NATIVE_SECRET_ID_BYTES} bytes.`); if (e.trim() !== e || e.startsWith("/") || e.includes("\\")) throw invalidSecretId("leading/trailing whitespace, absolute paths, and backslashes are not allowed."); for (let t = 0; t < e.length; t += 1) { const n = e.charCodeAt(t); if (n < 32 || n > 126) throw invalidSecretId("only printable ASCII characters are allowed."); } if (e.split("/").some((e) => e === "." || e === "..")) throw invalidSecretId("\".\" and \"..\" path segments are not allowed."); } function isCanonicalExecSecretId(e) { return EXEC_SECRET_ID_PATTERN.test(e) && !e.split("/").some((e) => e === "." || e === ".."); } function assertReferenceShape(e) { const t = (e.startsWith("op://") ? e.slice(5) : e).split("?", 1)[0].split("/"); if (![3, 4].includes(t.length) || t.some((e) => e.length === 0)) throw invalidSecretId("use \"op://<vault>/<item>/<field>\", \"<vault>/<item>/<field>\", or \"<vault>/<item>/<section>/<field>\"."); } function encodeOnePasswordSecretId(e) { assertSafeNativeSecretId(e); assertReferenceShape(e); if (isCanonicalExecSecretId(e) && !e.startsWith(ENCODED_SECRET_ID_PREFIX)) return e; const t = `${ENCODED_SECRET_ID_PREFIX}${Buffer.from(e, "utf8").toString("base64url")}`; if (!isCanonicalExecSecretId(t)) throw invalidSecretId("the encoded reference exceeds OpenClaw's exec SecretRef limit; use 1Password vault, item, section, and field IDs to shorten it."); return t; } function decodeOnePasswordSecretId(e) { if (!e.startsWith(ENCODED_SECRET_ID_PREFIX)) return e; const t = e.slice(6); if (!/^[A-Za-z0-9_-]+$/.test(t)) throw invalidSecretId("the encoded reference is malformed."); const n = Buffer.from(t, "base64url").toString("utf8"); if (Buffer.from(n, "utf8").toString("base64url") !== t) throw invalidSecretId("the encoded reference is malformed."); return n; } function resolveOnePasswordSecretReference(e) { const t = decodeOnePasswordSecretId(e); assertSafeNativeSecretId(t); assertReferenceShape(t); if (t.startsWith("op://")) return t; return `op://${t}`; } //#endregion export { resolveOnePasswordSecretReference as n, encodeOnePasswordSecretId as t };