Skip to content

Commit 20b453f

Browse files
authored
fix(agents): reject forged plugin owner authority (#104872)
* fix(security): bind channel owner authority to trusted runtime * fix(plugins): preserve untrusted channel guest ingress * fix(plugins): snapshot channel ingress authority once
1 parent e827006 commit 20b453f

15 files changed

Lines changed: 205 additions & 9 deletions

File tree

‎docs/plugins/sdk-runtime.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Provider and channel execution paths must use the active runtime config snapshot
5555

5656
## Reusable runtime utilities
5757

58+
Channel plugins must admit authenticated agent turns through their injected
59+
`api.runtime.agent.runCommandFromIngress(options, runtime)` capability. The host
60+
accepts owner authority only from the exact active, trusted plugin registered for
61+
`options.messageChannel`; guest turns retain their non-owner identity. The public
62+
`agentCommandFromIngress` SDK helper never accepts a caller-supplied owner claim.
63+
5864
Model-picker integrations use two focused runtime subpaths. Import the typed
5965
`ModelPickerAction` and `ModelPickerCapabilityProfile` contracts from
6066
`openclaw/plugin-sdk/interactive-runtime`. Import

‎extensions/discord/src/voice/ingress.test.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ const mocks = vi.hoisted(() => ({
1313
})),
1414
}));
1515

16-
vi.mock("openclaw/plugin-sdk/agent-runtime", () => ({
17-
agentCommandFromIngress: mocks.agentCommandFromIngress,
16+
vi.mock("../runtime.js", () => ({
17+
getDiscordRuntime: () => ({
18+
agent: { runCommandFromIngress: mocks.agentCommandFromIngress },
19+
}),
1820
}));
1921

2022
import { runDiscordVoiceAgentTurn } from "./ingress.js";
@@ -53,4 +55,28 @@ describe("Discord voice ingress execution correlation", () => {
5355
expect(input).not.toHaveProperty("runId");
5456
}
5557
});
58+
59+
it.each([true, false])("preserves authenticated Discord speaker owner=%s", async (owner) => {
60+
await runDiscordVoiceAgentTurn({
61+
entry: {
62+
guildId: "guild-1",
63+
channelId: "channel-1",
64+
route: { agentId: "main", sessionKey: "agent:main:discord:channel:channel-1" },
65+
} as never,
66+
accountId: "work",
67+
userId: owner ? "owner-1" : "guest-1",
68+
message: "run the tool",
69+
cfg: {} as never,
70+
discordConfig: {} as never,
71+
runtime: { log: vi.fn(), error: vi.fn() } as never,
72+
context: { senderIsOwner: owner, speakerLabel: owner ? "Owner" : "Guest" },
73+
fetchGuildName: vi.fn(async () => "Guild"),
74+
speakerContext: {} as never,
75+
});
76+
77+
expect(mocks.agentCommandFromIngress).toHaveBeenLastCalledWith(
78+
expect.objectContaining({ messageChannel: "discord", senderIsOwner: owner }),
79+
expect.anything(),
80+
);
81+
});
5682
});

