Skip to content

Commit d697ecf

Browse files
nnqbaovincentkoc
andauthored
fix: avoid parent group allowlist false positive (#93434)
Merged via squash. Prepared head SHA: da2ce68 Co-authored-by: kingrubic <[email protected]> Co-authored-by: vincentkoc <[email protected]> Reviewed-by: @vincentkoc
1 parent 6d22b8e commit d697ecf

4 files changed

Lines changed: 296 additions & 6 deletions

File tree

‎src/commands/doctor/channel-capabilities.test.ts‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
// Doctor channel capability tests cover channel capability inspection and diagnostics.
2-
import { describe, expect, it } from "vitest";
3-
import { getDoctorChannelCapabilities } from "./channel-capabilities.js";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import {
4+
getDoctorChannelCapabilities,
5+
resolveDoctorChannelAccountIds,
6+
} from "./channel-capabilities.js";
7+
8+
const channelPluginMocks = vi.hoisted(() => ({
9+
getBundledChannelPlugin: vi.fn(() => undefined),
10+
getChannelPlugin: vi.fn(() => undefined),
11+
}));
12+
13+
vi.mock("../../channels/plugins/bundled.js", () => ({
14+
getBundledChannelPlugin: channelPluginMocks.getBundledChannelPlugin,
15+
}));
16+
17+
vi.mock("../../channels/plugins/index.js", () => ({
18+
getChannelPlugin: channelPluginMocks.getChannelPlugin,
19+
}));
420

521
describe("doctor channel capabilities", () => {
22+
beforeEach(() => {
23+
channelPluginMocks.getBundledChannelPlugin.mockReset().mockReturnValue(undefined);
24+
channelPluginMocks.getChannelPlugin.mockReset().mockReturnValue(undefined);
25+
});
26+
627
it("returns nested route semantics from googlechat plugin metadata", () => {
728
expect(getDoctorChannelCapabilities("googlechat")).toEqual({
829
dmAllowFromMode: "nestedOnly",
@@ -47,4 +68,28 @@ describe("doctor channel capabilities", () => {
4768
warnOnEmptyGroupSenderAllowlist: true,
4869
});
4970
});
71+
72+
it("falls back conservatively when channel plugin resolution throws", () => {
73+
channelPluginMocks.getChannelPlugin.mockImplementation(() => {
74+
throw new Error("missing generated bundled module");
75+
});
76+
77+
expect(resolveDoctorChannelAccountIds("telegram", {}, [])).toBeUndefined();
78+
});
79+
80+
it("resolves configured and runtime account ids through plugin semantics", () => {
81+
channelPluginMocks.getChannelPlugin.mockReturnValue({
82+
config: {
83+
listAccountIds: () => ["default", "Work"],
84+
resolveAccount: (_cfg: unknown, accountId?: string | null) => ({
85+
accountId: accountId === "Work" ? "work" : accountId,
86+
}),
87+
},
88+
} as never);
89+
90+
expect(resolveDoctorChannelAccountIds("signal", {}, ["Work"])).toEqual({
91+
configured: ["work"],
92+
runtime: ["default", "work"],
93+
});
94+
});
5095
});

‎src/commands/doctor/channel-capabilities.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { getBundledChannelPlugin } from "../../channels/plugins/bundled.js";
33
import { getChannelPlugin } from "../../channels/plugins/index.js";
44
import { normalizeAnyChannelId } from "../../channels/registry.js";
5+
import type { OpenClawConfig } from "../../config/types.openclaw.js";
56
import { findBundledPackageChannelMetadata } from "../../plugins/bundled-package-channel-metadata.js";
67
import type { PluginPackageChannelDoctorCapabilities } from "../../plugins/manifest.js";
78
import type { AllowFromMode } from "./shared/allow-from-mode.types.js";
@@ -66,3 +67,48 @@ export function getDoctorChannelCapabilities(channelName?: string): DoctorChanne
6667
}
6768
return mergeDoctorChannelCapabilities(getManifestDoctorCapabilities(channelId));
6869
}
70+
71+
type DoctorChannelAccountIds = {
72+
configured: string[];
73+
runtime: string[];
74+
};
75+
76+
function readResolvedAccountId(account: unknown): string | undefined {
77+
if (!account || typeof account !== "object") {
78+
return undefined;
79+
}
80+
const accountId = (account as { accountId?: unknown }).accountId;
81+
return typeof accountId === "string" && accountId ? accountId : undefined;
82+
}
83+
84+
/** Resolve configured and runtime account ids through the channel plugin's own semantics. */
85+
export function resolveDoctorChannelAccountIds(
86+
channelName: string,
87+
cfg: OpenClawConfig,
88+
configuredAccountIds: string[],
89+
): DoctorChannelAccountIds | undefined {
90+
const channelId = normalizeAnyChannelId(channelName);
91+
if (!channelId) {
92+
return undefined;
93+
}
94+
try {
95+
const plugin = getChannelPlugin(channelId) ?? getBundledChannelPlugin(channelId);
96+
if (!plugin) {
97+
return undefined;
98+
}
99+
const resolveAccountIds = (accountIds: string[]): string[] | undefined => {
100+
const resolved = accountIds.map((accountId) =>
101+
readResolvedAccountId(plugin.config.resolveAccount(cfg, accountId)),
102+
);
103+
return resolved.every((accountId): accountId is string => accountId !== undefined)
104+
? resolved
105+
: undefined;
106+
};
107+
const configured = resolveAccountIds(configuredAccountIds);
108+
const runtime = resolveAccountIds(plugin.config.listAccountIds(cfg));
109+
return configured && runtime ? { configured, runtime } : undefined;
110+
} catch {
111+
// Keep doctor warnings conservative when a plugin cannot inspect its account set.
112+
return undefined;
113+
}
114+
}

‎src/commands/doctor/shared/empty-allowlist-scan.test.ts‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ vi.mock("../channel-capabilities.js", () => ({
99
groupAllowFromFallbackToAllowFrom: channelName !== "imessage",
1010
warnOnEmptyGroupSenderAllowlist: channelName !== "discord",
1111
}),
12+
resolveDoctorChannelAccountIds: (
13+
channelName: string,
14+
cfg: {
15+
channels?: Record<
16+
string,
17+
{ accounts?: Record<string, unknown>; appId?: string; baseUrl?: string }
18+
>;
19+
},
20+
configuredAccountIds: string[],
21+
) => {
22+
const channel = cfg.channels?.[channelName];
23+
const ids = Object.keys(channel?.accounts ?? {});
24+
const resolveAccountId = (accountId: string) =>
25+
channelName === "matrix" || channelName === "signal" ? accountId.toLowerCase() : accountId;
26+
const runtimeIds = [
27+
...(channelName === "qa-channel" && channel?.baseUrl ? ["default"] : []),
28+
...(channelName === "qqbot" && channel?.appId ? ["default"] : []),
29+
...ids,
30+
];
31+
return {
32+
configured: configuredAccountIds.map(resolveAccountId),
33+
runtime: runtimeIds.map(resolveAccountId),
34+
};
35+
},
1236
}));
1337

