Skip to content

fix(discord): keep mention labels literal in text substitution - #130778

Merged
steipete merged 1 commit into
mainfrom
steipete/discord-mention-replace
Aug 27, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/discord-mention-replace

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Discord inbound mention substitution passes user-controlled display names as the replacement string to String.prototype.replace, which interprets $ sequences. Any message mentioning a user whose name contains a $ pattern gets silently corrupted before it reaches the agent, channel history, and reply/forward context:

  • a name containing $$ (e.g. big$$money) renders as big$money;
  • a crafted name of $' duplicates the trailing half of the message; $` duplicates the leading half; $& reinserts the raw <@id> markup — a mild injection vector, since a mentioned user's name can rewrite other people's message text as the agent sees it.

The same site rendered @undefined when a mention payload carried neither globalName nor username.

Why This Change Was Made

resolveDiscordMentions (message-text.ts) is the shared projection used by the message body, history entries, and reply/forward context, so the corruption propagated everywhere Discord text is read. The fix keeps the label literal by using a replacer callback (() => @${label}``) and falls back to the user ID for nameless users, matching the existing @id conventions in `message-forwarded.ts:112` and `format.ts:38`. Sibling channels are unaffected: Slack strips mentions with a static replacement and Telegram mentions are already plain text; a repo-wide sweep found this to be the only dynamic-regex + templated-replacement site in the channel plugins. The defect exists in shipped stable `v2026.6.1`.

Production LOC: +2/−2 (net 0); tests +26.

User Impact

Discord messages mentioning users with $ sequences in their names now reach the agent verbatim, and a crafted display name can no longer duplicate or rewrite message content. Nameless mention payloads render as @<id> instead of @undefined.

Evidence

  • Node-level reproduction on pre-fix code confirmed suffix/prefix duplication, raw-markup reinsertion, $$→$ collapse, and @undefined.
  • New regressions cover display names and usernames, <@id>/<@!id> markup, repeated mentions, all four active $ substitution patterns, and absent/empty names; 6 fail on pre-fix code, 25/25 pass post-fix (node scripts/run-vitest.mjs extensions/discord/src/monitor/message-text, re-run green after rebase onto latest origin/main).
  • check-changed extension lanes green locally (formatting, types, lint, dead-export scans); import-cycles guard green.
  • Codex autoreview: clean, no accepted/actionable findings.

@clawsweeper

clawsweeper Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XS maintainer Maintainer-authored PR labels Aug 27, 2026
@steipete
steipete marked this pull request as ready for review August 27, 2026 07:16
@steipete
steipete merged commit 0c5fb0f into main Aug 27, 2026
161 checks passed
@steipete
steipete deleted the steipete/discord-mention-replace branch August 27, 2026 07:27
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord maintainer Maintainer-authored PR size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant