Skip to content

Commit 8c2d5f9

Browse files
authored
fix(security): warn on main-scoped group rooms (#125054)
* fix(security): warn on main-scoped group rooms * fix(security): ignore direct-only group scope bindings * fix(security): honor group binding precedence
1 parent 8f14578 commit 8c2d5f9

5 files changed

Lines changed: 255 additions & 1 deletion

File tree

‎docs/cli/security.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ If Gateway password auth is supplied only at startup, pass the same value with `
3030

3131
**DM/trust model**
3232

33-
- Warns when multiple DM senders share the main session and recommends secure DM mode: `session.dmScope="per-channel-peer"` (or `per-account-channel-peer` for multi-account channels) for shared inboxes. This is cooperative/shared-inbox hardening, not isolation for mutually untrusted operators; split trust boundaries with separate gateways (or separate OS users/hosts) for that. The same caveat applies to `session.groupScope: "main"` bindings, which merge every member of the matched room into the main session — reserve them for trusted rooms (see [Groups](/channels/groups#session-keys)).
33+
- Warns when multiple DM senders share the main session and recommends secure DM mode: `session.dmScope="per-channel-peer"` (or `per-account-channel-peer` for multi-account channels) for shared inboxes. This is cooperative/shared-inbox hardening, not isolation for mutually untrusted operators; split trust boundaries with separate gateways (or separate OS users/hosts) for that.
34+
- Emits `security.trust_model.group_scope_main` when global `session.groupScope="main"` or a binding override merges group/channel rooms into the main session. Every member of each matched room shares that context, so reserve this for trusted rooms (see [Groups](/channels/groups#session-keys)).
3435
- Emits `security.trust_model.multi_user_heuristic` when config suggests likely shared-user ingress (for example open DM/group policy, configured group targets, or wildcard sender rules) — OpenClaw's default trust model is personal-assistant (one operator), not hostile multi-tenant isolation. For intentional shared-user setups: sandbox all sessions, keep filesystem access workspace-scoped, and keep personal/private identities or credentials off that runtime.
3536
- Warns when small models (`<=300B` parameters) are used without sandboxing and with web/browser tools enabled.
3637

‎src/commands/agents.binding-format.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ export function describeBinding(binding: AgentRouteBinding): string {
1717
if (match.teamId) {
1818
parts.push(`team=${match.teamId}`);
1919
}
20+
if (match.roles?.length) {
21+
parts.push(`roles=${match.roles.join(",")}`);
22+
}
2023
return parts.join(" ");
2124
}

‎src/routing/resolve-route.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,67 @@ export function resolveAgentRoute(input: ResolveAgentRouteInput): ResolvedAgentR
828828
);
829829
}
830830

831+
/** @internal Lists bindings selectable by at least one group/channel route under runtime precedence. */
832+
export function listEffectiveGroupRouteBindings(cfg: OpenClawConfig) {
833+
const bindings = listBindings(cfg);
834+
const usedIds = new Set<string>();
835+
for (const binding of bindings) {
836+
usedIds.add(normalizeAccountId(binding.match.accountId));
837+
for (const value of [binding.match.peer?.id, binding.match.guildId, binding.match.teamId]) {
838+
const normalized = normalizeRouteBindingId(value);
839+
if (normalized) {
840+
usedIds.add(normalized);
841+
}
842+
}
843+
}
844+
let sentinel = "openclaw-audit-route";
845+
while (usedIds.has(sentinel)) {
846+
sentinel += "-next";
847+
}
848+
849+
const markerForIndex = (index: number) => `audit-binding-${index}`;
850+
const probeCfg: OpenClawConfig = {
851+
...cfg,
852+
agents: { entries: {} },
853+
bindings: bindings.map((binding, index) => ({ ...binding, agentId: markerForIndex(index) })),
854+
};
855+
856+
return bindings.filter((binding, index) => {
857+
const match = normalizeBindingMatch(binding.match);
858+
if (
859+
match.peer.state === "invalid" ||
860+
((match.peer.state === "valid" || match.peer.state === "wildcard-kind") &&
861+
match.peer.kind === "direct")
862+
) {
863+
return false;
864+
}
865+
const peer: RoutePeer =
866+
match.peer.state === "valid"
867+
? { kind: match.peer.kind, id: match.peer.id }
868+
: match.peer.state === "wildcard-kind"
869+
? { kind: match.peer.kind, id: sentinel }
870+
: { kind: "group", id: sentinel };
871+
const accountId = match.accountPattern === "*" ? sentinel : match.accountPattern;
872+
const roleWitnesses = match.roles?.map((role) => [role]) ?? [[]];
873+
874+
// Equality/wildcard fields need one fresh value for each open domain. Role matching
875+
// is positive OR, so singleton candidate roles prove existence without sampling.
876+
return roleWitnesses.some(
877+
(memberRoleIds) =>
878+
resolveAgentRoute({
879+
cfg: probeCfg,
880+
channel: binding.match.channel,
881+
defaultAgentId: DEFAULT_AGENT_ID,
882+
accountId,
883+
peer,
884+
guildId: match.guildId,
885+
teamId: match.teamId,
886+
memberRoleIds,
887+
}).agentId === markerForIndex(index),
888+
);
889+
});
890+
}
891+
831892
/** @internal Resolves fallback precedence for an unknown direct peer. */
832893
export function resolveUnknownDirectMessageRoute(
833894
input: Pick<ResolveAgentRouteInput, "cfg" | "channel" | "accountId" | "dmScope" | "groupScope">,

‎src/security/audit-extra.sync.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { isDangerousNetworkMode, normalizeNetworkMode } from "../agents/sandbox/
1313
import { getBlockedBindReason } from "../agents/sandbox/validate-sandbox-security.js";
1414
import { isToolAllowedByPolicies } from "../agents/tool-policy-match.js";
1515
import { formatCliCommand } from "../cli/command-format.js";
16+
import { describeBinding } from "../commands/agents.binding-format.js";
1617
import type { GatewayAuthConfig } from "../config/types.gateway.js";
1718
import type { OpenClawConfig } from "../config/types.openclaw.js";
1819
import type { AgentToolsConfig } from "../config/types.tools.js";
@@ -23,6 +24,7 @@ import {
2324
listDangerousPluginNodeCommands,
2425
resolveNodeCommandAllowlist,
2526
} from "../gateway/node-command-policy.js";
27+
import { listEffectiveGroupRouteBindings } from "../routing/resolve-route.js";
2628
import { collectAuditModelRefs } from "./audit-model-refs.js";
2729
import { GATEWAY_CONTROL_PLANE_TOOLS } from "./dangerous-tools.js";
2830

@@ -1241,6 +1243,31 @@ export function collectExposureMatrixFindings(cfg: OpenClawConfig): SecurityAudi
12411243

12421244
export function collectLikelyMultiUserSetupFindings(cfg: OpenClawConfig): SecurityAuditFinding[] {
12431245
const findings: SecurityAuditFinding[] = [];
1246+
const mainGroupScopes = listEffectiveGroupRouteBindings(cfg)
1247+
.filter((binding) => binding.session?.groupScope === "main")
1248+
.map(
1249+
(binding) =>
1250+
`- bindings[].session.groupScope="main": ${describeBinding(binding)} (agent=${binding.agentId})`,
1251+
);
1252+
if (cfg.session?.groupScope === "main") {
1253+
mainGroupScopes.unshift(
1254+
'- session.groupScope="main" (global: all group/channel rooms unless a binding overrides it)',
1255+
);
1256+
}
1257+
if (mainGroupScopes.length > 0) {
1258+
findings.push({
1259+
checkId: "security.trust_model.group_scope_main",
1260+
severity: "warn",
1261+
title: "Group rooms share the main session",
1262+
detail:
1263+
"The following group routing scopes merge room conversations into the agent main session:\n" +
1264+
mainGroupScopes.join("\n") +
1265+
"\nEvery member of each affected room shares the main-session context. Use this only for mutually trusted rooms.",
1266+
remediation:
1267+
'Use session.groupScope="per-group" globally and remove binding overrides, or reserve "main" for rooms whose members you trust: https://docs.openclaw.ai/channels/groups#session-keys',
1268+
});
1269+
}
1270+
12441271
const signals = listPotentialMultiUserSignals(cfg);
12451272
if (signals.length === 0) {
12461273
return findings;

‎src/security/audit-trust-model.test.ts‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ function requireMultiUserHeuristicFinding(findings: ReturnType<typeof audit>) {
2020
return finding;
2121
}
2222

23+
function requireGroupScopeMainFinding(findings: ReturnType<typeof audit>) {
24+
const finding = findings.find(
25+
(entry) => entry.checkId === "security.trust_model.group_scope_main",
26+
);
27+
if (!finding) {
28+
throw new Error("Expected group-scope main finding");
29+
}
30+
return finding;
31+
}
32+
2333
describe("security audit trust model findings", () => {
2434
it("evaluates trust-model exposure findings", () => {
2535
const cases = [
@@ -140,6 +150,158 @@ describe("security audit trust model findings", () => {
140150
(finding) => finding.checkId === "security.trust_model.multi_user_heuristic",
141151
),
142152
).toBe(false);
153+
expect(
154+
findings.some((finding) => finding.checkId === "security.trust_model.group_scope_main"),
155+
).toBe(false);
156+
},
157+
},
158+
{
159+
name: "warns when global group scope shares all rooms with the main session",
160+
cfg: {
161+
session: { groupScope: "main" },
162+
} satisfies OpenClawConfig,
163+
assert: (findings: ReturnType<typeof audit>) => {
164+
const finding = requireGroupScopeMainFinding(findings);
165+
expect(finding).toMatchObject({
166+
severity: "warn",
167+
title: "Group rooms share the main session",
168+
});
169+
expect(finding.detail).toContain('session.groupScope="main"');
170+
expect(finding.detail).toContain("all group/channel rooms");
171+
expect(finding.remediation).toContain(
172+
"https://docs.openclaw.ai/channels/groups#session-keys",
173+
);
174+
},
175+
},
176+
{
177+
name: "warns with the matched room for binding group scope",
178+
cfg: {
179+
session: { groupScope: "per-group" },
180+
bindings: [
181+
{
182+
agentId: "support",
183+
match: {
184+
channel: "discord",
185+
accountId: "work",
186+
peer: { kind: "channel", id: "1234567890" },
187+
guildId: "9876543210",
188+
roles: ["operators", "reviewers"],
189+
},
190+
session: { groupScope: "main" },
191+
},
192+
],
193+
} satisfies OpenClawConfig,
194+
assert: (findings: ReturnType<typeof audit>) => {
195+
const finding = requireGroupScopeMainFinding(findings);
196+
expect(finding.severity).toBe("warn");
197+
expect(finding.detail).toContain(
198+
"discord accountId=work peer=channel:1234567890 guild=9876543210 roles=operators,reviewers",
199+
);
200+
expect(finding.detail).not.toContain("all group/channel rooms");
201+
},
202+
},
203+
{
204+
name: "does not warn for a direct-only binding group scope",
205+
cfg: {
206+
bindings: [
207+
{
208+
agentId: "support",
209+
match: {
210+
channel: "whatsapp",
211+
peer: { kind: "direct", id: "user-a" },
212+
},
213+
session: { groupScope: "main" },
214+
},
215+
],
216+
} satisfies OpenClawConfig,
217+
assert: (findings: ReturnType<typeof audit>) => {
218+
expect(
219+
findings.some((finding) => finding.checkId === "security.trust_model.group_scope_main"),
220+
).toBe(false);
221+
},
222+
},
223+
{
224+
name: "does not warn for a main-scoped room binding shadowed by an earlier equivalent binding",
225+
cfg: {
226+
bindings: [
227+
{
228+
agentId: "isolated",
229+
match: {
230+
channel: "discord",
231+
accountId: "work",
232+
peer: { kind: "group", id: "room-1" },
233+
},
234+
session: { groupScope: "per-group" },
235+
},
236+
{
237+
agentId: "shared",
238+
match: {
239+
channel: "discord",
240+
accountId: "work",
241+
peer: { kind: "channel", id: "room-1" },
242+
},
243+
session: { groupScope: "main" },
244+
},
245+
],
246+
} satisfies OpenClawConfig,
247+
assert: (findings: ReturnType<typeof audit>) => {
248+
expect(
249+
findings.some((finding) => finding.checkId === "security.trust_model.group_scope_main"),
250+
).toBe(false);
251+
},
252+
},
253+
{
254+
name: "warns when a main-scoped room binding precedes an equivalent binding",
255+
cfg: {
256+
bindings: [
257+
{
258+
agentId: "shared",
259+
match: {
260+
channel: "discord",
261+
accountId: "work",
262+
peer: { kind: "channel", id: "room-1" },
263+
},
264+
session: { groupScope: "main" },
265+
},
266+
{
267+
agentId: "isolated",
268+
match: {
269+
channel: "discord",
270+
accountId: "work",
271+
peer: { kind: "group", id: "room-1" },
272+
},
273+
session: { groupScope: "per-group" },
274+
},
275+
],
276+
} satisfies OpenClawConfig,
277+
assert: (findings: ReturnType<typeof audit>) => {
278+
const finding = requireGroupScopeMainFinding(findings);
279+
expect(finding.detail).toContain("discord accountId=work peer=channel:room-1");
280+
},
281+
},
282+
{
283+
name: "warns for a more-specific main-scoped room binding after a broader binding",
284+
cfg: {
285+
bindings: [
286+
{
287+
agentId: "isolated",
288+
match: { channel: "discord", accountId: "work" },
289+
session: { groupScope: "per-group" },
290+
},
291+
{
292+
agentId: "shared",
293+
match: {
294+
channel: "discord",
295+
accountId: "work",
296+
peer: { kind: "group", id: "room-1" },
297+
},
298+
session: { groupScope: "main" },
299+
},
300+
],
301+
} satisfies OpenClawConfig,
302+
assert: (findings: ReturnType<typeof audit>) => {
303+
const finding = requireGroupScopeMainFinding(findings);
304+
expect(finding.detail).toContain("discord accountId=work peer=group:room-1");
143305
},
144306
},
145307
{

0 commit comments

Comments
 (0)