Skip to content

Commit 00fc1bd

Browse files
sjf-oasjf
andauthored
fix(slack): scope enterprise channel and user policies by workspace (#122346)
* fix(slack): scope channel policies by workspace * fix(slack): scope user policies by workspace * fix(slack): preserve workspace policies at ingress * test(slack): use canonical workspace ids * chore(slack): remove stale allowlist import * fix(slack): require workspace-scoped grid policies * test(slack): scope enterprise policy fixtures * style(slack): format workspace policy changes * fix(slack): preserve workspace policy wildcards * fix(slack): retain workspace system-event policies * fix(slack): scope bot policy identities * fix(slack): retain workspace in group policy * fix(slack): preserve workspace DM allowlists --------- Co-authored-by: Sarah Fortune <[email protected]>
1 parent a7e4065 commit 00fc1bd

32 files changed

Lines changed: 882 additions & 93 deletions

‎docs/channels/slack.md‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,17 @@ the enterprise account with the same Request URL path:
268268
allowFrom: ["*"],
269269
groupPolicy: "allowlist",
270270
channels: {
271-
C0123456789: { requireMention: true },
271+
"team:T0123456789:channel:C0123456789": { requireMention: true },
272272
},
273273
},
274274
},
275275
}
276276
```
277277

278+
For each selected workspace, open it in Slack's web app and copy the `T...`
279+
workspace ID from `https://app.slack.com/client/T.../...`. Use that workspace ID
280+
with the channel's `C...` ID in every qualified policy key, as shown above.
281+
278282
At startup, OpenClaw uses Slack `auth.test` to detect whether the token belongs
279283
to a workspace installation or an Enterprise Grid org-wide installation. No
280284
installation-mode setting is required. Slack remains the source of truth for
@@ -324,14 +328,18 @@ validated listener-owned client remains in the active event turn. The
324328
in-memory send queue and thread-participation records are partitioned by that
325329
event's workspace; the client itself is never serialized or persisted.
326330

327-
Channel policy keys accept raw stable Slack channel IDs, `channel:<id>`, or the
328-
`"*"` wildcard. `dm.groupChannels` accepts raw stable channel IDs or
329-
`channel:<id>`, but not `"*"`. OpenClaw normalizes the ID forms to the raw
330-
channel ID for runtime matching; the channel prefixes `slack:`, `group:`, and
331-
`mpim:` fail startup.
332-
333-
User policy entries in `allowFrom`, `reactionAllowlist`, and per-channel `users`
334-
accept raw stable Slack user IDs, `slack:<user-id>`, `user:<user-id>`, or `"*"`.
331+
Enterprise channel policy keys must use
332+
`team:<team-id>:channel:<channel-id>` or the `"*"` wildcard.
333+
`dm.groupChannels` requires the workspace-qualified form and does not accept
334+
`"*"`. A delivered Enterprise event never falls back from its qualified
335+
workspace and channel identity to a bare channel ID. Workspace installations
336+
retain raw stable channel IDs and `channel:<id>` compatibility. The channel
337+
prefixes `slack:`, `group:`, and `mpim:` fail startup.
338+
339+
Enterprise user policy entries in `allowFrom`, `reactionAllowlist`, and
340+
per-channel `users` must use `team:<team-id>:user:<user-id>` or `"*"`. A
341+
workspace-scoped sender never matches a bare user ID. Workspace installations
342+
retain raw stable user IDs, `slack:<user-id>`, and `user:<user-id>` compatibility.
335343
Enterprise `toolsBySender` keys accept raw stable user IDs, `id:<user-id>`,
336344
`channel:slack:<user-id>`, or `"*"`. Names, slugs, display names, and email
337345
addresses fail startup. IDs must use Slack's canonical uppercase prefix and body
@@ -351,8 +359,9 @@ rejected before authorization or system-event handling.
351359
Enterprise DMs support the same `disabled`, `open`, `allowlist`, and `pairing`
352360
policies as workspace installs. Pairing approvals are stored as
353361
`team:<team-id>:user:<user-id>` and are applied only to events from that
354-
workspace. Explicit account `allowFrom` entries remain organization-wide;
355-
channel and sender policy continues to apply to channel messages.
362+
workspace. Explicit account `allowFrom` entries use the same qualified form and
363+
apply only to that workspace; channel and sender policy continues to apply to
364+
channel messages.
356365

357366
## Install
358367

