mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 11:57:39 +00:00
refactor(discord): share component allowlist check
This commit is contained in:
@@ -205,6 +205,40 @@ async function ensureGuildComponentMemberAllowed(params: {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ensureAgentComponentInteractionAllowed(params: {
|
||||||
|
ctx: AgentComponentContext;
|
||||||
|
interaction: AgentComponentInteraction;
|
||||||
|
channelId: string;
|
||||||
|
rawGuildId: string | undefined;
|
||||||
|
memberRoleIds: string[];
|
||||||
|
user: DiscordUser;
|
||||||
|
replyOpts: { ephemeral?: boolean };
|
||||||
|
componentLabel: string;
|
||||||
|
unauthorizedReply: string;
|
||||||
|
}): Promise<{ parentId: string | undefined } | null> {
|
||||||
|
const guildInfo = resolveDiscordGuildEntry({
|
||||||
|
guild: params.interaction.guild ?? undefined,
|
||||||
|
guildEntries: params.ctx.guildEntries,
|
||||||
|
});
|
||||||
|
const channelCtx = resolveDiscordChannelContext(params.interaction);
|
||||||
|
const memberAllowed = await ensureGuildComponentMemberAllowed({
|
||||||
|
interaction: params.interaction,
|
||||||
|
guildInfo,
|
||||||
|
channelId: params.channelId,
|
||||||
|
rawGuildId: params.rawGuildId,
|
||||||
|
channelCtx,
|
||||||
|
memberRoleIds: params.memberRoleIds,
|
||||||
|
user: params.user,
|
||||||
|
replyOpts: params.replyOpts,
|
||||||
|
componentLabel: params.componentLabel,
|
||||||
|
unauthorizedReply: params.unauthorizedReply,
|
||||||
|
});
|
||||||
|
if (!memberAllowed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return { parentId: channelCtx.parentId };
|
||||||
|
}
|
||||||
|
|
||||||
export type AgentComponentContext = {
|
export type AgentComponentContext = {
|
||||||
cfg: OpenClawConfig;
|
cfg: OpenClawConfig;
|
||||||
accountId: string;
|
accountId: string;
|
||||||
@@ -430,29 +464,23 @@ export class AgentComponentButton extends Button {
|
|||||||
memberRoleIds,
|
memberRoleIds,
|
||||||
} = interactionCtx;
|
} = interactionCtx;
|
||||||
|
|
||||||
// P2 FIX: Check user allowlist before processing component interaction
|
// Check user allowlist before processing component interaction
|
||||||
// This prevents unauthorized users from injecting system events
|
// This prevents unauthorized users from injecting system events.
|
||||||
const guildInfo = resolveDiscordGuildEntry({
|
const allowed = await ensureAgentComponentInteractionAllowed({
|
||||||
guild: interaction.guild ?? undefined,
|
ctx: this.ctx,
|
||||||
guildEntries: this.ctx.guildEntries,
|
|
||||||
});
|
|
||||||
const channelCtx = resolveDiscordChannelContext(interaction);
|
|
||||||
const { parentId } = channelCtx;
|
|
||||||
const memberAllowed = await ensureGuildComponentMemberAllowed({
|
|
||||||
interaction,
|
interaction,
|
||||||
guildInfo,
|
|
||||||
channelId,
|
channelId,
|
||||||
rawGuildId,
|
rawGuildId,
|
||||||
channelCtx,
|
|
||||||
memberRoleIds,
|
memberRoleIds,
|
||||||
user,
|
user,
|
||||||
replyOpts,
|
replyOpts,
|
||||||
componentLabel: "button",
|
componentLabel: "button",
|
||||||
unauthorizedReply: "You are not authorized to use this button.",
|
unauthorizedReply: "You are not authorized to use this button.",
|
||||||
});
|
});
|
||||||
if (!memberAllowed) {
|
if (!allowed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const { parentId } = allowed;
|
||||||
|
|
||||||
// Resolve route with full context (guildId, proper peer kind, parentPeer)
|
// Resolve route with full context (guildId, proper peer kind, parentPeer)
|
||||||
const route = resolveAgentRoute({
|
const route = resolveAgentRoute({
|
||||||
@@ -537,28 +565,22 @@ export class AgentSelectMenu extends StringSelectMenu {
|
|||||||
memberRoleIds,
|
memberRoleIds,
|
||||||
} = interactionCtx;
|
} = interactionCtx;
|
||||||
|
|
||||||
// Check user allowlist before processing component interaction
|
// Check user allowlist before processing component interaction.
|
||||||
const guildInfo = resolveDiscordGuildEntry({
|
const allowed = await ensureAgentComponentInteractionAllowed({
|
||||||
guild: interaction.guild ?? undefined,
|
ctx: this.ctx,
|
||||||
guildEntries: this.ctx.guildEntries,
|
|
||||||
});
|
|
||||||
const channelCtx = resolveDiscordChannelContext(interaction);
|
|
||||||
const { parentId } = channelCtx;
|
|
||||||
const memberAllowed = await ensureGuildComponentMemberAllowed({
|
|
||||||
interaction,
|
interaction,
|
||||||
guildInfo,
|
|
||||||
channelId,
|
channelId,
|
||||||
rawGuildId,
|
rawGuildId,
|
||||||
channelCtx,
|
|
||||||
memberRoleIds,
|
memberRoleIds,
|
||||||
user,
|
user,
|
||||||
replyOpts,
|
replyOpts,
|
||||||
componentLabel: "select",
|
componentLabel: "select",
|
||||||
unauthorizedReply: "You are not authorized to use this select menu.",
|
unauthorizedReply: "You are not authorized to use this select menu.",
|
||||||
});
|
});
|
||||||
if (!memberAllowed) {
|
if (!allowed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const { parentId } = allowed;
|
||||||
|
|
||||||
// Extract selected values
|
// Extract selected values
|
||||||
const values = interaction.values ?? [];
|
const values = interaction.values ?? [];
|
||||||
|
|||||||
Reference in New Issue
Block a user