Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
350 lines
12 KiB
TypeScript
350 lines
12 KiB
TypeScript
// Matrix plugin module implements approval native behavior.
|
|
import {
|
|
createChannelApprovalCapability,
|
|
createApproverRestrictedNativeApprovalCapability,
|
|
splitChannelApprovalCapability,
|
|
} from "openclaw/plugin-sdk/approval-delivery-runtime";
|
|
import { createLazyChannelApprovalNativeRuntimeAdapter } from "openclaw/plugin-sdk/approval-handler-adapter-runtime";
|
|
import type { ChannelApprovalNativeRuntimeAdapter } from "openclaw/plugin-sdk/approval-handler-runtime";
|
|
import {
|
|
createChannelNativeOriginTargetResolver,
|
|
resolveApprovalRequestSessionConversation,
|
|
} from "openclaw/plugin-sdk/approval-native-runtime";
|
|
import type {
|
|
ExecApprovalRequest,
|
|
PluginApprovalRequest,
|
|
} from "openclaw/plugin-sdk/approval-runtime";
|
|
import type { ChannelApprovalCapability } from "openclaw/plugin-sdk/channel-contract";
|
|
import {
|
|
normalizeLowercaseStringOrEmpty,
|
|
normalizeOptionalStringifiedId,
|
|
} from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
import { getMatrixApprovalAuthApprovers, matrixApprovalAuth } from "./approval-auth.js";
|
|
import { normalizeMatrixApproverId } from "./approval-ids.js";
|
|
import {
|
|
getMatrixApprovalApprovers,
|
|
getMatrixExecApprovalApprovers,
|
|
isMatrixAnyApprovalClientEnabled,
|
|
isMatrixApprovalClientEnabled,
|
|
isMatrixExecApprovalClientEnabled,
|
|
isMatrixExecApprovalAuthorizedSender,
|
|
resolveMatrixExecApprovalTarget,
|
|
shouldHandleMatrixApprovalRequest,
|
|
} from "./exec-approvals.js";
|
|
import { listMatrixAccountIds } from "./matrix/accounts.js";
|
|
import { normalizeMatrixUserId } from "./matrix/monitor/allowlist.js";
|
|
import { resolveMatrixTargetIdentity } from "./matrix/target-ids.js";
|
|
import type { CoreConfig } from "./types.js";
|
|
|
|
type ApprovalRequest = ExecApprovalRequest | PluginApprovalRequest;
|
|
type ApprovalKind = "exec" | "plugin";
|
|
type MatrixOriginTarget = { to: string; threadId?: string };
|
|
|
|
function normalizeComparableTarget(value: string): string {
|
|
const target = resolveMatrixTargetIdentity(value);
|
|
if (!target) {
|
|
return normalizeLowercaseStringOrEmpty(value);
|
|
}
|
|
if (target.kind === "user") {
|
|
return `user:${normalizeMatrixUserId(target.id)}`;
|
|
}
|
|
return `${normalizeLowercaseStringOrEmpty(target.kind)}:${target.id}`;
|
|
}
|
|
|
|
function resolveMatrixNativeTarget(raw: string): string | null {
|
|
const target = resolveMatrixTargetIdentity(raw);
|
|
if (!target) {
|
|
return null;
|
|
}
|
|
return target.kind === "user" ? `user:${target.id}` : `room:${target.id}`;
|
|
}
|
|
|
|
function resolveTurnSourceMatrixOriginTarget(request: ApprovalRequest): MatrixOriginTarget | null {
|
|
const turnSourceChannel = normalizeLowercaseStringOrEmpty(request.request.turnSourceChannel);
|
|
const turnSourceTo = request.request.turnSourceTo?.trim() || "";
|
|
const target = resolveMatrixNativeTarget(turnSourceTo);
|
|
if (turnSourceChannel !== "matrix" || !target) {
|
|
return null;
|
|
}
|
|
return {
|
|
to: target,
|
|
threadId: normalizeOptionalStringifiedId(request.request.turnSourceThreadId),
|
|
};
|
|
}
|
|
|
|
function resolveSessionMatrixOriginTarget(sessionTarget: {
|
|
to: string;
|
|
threadId?: string | number | null;
|
|
}): MatrixOriginTarget | null {
|
|
const target = resolveMatrixNativeTarget(sessionTarget.to);
|
|
if (!target) {
|
|
return null;
|
|
}
|
|
return {
|
|
to: target,
|
|
threadId: normalizeOptionalStringifiedId(sessionTarget.threadId),
|
|
};
|
|
}
|
|
|
|
function normalizeMatrixOriginTarget(target: MatrixOriginTarget): MatrixOriginTarget {
|
|
return {
|
|
...target,
|
|
to: normalizeComparableTarget(target.to),
|
|
};
|
|
}
|
|
|
|
function hasMatrixPluginApprovers(params: { cfg: CoreConfig; accountId?: string | null }): boolean {
|
|
return getMatrixApprovalAuthApprovers(params).length > 0;
|
|
}
|
|
|
|
function availabilityState(enabled: boolean) {
|
|
return enabled ? ({ kind: "enabled" } as const) : ({ kind: "disabled" } as const);
|
|
}
|
|
|
|
function hasMatrixApprovalApprovers(params: {
|
|
cfg: CoreConfig;
|
|
accountId?: string | null;
|
|
approvalKind: ApprovalKind;
|
|
}): boolean {
|
|
return (
|
|
getMatrixApprovalApprovers({
|
|
cfg: params.cfg,
|
|
accountId: params.accountId,
|
|
approvalKind: params.approvalKind,
|
|
}).length > 0
|
|
);
|
|
}
|
|
|
|
function hasAnyMatrixApprovalApprovers(params: {
|
|
cfg: CoreConfig;
|
|
accountId?: string | null;
|
|
}): boolean {
|
|
return (
|
|
getMatrixExecApprovalApprovers(params).length > 0 ||
|
|
getMatrixApprovalAuthApprovers(params).length > 0
|
|
);
|
|
}
|
|
|
|
function isMatrixPluginAuthorizedSender(params: {
|
|
cfg: CoreConfig;
|
|
accountId?: string | null;
|
|
senderId?: string | null;
|
|
}): boolean {
|
|
const normalizedSenderId = params.senderId
|
|
? normalizeMatrixApproverId(params.senderId)
|
|
: undefined;
|
|
if (!normalizedSenderId) {
|
|
return false;
|
|
}
|
|
return getMatrixApprovalAuthApprovers(params).includes(normalizedSenderId);
|
|
}
|
|
|
|
function resolveSuppressionAccountId(params: {
|
|
target: { accountId?: string | null };
|
|
request: { request: { turnSourceAccountId?: string | null } };
|
|
}): string | undefined {
|
|
return (
|
|
params.target.accountId?.trim() ||
|
|
params.request.request.turnSourceAccountId?.trim() ||
|
|
undefined
|
|
);
|
|
}
|
|
|
|
const resolveMatrixOriginTarget = createChannelNativeOriginTargetResolver({
|
|
channel: "matrix",
|
|
shouldHandleRequest: ({ cfg, accountId, request }) =>
|
|
shouldHandleMatrixApprovalRequest({
|
|
cfg,
|
|
accountId,
|
|
request,
|
|
}),
|
|
resolveTurnSourceTarget: resolveTurnSourceMatrixOriginTarget,
|
|
resolveSessionTarget: resolveSessionMatrixOriginTarget,
|
|
normalizeTargetForMatch: normalizeMatrixOriginTarget,
|
|
resolveFallbackTarget: (request) => {
|
|
const sessionConversation = resolveApprovalRequestSessionConversation({
|
|
request,
|
|
channel: "matrix",
|
|
});
|
|
if (!sessionConversation) {
|
|
return null;
|
|
}
|
|
const target = resolveMatrixNativeTarget(sessionConversation.id);
|
|
if (!target) {
|
|
return null;
|
|
}
|
|
return {
|
|
to: target,
|
|
threadId: normalizeOptionalStringifiedId(sessionConversation.threadId),
|
|
};
|
|
},
|
|
});
|
|
|
|
function resolveMatrixApproverDmTargets(params: {
|
|
cfg: CoreConfig;
|
|
accountId?: string | null;
|
|
approvalKind: ApprovalKind;
|
|
request: ApprovalRequest;
|
|
}): { to: string }[] {
|
|
if (!shouldHandleMatrixApprovalRequest(params)) {
|
|
return [];
|
|
}
|
|
return getMatrixApprovalApprovers(params)
|
|
.map((approver) => {
|
|
const normalized = normalizeMatrixUserId(approver);
|
|
return normalized ? { to: `user:${normalized}` } : null;
|
|
})
|
|
.filter((target): target is { to: string } => target !== null);
|
|
}
|
|
|
|
const matrixNativeApprovalCapability = createApproverRestrictedNativeApprovalCapability({
|
|
channel: "matrix",
|
|
channelLabel: "Matrix",
|
|
describeExecApprovalSetup: ({
|
|
accountId,
|
|
}: Parameters<NonNullable<ChannelApprovalCapability["describeExecApprovalSetup"]>>[0]) => {
|
|
const prefix =
|
|
accountId && accountId !== "default"
|
|
? `channels.matrix.accounts.${accountId}`
|
|
: "channels.matrix";
|
|
return `Approve it from the Web UI or terminal UI for now. Matrix supports native exec approvals for this account. Configure \`${prefix}.execApprovals.approvers\` or \`${prefix}.dm.allowFrom\`; leave \`${prefix}.execApprovals.enabled\` unset/\`auto\` or set it to \`true\`.`;
|
|
},
|
|
listAccountIds: listMatrixAccountIds,
|
|
hasApprovers: ({ cfg, accountId }) =>
|
|
hasAnyMatrixApprovalApprovers({
|
|
cfg: cfg as CoreConfig,
|
|
accountId,
|
|
}),
|
|
isExecAuthorizedSender: ({ cfg, accountId, senderId }) =>
|
|
isMatrixExecApprovalAuthorizedSender({ cfg, accountId, senderId }),
|
|
isPluginAuthorizedSender: ({ cfg, accountId, senderId }) =>
|
|
isMatrixPluginAuthorizedSender({
|
|
cfg: cfg as CoreConfig,
|
|
accountId,
|
|
senderId,
|
|
}),
|
|
isNativeDeliveryEnabled: ({ cfg, accountId }) =>
|
|
isMatrixExecApprovalClientEnabled({ cfg, accountId }),
|
|
resolveNativeDeliveryMode: ({ cfg, accountId }) =>
|
|
resolveMatrixExecApprovalTarget({ cfg, accountId }),
|
|
requireMatchingTurnSourceChannel: true,
|
|
resolveSuppressionAccountId,
|
|
resolveOriginTarget: resolveMatrixOriginTarget,
|
|
resolveApproverDmTargets: resolveMatrixApproverDmTargets,
|
|
notifyOriginWhenDmOnly: true,
|
|
nativeRuntime: createLazyChannelApprovalNativeRuntimeAdapter({
|
|
eventKinds: ["exec", "plugin"],
|
|
isConfigured: ({ cfg, accountId }) =>
|
|
isMatrixAnyApprovalClientEnabled({
|
|
cfg,
|
|
accountId,
|
|
}),
|
|
shouldHandle: ({ cfg, accountId, request }) =>
|
|
shouldHandleMatrixApprovalRequest({
|
|
cfg,
|
|
accountId,
|
|
request,
|
|
}),
|
|
load: async () =>
|
|
(await import("./approval-handler.runtime.js"))
|
|
.matrixApprovalNativeRuntime as unknown as ChannelApprovalNativeRuntimeAdapter,
|
|
}),
|
|
});
|
|
|
|
const splitMatrixApprovalCapability = splitChannelApprovalCapability(
|
|
matrixNativeApprovalCapability,
|
|
);
|
|
const matrixBaseNativeApprovalAdapter = splitMatrixApprovalCapability.native;
|
|
const matrixBaseDeliveryAdapter = splitMatrixApprovalCapability.delivery;
|
|
type MatrixForwardingSuppressionParams = Parameters<
|
|
NonNullable<NonNullable<typeof matrixBaseDeliveryAdapter>["shouldSuppressForwardingFallback"]>
|
|
>[0];
|
|
const matrixDeliveryAdapter = matrixBaseDeliveryAdapter && {
|
|
...matrixBaseDeliveryAdapter,
|
|
shouldSuppressForwardingFallback: (params: MatrixForwardingSuppressionParams) => {
|
|
const accountId = resolveSuppressionAccountId(params);
|
|
if (
|
|
!hasMatrixApprovalApprovers({
|
|
cfg: params.cfg as CoreConfig,
|
|
accountId,
|
|
approvalKind: params.approvalKind,
|
|
})
|
|
) {
|
|
return false;
|
|
}
|
|
return matrixBaseDeliveryAdapter.shouldSuppressForwardingFallback?.(params) ?? false;
|
|
},
|
|
};
|
|
const matrixNativeAdapter = matrixBaseNativeApprovalAdapter && {
|
|
describeDeliveryCapabilities: (
|
|
params: Parameters<typeof matrixBaseNativeApprovalAdapter.describeDeliveryCapabilities>[0],
|
|
) => {
|
|
const capabilities = matrixBaseNativeApprovalAdapter.describeDeliveryCapabilities(params);
|
|
const hasApprovers = hasMatrixApprovalApprovers({
|
|
cfg: params.cfg as CoreConfig,
|
|
accountId: params.accountId,
|
|
approvalKind: params.approvalKind,
|
|
});
|
|
const clientEnabled = isMatrixApprovalClientEnabled({
|
|
cfg: params.cfg,
|
|
accountId: params.accountId,
|
|
approvalKind: params.approvalKind,
|
|
});
|
|
return {
|
|
...capabilities,
|
|
enabled: capabilities.enabled && hasApprovers && clientEnabled,
|
|
};
|
|
},
|
|
resolveOriginTarget: matrixBaseNativeApprovalAdapter.resolveOriginTarget,
|
|
resolveApproverDmTargets: matrixBaseNativeApprovalAdapter.resolveApproverDmTargets,
|
|
};
|
|
|
|
export const matrixApprovalCapability = createChannelApprovalCapability({
|
|
authorizeActorAction: (
|
|
params: Parameters<NonNullable<ChannelApprovalCapability["authorizeActorAction"]>>[0],
|
|
) => {
|
|
if (params.approvalKind !== "plugin") {
|
|
return matrixNativeApprovalCapability.authorizeActorAction?.(params) ?? { authorized: true };
|
|
}
|
|
if (
|
|
!hasMatrixPluginApprovers({
|
|
cfg: params.cfg as CoreConfig,
|
|
accountId: params.accountId,
|
|
})
|
|
) {
|
|
return {
|
|
authorized: false,
|
|
reason: "❌ Matrix plugin approvals are not enabled for this bot account.",
|
|
} as const;
|
|
}
|
|
return matrixApprovalAuth.authorizeActorAction(params);
|
|
},
|
|
getActionAvailabilityState: (
|
|
params: Parameters<NonNullable<ChannelApprovalCapability["getActionAvailabilityState"]>>[0],
|
|
) => {
|
|
if (params.approvalKind === "plugin") {
|
|
return availabilityState(
|
|
hasMatrixPluginApprovers({
|
|
cfg: params.cfg as CoreConfig,
|
|
accountId: params.accountId,
|
|
}),
|
|
);
|
|
}
|
|
return (
|
|
matrixNativeApprovalCapability.getActionAvailabilityState?.(params) ?? {
|
|
kind: "disabled",
|
|
}
|
|
);
|
|
},
|
|
getExecInitiatingSurfaceState: (
|
|
params: Parameters<NonNullable<ChannelApprovalCapability["getExecInitiatingSurfaceState"]>>[0],
|
|
) =>
|
|
matrixNativeApprovalCapability.getExecInitiatingSurfaceState?.(params) ??
|
|
({ kind: "disabled" } as const),
|
|
describeExecApprovalSetup: matrixNativeApprovalCapability.describeExecApprovalSetup,
|
|
delivery: matrixDeliveryAdapter,
|
|
nativeRuntime: matrixNativeApprovalCapability.nativeRuntime,
|
|
native: matrixNativeAdapter,
|
|
render: matrixNativeApprovalCapability.render,
|
|
});
|