openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 9.74 kB
JavaScript
import{c as normalizeOptionalLowercaseString,g as readStringValue}from"./string-coerce-CpTFcSwY.mjs";import{r as resolveHomeRelativePath,t as expandHomePrefix}from"./home-dir-DEN5DisQ.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as ok,t as err}from"./result-CkiK_t2O.mjs";import{t as DEFAULT_AGENT_ID}from"./session-key-BPgKfU2m.mjs";import{An as never,Et as _enum,Jn as string,Lt as boolean,Nt as array,Pn as number,Sn as looseObject,Un as record,or as union,sr as unknown,xn as literal}from"./schemas-DuCQi-Qb.mjs";import path from"node:path";
//!src/infra/exec-approvals-config.ts
const execSecuritySchema=_enum([`allowlist`,`full`,`deny`]);const execAskSchema=_enum([`always`,`off`,`on-miss`]);const persistedExecApprovalPolicySchema=looseObject({security:execSecuritySchema.optional(),ask:execAskSchema.optional(),askFallback:execSecuritySchema.optional(),autoAllowSkills:boolean().optional()});function normalizePersistedAllowlistSource(e){return e===`allow-always`?e:void 0}const persistedExecAllowlistEntrySchema=looseObject({pattern:string().refine(e=>e.trim().length>0),id:string().min(1),source:literal(`allow-always`).optional(),commandText:never().optional(),argPattern:string().optional(),lastUsedAt:number().finite().optional(),lastUsedCommand:string().optional(),lastResolvedPath:string().optional()});const legacyExecAllowlistEntrySchema=union([string().trim().min(1),persistedExecAllowlistEntrySchema.extend({id:string().optional(),source:string().transform(normalizePersistedAllowlistSource).optional(),commandText:string().optional()})]).transform(e=>typeof e===`string`?{pattern:e}:e);function createExecApprovalsSchema(e,t){const n=persistedExecApprovalPolicySchema.extend({allowlist:array(e).optional(),mcpTools:array(looseObject({server:string().refine(e=>e.trim().length>0),tool:string().refine(e=>e.trim().length>0),source:literal(`allow-always`),addedAt:number().finite().nonnegative(),lastUsedAt:number().finite().nonnegative().optional()})).optional()});const r=unknown().refine(e=>!isRecord(e)||!Object.hasOwn(e,`__proto__`)).pipe(record(string(),n)).refine(e=>t||!Object.hasOwn(e,`default`));return looseObject({version:literal(1),socket:looseObject({path:string().optional(),token:string().optional()}).optional(),defaults:persistedExecApprovalPolicySchema.optional(),agents:r.optional()})}const persistedExecApprovalsSchema=createExecApprovalsSchema(persistedExecAllowlistEntrySchema,false);const legacyExecApprovalsSchema=createExecApprovalsSchema(legacyExecAllowlistEntrySchema,true);const LEGACY_EXEC_APPROVALS_DIAGNOSTIC=`Legacy exec approval policy requires Doctor repair`;const DEFAULT_SECURITY=`full`;const DEFAULT_ASK=`off`;const DEFAULT_EXEC_APPROVAL_ASK_FALLBACK=`deny`;const DEFAULT_AUTO_ALLOW_SKILLS=false;const DEFAULT_EXEC_APPROVALS_STATE_DIR=`~/.openclaw`;const EXEC_APPROVALS_FILE=`exec-approvals.json`;const EXEC_APPROVALS_SOCKET=`exec-approvals.sock`;function resolveExecApprovalsStateDir(e=process.env){const t=e.OPENCLAW_STATE_DIR?.trim();if(t){const n=resolveHomeRelativePath(t,{env:e});return{path:n,displayPath:n}}return{path:expandHomePrefix(DEFAULT_EXEC_APPROVALS_STATE_DIR,{env:e}),displayPath:DEFAULT_EXEC_APPROVALS_STATE_DIR}}function resolveExecApprovalsPath(e=process.env){return path.join(resolveExecApprovalsStateDir(e).path,EXEC_APPROVALS_FILE)}function resolveExecApprovalsSocketPath(e=process.env){return path.join(resolveExecApprovalsStateDir(e).path,EXEC_APPROVALS_SOCKET)}function resolveExecApprovalsDisplayPath(e=process.env){const t=resolveExecApprovalsStateDir(e).displayPath;const n=path.join(`state`,`openclaw.sqlite#exec_approvals_config`);return t===DEFAULT_EXEC_APPROVALS_STATE_DIR?`${t}/${n}`:path.join(t,n)}function createFailClosedExecApprovalsFallback(){return normalizeExecApprovalsInternal({version:1,defaults:{security:`deny`,ask:`off`,askFallback:`deny`,autoAllowSkills:false},agents:{}})}const diagnosticFields=/* @__PURE__ */ new Set([`version`,`socket`,`path`,`token`,`defaults`,`agents`,`security`,`ask`,`askFallback`,`autoAllowSkills`,`allowlist`,`mcpTools`,`server`,`tool`,`addedAt`,`pattern`,`id`,`source`,`commandText`,`argPattern`,`lastUsedAt`,`lastUsedCommand`,`lastResolvedPath`]);const diagnosticTypeReasons=/* @__PURE__ */ new Map([[`string`,`expected a string`],[`number`,`expected a finite number`],[`boolean`,`expected a boolean`],[`array`,`expected an array`],[`object`,`expected an object`]]);function formatPersistedExecApprovalsIssue(e,t){const n=e.code===`invalid_union`?e.errors[1]?.find(e=>e.path.length===1):void 0;const r=n??e;const i=n?[...e.path,...n.path]:e.path;let a=``;for(const[e,n]of i.slice(0,5).entries())if(e===1&&i[0]===`agents`){const e=isRecord(t)&&isRecord(t.agents)?t.agents:{};const r=typeof n===`string`?Object.keys(e).indexOf(n)+1:0;if(!r)break;a+=` entry #${r}`}else if(e===3&&(i[2]===`allowlist`||i[2]===`mcpTools`)&&typeof n===`number`&&Number.isSafeInteger(n)&&n>=0)a+=`[${n}]`;else if(typeof n===`string`&&diagnosticFields.has(n))a+=`${a?`.`:``}${n}`;else break;let o=`invalid value`;switch(r.code){case`invalid_type`:o=diagnosticTypeReasons.get(r.expected)??o;break;case`invalid_value`:o=`expected a supported value`;break;case`invalid_union`:o=`expected a non-empty string or an object with a non-empty pattern`;break;case`too_small`:o=`expected a non-empty string`;break;case`custom`:if(i.at(-1)===`pattern`)o=`expected a non-empty string`}return`${a||`document`}: ${o}`}function parseExecApprovals(e,t){let n;try{n=JSON.parse(e)}catch{return err(`invalid JSON syntax`)}try{const e=t.safeParse(n);if(e.success)return ok(e.data);if(t===persistedExecApprovalsSchema&&legacyExecApprovalsSchema.safeParse(n).success)return err(LEGACY_EXEC_APPROVALS_DIAGNOSTIC);const r=e.error.issues[0];return err(r?formatPersistedExecApprovalsIssue(r,n):`invalid approvals structure`)}catch{return err(`invalid approvals structure`)}}function parsePersistedExecApprovals(e){return parseExecApprovals(e,persistedExecApprovalsSchema)}function parseLegacyExecApprovals(e){const t=parseExecApprovals(e,legacyExecApprovalsSchema);return t.ok?ok(normalizeExecApprovalsInternal(t.value)):t}function tryParsePersistedExecApprovals(e){const t=parsePersistedExecApprovals(e);return t.ok?t.value:null}function mergeLegacyAgent(e,t){const n=[];const r=/* @__PURE__ */ new Set;const pushEntry=e=>{const t=normalizeOptionalLowercaseString(e.pattern);if(!t)return;const i=`${t}\x00${e.argPattern?.trim()??``}`;if(r.has(i))return;r.add(i);n.push(e)};for(const t of e.allowlist??[])pushEntry(t);for(const e of t.allowlist??[])pushEntry(e);return{...t,...e,security:e.security??t.security,ask:e.ask??t.ask,askFallback:e.askFallback??t.askFallback,autoAllowSkills:e.autoAllowSkills??t.autoAllowSkills,allowlist:n.length>0?n:void 0,mcpTools:e.mcpTools||t.mcpTools?[...e.mcpTools??[],...(t.mcpTools??[]).filter(t=>!e.mcpTools?.some(e=>e.server===t.server&&e.tool===t.tool))]:void 0}}function coerceAllowlistEntries(e){if(!Array.isArray(e)||e.length===0)return Array.isArray(e)?e:void 0;let t=false;const n=[];for(const r of e)if(typeof r===`string`){const e=r.trim();t=true;if(e)n.push({pattern:e})}else if(r&&typeof r===`object`&&!Array.isArray(r)){const e=r.pattern;if(typeof e===`string`&&e.trim().length>0)n.push(r);else t=true}else t=true;return t?n.length>0?n:void 0:e}function normalizeAllowlistMetadata(e){if(!Array.isArray(e)||e.length===0)return e;let t=false;const n=e.map(e=>{let n=e;const r=normalizePersistedAllowlistSource(n.source??``);if(n.source!==r)n={...n,source:r};if(!n.id)n={...n,id:crypto.randomUUID()};if(typeof n.commandText===`string`){const{commandText:e,...t}=n;n=t}t||=n!==e;return n});return t?n:e}function sanitizePolicyEnum(e,t){const n=readStringValue(e)?.trim();return t.find(e=>e===n)}function sanitizeExecApprovalPolicy(e){return{security:sanitizePolicyEnum(e?.security,execSecuritySchema.options),ask:sanitizePolicyEnum(e?.ask,execAskSchema.options),askFallback:sanitizePolicyEnum(e?.askFallback,execSecuritySchema.options),autoAllowSkills:e?.autoAllowSkills}}function normalizeExecApprovalsInternal(e){const{path:t,token:n}=e.socket??{};const r=t?.trim();const i=n?.trim();const a={...e.agents};const o=a.default;if(o){const e=a[DEFAULT_AGENT_ID];a[DEFAULT_AGENT_ID]=e?mergeLegacyAgent(e,o):o;delete a.default}for(const[e,t]of Object.entries(a)){const n=normalizeAllowlistMetadata(coerceAllowlistEntries(t.allowlist));const r=sanitizeExecApprovalPolicy(t);if(n!==t.allowlist||r.security!==t.security||r.ask!==t.ask||r.askFallback!==t.askFallback)a[e]={...t,allowlist:n,security:r.security,ask:r.ask,askFallback:r.askFallback}}const s=sanitizeExecApprovalPolicy(e.defaults);return{version:1,socket:{path:r&&r.length>0?r:void 0,token:i&&i.length>0?i:void 0},defaults:s,agents:a}}function stripExecApprovalsSocketCredentials(e){const{path:t,token:n,...r}=e.socket??{};return{...e,socket:Object.keys(r).length?r:void 0}}function stripExecApprovalsRawSocketCredentials(e){let t;try{t=JSON.parse(e)}catch{return e}if(!isRecord(t)||!isRecord(t.socket)||!Object.hasOwn(t.socket,`path`)&&!Object.hasOwn(t.socket,`token`))return e;const{path:n,token:r,...i}=t.socket;if(Object.keys(i).length)t.socket=i;else delete t.socket;return`${JSON.stringify(t,null,2)}\n`}export{LEGACY_EXEC_APPROVALS_DIAGNOSTIC as a,parseLegacyExecApprovals as c,resolveExecApprovalsPath as d,resolveExecApprovalsSocketPath as f,tryParsePersistedExecApprovals as h,DEFAULT_SECURITY as i,parsePersistedExecApprovals as l,stripExecApprovalsSocketCredentials as m,DEFAULT_AUTO_ALLOW_SKILLS as n,createFailClosedExecApprovalsFallback as o,stripExecApprovalsRawSocketCredentials as p,DEFAULT_EXEC_APPROVAL_ASK_FALLBACK as r,normalizeExecApprovalsInternal as s,DEFAULT_ASK as t,resolveExecApprovalsDisplayPath as u};