Skip to content

Commit 3f60f7b

Browse files
committed
feat(whatsapp): per-group allowFrom for sender authorization (#69926)
Bring WhatsApp to parity with Feishu, IRC, LINE, Telegram, and Nextcloud-talk by accepting a per-group `allowFrom` list on `channels.whatsapp.groups.<jid>` (and the matching account-scoped entry). A non-empty per-group `allowFrom` overrides (does not merge with) the channel/account-level `groupAllowFrom` for that one JID, while groups without an override keep the channel/account-level list. Empty arrays fall back to the channel/account-level allowlist. The resolver respects the same multi-account inheritance guard as the rest of the WhatsApp config: account-scoped `groups[<jid>].allowFrom` takes precedence over a root-scope entry for the same JID, so root groups do not silently leak into accounts with their own groups map. Changes: - `WhatsAppGroupEntrySchema` and `WhatsAppGroupConfig` gain an optional `allowFrom: string[]` (E.164 phone numbers). - New `resolveWhatsAppConversationGroupAllowFrom` resolver in `inbound-policy.ts`, wired into `resolveWhatsAppIngressAccess` so the effective `groupAllowFrom` passed to `resolveStableChannelMessageIngress` reflects the per-group override for group conversations. - `bundled-channel-config-metadata.generated.ts` regenerated to surface the new field in the bundled channel schema. - `docs/channels/whatsapp.md` documents the per-group override and shows a mixed-trust example. Tests: - New `extensions/whatsapp/src/inbound-policy.test.ts` covers the resolver semantics: override applied, empty list falls back, unrelated groups inherit the account-level list, account-scoped groups override root-scope entries, missing groups map returns the account-level allowlist. - `extensions/whatsapp/src/config-schema.test.ts` gains two cases for the new field at root and account scope. Fixes #69926.
1 parent 3303b2f commit 3f60f7b

8 files changed

Lines changed: 258 additions & 3 deletions

File tree

‎CHANGELOG.md‎

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

3131
### Changes
3232

33+
- WhatsApp: add per-group `allowFrom` on `channels.whatsapp.groups.<jid>` (and the matching account-scoped entry) so each group can carry its own sender allowlist. A non-empty per-group `allowFrom` overrides (does not merge with) the channel/account-level `groupAllowFrom` for that JID, while groups without an override keep the existing channel/account-level list. Matches the per-group `allowFrom` already shipped by Feishu, IRC, LINE, Telegram, and Nextcloud-talk. Fixes #69926. Thanks @jack-stormentswe.
3334
- Gateway/OpenAI HTTP: honor `max_completion_tokens` and `max_tokens` on inbound `/v1/chat/completions` requests so client-provided token caps reach the upstream provider via `streamParams.maxTokens`, with `max_completion_tokens` taking precedence when both are sent. Thanks @Lellansin.
3435
- Models/OpenAI CLI auth: make `openclaw models auth login --provider openai` start the ChatGPT/Codex account login by default, while `--method api-key` remains the explicit OpenAI API-key setup path.
3536
- Google/Gemini: normalize retired Gemini 3 Pro Preview ids inside explicit SDK OAuth auth-result config patches, so provider helpers emit `google/gemini-3.1-pro-preview` for Gemini 3.1 testing.

‎docs/channels/whatsapp.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,34 @@ content and identifiers.
255255
- if `groupAllowFrom` is unset, runtime falls back to `allowFrom` when available
256256
- sender allowlists are evaluated before mention/reply activation
257257

258+
Per-group sender overrides:
259+
260+
- `channels.whatsapp.groups.<jid>.allowFrom` (or the matching account-scoped entry) accepts an E.164 list that **overrides** (does not merge with) the channel/account-level `groupAllowFrom` for that one JID
261+
- leave `allowFrom` unset (or empty) for a group to keep the channel/account-level allowlist
262+
- matches the same per-group `allowFrom` already supported by Feishu, IRC, LINE, Telegram, and Nextcloud-talk
263+
264+
```json5
265+
{
266+
channels: {
267+
whatsapp: {
268+
groupPolicy: "allowlist",
269+
groupAllowFrom: ["+15550001111"],
270+
groups: {
271+
272+
// Customer-support group: only staff numbers may drive the agent.
273+
allowFrom: ["+15550003333", "+15550004444"],
274+
},
275+
276+
// Personal/private group: only the operator may drive the agent.
277+
allowFrom: ["+15550005555"],
278+
},
279+
// Other groups inherit the channel-level groupAllowFrom above.
280+
},
281+
},
282+
},
283+
}
284+
```
285+
258286
Note: if no `channels.whatsapp` block exists at all, runtime group-policy fallback is `allowlist` (with a warning log), even if `channels.defaults.groupPolicy` is set.
259287

260288
</Tab>

‎extensions/whatsapp/src/config-schema.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,46 @@ describe("whatsapp config schema", () => {
118118
},
119119
});
120120
});
121+
122+
it("accepts per-group allowFrom on a channel-level group entry", () => {
123+
const res = expectWhatsAppConfigValid({
124+
groupPolicy: "allowlist",
125+
groupAllowFrom: ["+15550001111"],
126+
groups: {
127+
128+
allowFrom: ["+15550003333", "+15550004444"],
129+
requireMention: true,
130+
},
131+
},
132+
});
133+
134+
if (res.success) {
135+
expect(res.data.groups?.["[email protected]"]?.allowFrom).toEqual([
136+
"+15550003333",
137+
"+15550004444",
138+
]);
139+
}
140+
});
141+
142+
it("accepts per-group allowFrom on an account-scoped group entry", () => {
143+
const res = expectWhatsAppConfigValid({
144+
accounts: {
145+
work: {
146+
groupPolicy: "allowlist",
147+
groupAllowFrom: ["+15550001111"],
148+
groups: {
149+
150+
allowFrom: ["+15550005555"],
151+
},
152+
},
153+
},
154+
},
155+
});
156+
157+
if (res.success) {
158+
expect(res.data.accounts?.work?.groups?.["[email protected]"]?.allowFrom).toEqual([
159+
"+15550005555",
160+
]);
161+
}
162+
});
121163
});
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
2+
import { describe, expect, it } from "vitest";
3+
import {
4+
resolveWhatsAppConversationGroupAllowFrom,
5+
resolveWhatsAppInboundPolicy,
6+
} from "./inbound-policy.js";
7+
8+
function buildPolicyForCfg(cfg: OpenClawConfig) {
9+
return resolveWhatsAppInboundPolicy({ cfg, accountId: null, selfE164: null });
10+
}
11+
12+
describe("resolveWhatsAppConversationGroupAllowFrom (#69926)", () => {
13+
it("returns the account-level groupAllowFrom when no per-group allowFrom is configured", () => {
14+
const policy = buildPolicyForCfg({
15+
channels: {
16+
whatsapp: {
17+
enabled: true,
18+
groupPolicy: "allowlist",
19+
groupAllowFrom: ["+15550001111", "+15550002222"],
20+
},
21+
},
22+
});
23+
24+
expect(
25+
resolveWhatsAppConversationGroupAllowFrom({
26+
policy,
27+
conversationId: "[email protected]",
28+
}),
29+
).toEqual(["+15550001111", "+15550002222"]);
30+
});
31+
32+
it("uses per-group allowFrom when defined for the conversation JID", () => {
33+
const policy = buildPolicyForCfg({
34+
channels: {
35+
whatsapp: {
36+
enabled: true,
37+
groupPolicy: "allowlist",
38+
groupAllowFrom: ["+15550001111"],
39+
groups: {
40+
41+
allowFrom: ["+15550003333"],
42+
},
43+
},
44+
},
45+
},
46+
});
47+
48+
expect(
49+
resolveWhatsAppConversationGroupAllowFrom({
50+
policy,
51+
conversationId: "[email protected]",
52+
}),
53+
).toEqual(["+15550003333"]);
54+
});
55+
56+
it("falls back to the account-level list when the per-group allowFrom is empty", () => {
57+
const policy = buildPolicyForCfg({
58+
channels: {
59+
whatsapp: {
60+
enabled: true,
61+
groupPolicy: "allowlist",
62+
groupAllowFrom: ["+15550001111"],
63+
groups: {
64+
65+
allowFrom: [],
66+
},
67+
},
68+
},
69+
},
70+
});
71+
72+
expect(
73+
resolveWhatsAppConversationGroupAllowFrom({
74+
policy,
75+
conversationId: "[email protected]",
76+
}),
77+
).toEqual(["+15550001111"]);
78+
});
79+
80+
it("leaves other group JIDs on the account-level allowlist when one group has an override", () => {
81+
const policy = buildPolicyForCfg({
82+
channels: {
83+
whatsapp: {
84+
enabled: true,
85+
groupPolicy: "allowlist",
86+
groupAllowFrom: ["+15550001111"],
87+
groups: {
88+
89+
allowFrom: ["+15550003333"],
90+
},
91+
},
92+
},
93+
},
94+
});
95+
96+
expect(
97+
resolveWhatsAppConversationGroupAllowFrom({
98+
policy,
99+
conversationId: "[email protected]",
100+
}),
101+
).toEqual(["+15550001111"]);
102+
});
103+
104+
it("honours an account-scoped groups[<jid>].allowFrom over a root-scope account default", () => {
105+
const policy = resolveWhatsAppInboundPolicy({
106+
cfg: {
107+
channels: {
108+
whatsapp: {
109+
enabled: true,
110+
groupPolicy: "allowlist",
111+
groupAllowFrom: ["+15550001111"],
112+
groups: {
113+
114+
allowFrom: ["+15550009999"],
115+
},
116+
},
117+
accounts: {
118+
work: {
119+
groupAllowFrom: ["+15550008888"],
120+
groups: {
121+
122+
allowFrom: ["+15550007777"],
123+
},
124+
},
125+
},
126+
},
127+
},
128+
},
129+
} as OpenClawConfig,
130+
accountId: "work",
131+
selfE164: null,
132+
});
133+
134+
expect(
135+
resolveWhatsAppConversationGroupAllowFrom({
136+
policy,
137+
conversationId: "[email protected]",
138+
}),
139+
).toEqual(["+15550007777"]);
140+
});
141+
142+
it("returns the account-level list when the policy has no groups map at all", () => {
143+
const policy = buildPolicyForCfg({
144+
channels: {
145+
whatsapp: {
146+
enabled: true,
147+
groupPolicy: "allowlist",
148+
groupAllowFrom: ["+15550001111"],
149+
},
150+
},
151+
});
152+
153+
expect(
154+
resolveWhatsAppConversationGroupAllowFrom({
155+
policy,
156+
conversationId: "[email protected]",
157+
}),
158+
).toEqual(["+15550001111"]);
159+
});
160+
});

