fix(auto-reply): prevent sender spoofing in group prompts

This commit is contained in:
Peter Steinberger
2026-02-10 00:35:56 -06:00
parent 8ff1618bfc
commit 53273b490b
42 changed files with 405 additions and 243 deletions

View File

@@ -1,7 +1,6 @@
import type { FinalizedMsgContext, MsgContext } from "../templating.js";
import { normalizeChatType } from "../../channels/chat-type.js";
import { resolveConversationLabel } from "../../channels/conversation-label.js";
import { formatInboundBodyWithSenderMeta } from "./inbound-sender-meta.js";
import { normalizeInboundTextNewlines } from "./inbound-text.js";
export type FinalizeInboundContextOptions = {
@@ -45,7 +44,11 @@ export function finalizeInboundContext<T extends Record<string, unknown>>(
const bodyForAgentSource = opts.forceBodyForAgent
? normalized.Body
: (normalized.BodyForAgent ?? normalized.Body);
: (normalized.BodyForAgent ??
// Prefer "clean" text over legacy envelope-shaped Body when upstream forgets to set BodyForAgent.
normalized.CommandBody ??
normalized.RawBody ??
normalized.Body);
normalized.BodyForAgent = normalizeInboundTextNewlines(bodyForAgentSource);
const bodyForCommandsSource = opts.forceBodyForCommands
@@ -66,14 +69,6 @@ export function finalizeInboundContext<T extends Record<string, unknown>>(
normalized.ConversationLabel = explicitLabel;
}
// Ensure group/channel messages retain a sender meta line even when the body is a
// structured envelope (e.g. "[Signal ...] Alice: hi").
normalized.Body = formatInboundBodyWithSenderMeta({ ctx: normalized, body: normalized.Body });
normalized.BodyForAgent = formatInboundBodyWithSenderMeta({
ctx: normalized,
body: normalized.BodyForAgent,
});
// Always set. Default-deny when upstream forgets to populate it.
normalized.CommandAuthorized = normalized.CommandAuthorized === true;