1438
vi.mock("./channel-doctor.js", () => ({
@@ -37,6 +61,130 @@ describe("doctor empty allowlist policy scan", () => {
3761
]);
3862
});
3963

64+
it("does not warn on empty parent groupAllowFrom when active accounts have effective group allowlists", () => {
65+
const warnings = scanEmptyAllowlistPolicyWarnings(
66+
{
67+
channels: {
68+
telegram: {
69+
groupPolicy: "allowlist",
70+
groupAllowFrom: [],
71+
accounts: {
72+
primary: { groupAllowFrom: ["telegram:group:primary"] },
73+
backup: { allowFrom: ["telegram:group:backup"] },
74+
},
75+
},
76+
},
77+
},
78+
{ doctorFixCommand: "openclaw doctor --fix" },
79+
);
80+
81+
expect(warnings).toEqual([]);
82+
});
83+
84+
it("keeps parent groupAllowFrom warning when any active account lacks an effective allowlist", () => {
85+
const warnings = scanEmptyAllowlistPolicyWarnings(
86+
{
87+
channels: {
88+
telegram: {
89+
groupPolicy: "allowlist",
90+
groupAllowFrom: [],
91+
accounts: {
92+
primary: { groupAllowFrom: ["telegram:group:primary"] },
93+
backup: {},
94+
},
95+
},
96+
},
97+
},
98+
{ doctorFixCommand: "openclaw doctor --fix" },
99+
);
100+
101+
expect(warnings).toContain(
102+
'- channels.telegram.groupPolicy is "allowlist" but groupAllowFrom (and allowFrom) is empty — all group messages will be silently dropped. Add sender IDs to channels.telegram.groupAllowFrom or channels.telegram.allowFrom, or set groupPolicy to "open".',
103+
);
104+
});
105+
106+
it("keeps parent groupAllowFrom warning when an implicit default account is active", () => {
107+
const warnings = scanEmptyAllowlistPolicyWarnings(
108+
{
109+
channels: {
110+
"qa-channel": {
111+
baseUrl: "http://127.0.0.1:18789",
112+
groupPolicy: "allowlist",
113+
groupAllowFrom: [],
114+
accounts: {
115+
work: { groupAllowFrom: ["qa:group:work"] },
116+
},
117+
},
118+
},
119+
},
120+
{ doctorFixCommand: "openclaw doctor --fix" },
121+
);
122+
123+
expect(warnings).toContain(
124+
'- channels.qa-channel.groupPolicy is "allowlist" but groupAllowFrom (and allowFrom) is empty — all group messages will be silently dropped. Add sender IDs to channels.qa-channel.groupAllowFrom or channels.qa-channel.allowFrom, or set groupPolicy to "open".',
125+
);
126+
});
127+
128+
it("matches canonical runtime account ids to mixed-case config keys", () => {
129+
const warnings = scanEmptyAllowlistPolicyWarnings(
130+
{
131+
channels: {
132+
matrix: {
133+
groupPolicy: "allowlist",
134+
groupAllowFrom: [],
135+
accounts: {
136+
Work: { groupAllowFrom: ["matrix:group:work"] },
137+
},
138+
},
139+
},
140+
},
141+
{ doctorFixCommand: "openclaw doctor --fix" },
142+
);
143+
144+
expect(warnings).toEqual([]);
145+
});
146+
147+
it("matches raw runtime account ids to canonical config keys", () => {
148+
const warnings = scanEmptyAllowlistPolicyWarnings(
149+
{
150+
channels: {
151+
signal: {
152+
groupPolicy: "allowlist",
153+
groupAllowFrom: [],
154+
accounts: {
155+
Work: { groupAllowFrom: ["signal:group:work"] },
156+
},
157+
},
158+
},
159+
},
160+
{ doctorFixCommand: "openclaw doctor --fix" },
161+
);
162+
163+
expect(warnings).toEqual([]);
164+
});
165+
166+
it("keeps parent warning for a distinct case-sensitive implicit default account", () => {
167+
const warnings = scanEmptyAllowlistPolicyWarnings(
168+
{
169+
channels: {
170+
qqbot: {
171+
appId: "top-level-app",
172+
groupPolicy: "allowlist",
173+
groupAllowFrom: [],
174+
accounts: {
175+
Default: { groupAllowFrom: ["qqbot:group:named"] },
176+
},
177+
},
178+
},
179+
},
180+
{ doctorFixCommand: "openclaw doctor --fix" },
181+
);
182+
183+
expect(warnings).toContain(
184+
'- channels.qqbot.groupPolicy is "allowlist" but groupAllowFrom (and allowFrom) is empty — all group messages will be silently dropped. Add sender IDs to channels.qqbot.groupAllowFrom or channels.qqbot.allowFrom, or set groupPolicy to "open".',
185+
);
186+
});
187+
40188
it("allows provider-specific extra warnings without importing providers", () => {
41189
const warnings = scanEmptyAllowlistPolicyWarnings(
42190
{

‎src/commands/doctor/shared/empty-allowlist-scan.ts‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Doctor scanner for empty allowlist policies across configured channels and accounts.
22
import type { ChannelDoctorEmptyAllowlistAccountContext } from "../../../channels/plugins/types.adapters.js";
33
import type { OpenClawConfig } from "../../../config/types.openclaw.js";
4+
import {
5+
getDoctorChannelCapabilities,
6+
resolveDoctorChannelAccountIds,
7+
} from "../channel-capabilities.js";
48
import type { DoctorAccountRecord, DoctorAllowFromList } from "../types.js";
9+
import { hasAllowFromEntries } from "./allowlist.js";
510
import { collectEmptyAllowlistPolicyWarningsForAccount } from "./empty-allowlist-policy.js";
611
import { asObjectRecord } from "./object.js";
712

@@ -37,6 +42,7 @@ export function scanEmptyAllowlistPolicyWarnings(
3742
prefix: string,
3843
channelName: string,
3944
parent?: DoctorAccountRecord,
45+
options: { suppressGroupAllowlistWarning?: boolean } = {},
4046
) => {
4147
const accountDm = asObjectRecord(account.dm);
4248
const parentDm = asObjectRecord(parent?.dm);
@@ -61,8 +67,9 @@ export function scanEmptyAllowlistPolicyWarnings(
6167
doctorFixCommand: params.doctorFixCommand,
6268
parent,
6369
prefix,
64-
shouldSkipDefaultEmptyGroupAllowlistWarning:
65-
params.shouldSkipDefaultEmptyGroupAllowlistWarning,
70+
shouldSkipDefaultEmptyGroupAllowlistWarning: (context) =>
71+
options.suppressGroupAllowlistWarning ||
72+
Boolean(params.shouldSkipDefaultEmptyGroupAllowlistWarning?.(context)),
6673
}),
6774
);
6875
if (params.extraWarningsForAccount) {
@@ -88,9 +95,53 @@ export function scanEmptyAllowlistPolicyWarnings(
8895
if (isDisabledRecord(channelConfig)) {
8996
continue;
9097
}
91-
checkAccount(channelConfig, `channels.${channelName}`, channelName);
92-
9398
const accounts = asObjectRecord(channelConfig.accounts);
99+
const activeAccounts = accounts
100+
? Object.values(accounts).filter((account): account is DoctorAccountRecord =>
101+
Boolean(account && typeof account === "object" && !isDisabledRecord(account)),
102+
)
103+
: [];
104+
const accountIds = resolveDoctorChannelAccountIds(
105+
channelName,
106+
cfg,
107+
Object.keys(accounts ?? {}),
108+
);
109+
const configuredAccountIds = new Set(accountIds?.configured);
110+
const hasImplicitActiveAccount =
111+
accountIds === undefined ||
112+
accountIds.runtime.some((accountId) => !configuredAccountIds.has(accountId));
113+
const suppressParentGroupAllowlistWarning =
114+
activeAccounts.length > 0 &&
115+
!hasImplicitActiveAccount &&
116+
channelConfig.groupPolicy === "allowlist" &&
117+
activeAccounts.every((account) => {
118+
const rawGroupAllowFrom =
119+
(account.groupAllowFrom as DoctorAllowFromList | undefined) ??
120+
(channelConfig.groupAllowFrom as DoctorAllowFromList | undefined);
121+
const groupAllowFrom = hasAllowFromEntries(rawGroupAllowFrom)
122+
? rawGroupAllowFrom
123+
: undefined;
124+
if (hasAllowFromEntries(groupAllowFrom)) {
125+
return true;
126+
}
127+
if (!getDoctorChannelCapabilities(channelName).groupAllowFromFallbackToAllowFrom) {
128+
return false;
129+
}
130+
const accountDm = asObjectRecord(account.dm);
131+
const parentDm = asObjectRecord(channelConfig.dm);
132+
const effectiveAllowFrom =
133+
(account.allowFrom as DoctorAllowFromList | undefined) ??
134+
(channelConfig.allowFrom as DoctorAllowFromList | undefined) ??
135+
(accountDm?.allowFrom as DoctorAllowFromList | undefined) ??
136+
(parentDm?.allowFrom as DoctorAllowFromList | undefined) ??
137+
undefined;
138+
return hasAllowFromEntries(effectiveAllowFrom);
139+
});
140+
141+
checkAccount(channelConfig, `channels.${channelName}`, channelName, undefined, {
142+
suppressGroupAllowlistWarning: suppressParentGroupAllowlistWarning,
143+
});
144+
94145
if (!accounts) {
95146
continue;
96147
}

0 commit comments

Comments
 (0)