‎extensions/whatsapp/src/inbound-policy.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ export function resolveWhatsAppInboundPolicy(params: {
133133
};
134134
}
135135

136+
export function resolveWhatsAppConversationGroupAllowFrom(params: {
137+
policy: ResolvedWhatsAppInboundPolicy;
138+
conversationId: string;
139+
}): string[] {
140+
const groups = params.policy.account.groups;
141+
if (groups && typeof groups === "object") {
142+
const groupId = resolveGroupConversationId(params.conversationId);
143+
const entry = groups[groupId] ?? groups[params.conversationId];
144+
const perGroupAllowFrom = Array.isArray(entry?.allowFrom) ? entry.allowFrom : undefined;
145+
if (perGroupAllowFrom && perGroupAllowFrom.length > 0) {
146+
return perGroupAllowFrom;
147+
}
148+
}
149+
return params.policy.groupAllowFrom;
150+
}
151+
136152
export async function resolveWhatsAppIngressAccess(params: {
137153
cfg: OpenClawConfig;
138154
policy: ResolvedWhatsAppInboundPolicy;
@@ -148,6 +164,12 @@ export async function resolveWhatsAppIngressAccess(params: {
148164
dmSenderId: params.dmSenderId,
149165
});
150166
const dmAllowFrom = [...params.policy.dmAllowFrom, ...samePhoneDmAllowFrom];
167+
const effectiveGroupAllowFrom = params.isGroup
168+
? resolveWhatsAppConversationGroupAllowFrom({
169+
policy: params.policy,
170+
conversationId: params.conversationId,
171+
})
172+
: params.policy.groupAllowFrom;
151173
return await resolveStableChannelMessageIngress({
152174
channelId: "whatsapp",
153175
accountId: params.policy.account.accountId,
@@ -171,7 +193,7 @@ export async function resolveWhatsAppIngressAccess(params: {
171193
groupAllowFromFallbackToAllowFrom: false,
172194
},
173195
allowFrom: dmAllowFrom,
174-
groupAllowFrom: params.policy.groupAllowFrom,
196+
groupAllowFrom: effectiveGroupAllowFrom,
175197
command: params.includeCommand === true ? {} : undefined,
176198
});
177199
}

‎src/config/bundled-channel-config-metadata.generated.ts‎

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

‎src/config/types.whatsapp.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type WhatsAppGroupConfig = {
2828
toolsBySender?: GroupToolPolicyBySenderConfig;
2929
/** Optional system prompt for this group. */
3030
systemPrompt?: string;
31+
allowFrom?: string[];
3132
};
3233

3334
export type WhatsAppDirectConfig = {

‎src/config/zod-schema.providers-whatsapp.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const WhatsAppGroupEntrySchema = z
2424
tools: ToolPolicySchema,
2525
toolsBySender: ToolPolicyBySenderSchema,
2626
systemPrompt: z.string().optional(),
27+
allowFrom: z.array(z.string()).optional(),
2728
})
2829
.strict()
2930
.optional();

0 commit comments

Comments
 (0)