Skip to content

Commit a509254

Browse files
authored
feat(security): warn on inert mutable allowFrom entries (#131129)
* feat(security): warn on inert mutable allowFrom entries Report configured entries that cannot authorize with name matching disabled, and preview lockout counts before disabling mutable matching. Distinguish mutable aliases from exact identifiers with unproven ownership in audit text. Reuse channel ingress declarations through one SDK classifier without changing admission policy. Keep Discord audit findings redacted and exclude symbolic access-group references from mutable-identifier warnings. Refs #131099. * test(security): keep entry-authentication audit coverage in its lanes
1 parent 9a579cf commit a509254

37 files changed

Lines changed: 1037 additions & 571 deletions

‎docs/gateway/security/audit-checks.md‎

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

‎docs/plugins/sdk-channel-ingress.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ field it wants counted. A field missing from a supplied map is treated as
162162
`unverified`, even if its identity descriptor declares a stronger static claim.
163163
Channels with static strength omit the map entirely.
164164

165+
Expose `classifyEntryAuthentication: identityEntryAuthenticationClassifier(identity)`
166+
from the security adapter's `resolveDmPolicy` result, importing the helper from
167+
`openclaw/plugin-sdk/channel-ingress-runtime`. It uses the identity descriptor's
168+
entry normalizers and returns the strongest static claim among accepting fields,
169+
or `undefined` when none accepts the entry; wildcard entries are excluded.
170+
The [security audit](/gateway/security/audit-checks) counts configured `allowFrom`
171+
entries that depend only on mutable identifiers: it warns when name matching is
172+
disabled and, when enabled, previews how many entries would stop authorizing
173+
after disabling it. Findings contain counts and config paths, not raw entries;
174+
pairing-store approvals are outside this check.
175+
Symbolic `accessGroup:` references resolve membership separately and are not
176+
counted as mutable identifiers.
177+
165178
Existing plugins remain source-compatible during the deprecation window:
166179

167180
| Deprecated field | Exact mapping |

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

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,21 @@ import {
33
type AccessGroupMembershipFact,
44
type ChannelIngressEventInput,
55
type ChannelIngressContextBinding,
6-
type ChannelIngressIdentifierKind,
76
type IdentifierAuthentication,
87
createChannelIngressResolver,
9-
defineStableChannelIngressIdentity,
108
type ChannelIngressIdentitySubjectInput,
119
type ResolveChannelMessageIngressParams,
1210
} from "openclaw/plugin-sdk/channel-ingress-runtime";
1311
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
1412
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
1513
import type { RequestClient } from "../internal/discord.js";
1614
import { canViewDiscordGuildChannel } from "../send.permissions.js";
17-
import { normalizeDiscordAllowList } from "./allow-list.js";
15+
import { discordIngressIdentity } from "./ingress-identity.js";
1816

19-
const DISCORD_ALLOW_LIST_PREFIXES = ["discord:", "user:", "pk:"];
2017
const DISCORD_CHANNEL_ID = "discord";
21-
const DISCORD_USER_ID_KIND = "stable-id" satisfies ChannelIngressIdentifierKind;
22-
const DISCORD_USER_NAME_KIND = "username" satisfies ChannelIngressIdentifierKind;
2318

2419
export type DiscordDmPolicy = "open" | "pairing" | "allowlist" | "disabled";
2520

26-
function normalizeDiscordIdEntry(entry: string): string | null {
27-
const text = entry.trim();
28-
if (!text) {
29-
return null;
30-
}
31-
const maybeId = text.replace(/^<@!?/, "").replace(/>$/, "");
32-
if (/^\d+$/.test(maybeId)) {
33-
return maybeId;
34-
}
35-
const prefix = DISCORD_ALLOW_LIST_PREFIXES.find((entryPrefix) => text.startsWith(entryPrefix));
36-
if (prefix) {
37-
const candidate = text.slice(prefix.length).trim();
38-
return candidate || null;
39-
}
40-
return null;
41-
}
42-
43-
function normalizeDiscordNameEntry(entry: string): string | null {
44-
const text = entry.trim();
45-
if (!text || text === "*" || normalizeDiscordIdEntry(text) || /#\d{4}$/.test(text)) {
46-
return null;
47-
}
48-
const nameSlug = normalizeDiscordAllowList([text], DISCORD_ALLOW_LIST_PREFIXES)
49-
?.names.values()
50-
.next().value;
51-
return typeof nameSlug === "string" && nameSlug ? nameSlug : null;
52-
}
53-
54-
function normalizeDiscordTagEntry(entry: string): string | null {
55-
const text = entry.trim();
56-
return /#\d{4}$/.test(text) ? normalizeDiscordNameSubject(text) : null;
57-
}
58-
59-
function normalizeDiscordNameSubject(value: string): string | null {
60-
const nameSlug = normalizeDiscordAllowList([value], DISCORD_ALLOW_LIST_PREFIXES)
61-
?.names.values()
62-
.next().value;
63-
return typeof nameSlug === "string" && nameSlug ? nameSlug : null;
64-
}
65-
66-
const discordIngressIdentity = defineStableChannelIngressIdentity({
67-
key: "discordUserId",
68-
kind: DISCORD_USER_ID_KIND,
69-
// Discord binds author/user.id on events delivered over the authenticated bot-token session.
70-
authentication: "verified",
71-
normalizeEntry: normalizeDiscordIdEntry,
72-
normalizeSubject: (value) => value.trim() || null,
73-
sensitivity: "pii",
74-
aliases: (
75-
[
76-
["discordUserName", normalizeDiscordNameEntry],
77-
["discordUserTag", normalizeDiscordTagEntry],
78-
] as const
79-
).map(([key, normalizeEntry]) => ({
80-
key,
81-
kind: DISCORD_USER_NAME_KIND,
82-
normalizeEntry,
83-
normalizeSubject: normalizeDiscordNameSubject,
84-
authentication: "mutable",
85-
sensitivity: "pii",
86-
})),
87-
});
88-
8921
function createDiscordDmIngressSubject(sender: {
9022
id: string;
9123
name?: string;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {
2+
defineStableChannelIngressIdentity,
3+
type ChannelIngressIdentifierKind,
4+
} from "openclaw/plugin-sdk/channel-ingress-runtime";
5+
import { normalizeDiscordAllowList } from "./allow-list.js";
6+
7+
const DISCORD_ALLOW_LIST_PREFIXES = ["discord:", "user:", "pk:"];
8+
const DISCORD_USER_ID_KIND = "stable-id" satisfies ChannelIngressIdentifierKind;
9+
const DISCORD_USER_NAME_KIND = "username" satisfies ChannelIngressIdentifierKind;
10+
11+
function normalizeDiscordIdEntry(entry: string): string | null {
12+
const text = entry.trim();
13+
if (!text) {
14+
return null;
15+
}
16+
const maybeId = text.replace(/^<@!?/, "").replace(/>$/, "");
17+
if (/^\d+$/.test(maybeId)) {
18+
return maybeId;
19+
}
20+
const prefix = DISCORD_ALLOW_LIST_PREFIXES.find((entryPrefix) => text.startsWith(entryPrefix));
21+
if (prefix) {
22+
const candidate = text.slice(prefix.length).trim();
23+
return candidate || null;
24+
}
25+
return null;
26+
}
27+
28+
function normalizeDiscordNameEntry(entry: string): string | null {
29+
const text = entry.trim();
30+
if (!text || text === "*" || normalizeDiscordIdEntry(text) || /#\d{4}$/.test(text)) {
31+
return null;
32+
}
33+
const nameSlug = normalizeDiscordAllowList([text], DISCORD_ALLOW_LIST_PREFIXES)
34+
?.names.values()
35+
.next().value;
36+
return typeof nameSlug === "string" && nameSlug ? nameSlug : null;
37+
}
38+
39+
function normalizeDiscordTagEntry(entry: string): string | null {
40+
const text = entry.trim();
41+
return /#\d{4}$/.test(text) ? normalizeDiscordNameSubject(text) : null;
42+
}
43+
44+
function normalizeDiscordNameSubject(value: string): string | null {
45+
const nameSlug = normalizeDiscordAllowList([value], DISCORD_ALLOW_LIST_PREFIXES)
46+
?.names.values()
47+
.next().value;
48+
return typeof nameSlug === "string" && nameSlug ? nameSlug : null;
49+
}
50+
51+
export const discordIngressIdentity = defineStableChannelIngressIdentity({
52+
key: "discordUserId",
53+
kind: DISCORD_USER_ID_KIND,
54+
// Discord binds author/user.id on events delivered over the authenticated bot-token session.
55+
authentication: "verified",
56+
normalizeEntry: normalizeDiscordIdEntry,
57+
normalizeSubject: (value) => value.trim() || null,
58+
sensitivity: "pii",
59+
aliases: (
60+
[
61+
["discordUserName", normalizeDiscordNameEntry],
62+
["discordUserTag", normalizeDiscordTagEntry],
63+
] as const
64+
).map(([key, normalizeEntry]) => ({
65+
key,
66+
kind: DISCORD_USER_NAME_KIND,
67+
normalizeEntry,
68+
normalizeSubject: normalizeDiscordNameSubject,
69+
authentication: "mutable",
70+
sensitivity: "pii",
71+
})),
72+
});

‎extensions/discord/src/security-audit.test.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe("Discord security audit findings", () => {
207207
config: {
208208
enabled: true,
209209
token: "t",
210-
allowFrom: ["Alice#1234", "<@123456789012345678>"],
210+
allowFrom: ["Alice#1234", " Alice#1234 ", "second.operator", "<@123456789012345678>"],
211211
guilds: {
212212
"123": {
213213
users: ["trusted.operator"],
@@ -222,12 +222,21 @@ describe("Discord security audit findings", () => {
222222
storeAllowFrom: ["team.owner"],
223223
expectNameBasedSeverity: "warn",
224224
detailIncludes: [
225-
"channels.discord.allowFrom:Alice#1234",
226-
"channels.discord.guilds.123.users:trusted.operator",
227-
"channels.discord.guilds.123.channels.general.users:security-team",
228-
"~/.openclaw/credentials/discord-allowFrom.json:team.owner",
225+
"Found 5 name/tag entries",
226+
"channels.discord.allowFrom (2)",
227+
"channels.discord.guilds.123.users (1)",
228+
"channels.discord.guilds.123.channels.general.users (1)",
229+
"Discord pairing store (1)",
230+
],
231+
detailExcludes: [
232+
"Alice#1234",
233+
"second.operator",
234+
"trusted.operator",
235+
"security-team",
236+
"team.owner",
237+
"<@123456789012345678>",
238+
"987654321098765432",
229239
],
230-
detailExcludes: ["<@123456789012345678>"],
231240
},
232241
{
233242
name: "marks Discord name-based allowlists as break-glass when dangerous matching is enabled",
@@ -239,6 +248,7 @@ describe("Discord security audit findings", () => {
239248
} satisfies DiscordAccountConfig,
240249
expectNameBasedSeverity: "info",
241250
detailIncludes: ["out-of-scope"],
251+
detailExcludes: ["Alice#1234"],
242252
},
243253
{
244254
name: "audits name-based allowlists on non-default Discord accounts",
@@ -251,7 +261,8 @@ describe("Discord security audit findings", () => {
251261
allowFrom: ["Alice#1234"],
252262
} satisfies DiscordAccountConfig,
253263
expectNameBasedSeverity: "warn",
254-
detailIncludes: ["channels.discord.accounts.beta.allowFrom:Alice#1234"],
264+
detailIncludes: ["channels.discord.accounts.beta.allowFrom (1)"],
265+
detailExcludes: ["Alice#1234"],
255266
},
256267
{
257268
name: "does not warn when Discord allowlists use ID-style entries only",
@@ -302,7 +313,7 @@ describe("Discord security audit findings", () => {
302313
expect(nameBasedFinding.detail).toContain(snippet);
303314
}
304315
for (const snippet of testCase.detailExcludes ?? []) {
305-
expect(nameBasedFinding.detail).not.toContain(snippet);
316+
expect(JSON.stringify(findings)).not.toContain(snippet);
306317
}
307318
}
308319
});

‎extensions/discord/src/security-audit.ts‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,18 @@ function listBroadMemberTargetPaths(params: {
5656
}
5757

5858
function addDiscordNameBasedEntries(params: {
59-
target: Set<string>;
59+
target: Map<string, number>;
6060
values: unknown;
6161
source: string;
6262
}) {
6363
if (!Array.isArray(params.values)) {
6464
return;
6565
}
66-
for (const value of params.values) {
67-
if (!isDiscordMutableAllowEntry(String(value))) {
68-
continue;
69-
}
70-
const text = normalizeOptionalString(String(value)) ?? "";
71-
if (!text) {
72-
continue;
73-
}
74-
params.target.add(`${params.source}:${text}`);
66+
const entries = new Set(
67+
params.values.map((value) => String(value).trim()).filter(isDiscordMutableAllowEntry),
68+
);
69+
if (entries.size > 0) {
70+
params.target.set(params.source, entries.size);
7571
}
7672
}
7773

@@ -96,7 +92,7 @@ export async function collectDiscordSecurityAuditFindings(params: {
9692
const storeAllowFrom = await readChannelAllowFromStore("discord", process.env, accountId).catch(
9793
() => [],
9894
);
99-
const discordNameBasedAllowEntries = new Set<string>();
95+
const discordNameBasedAllowEntries = new Map<string, number>();
10096
const discordPathPrefix =
10197
params.orderedAccountIds.length > 1 || params.hasExplicitAccountPath
10298
? `channels.discord.accounts.${accountId}`
@@ -136,7 +132,7 @@ export async function collectDiscordSecurityAuditFindings(params: {
136132
addDiscordNameBasedEntries({
137133
target: discordNameBasedAllowEntries,
138134
values: storeAllowFrom,
139-
source: "~/.openclaw/credentials/discord-allowFrom.json",
135+
source: "Discord pairing store",
140136
});
141137

142138
const guildEntries = (discordCfg.guilds as Record<string, unknown> | undefined) ?? {};
@@ -168,11 +164,12 @@ export async function collectDiscordSecurityAuditFindings(params: {
168164
}
169165

170166
if (discordNameBasedAllowEntries.size > 0) {
171-
const examples = Array.from(discordNameBasedAllowEntries).slice(0, 5);
167+
const counts = Array.from(discordNameBasedAllowEntries);
168+
const entryCount = counts.reduce((total, [, count]) => total + count, 0);
169+
const sources = counts.slice(0, 5).map(([source, count]) => `${source} (${count})`);
172170
const more =
173-
discordNameBasedAllowEntries.size > examples.length
174-
? ` (+${discordNameBasedAllowEntries.size - examples.length} more)`
175-
: "";
171+
counts.length > sources.length ? ` (+${counts.length - sources.length} more sources)` : "";
172+
const summary = `Found ${entryCount} name/tag entries: ${sources.join(", ")}${more}.`;
176173
findings.push({
177174
checkId: "channels.discord.allowFrom.name_based_entries",
178175
severity: dangerousNameMatchingEnabled ? "info" : "warn",
@@ -181,9 +178,9 @@ export async function collectDiscordSecurityAuditFindings(params: {
181178
: "Discord allowlist contains name or tag entries",
182179
detail: dangerousNameMatchingEnabled
183180
? "Discord name/tag allowlist matching is explicitly enabled via dangerouslyAllowNameMatching. This mutable-identity mode is operator-selected break-glass behavior and out-of-scope for vulnerability reports by itself. " +
184-
`Found: ${examples.join(", ")}${more}.`
181+
summary
185182
: "Discord name/tag allowlist matching uses normalized slugs and can collide across users. " +
186-
`Found: ${examples.join(", ")}${more}.`,
183+
summary,
187184
remediation: dangerousNameMatchingEnabled
188185
? "Prefer stable Discord IDs (or <@id>/user:<id>/pk:<id>), then disable dangerouslyAllowNameMatching."
189186
: "Prefer stable Discord IDs (or <@id>/user:<id>/pk:<id>) in channels.discord.allowFrom and channels.discord.guilds.*.users, or explicitly opt in with dangerouslyAllowNameMatching=true if you accept the risk.",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Discord tests cover the security adapter's entry-authentication classification.
2+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
3+
import { describe, expect, it } from "vitest";
4+
import { resolveDiscordAccount } from "./accounts.js";
5+
import { discordSecurityAdapter } from "./security.js";
6+
7+
describe("discordSecurityAdapter.resolveDmPolicy", () => {
8+
it("classifies snowflake entries verified and tag entries mutable", () => {
9+
const cfg = { channels: { discord: { token: "test-token" } } } as OpenClawConfig;
10+
const account = resolveDiscordAccount({ cfg, accountId: "default" });
11+
const policy = discordSecurityAdapter.resolveDmPolicy?.({
12+
cfg,
13+
accountId: "default",
14+
account,
15+
});
16+
17+
expect(policy?.classifyEntryAuthentication?.("123456789012345678")).toBe("verified");
18+
expect(policy?.classifyEntryAuthentication?.("alice#0001")).toBe("mutable");
19+
expect(policy?.classifyEntryAuthentication?.("alice")).toBe("mutable");
20+
});
21+
});

‎extensions/discord/src/security.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Discord plugin module implements security behavior.
22
import { createScopedDmSecurityResolver } from "openclaw/plugin-sdk/channel-config-helpers";
3+
import { identityEntryAuthenticationClassifier } from "openclaw/plugin-sdk/channel-ingress-runtime";
34
import {
45
createConditionalWarningCollector,
56
createOpenProviderConfiguredRouteWarningCollector,
@@ -11,6 +12,7 @@ import {
1112
type ResolvedDiscordAccount,
1213
} from "./accounts.js";
1314
import type { ChannelPlugin } from "./channel-api.js";
15+
import { discordIngressIdentity } from "./monitor/ingress-identity.js";
1416

1517
const resolveDiscordDmPolicy = createScopedDmSecurityResolver<ResolvedDiscordAccount>({
1618
channelKey: "discord",
@@ -21,6 +23,7 @@ const resolveDiscordDmPolicy = createScopedDmSecurityResolver<ResolvedDiscordAcc
2123
allowFrom: resolveDiscordAccountAllowFrom({ cfg, accountId: account.accountId }),
2224
}),
2325
policyPathSuffix: "dmPolicy",
26+
classifyEntryAuthentication: identityEntryAuthenticationClassifier(discordIngressIdentity),
2427
normalizeEntry: (raw) =>
2528
raw
2629
.trim()

‎extensions/googlechat/src/channel.adapters.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ChannelThreadingContext,
55
ChannelThreadingToolContext,
66
} from "openclaw/plugin-sdk/channel-contract";
7+
import { identityEntryAuthenticationClassifier } from "openclaw/plugin-sdk/channel-ingress-runtime";
78
import {
89
createMessageReceiptFromOutboundResults,
910
defineChannelMessageAdapter,
@@ -39,6 +40,7 @@ import {
3940
sanitizeGoogleChatText,
4041
} from "./format.js";
4142
import { resolveGoogleChatGroupRequireMention } from "./group-policy.js";
43+
import { googleChatIngressIdentity } from "./ingress-identity.js";
4244

4345
const loadGoogleChatChannelRuntime = createLazyRuntimeNamedExport(
4446
() => import("./channel.runtime.js"),
@@ -124,6 +126,7 @@ export const googlechatSecurityAdapter = {
124126
resolvePolicy: (account: ResolvedGoogleChatAccount) => account.config.dmPolicy,
125127
resolveAllowFrom: (account: ResolvedGoogleChatAccount) => account.config.allowFrom,
126128
allowFromPathSuffix: "",
129+
classifyEntryAuthentication: identityEntryAuthenticationClassifier(googleChatIngressIdentity),
127130
normalizeEntry: (raw: string) => formatGoogleChatAllowFromEntry(raw),
128131
},
129132
collectWarnings: collectGoogleChatSecurityWarnings,

0 commit comments

Comments
 (0)