mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:51:23 +00:00
fix(config): fail closed allowlist-only group policy
Co-authored-by: etereo <etereo@users.noreply.github.com>
This commit is contained in:
92
src/config/group-policy.test.ts
Normal file
92
src/config/group-policy.test.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "./config.js";
|
||||
import { resolveChannelGroupPolicy } from "./group-policy.js";
|
||||
|
||||
describe("resolveChannelGroupPolicy", () => {
|
||||
it("fails closed when groupPolicy=allowlist and groups are missing", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
whatsapp: {
|
||||
groupPolicy: "allowlist",
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
const policy = resolveChannelGroupPolicy({
|
||||
cfg,
|
||||
channel: "whatsapp",
|
||||
groupId: "123@g.us",
|
||||
});
|
||||
|
||||
expect(policy.allowlistEnabled).toBe(true);
|
||||
expect(policy.allowed).toBe(false);
|
||||
});
|
||||
|
||||
it("allows configured groups when groupPolicy=allowlist", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
whatsapp: {
|
||||
groupPolicy: "allowlist",
|
||||
groups: {
|
||||
"123@g.us": { requireMention: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
const policy = resolveChannelGroupPolicy({
|
||||
cfg,
|
||||
channel: "whatsapp",
|
||||
groupId: "123@g.us",
|
||||
});
|
||||
|
||||
expect(policy.allowlistEnabled).toBe(true);
|
||||
expect(policy.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it("blocks all groups when groupPolicy=disabled", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
whatsapp: {
|
||||
groupPolicy: "disabled",
|
||||
groups: {
|
||||
"*": { requireMention: false },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
const policy = resolveChannelGroupPolicy({
|
||||
cfg,
|
||||
channel: "whatsapp",
|
||||
groupId: "123@g.us",
|
||||
});
|
||||
|
||||
expect(policy.allowed).toBe(false);
|
||||
});
|
||||
|
||||
it("respects account-scoped groupPolicy overrides", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
whatsapp: {
|
||||
groupPolicy: "open",
|
||||
accounts: {
|
||||
work: {
|
||||
groupPolicy: "allowlist",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
const policy = resolveChannelGroupPolicy({
|
||||
cfg,
|
||||
channel: "whatsapp",
|
||||
accountId: "work",
|
||||
groupId: "123@g.us",
|
||||
});
|
||||
|
||||
expect(policy.allowlistEnabled).toBe(true);
|
||||
expect(policy.allowed).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -143,6 +143,33 @@ function resolveChannelGroups(
|
||||
return accountGroups ?? channelConfig.groups;
|
||||
}
|
||||
|
||||
type ChannelGroupPolicyMode = "open" | "allowlist" | "disabled";
|
||||
|
||||
function resolveChannelGroupPolicyMode(
|
||||
cfg: OpenClawConfig,
|
||||
channel: GroupPolicyChannel,
|
||||
accountId?: string | null,
|
||||
): ChannelGroupPolicyMode | undefined {
|
||||
const normalizedAccountId = normalizeAccountId(accountId);
|
||||
const channelConfig = cfg.channels?.[channel] as
|
||||
| {
|
||||
groupPolicy?: ChannelGroupPolicyMode;
|
||||
accounts?: Record<string, { groupPolicy?: ChannelGroupPolicyMode }>;
|
||||
}
|
||||
| undefined;
|
||||
if (!channelConfig) {
|
||||
return undefined;
|
||||
}
|
||||
const accountPolicy =
|
||||
channelConfig.accounts?.[normalizedAccountId]?.groupPolicy ??
|
||||
channelConfig.accounts?.[
|
||||
Object.keys(channelConfig.accounts ?? {}).find(
|
||||
(key) => key.toLowerCase() === normalizedAccountId.toLowerCase(),
|
||||
) ?? ""
|
||||
]?.groupPolicy;
|
||||
return accountPolicy ?? channelConfig.groupPolicy;
|
||||
}
|
||||
|
||||
export function resolveChannelGroupPolicy(params: {
|
||||
cfg: OpenClawConfig;
|
||||
channel: GroupPolicyChannel;
|
||||
@@ -152,14 +179,17 @@ export function resolveChannelGroupPolicy(params: {
|
||||
}): ChannelGroupPolicy {
|
||||
const { cfg, channel } = params;
|
||||
const groups = resolveChannelGroups(cfg, channel, params.accountId);
|
||||
const allowlistEnabled = Boolean(groups && Object.keys(groups).length > 0);
|
||||
const groupPolicy = resolveChannelGroupPolicyMode(cfg, channel, params.accountId);
|
||||
const hasGroups = Boolean(groups && Object.keys(groups).length > 0);
|
||||
const allowlistEnabled = groupPolicy === "allowlist" || hasGroups;
|
||||
const normalizedId = params.groupId?.trim();
|
||||
const groupConfig = normalizedId
|
||||
? resolveChannelGroupConfig(groups, normalizedId, params.groupIdCaseInsensitive)
|
||||
: undefined;
|
||||
const defaultConfig = groups?.["*"];
|
||||
const allowAll = allowlistEnabled && Boolean(groups && Object.hasOwn(groups, "*"));
|
||||
const allowed = !allowlistEnabled || allowAll || Boolean(groupConfig);
|
||||
const allowed =
|
||||
groupPolicy === "disabled" ? false : !allowlistEnabled || allowAll || Boolean(groupConfig);
|
||||
return {
|
||||
allowlistEnabled,
|
||||
allowed,
|
||||
|
||||
Reference in New Issue
Block a user