openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
321 lines (320 loc) • 13.5 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { i as getPluginRegistryForContext } from "./gateway-request-scope-BvvDhs5D.mjs";
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs";
import { a as READ_SCOPE, f as isOperatorScope, i as QUESTIONS_SCOPE, l as TALK_SECRETS_SCOPE, n as APPROVALS_SCOPE, r as PAIRING_SCOPE, s as SESSION_WRITE_SCOPE, t as ADMIN_SCOPE, u as WRITE_SCOPE } from "./operator-scopes-CULmYfyY.mjs";
import { N as isAdminOnlyNodeInvokeCommand, P as isBrowserProxyNodeInvokeCommand } from "./node-commands-BfMJIlXP.mjs";
import { a as resolveReservedGatewayMethodScope } from "./descriptor-Qgpy2kQR.mjs";
import { n as operatorScopeSatisfied, o as roleScopesAllow } from "./operator-scope-compat-CpFHu-FC.mjs";
import { n as validateSessionsMoveParams, t as validateSessionsDispatchParams } from "./session-placement-validators-CTVLM1u3.mjs";
import { a as isDynamicOperatorGatewayMethod, l as resolveCoreOperatorGatewayMethodScope, r as isCoreGatewayMethodClassified } from "./core-method-policy-CcbxxGLc.mjs";
//#region src/shared/session-method-scopes-base.ts
const SESSION_READ_METHODS = /* @__PURE__ */ new Set([
"agent.identity.get",
"agents.list",
"artifacts.list",
"artifacts.get",
"artifacts.download",
"board.get",
"canvas.document.preview",
"canvas.document.view",
"controlUi.sessionPullRequests.subscribe",
"controlUi.sessionPullRequests.checks",
"mentions.dismiss",
"mentions.list",
"models.list",
"progressCard.get",
"projects.list",
"projects.checkoutOptions",
"session.suggestions.list",
"session.reactions.list",
"sessions.groups.list",
"sessions.processes.list",
"sessions.list",
"sessions.subscribe",
"sessions.messages.subscribe",
"sessions.messages.unsubscribe",
"sessions.viewers.set",
"sessions.preview",
"sessions.describe",
"sessions.diff",
"sessions.branches.list",
"sessions.get",
"sessions.github.options",
"sessions.github.readiness",
"sessions.github.status",
"sessions.resolve",
"sessions.search",
"sessions.files.list",
"sessions.files.get",
"sessions.files.download",
"sessions.files.assets",
"sessions.setInvolvement",
"chat.history",
"chat.startup",
"chat.metadata",
"chat.message.get",
"session.members.list",
"session.members.listEvidence",
"themes.get",
"themes.list",
"users.mentionable",
"users.personalFile.get",
"users.personalFile.set",
"users.prefs.get",
"users.prefs.set",
"users.self",
"users.setAvatar",
"users.setDisplayName"
]);
const SESSION_WRITE_METHODS = /* @__PURE__ */ new Set([
"board.update",
"board.widget.put",
"question.request",
"question.waitAnswer",
"question.resolve",
"question.get",
"question.list",
"chat.send",
"chat.abort",
"sessions.create",
"sessions.patch",
"sessions.patchMany",
"sessions.fork",
"sessions.send",
"sessions.steer",
"sessions.processes.stop",
"sessions.abort",
"sessions.goal.update",
"sessions.goal.clear"
]);
/** Admission only: reads retain sharing policy; mutation owners must bind the caller's own row. */
function resolveSessionMethodScope(e, t) {
if (e === "sessions.create" && isRecord(t) && (t.deviceFolder !== void 0 || t.projectGitUrl !== void 0)) return;
if (SESSION_READ_METHODS.has(e)) return "operator.sessions.read";
if (SESSION_WRITE_METHODS.has(e) && resolveBaseSessionMutationRequiredScope(e, t) !== "operator.admin") return "operator.sessions.write";
}
const SESSIONS_PATCH_WRITE_SCOPE_MUTATIONS = /* @__PURE__ */ new Set([
"label",
"autoLabel",
"icon",
"color",
"category",
"boardFace",
"boardPresentation",
"pinned",
"sidebarRoot",
"archived",
"snoozedUntil",
"unread",
"model",
"agentRuntime",
"thinkingLevel",
"cyberAccessProgram",
"fastMode",
"permissionMode",
"communication"
]);
const SESSIONS_PATCH_WRITE_SCOPE_ENVELOPE_FIELDS = /* @__PURE__ */ new Set([
"key",
"agentId",
"expectedSessionId",
"expectedLifecycleRevision",
"expectedPermissionMode",
"expectedMarkedUnreadAt",
"expectedSidebarRoot",
"expectedCategory",
"expectedArchived",
"expectedSidebarAncestors"
]);
const SESSIONS_DELETE_WRITE_SCOPE_FIELDS = /* @__PURE__ */ new Set([
"key",
"agentId",
"deleteTranscript",
"expectedSessionId",
"archivedOnly"
]);
/** Browser-safe session mutation policy for methods without protocol validation. */
function resolveBaseSessionMutationRequiredScope(e, t) {
if (e === "sessions.create") {
if (!isRecord(t)) return "operator.write";
if (t.incognito === true || typeof t.key === "string" && isIncognitoSessionKey(t.key) || typeof t.parentSessionKey === "string" && isIncognitoSessionKey(t.parentSessionKey) || Object.hasOwn(t, "execNode") || Object.hasOwn(t, "toolOverrides") || t.permissionMode === "full") return "operator.admin";
return "operator.write";
}
if (e === "sessions.patch") {
if (!isRecord(t)) return "operator.write";
if (t.permissionMode === "full" || Object.hasOwn(t, "sandboxMode")) return "operator.admin";
return Object.keys(t).every((e) => SESSIONS_PATCH_WRITE_SCOPE_ENVELOPE_FIELDS.has(e) || SESSIONS_PATCH_WRITE_SCOPE_MUTATIONS.has(e)) ? "operator.write" : "operator.admin";
}
if (e === "sessions.patchMany") {
if (!isRecord(t) || !isRecord(t.patch)) return "operator.write";
if (t.patch.permissionMode === "full" || Object.hasOwn(t.patch, "sandboxMode")) return "operator.admin";
return Object.keys(t.patch).every((e) => SESSIONS_PATCH_WRITE_SCOPE_MUTATIONS.has(e)) ? "operator.write" : "operator.admin";
}
if (e === "sessions.delete") {
if (!isRecord(t) || t.archivedOnly !== true) return "operator.admin";
return Object.keys(t).every((e) => SESSIONS_DELETE_WRITE_SCOPE_FIELDS.has(e)) ? "operator.write" : "operator.admin";
}
}
//#endregion
//#region src/shared/session-method-scopes.ts
/** Returns the exact Gateway/CLI scope for params-aware session mutations. */
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);
}
//#endregion
//#region src/gateway/agent-command-policy.ts
/** Commands routed through `agent` that mutate session lifecycle state. */
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);
}
//#endregion
//#region 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);
}
//#endregion
//#region src/gateway/method-scopes.ts
/** Default scopes granted to CLI/operator clients when no narrower local policy is known. */
const CLI_DEFAULT_OPERATOR_SCOPES = [
ADMIN_SCOPE,
READ_SCOPE,
WRITE_SCOPE,
APPROVALS_SCOPE,
QUESTIONS_SCOPE,
PAIRING_SCOPE,
TALK_SECRETS_SCOPE
];
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 [];
}
/** Projects requested scopes through the original grant and this call's exact scope policy. */
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 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.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;
}
//#endregion
export { isGatewayMethodClassified as a, isForbiddenBrowserProxyMutation as c, isApprovalMethod as i, AGENT_SESSION_RESET_COMMAND_RE as l, authorizeOperatorScopesForMethod as n, projectOperatorScopesForMethod as o, authorizeOperatorScopesForRequiredScope as r, resolveLeastPrivilegeOperatorScopesForMethod as s, CLI_DEFAULT_OPERATOR_SCOPES as t, resolveSessionMethodScope as u };