openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
9 lines • 8.32 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{i as getPluginRegistryForContext}from"./gateway-request-scope-7deHmsb_.mjs";import{a as QUESTIONS_SCOPE,c as SESSION_WRITE_SCOPE,d as WRITE_SCOPE,i as PAIRING_SCOPE,l as TALK_SCOPE,n as APPROVALS_SCOPE,o as READ_SCOPE,p as isOperatorScope,r as CRON_SCOPE,t as ADMIN_SCOPE,u as TALK_SECRETS_SCOPE}from"./operator-scopes-bEvB7E5X.mjs";import{F as isBrowserProxyNodeInvokeCommand,P as isAdminOnlyNodeInvokeCommand}from"./node-commands-BbC002V7.mjs";import{a as resolveReservedGatewayMethodScope}from"./descriptor-C1vIjnix.mjs";import{n as operatorScopeSatisfied,o as roleScopesAllow}from"./operator-scope-compat-TmgAKmD2.mjs";import{n as resolveSessionMethodScope,t as resolveBaseSessionMutationRequiredScope}from"./session-method-scopes-base-Djh5JSNY.mjs";import{n as validateSessionsMoveParams,t as validateSessionsDispatchParams}from"./session-placement-validators-BRg7PEL-.mjs";import{a as isDynamicOperatorGatewayMethod,l as resolveCoreOperatorGatewayMethodScope,r as isCoreGatewayMethodClassified}from"./core-method-policy-DeU1xgVA.mjs";
//!src/shared/session-method-scopes.ts
function resolveDynamicSessionMutationRequiredScope(e,t){if(e===`sessions.dispatch`){if(!validateSessionsDispatchParams(t))return`operator.write`;return t.profileId!==void 0||t.deviceId!==void 0||t.autoDevice===true?`operator.write`:`operator.admin`}if(e===`sessions.move`)return validateSessionsMoveParams(t)&&t.target.kind===`profile`?`operator.admin`:`operator.write`;return resolveBaseSessionMutationRequiredScope(e,t)}
//!src/gateway/agent-command-policy.ts
const AGENT_SESSION_RESET_COMMAND_RE=/^\/(new|reset)(?:\s+([\s\S]*))?$/i;function isAgentSessionResetCommand(e){return typeof e===`string`&&AGENT_SESSION_RESET_COMMAND_RE.test(e)}
//!src/gateway/node-browser-proxy-policy.ts
function isForbiddenBrowserProxyMutation(e){if(!e||typeof e!==`object`)return false;const t=e;const n=normalizeOptionalString(t.method)?.toUpperCase();const r=normalizeOptionalString(t.path);if(!n||!r)return false;const i=r.startsWith(`/`)?r:`/${r}`;const a=i.length<=1?i:i.replace(/\/+$/,``);if(n===`POST`&&(a===`/profiles/create`||a===`/reset-profile`))return true;return n===`DELETE`&&/^\/profiles\/[^/]+$/.test(a)}
//!src/gateway/method-scopes.ts
const CLI_DEFAULT_OPERATOR_SCOPES=[ADMIN_SCOPE,READ_SCOPE,WRITE_SCOPE,APPROVALS_SCOPE,QUESTIONS_SCOPE,PAIRING_SCOPE,TALK_SECRETS_SCOPE];function resolveAgentRuntimeCronScope(e){switch(e){case`cron.add`:case`cron.update`:case`cron.remove`:case`cron.run`:case`cron.get`:case`cron.list`:case`cron.runs`:case`cron.history`:case`cron.scratch.get`:case`cron.scratch.set`:return CRON_SCOPE;default:return}}function resolveScopedMethod(e){const t=resolveCoreOperatorGatewayMethodScope(e);if(t)return t;const n=resolveReservedGatewayMethodScope(e);if(n)return n;const r=(getPluginRegistryForContext()?.gatewayMethodDescriptors?.find(t=>t.name===e))?.scope;return r===`node`||r===`dynamic`?void 0:r}function isApprovalMethod(e){return resolveScopedMethod(e)===APPROVALS_SCOPE}function resolveSessionActionRegisteredScopes(e){const t=asOptionalRecord(e);if(!t)return;const n=normalizeOptionalString(t.pluginId);const r=normalizeOptionalString(t.actionId);if(!n||!r)return;const i=getPluginRegistryForContext()?.sessionActions?.find(e=>e.pluginId===n&&e.action.id===r);if(!i)return;const a=i.action.requiredScopes;return a&&a.length>0?[...a]:[WRITE_SCOPE]}function resolveSessionActionLeastPrivilegeScopes(e){const t=resolveSessionActionRegisteredScopes(e);if(t)return t;const n=asOptionalRecord(e);if(n){const e=normalizeOptionalString(n.pluginId);const t=normalizeOptionalString(n.actionId);if(e&&t)return[...CLI_DEFAULT_OPERATOR_SCOPES]}return[WRITE_SCOPE]}function resolveDynamicLeastPrivilegeOperatorScopesForMethod(e,t){if(e===`plugins.sessionAction`)return resolveSessionActionLeastPrivilegeScopes(t);const n=asOptionalRecord(t);if(e===`sessions.github.action`)return[n?.action===`inspect_pr`||n?.action===`prepare_pr`||n?.action===`refresh_base`||n?.action===`edit`||n?.action===`request_review`||n?.action===`upload_evidence`||n?.action===`ready`&&n.publishingSessionKey===void 0?SESSION_WRITE_SCOPE:WRITE_SCOPE];if(e===`agent`)return isAgentSessionResetCommand(n?.message)?[ADMIN_SCOPE]:[WRITE_SCOPE];if(e===`node.invoke`){const e=n?.command;if(isBrowserProxyNodeInvokeCommand(e)&&isForbiddenBrowserProxyMutation(n?.params))return[WRITE_SCOPE];return isAdminOnlyNodeInvokeCommand(e)?[ADMIN_SCOPE]:[WRITE_SCOPE]}if(e===`talk.config`)return n?.includeSecrets===true?[READ_SCOPE,TALK_SECRETS_SCOPE]:[READ_SCOPE];if(e===`environments.list`){const e=n&&`runtimeId`in n?n.runtimeId:void 0;return typeof e===`string`&&e?[WRITE_SCOPE]:[READ_SCOPE]}if(e===`channels.pairing.approve`)return n?.bootstrapCommandOwner===true?[PAIRING_SCOPE,ADMIN_SCOPE]:[PAIRING_SCOPE];if(e===`fs.listDir`)return[n&&Object.hasOwn(n,`nodeId`)?ADMIN_SCOPE:WRITE_SCOPE];if(e===`sessions.patch`||e===`sessions.patchMany`||e===`sessions.create`||e===`sessions.dispatch`||e===`sessions.move`)return[resolveDynamicSessionMutationRequiredScope(e,t)??`operator.write`];if(e===`sessions.delete`)return[resolveDynamicSessionMutationRequiredScope(e,t)??`operator.admin`];return[WRITE_SCOPE]}function findMissingOperatorScope(e,t){return e.find(e=>!operatorScopeSatisfied(e,t))}function resolveLeastPrivilegeOperatorScopesForMethod(e,t){if(isDynamicOperatorGatewayMethod(e))return resolveDynamicLeastPrivilegeOperatorScopesForMethod(e,t);const n=resolveScopedMethod(e);if(n)return[n];return[]}function projectOperatorScopesForMethod(e){const t=e.requiredScope?[e.requiredScope]:resolveLeastPrivilegeOperatorScopesForMethod(e.method,e.requestParams);const n=e.sessionScope??resolveSessionMethodScope(e.method,e.requestParams);return e.requestedScopes.flatMap(r=>{if(roleScopesAllow({role:`operator`,requestedScopes:[r],allowedScopes:e.allowedScopes}))return[r];if(!isOperatorScope(r)||!t.includes(r))return[];const i=authorizeOperatorScopesForRequiredScope(r,e.allowedScopes,n,e.method);return i.allowed&&i.sessionScope?[i.sessionScope]:[]})}function fitOperatorScopesToGrant(e){const{method:t,requestParams:n,scopes:r,grantedScopes:i}=e;if(roleScopesAllow({role:`operator`,requestedScopes:r,allowedScopes:i}))return r;const a=projectOperatorScopesForMethod({method:t,requestParams:n,requestedScopes:r,allowedScopes:i}).filter(isOperatorScope);return a.length>0&&authorizeOperatorScopesForMethod(t,a,n).allowed?a:r}function authorizeOperatorScopesForMethod(e,t,n){if(t.includes(`operator.admin`))return{allowed:true};if(isDynamicOperatorGatewayMethod(e)){if(e===`plugins.sessionAction`){const e=resolveSessionActionRegisteredScopes(n);const r=asOptionalRecord(n);if(!e&&r){const e=normalizeOptionalString(r.pluginId);const n=normalizeOptionalString(r.actionId);if(!e||!n)return t.some(e=>isOperatorScope(e))?{allowed:true}:{allowed:false,missingScope:WRITE_SCOPE}}const i=findMissingOperatorScope(e??[`operator.write`],t);return i?{allowed:false,missingScope:i}:{allowed:true}}const r=findMissingOperatorScope(resolveDynamicLeastPrivilegeOperatorScopesForMethod(e,n),t);return r?authorizeOperatorScopesForRequiredScope(r,t,resolveSessionMethodScope(e,n),e):{allowed:true}}return authorizeOperatorScopesForRequiredScope(resolveScopedMethod(e)??`operator.admin`,t,resolveSessionMethodScope(e,n),e)}function authorizeOperatorScopesForRequiredScope(e,t,n,r){if(operatorScopeSatisfied(e,t))return{allowed:true};if((e===`operator.read`&&n===`operator.sessions.read`||(e===`operator.write`||e===`operator.talk`||e===`operator.questions`&&r?.startsWith(`question.`))&&n===`operator.sessions.write`)&&operatorScopeSatisfied(n,t))return{allowed:true,sessionScope:n};return{allowed:false,missingScope:e}}function isGatewayMethodClassified(e){return isCoreGatewayMethodClassified(e)||resolveScopedMethod(e)!==void 0}export{isApprovalMethod as a,resolveAgentRuntimeCronScope as c,AGENT_SESSION_RESET_COMMAND_RE as d,fitOperatorScopesToGrant as i,resolveLeastPrivilegeOperatorScopesForMethod as l,authorizeOperatorScopesForMethod as n,isGatewayMethodClassified as o,authorizeOperatorScopesForRequiredScope as r,projectOperatorScopesForMethod as s,CLI_DEFAULT_OPERATOR_SCOPES as t,isForbiddenBrowserProxyMutation as u};