Skip to content

Commit 20945b8

Browse files
committed
feat: generalize message access groups (#75813)
1 parent b217cd0 commit 20945b8

30 files changed

Lines changed: 885 additions & 92 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Docs: https://docs.openclaw.ai
88

99
- Providers/OpenAI: add `extraBody`/`extra_body` passthrough for OpenAI-compatible TTS endpoints, so custom speech servers can receive fields such as `lang` in `/audio/speech` requests. Fixes #39900. Thanks @R3NK0R.
1010
- Dependencies: refresh workspace dependency pins, including TypeBox 1.1.37, AWS SDK 3.1041.0, Microsoft Teams 2.0.9, and Marked 18.0.3. Thanks @mariozechner, @aws, and @microsoft.
11+
- Discord/channels: add reusable message-channel access groups plus Discord channel-audience DM authorization, so allowlists can reference `accessGroup:<name>` across channel auth paths. (#75813) Thanks @clawsweeper.
1112

1213
### Fixes
1314

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
e14ddc6b9859128d4c5561cf80f322b7b24e0f87dac5bff170afbf2d6a9c3711 config-baseline.json
2-
2b1eac57f1b08b461e4cb9931a766f472c668e18aedd78e2af89541d8b476933 config-baseline.core.json
1+
74530fefef9ed55cab302802bc0be413ec56929e73c12d4bf4f1e4d290813adc config-baseline.json
2+
21db87c2ebec8844e20bf66ea474c08f3adab842234ff334870fe3e8d87995b4 config-baseline.core.json
33
c401cd3450f1737bc92418cfea301d20b54b7fbef9e6049834acc01af338e538 config-baseline.channel.json
44
7731a0b93cb335b56fac4c807447ba659fea51ea7a6cd844dc0ef5616669ee75 config-baseline.plugin.json
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
edf54c8ce4c65d44ade9953509b1c3264f4ed12c8bf8eb0a13703a76d185f744 plugin-sdk-api-baseline.json
2-
2418f2484d2d5b40ec8c9b3b92562c76abae43845bb18af0d59706848422555c plugin-sdk-api-baseline.jsonl
1+
0f9284c6349bf03d3d89c1d25031031840dae4ade032622ca212240ed19829f6 plugin-sdk-api-baseline.json
2+
33706cf425386717973cc87357ae5e0df432dd5a519b4faea8b38e21d7daae78 plugin-sdk-api-baseline.jsonl

‎docs/channels/discord.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,29 @@ Example:
452452
<Tab title="DM access groups">
453453
Discord DMs can use dynamic `accessGroup:<name>` entries in `channels.discord.allowFrom`.
454454

455+
Access group names are shared across message channels. Use `type: "message.senders"` for a static group whose members are expressed in each channel's normal `allowFrom` syntax, or `type: "discord.channelAudience"` when a Discord channel's current `ViewChannel` audience should define membership dynamically.
456+
457+
```json5
458+
{
459+
accessGroups: {
460+
operators: {
461+
type: "message.senders",
462+
members: {
463+
"*": ["global-owner-id"],
464+
discord: ["discord:123456789012345678"],
465+
telegram: ["987654321"],
466+
},
467+
},
468+
},
469+
channels: {
470+
discord: {
471+
dmPolicy: "allowlist",
472+
allowFrom: ["accessGroup:operators"],
473+
},
474+
},
475+
}
476+
```
477+
455478
A Discord text channel has no separate member list. `type: "discord.channelAudience"` models membership as: the DM sender is a member of the configured guild and currently has effective `ViewChannel` permission on the configured channel after role and channel overwrites are applied.
456479

457480
Example: allow anyone who can see `#maintainers` to DM the bot, while keeping DMs closed to everyone else.

‎docs/channels/pairing.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ access; they do not add more owners.
4747

4848
Supported channels: `bluebubbles`, `discord`, `feishu`, `googlechat`, `imessage`, `irc`, `line`, `matrix`, `mattermost`, `msteams`, `nextcloud-talk`, `nostr`, `openclaw-weixin`, `signal`, `slack`, `synology-chat`, `telegram`, `twitch`, `whatsapp`, `zalo`, `zalouser`.
4949

50+
### Reusable sender groups
51+
52+
Use top-level `accessGroups` when the same trusted sender set should apply to multiple message channels or to both DM and group allowlists. Static sender groups use `type: "message.senders"` and list members in each channel's normal `allowFrom` syntax.
53+
54+
```json5
55+
{
56+
accessGroups: {
57+
operators: {
58+
type: "message.senders",
59+
members: {
60+
"*": ["global-owner-id"],
61+
discord: ["discord:123456789012345678"],
62+
telegram: ["987654321"],
63+
whatsapp: ["+15551234567"],
64+
},
65+
},
66+
},
67+
channels: {
68+
telegram: { dmPolicy: "allowlist", allowFrom: ["accessGroup:operators"] },
69+
whatsapp: { groupPolicy: "allowlist", groupAllowFrom: ["accessGroup:operators"] },
70+
},
71+
}
72+
```
73+
74+
The `"*"` member list is shared by all message channels. Channel-specific lists are checked with that channel's own sender matching rules.
75+
5076
### Where the state lives
5177

5278
Stored under `~/.openclaw/credentials/`:
Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,63 @@
1+
import {
2+
ACCESS_GROUP_ALLOW_FROM_PREFIX,
3+
parseAccessGroupAllowFromEntry,
4+
resolveAccessGroupAllowFromMatches,
5+
type AccessGroupMembershipResolver,
6+
} from "openclaw/plugin-sdk/command-auth";
17
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
28
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
39
import type { RequestClient } from "../internal/discord.js";
410
import { canViewDiscordGuildChannel } from "../send.permissions.js";
511

6-
export const DISCORD_ACCESS_GROUP_PREFIX = "accessGroup:";
12+
export const DISCORD_ACCESS_GROUP_PREFIX = ACCESS_GROUP_ALLOW_FROM_PREFIX;
713

814
export function parseDiscordAccessGroupEntry(entry: string): string | null {
9-
const trimmed = entry.trim();
10-
if (!trimmed.startsWith(DISCORD_ACCESS_GROUP_PREFIX)) {
11-
return null;
12-
}
13-
const name = trimmed.slice(DISCORD_ACCESS_GROUP_PREFIX.length).trim();
14-
return name.length > 0 ? name : null;
15+
return parseAccessGroupAllowFromEntry(entry);
1516
}
1617

17-
export async function resolveDiscordDmAccessGroupEntries(params: {
18-
cfg?: OpenClawConfig;
19-
allowFrom: string[];
20-
sender: { id: string };
21-
accountId: string;
18+
export function createDiscordAccessGroupMembershipResolver(params: {
2219
token?: string;
2320
rest?: RequestClient;
24-
}): Promise<string[]> {
25-
const names = Array.from(
26-
new Set(
27-
params.allowFrom
28-
.map((entry) => parseDiscordAccessGroupEntry(entry))
29-
.filter((entry): entry is string => entry != null),
30-
),
31-
);
32-
if (names.length === 0 || !params.cfg?.accessGroups) {
33-
return [];
34-
}
35-
36-
const matched: string[] = [];
37-
for (const name of names) {
38-
const group = params.cfg.accessGroups[name];
39-
if (!group) {
40-
continue;
41-
}
21+
}): AccessGroupMembershipResolver {
22+
return async ({ cfg, name, group, accountId, senderId }) => {
4223
if (group.type !== "discord.channelAudience") {
43-
continue;
24+
return false;
4425
}
4526
const membership = group.membership ?? "canViewChannel";
4627
if (membership !== "canViewChannel") {
47-
continue;
28+
return false;
4829
}
49-
const allowed = await canViewDiscordGuildChannel(
50-
group.guildId,
51-
group.channelId,
52-
params.sender.id,
53-
{
54-
cfg: params.cfg,
55-
accountId: params.accountId,
56-
token: params.token,
57-
rest: params.rest,
58-
},
59-
).catch((err) => {
60-
logVerbose(
61-
`discord: accessGroup:${name} lookup failed for user ${params.sender.id}: ${String(err)}`,
62-
);
30+
return await canViewDiscordGuildChannel(group.guildId, group.channelId, senderId, {
31+
cfg,
32+
accountId,
33+
token: params.token,
34+
rest: params.rest,
35+
}).catch((err) => {
36+
logVerbose(`discord: accessGroup:${name} lookup failed for user ${senderId}: ${String(err)}`);
6337
return false;
6438
});
65-
if (allowed) {
66-
matched.push(`${DISCORD_ACCESS_GROUP_PREFIX}${name}`);
67-
}
68-
}
69-
return matched;
39+
};
40+
}
41+
42+
export async function resolveDiscordDmAccessGroupEntries(params: {
43+
cfg?: OpenClawConfig;
44+
allowFrom: string[];
45+
sender: { id: string };
46+
accountId: string;
47+
token?: string;
48+
rest?: RequestClient;
49+
isSenderAllowed?: (senderId: string, allowFrom: string[]) => boolean;
50+
}): Promise<string[]> {
51+
return await resolveAccessGroupAllowFromMatches({
52+
cfg: params.cfg,
53+
allowFrom: params.allowFrom,
54+
channel: "discord",
55+
accountId: params.accountId,
56+
senderId: params.sender.id,
57+
isSenderAllowed: params.isSenderAllowed,
58+
resolveMembership: createDiscordAccessGroupMembershipResolver({
59+
token: params.token,
60+
rest: params.rest,
61+
}),
62+
});
7063
}

‎extensions/discord/src/monitor/agent-components-dm-auth.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ async function ensureDmComponentAuthorized(params: {
5757
sender: { id: user.id },
5858
accountId: ctx.accountId,
5959
token: ctx.token,
60+
isSenderAllowed: (senderId, allowFrom) =>
61+
resolveAllowMatch(allowFrom).allowed || allowFrom.includes(senderId),
6062
});
6163
return matchedGroups.length > 0
6264
? resolveAllowMatch([...entries, `discord:${user.id}`])

‎extensions/discord/src/monitor/dm-command-auth.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,33 @@ describe("resolveDiscordDmCommandAccess", () => {
127127
expect(result.commandAuthorized).toBe(true);
128128
});
129129

130+
it("authorizes allowlist DMs from a generic message sender access group", async () => {
131+
const result = await resolveDiscordDmCommandAccess({
132+
accountId: "default",
133+
dmPolicy: "allowlist",
134+
configuredAllowFrom: ["accessGroup:owners"],
135+
sender,
136+
allowNameMatching: false,
137+
useAccessGroups: true,
138+
cfg: {
139+
accessGroups: {
140+
owners: {
141+
type: "message.senders",
142+
members: {
143+
discord: ["discord:123"],
144+
telegram: ["987"],
145+
},
146+
},
147+
},
148+
},
149+
readStoreAllowFrom: async () => [],
150+
});
151+
152+
expect(canViewDiscordGuildChannelMock).not.toHaveBeenCalled();
153+
expect(result.decision).toBe("allow");
154+
expect(result.commandAuthorized).toBe(true);
155+
});
156+
130157
it("fails closed when a Discord channel audience access group lookup rejects", async () => {
131158
canViewDiscordGuildChannelMock.mockRejectedValueOnce(new Error("missing intent"));
132159

‎extensions/discord/src/monitor/dm-command-auth.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { expandAllowFromWithAccessGroups } from "openclaw/plugin-sdk/command-auth";
12
import { resolveCommandAuthorizedFromAuthorizers } from "openclaw/plugin-sdk/command-auth-native";
23
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
34
import {
@@ -6,7 +7,7 @@ import {
67
type DmGroupAccessDecision,
78
} from "openclaw/plugin-sdk/security-runtime";
89
import type { RequestClient } from "../internal/discord.js";
9-
import { resolveDiscordDmAccessGroupEntries } from "./access-groups.js";
10+
import { createDiscordAccessGroupMembershipResolver } from "./access-groups.js";
1011
import { normalizeDiscordAllowList, resolveDiscordAllowListMatch } from "./allow-list.js";
1112

1213
const DISCORD_ALLOW_LIST_PREFIXES = ["discord:", "user:", "pk:"];
@@ -50,11 +51,24 @@ async function expandAllowFromWithDiscordAccessGroups(params: {
5051
token?: string;
5152
rest?: RequestClient;
5253
}) {
53-
const matchedGroups = await resolveDiscordDmAccessGroupEntries(params);
54-
if (matchedGroups.length === 0) {
55-
return params.allowFrom;
56-
}
57-
return [...params.allowFrom, `discord:${params.sender.id}`];
54+
return await expandAllowFromWithAccessGroups({
55+
cfg: params.cfg,
56+
allowFrom: params.allowFrom,
57+
channel: "discord",
58+
accountId: params.accountId,
59+
senderId: params.sender.id,
60+
senderAllowEntry: `discord:${params.sender.id}`,
61+
isSenderAllowed: (senderId, allowFrom) =>
62+
resolveSenderAllowMatch({
63+
allowEntries: allowFrom,
64+
sender: { id: senderId },
65+
allowNameMatching: false,
66+
}).allowed,
67+
resolveMembership: createDiscordAccessGroupMembershipResolver({
68+
token: params.token,
69+
rest: params.rest,
70+
}),
71+
});
5872
}
5973

6074
export async function resolveDiscordDmCommandAccess(params: {

‎extensions/discord/src/send.permissions.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,16 @@ function resolveMemberChannelPermissionBits(params: {
105105
permissions = addPermissionBits(permissions, overwrite.allow ?? "0");
106106
}
107107
}
108+
let roleDeny = 0n;
109+
let roleAllow = 0n;
108110
for (const overwrite of overwrites) {
109111
if (params.member.roles?.includes(overwrite.id)) {
110-
permissions = removePermissionBits(permissions, overwrite.deny ?? "0");
111-
permissions = addPermissionBits(permissions, overwrite.allow ?? "0");
112+
roleDeny = addPermissionBits(roleDeny, overwrite.deny ?? "0");
113+
roleAllow = addPermissionBits(roleAllow, overwrite.allow ?? "0");
112114
}
113115
}
116+
permissions = permissions & ~roleDeny;
117+
permissions = permissions | roleAllow;
114118
for (const overwrite of overwrites) {
115119
if (overwrite.id === params.userId) {
116120
permissions = removePermissionBits(permissions, overwrite.deny ?? "0");

0 commit comments

Comments
 (0)