‎extensions/discord/src/voice/ingress.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Discord plugin module implements ingress behavior.
2-
import { agentCommandFromIngress } from "openclaw/plugin-sdk/agent-runtime";
32
import type { DiscordAccountConfig, OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
43
import { resolveRealtimeBootstrapContextInstructions } from "openclaw/plugin-sdk/realtime-bootstrap-context";
54
import { createSubsystemLogger, type RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
65
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
76
import { formatMention } from "../mentions.js";
87
import { normalizeDiscordSlug } from "../monitor/allow-list.js";
98
import { buildDiscordGroupSystemPrompt } from "../monitor/inbound-context.js";
9+
import { getDiscordRuntime } from "../runtime.js";
1010
import { authorizeDiscordVoiceIngress } from "./access.js";
1111
import type { VoiceSessionEntry } from "./session.js";
1212
import type { DiscordVoiceSpeakerContextResolver } from "./speaker-context.js";
@@ -133,7 +133,7 @@ export async function runDiscordVoiceAgentTurn(params: {
133133
return null;
134134
}
135135
const voiceModel = normalizeOptionalString(params.discordConfig.voice?.model);
136-
const result = await agentCommandFromIngress(
136+
const result = await getDiscordRuntime().agent.runCommandFromIngress(
137137
{
138138
message: params.message,
139139
sessionKey: params.entry.route.sessionKey,

‎extensions/discord/src/voice/voice-test-mocks.test-support.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ vi.mock("./participant-context.js", async () => {
428428

429429
vi.mock("../runtime.js", () => ({
430430
getDiscordRuntime: () => ({
431+
agent: { runCommandFromIngress: agentCommandMock },
431432
mediaUnderstanding: {
432433
transcribeAudioFile: transcribeAudioFileMock,
433434
},

‎scripts/e2e/lib/codex-npm-plugin-live/followthrough-turn.mjs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const result = await agentCommandFromIngress(
4747
messageChannel: "webchat",
4848
channel: "webchat",
4949
sourceReplyDeliveryMode: "message_tool_only",
50-
senderIsOwner: true,
5150
allowModelOverride: true,
5251
// The embedded one-shot path retires bundled runtime resources; the Codex
5352
// harness uses this signal to close its shared app-server client and child.

‎src/agents/agent-command-execution-identity.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export function sanitizePublicAgentCommandIngressOpts(
184184
): AgentCommandGatewayIngressOpts {
185185
return withoutAgentCommandExecutionIdentitySpawnFacts({
186186
...opts,
187+
senderIsOwner: false,
187188
mainRestartRecoveryOwnerLease: undefined,
188189
mainRestartRecoveryAdmitted: undefined,
189190
mainRestartRecoveryAttempt: undefined,

‎src/agents/command/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export type AgentCommandIngressOpts = Omit<
247247
| "onAdmittedRunContext"
248248
| "onPostAdmittedRunContext"
249249
> & {
250-
/** Trusted sender identity bit for command/channel-action auth; defaults false for ingress. */
250+
/** @deprecated Public ingress ignores owner claims; use the host-injected channel runtime. */
251251
senderIsOwner?: boolean;
252252
/** Ingress callsites must always pass explicit model-override authorization state. */
253253
allowModelOverride: boolean;

‎src/commands/agent.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ describe("agentCommand", () => {
862862
agentId: "main",
863863
runId: "public-ingress-run",
864864
allowModelOverride: false,
865+
senderIsOwner: true,
865866
mainRestartRecoveryAdmitted: true,
866867
mainRestartRecoveryAttempt: 1,
867868
mainRestartRecoveryOwnerLease: {
@@ -890,6 +891,9 @@ describe("agentCommand", () => {
890891
expect(prepare).toHaveBeenCalledWith(
891892
expect.objectContaining({ admission: undefined, runId: "public-ingress-run" }),
892893
);
894+
expect(
895+
vi.mocked(attemptExecutionRuntime.runAgentAttempt).mock.calls.at(-1)?.[0].opts,
896+
).toMatchObject({ senderIsOwner: false });
893897
} finally {
894898
prepare.mockRestore();
895899
if (priorDescriptor) {

‎src/plugin-sdk/test-helpers/plugin-runtime-mock.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ export function createPluginRuntimeMock(overrides: DeepPartial<PluginRuntime> =
565565
{ id: "high", label: "high" },
566566
],
567567
})),
568+
runCommandFromIngress: vi.fn<PluginRuntime["agent"]["runCommandFromIngress"]>(),
568569
runEmbeddedAgent: runEmbeddedAgentMock,
569570
resolveAgentTimeoutMs: vi.fn<PluginRuntime["agent"]["resolveAgentTimeoutMs"]>(() => 30_000),
570571
ensureAgentWorkspace: vi

‎src/plugins/registry-runtime.channel-ingress.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
1212
import type { PluginOrigin } from "./plugin-origin.types.js";
1313
import { markPluginRegistryActive, markPluginRegistryRetired } from "./registry-lifecycle.js";
1414
import { createPluginRegistry } from "./registry.js";
15+
import { createPluginRuntime } from "./runtime/index.js";
1516
import type { PluginRuntime } from "./runtime/types.js";
1617
import { createPluginRecord } from "./status.test-fixtures.js";
1718

@@ -134,6 +135,91 @@ function inspect(context: object) {
134135
}
135136

136137
describe("bundled channel ingress runtime ownership", () => {
138+
it("binds authenticated owner turns to the exact live trusted channel plugin", async () => {
139+
const runtime = createPluginRuntime();
140+
const command = vi.fn(async () => ({ payloads: [] }));
141+
Object.defineProperty(runtime.agent, "runCommandFromIngress", {
142+
configurable: true,
143+
value: command,
144+
});
145+
const registryBuilder = createPluginRegistry({
146+
logger: { info() {}, warn() {}, error() {}, debug() {} },
147+
runtime,
148+
activateGlobalSideEffects: false,
149+
});
150+
const owner = createPluginRecord({ id: "discord", origin: "bundled" });
151+
const foreign = createPluginRecord({ id: "foreign", origin: "bundled" });
152+
const untrusted = createPluginRecord({ id: "impostor", origin: "workspace" });
153+
const ownerApi = registryBuilder.createApi(owner, { config: {} as OpenClawConfig });
154+
const foreignApi = registryBuilder.createApi(foreign, { config: {} as OpenClawConfig });
155+
const untrustedApi = registryBuilder.createApi(untrusted, { config: {} as OpenClawConfig });
156+
registryBuilder.registry.plugins.push(owner, foreign, untrusted);
157+
registryBuilder.registry.channels.push(
158+
{
159+
pluginId: "discord",
160+
plugin: { id: "discord" },
161+
source: owner.source,
162+
} as never,
163+
{
164+
pluginId: "impostor",
165+
plugin: { id: "community" },
166+
source: untrusted.source,
167+
} as never,
168+
);
169+
markPluginRegistryActive(registryBuilder.registry);
170+
const options = {
171+
message: "owner turn",
172+
messageChannel: "discord" as const,
173+
senderIsOwner: true,
174+
allowModelOverride: false,
175+
};
176+
const commandRuntime = { log: vi.fn(), error: vi.fn() } as never;
177+
const retained = ownerApi.runtime.agent.runCommandFromIngress;
178+
179+
await expect(retained(options, commandRuntime)).resolves.toEqual({ payloads: [] });
180+
expect(command).toHaveBeenCalledWith(options, commandRuntime);
181+
await expect(
182+
foreignApi.runtime.agent.runCommandFromIngress(options, commandRuntime),
183+
).rejects.toThrow('Plugin "foreign" cannot admit authenticated owner authority');
184+
const guestOptions = { ...options, messageChannel: "community", senderIsOwner: false };
185+
const retainedGuest = untrustedApi.runtime.agent.runCommandFromIngress;
186+
await expect(retainedGuest(guestOptions, commandRuntime)).resolves.toEqual({ payloads: [] });
187+
expect(command).toHaveBeenLastCalledWith(guestOptions, commandRuntime);
188+
let ownerClaimReads = 0;
189+
let channelReads = 0;
190+
const changingGuestOptions = {
191+
...guestOptions,
192+
get senderIsOwner() {
193+
return ownerClaimReads++ > 0;
194+
},
195+
get messageChannel() {
196+
return channelReads++ === 0 ? "community" : "discord";
197+
},
198+
};
199+
await expect(retainedGuest(changingGuestOptions, commandRuntime)).resolves.toEqual({
200+
payloads: [],
201+
});
202+
expect(command).toHaveBeenLastCalledWith(
203+
expect.objectContaining({ messageChannel: "community", senderIsOwner: false }),
204+
commandRuntime,
205+
);
206+
expect(ownerClaimReads).toBe(1);
207+
expect(channelReads).toBe(1);
208+
await expect(
209+
retainedGuest({ ...guestOptions, senderIsOwner: true }, commandRuntime),
210+
).rejects.toThrow('Plugin "impostor" cannot admit authenticated owner authority');
211+
212+
registryBuilder.rollbackPluginGlobalSideEffects(owner.id, owner);
213+
await expect(retained(options, commandRuntime)).rejects.toThrow(
214+
'Plugin "discord" cannot admit authenticated owner authority',
215+
);
216+
registryBuilder.rollbackPluginGlobalSideEffects(untrusted.id, untrusted);
217+
await expect(retainedGuest(guestOptions, commandRuntime)).rejects.toThrow(
218+
'Plugin "impostor" cannot admit authenticated owner authority',
219+
);
220+
expect(command).toHaveBeenCalledTimes(3);
221+
});
222+
137223
it("defers and preserves the exact active runtime across an inactive prepared load", async () => {
138224
let channelReads = 0;
139225
const channel = { inbound: { buildContext: buildChannelInboundEventContext } };

0 commit comments

Comments
 (0)