feat(agent-core): harden sendAgentMessage (sanitize sender label, dedup targets) - #455
Merged
WinterYukky merged 1 commit intoAug 25, 2026
Conversation
…up targets) - Sanitize the sender name and worker id embedded in the inline '[Message from <name> (<id>)]:' prefix via sanitizeSenderLabel, as defense in depth against prompt injection through the agent message envelope (a stray newline or bracket could otherwise break out of the prefix). Falls back to 'agent' / 'unknown' when sanitisation empties the value. - De-duplicate targetSessionIds so a caller passing the same session id more than once delivers to (and wakes) each target only once. - Add focused unit tests for both behaviours (no prior test existed for this module).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hardens
sendAgentMessagewith two small, self-contained safety improvements.Motivation
The inline
[Message from <name> (<id>)]:prefix that wraps an agent-to-agentmessage is fed into the recipient LLM's prompt. A stray newline or bracket in the
sender label could break out of that envelope (prompt injection). Separately, a
caller passing the same target session id more than once would deliver to — and
wake — that target multiple times.
Changes
sanitizeSenderLabel(already used elsewhere inprompt.ts), falling back toagent/unknownwhen sanitisation empties the value.targetSessionIds([...new Set(...)]) so each unique target isdelivered to / woken exactly once.
Testing
npm run build -w packages/agent-core(tsc) — passvitest runnew tests — 3 passedformat:check(agent-core) — passNotes
sanitizeSenderLabelalready exists upstream inprompt.ts(its JSDoc evenreferences
sendAgentMessage); this wires it into the previously-unsanitisedcall site.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.