@@ -1303,7 +1312,7 @@ Current Slack message actions include `send`, `upload-file`, `download-file`, `r
13031312
- `allowlist`
13041313
- `disabled`
13051314

1306-
Channel allowlist lives under `channels.slack.channels` and **must use stable Slack channel IDs** (for example `C12345678`) as config keys.
1315+
Channel allowlist lives under `channels.slack.channels` and **must use stable Slack channel IDs** (for example `C12345678`) as config keys. Enterprise Grid org installs require `team:<team-id>:channel:<channel-id>` so policies cannot cross workspace boundaries.
13071316

13081317
Runtime note: if `channels.slack` is completely missing (env-only setup), runtime falls back to `groupPolicy="allowlist"` and logs a warning (even if `channels.defaults.groupPolicy` is set).
13091318

@@ -1936,7 +1945,7 @@ Primary reference: [Configuration reference - Slack](/gateway/config-channels#sl
19361945
Check, in order:
19371946

19381947
- `groupPolicy`
1939-
- channel allowlist (`channels.slack.channels`) — **keys must be channel IDs** (`C12345678`), not names (`#channel-name`). Name-based keys silently fail under `groupPolicy: "allowlist"` because channel routing is ID-first by default. To find an ID: right-click the channel in Slack → **Copy link** — the `C...` value at the end of the URL is the channel ID.
1948+
- channel allowlist (`channels.slack.channels`) — **keys must be channel IDs** (`C12345678`) or workspace-qualified channel targets (`team:<team-id>:channel:<channel-id>`), not names (`#channel-name`). Name-based keys silently fail under `groupPolicy: "allowlist"` because channel routing is ID-first by default. To find an ID: right-click the channel in Slack → **Copy link** — the `C...` value at the end of the URL is the channel ID.
19401949
- `requireMention`
19411950
- per-channel `users` allowlist
19421951
- `messages.groupChat.visibleReplies`: normal group/channel requests default to `"automatic"`. If you opted into `"message_tool"` and logs show assistant text with no `message(action=send)` call, the model missed the visible message-tool path. Final text stays private in this mode; inspect the gateway verbose log for suppressed payload metadata, or set it to `"automatic"` if you want every normal assistant final reply posted through the legacy path.

‎docs/gateway/config-channels.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,11 @@ WhatsApp runs through the gateway's web channel (Baileys Web). It starts automat
468468
- Slack detects Enterprise Grid org-wide installations automatically from the
469469
bot token with `auth.test`; no installation-mode setting is required.
470470
Enterprise DMs support `disabled`, `open`, `allowlist`, and workspace-scoped
471-
`pairing`. Channel and user policies must use stable Slack IDs; mutable names
472-
and unsupported channel prefixes fail startup. Mention-pattern channel
473-
scopes and static route-binding peers use workspace-qualified Slack targets.
471+
`pairing`. Channel and user policies must use
472+
`team:<team-id>:channel:<channel-id>` or `team:<team-id>:user:<user-id>`;
473+
bare IDs, mutable names, and unsupported channel prefixes fail startup.
474+
Mention-pattern channel scopes and static route-binding peers use
475+
workspace-qualified Slack targets.
474476
Direct Socket Mode or HTTP messages, mentions, workspace-qualified actions,
475477
deferred delivery, proactive sends, supported event listeners and
476478
interactions, static route bindings, and Slack-native approvals from

‎extensions/slack/src/action-runtime.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type { ResolvedSlackAccount } from "./accounts.js";
1111
import { parseSlackBlocksInput } from "./blocks-input.js";
1212
import type { SlackConversationInfo } from "./channel-type.js";
1313
import { assertSlackDetachedTargetAllowed } from "./detached-target-admission.js";
14+
import { buildSlackChannelIdCandidates } from "./group-policy.js";
15+
import { getSlackInstallationKind } from "./installation-identity-state.js";
1416
import { SLACK_TEXT_LIMIT } from "./limits.js";
1517
import { resolveSlackChannelConfig } from "./monitor/channel-config.js";
1618
import { isSlackChannelAllowedByPolicy } from "./monitor/policy.js";
@@ -282,6 +284,7 @@ function resolveSlackChannelReadPolicy(params: {
282284
account: ResolvedSlackAccount;
283285
cfg: OpenClawConfig;
284286
channelId: string;
287+
teamId?: string;
285288
channelName?: string;
286289
conversationReadOrigin?: ConversationReadInvocationOrigin;
287290
metadataResolved?: boolean;
@@ -290,6 +293,8 @@ function resolveSlackChannelReadPolicy(params: {
290293
const channels = params.account.config.channels;
291294
const channelKeys = Object.keys(channels ?? {});
292295
const channelConfig = resolveSlackChannelConfig({
296+
teamId: params.teamId,
297+
allowUnscoped: getSlackInstallationKind(params.account.accountId) !== "enterprise",
293298
channelId: params.channelId,
294299
channelName: params.channelName,
295300
channels,
@@ -344,7 +349,7 @@ function resolveSlackChannelReadPolicy(params: {
344349
params.account.config.dm?.enabled !== false &&
345350
params.account.config.dm?.groupEnabled === true &&
346351
(params.currentConversation ||
347-
isSlackGroupDmTargetConfigured(params.account, params.channelId)),
352+
isSlackGroupDmTargetConfigured(params.account, params.channelId, params.teamId)),
348353
shouldResolveName,
349354
};
350355
}
@@ -461,16 +466,26 @@ async function assertSlackReadTargetAllowed(params: {
461466
}
462467
}
463468

464-
function isSlackGroupDmTargetConfigured(account: ResolvedSlackAccount, channelId: string): boolean {
469+
function isSlackGroupDmTargetConfigured(
470+
account: ResolvedSlackAccount,
471+
channelId: string,
472+
teamId?: string,
473+
): boolean {
465474
const entries = account.config.dm?.groupChannels ?? [];
466475
if (entries.length === 0) {
467476
return true;
468477
}
478+
const candidates = new Set(
479+
buildSlackChannelIdCandidates(channelId, teamId, {
480+
allowUnscoped: getSlackInstallationKind(account.accountId) !== "enterprise",
481+
}).map((candidate) => candidate.toLowerCase()),
482+
);
469483
const target = channelId.trim().toLowerCase();
470484
return entries.some((entry) => {
471485
const candidate = String(entry).trim().toLowerCase();
472486
return (
473487
candidate === "*" ||
488+
candidates.has(candidate) ||
474489
candidate === target ||
475490
candidate === `slack:${target}` ||
476491
candidate === `channel:${target}` ||

‎extensions/slack/src/doctor.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ describe("slack doctor", () => {
174174
).toBe(true);
175175
});
176176

177+
it("accepts workspace-qualified channel and user ids as stable policy entries", async () => {
178+
const warnings = await collectSlackWarnings({
179+
allowFrom: ["team:T11111111:user:U01234567"],
180+
channels: {
181+
"team:T11111111:channel:C01234567": {
182+
users: ["team:T11111111:user:U01234567"],
183+
},
184+
},
185+
});
186+
187+
expect(warnings).toEqual([]);
188+
});
189+
177190
it("warns for name-keyed allowlist channels but accepts routed ID forms (#81665)", async () => {
178191
const warnings = await collectSlackWarnings({
179192
channels: {
@@ -183,9 +196,11 @@ describe("slack doctor", () => {
183196
c0al2gdua7k: {},
184197
"channel:C0AL2GDUA7L": {},
185198
"channel:c0al2gdua7m": {},
199+
"team:T11111111:channel:C0AL2GDUA7S": {},
186200
D0AL2GDUA7Q: {},
187201
"channel:d0al2gdua7r": {},
188202
"channel:dabcdefgh": {},
203+
"team:T11111111:channel:D0AL2GDUA7T": {},
189204
"channel:customers": {},
190205
"CHANNEL:C0AL2GDUA7N": {},
191206
"channel:C0al2gdua7p": {},
@@ -208,10 +223,11 @@ describe("slack doctor", () => {
208223
const dmWarnings = warnings.filter((warning) =>
209224
warning.includes("is a Slack DM conversation ID"),
210225
);
211-
expect(dmWarnings).toHaveLength(3);
226+
expect(dmWarnings).toHaveLength(4);
212227
expect(dmWarnings[0]).toContain('channels.slack.channels."D0AL2GDUA7Q"');
213228
expect(dmWarnings[1]).toContain('channels.slack.channels."channel:d0al2gdua7r"');
214229
expect(dmWarnings[2]).toContain('channels.slack.channels."channel:dabcdefgh"');
230+
expect(dmWarnings[3]).toContain('channels.slack.channels."team:T11111111:channel:D0AL2GDUA7T"');
215231
expect(dmWarnings[0]).toContain("channels.slack.dmPolicy");
216232
});
217233

‎extensions/slack/src/doctor.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "./doctor-contract.js";
1515
import { probeSlack } from "./probe.js";
1616
import { isSlackMutableAllowEntry } from "./security-doctor.js";
17+
import { parseSlackTarget } from "./target-parsing.js";
1718

1819
const collectSlackMutableAllowlistWarnings =
1920
createDangerousNameMatchingMutableAllowlistWarningCollector({
@@ -45,7 +46,9 @@ const SLACK_CHANNEL_NAME_RE = /^[\p{L}\p{M}\p{N}_-]{1,80}$/u;
4546
const SLACK_CHANNEL_NAME_ALPHANUMERIC_RE = /[\p{L}\p{N}]/u;
4647

4748
function looksLikeSlackChannelId(channelKey: string): boolean {
49+
const workspaceChannelId = parseWorkspaceQualifiedChannelId(channelKey);
4850
return (
51+
(workspaceChannelId !== undefined && /^[CG]/i.test(workspaceChannelId)) ||
4952
SLACK_CANONICAL_CHANNEL_ID_RE.test(channelKey) ||
5053
SLACK_LOWERCASE_CHANNEL_ID_RE.test(channelKey) ||
5154
SLACK_PREFIXED_CANONICAL_CHANNEL_ID_RE.test(channelKey) ||
@@ -54,11 +57,26 @@ function looksLikeSlackChannelId(channelKey: string): boolean {
5457
}
5558

5659
function looksLikeSlackDmId(channelKey: string): boolean {
60+
const workspaceChannelId = parseWorkspaceQualifiedChannelId(channelKey);
5761
return (
58-
SLACK_CANONICAL_DM_ID_RE.test(channelKey) || SLACK_PREFIXED_LOWERCASE_DM_ID_RE.test(channelKey)
62+
(workspaceChannelId !== undefined && /^D/i.test(workspaceChannelId)) ||
63+
SLACK_CANONICAL_DM_ID_RE.test(channelKey) ||
64+
SLACK_PREFIXED_LOWERCASE_DM_ID_RE.test(channelKey)
5965
);
6066
}
6167

68+
function parseWorkspaceQualifiedChannelId(channelKey: string): string | undefined {
69+
if (!/^team:/i.test(channelKey)) {
70+
return undefined;
71+
}
72+
try {
73+
const target = parseSlackTarget(channelKey);
74+
return target?.kind === "channel" && target.teamId ? target.id : undefined;
75+
} catch {
76+
return undefined;
77+
}
78+
}
79+
6280
function looksLikeSlackChannelNameKey(channelKey: string): boolean {
6381
const name = channelKey.startsWith("#") ? channelKey.slice(1) : channelKey;
6482
return (

‎extensions/slack/src/group-policy.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
33
import { describe, expect, it } from "vitest";
44
import { resolveSlackGroupRequireMention, resolveSlackGroupToolPolicy } from "./group-policy.js";
5+
import { registerSlackInstallationState } from "./installation-identity-state.js";
56

67
const cfg = {
78
channels: {
@@ -100,6 +101,94 @@ describe("slack group policy", () => {
100101
},
101102
);
102103

104+
it("scopes Enterprise mention and tool policies to the event workspace", () => {
105+
const installationState = registerSlackInstallationState("default", "enterprise");
106+
const enterpriseCfg = {
107+
channels: {
108+
slack: {
109+
channels: {
110+
"team:T11111111:channel:C01234567": {
111+
requireMention: false,
112+
tools: { allow: ["message.send"] },
113+
},
114+
"team:T22222222:channel:C01234567": {
115+
requireMention: true,
116+
tools: { deny: ["exec"] },
117+
},
118+
},
119+
},
120+
},
121+
} as OpenClawConfig;
122+
123+
try {
124+
expect(
125+
resolveSlackGroupRequireMention({
126+
cfg: enterpriseCfg,
127+
groupId: "C01234567",
128+
groupSpace: "T11111111",
129+
}),
130+
).toBe(false);
131+
expect(
132+
resolveSlackGroupToolPolicy({
133+
cfg: enterpriseCfg,
134+
groupId: "C01234567",
135+
groupSpace: "T11111111",
136+
}),
137+
).toEqual({ allow: ["message.send"] });
138+
expect(
139+
resolveSlackGroupRequireMention({
140+
cfg: enterpriseCfg,
141+
groupId: "C01234567",
142+
groupSpace: "T22222222",
143+
}),
144+
).toBe(true);
145+
expect(
146+
resolveSlackGroupToolPolicy({
147+
cfg: enterpriseCfg,
148+
groupId: "C01234567",
149+
groupSpace: "T22222222",
150+
}),
151+
).toEqual({ deny: ["exec"] });
152+
} finally {
153+
installationState.release();
154+
}
155+
});
156+
157+
it("retains bare channel policy matching for workspace installs", () => {
158+
const installationState = registerSlackInstallationState("default", "workspace");
159+
const workspaceCfg = {
160+
channels: {
161+
slack: {
162+
channels: {
163+
C01234567: {
164+
requireMention: false,
165+
tools: { allow: ["message.send"] },
166+
},
167+
},
168+
},
169+
},
170+
} as OpenClawConfig;
171+
172+
try {
173+
expect(
174+
resolveSlackGroupRequireMention({
175+
cfg: workspaceCfg,
176+
groupId: "C01234567",
177+
groupSpace: "T11111111",
178+
}),
179+
).toBe(false);
180+
expect(
181+
resolveSlackGroupToolPolicy({
182+
cfg: workspaceCfg,
183+
groupId: "C01234567",
184+
groupSpace: "T11111111",
185+
}),
186+
).toEqual({ allow: ["message.send"] });
187+
} finally {
188+
installationState.release();
189+
}
190+
});
191+
103192
it("prefers the exact channel ID when case variants have different policies", () => {
104193
const caseSensitiveCfg = {
105194
channels: {

‎extensions/slack/src/group-policy.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,39 @@ import {
1212
import { buildChannelKeyCandidates } from "openclaw/plugin-sdk/channel-targets";
1313
import { normalizeHyphenSlug } from "openclaw/plugin-sdk/string-normalization-runtime";
1414
import { mergeSlackAccountConfig, resolveDefaultSlackAccountId } from "./accounts.js";
15+
import { getSlackInstallationKind } from "./installation-identity-state.js";
1516

1617
type SlackChannelPolicyEntry = {
1718
requireMention?: boolean;
1819
tools?: GroupToolPolicyConfig;
1920
toolsBySender?: GroupToolPolicyBySenderConfig;
2021
};
2122

22-
export function buildSlackChannelIdCandidates(channelId: string | null | undefined): string[] {
23+
export function buildSlackChannelIdCandidates(
24+
channelId: string | null | undefined,
25+
teamId?: string | null,
26+
options?: { allowUnscoped?: boolean },
27+
): string[] {
2328
const trimmedId = channelId?.trim();
2429
if (!trimmedId) {
2530
return [];
2631
}
2732
const lowercaseId = trimmedId.toLowerCase();
2833
const uppercaseId = trimmedId.toUpperCase();
34+
const exactTeamId = teamId || undefined;
35+
const lowercaseTeamId = exactTeamId?.toLowerCase();
36+
const uppercaseTeamId = exactTeamId?.toUpperCase();
2937
// Inbound Slack IDs are uppercase, but persisted session group IDs are lowercase.
38+
const scopedCandidates = buildChannelKeyCandidates(
39+
exactTeamId ? `team:${exactTeamId}:channel:${trimmedId}` : undefined,
40+
lowercaseTeamId ? `team:${lowercaseTeamId}:channel:${lowercaseId}` : undefined,
41+
uppercaseTeamId ? `team:${uppercaseTeamId}:channel:${uppercaseId}` : undefined,
42+
);
43+
if (exactTeamId && options?.allowUnscoped !== true) {
44+
return scopedCandidates;
45+
}
3046
return buildChannelKeyCandidates(
47+
...scopedCandidates,
3148
trimmedId,
3249
lowercaseId,
3350
uppercaseId,
@@ -69,8 +86,9 @@ function resolveSlackGroupPolicyScope(params: ChannelGroupContext) {
6986
| Record<string, SlackChannelPolicyEntry>
7087
| undefined;
7188
const channelName = params.groupChannel?.replace(/^#/, "");
89+
const allowUnscoped = getSlackInstallationKind(accountId) !== "enterprise";
7290
const candidates = buildChannelKeyCandidates(
73-
...buildSlackChannelIdCandidates(params.groupId),
91+
...buildSlackChannelIdCandidates(params.groupId, params.groupSpace, { allowUnscoped }),
7492
channelName ? `#${channelName}` : undefined,
7593
channelName,
7694
normalizeHyphenSlug(channelName),

0 commit comments

Comments
 (0)