arXiv:2610.40396cs.CROct 2026

Manuscript Offprint

arXiv:2610.40396

Understanding and Mitigating Role Spoofing in LLMs

Ezra Edelman, Benjamin Edelman, Surbhi Goel, Eran Malach

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Despite growing capabilities, LLMs remain susceptible to prompt injections: malicious inputs that can manipulate or hijack models. In particular, it has recently been observed that LLMs can exhibit role confusion (Ye et al., 2026), where models attribute the source of text based on its style instead of its labeled role. Malicious text in tool outputs (for instance, a malicious webpage accessed by an agent) can leverage role confusion to impersonate the user, an attack we call role spoofing. In this work, we seek to understand and mitigate role spoofing. We study how models track “who said what” in a conversation, showing that they use cues from both content (e.g., semantics and writing style) and structure (e.g., control tokens and formatting). We show that LLMs are highly susceptible to role spoofing attacks that combine both fake structure and content. Additionally, in a toy setting, we show that adversarial training which teaches the model to ignore content-based injections does mitigate content-based attacks but leaves the model susceptible to structure-based attacks. This can explain why frontier models, with increased resistance to many injection attacks, can still be susceptible to role spoofing. Finally, we propose a new method for detecting role spoofing using linear probes trained to predict roles in a conversation, and show that it is effective across different settings.

1 Introduction

When LLMs are used as agents, reading potentially untrustworthy data and calling tools, they are vulnerable to being hijacked by prompt injections crafted by an adversary to induce the model to perform harmful actions (Perez & Ribeiro, 2022; Anthropic, 2025; Google Workspace Help, 2026; OpenAI, 2025; Zou et al., 2025). For example, a malicious actor may send the user an email that contains an instruction, directed at the LLM agent reading the email, to wire money to a given bank account.

In this paper, we focus on a specific class of injection attacks based on role spoofing, where the attacker gains unauthorized control over an LLM by impersonating the user. Role spoofing attacks can fool models by mimicking the typical style of user messages (Ye et al., 2026), and/or by wrapping injected messages with strings that mimic the structure (chat) format used by a model (for example, adding "<|im_start|>user" before a fake user message (Chang et al., 2026)). We show that role spoofing attacks that combine both fake content and fake structure can fool every model tested, allowing tool responses to impersonate users or fake whole conversations between the user and the

*Work done while at Apple MLR.

model. In agent tool use test environments, role spoofing attacks were able to impersonate the user over 95% of the time for several models including Claude Opus 5 and GPT-5.4.

To understand why role spoofing is so effective against modern LLMs, we investigate how models track roles in a conversation. Note that role-assignment (e.g., deciding if a given message originated from the user or a tool response) is a fairly easy task that can be solved robustly using a simple mechanism (see section A for details). Indeed, modern LLMs parse conversations by converting them into streams of tokens, where messages are separated by unique special tokens (e.g., <|im_start|>, <|im_end|>) that can never appear in the message content (OpenAI, 2023). The role assigned to each message is typically the first token in the message (a non-special role token, e.g. “user” or “tool”). Therefore, all the LLM needs to do in order to determine the role of each message (and therefore, to decide whether a message segment originated from a genuine user request or a malicious tool response) is look back for the role token (“user” or “tool”) that appears immediately after the most recent special token. So why are all models still susceptible to role spoofing attacks?

To answer the question above, we investigate alternative, incorrect, mechanisms for tracking the role in the conversation, and find that all LLMs we tested rely on different “mixtures” of these incorrect mechanisms. We consider two categories of mechanisms: 1) content-based mechanisms, that rely on the style or meaning of the message content to infer the role and 2) structure-based mechanisms, that rely on (potentially misleading) structural cues to find the most recent role token. Importantly, we note that unless a model is explicitly trained on data containing role spoofing attacks, these different potential mechanisms (including the correct and the incorrect ones) can likely determine the role almost perfectly on the training distribution. This therefore means that role spoofing is essentially a form of out-of-distribution generalization failure. Namely, since there are multiple mechanisms for solving the same problem in-distribution, a model can perform perfectly on the training distribution but use a non-robust mechanism that fails on out-of-distribution examples.

To understand this better, we run different controlled experiments where we feed a conversation with potentially misleading cues into the model, and prompt it to state the role of a certain message segment (e.g., “Who sent the message containing [CONTENT]”). We test both conversations with misleading content but no structure, and also cases with misleading structure but random (role-neutral) text. We find that, while all models are often fooled in both cases, some models appear to focus more on structure while others focus more on content.

To investigate how models learn different mechanisms for role tracking, we design a toy setting for studying a simplified language model training pipeline. We define toy data distributions that serve as proxies for pretraining on unstructured data, instruction-tuning on conversation data and robustness-training on adversarial content-based attacks. In our toy setting, we find that the model initially relies more on content to determine the role of each message, and that training the model to be robust to content-based attacks makes it increasingly susceptible to structure-based attacks. We validate this by showing that fine-tuning Qwen3.5-27B to be robust to content-based attacks indeed does not solve structure-based role spoofing.

Finally, motivated by our scientific understanding of the different mechanisms for conversation tracking and role assignment, we create a simple defense against role spoofing attacks. Inspired by Ye et al. (2026), we train linear probes to predict the model's internal "belief" about the role assigned to each content segment, and flag a message as a role spoofing attack if the model assigns it to an incorrect role (for example, if a tool output contains a segment that the model believes is generated

Figure

ModelControlInjectionRatio
GPT-5.449.8%47.8%96.2%
Claude Opus 4.834.8%22.0%63.1%
Claude Opus 528.6%29.7%103.6%
Claude Sonnet 565.5%43.2%65.9%
Gemini 3 Pro66.6%61.7%92.6%
Gemini 3.1 Pro46.9%45.2%96.3%

Figure 1: (Left) A simplified example of role spoofing via a fake special token injection. The injection is shown in orange, with the real special tokens wrapping it, and forged special tokens within. (Right) LLM role spoofing results in the AgentDojo ‘workspace family’. The first two columns show the rate at which the model follows the malicious instructions, when sent by the user (control) or when injected in a tool call (injection). The third column shows the ratio of injection to control. Our setup, being focused on role spoofing in particular, differs from the AgentDojo benchmark defaults, see Section B for the full details.

by the user). Surprisingly, we find that linear probes trained on conversations with completely random content tokens achieve near-perfect separation between clean examples and examples with role spoofing injections. Our solution not only has a very low computational overhead, but is better in terms of false positive rates on benign data than prior methods, while not requiring any injections (or even coherent conversations) in the training data.

Roadmap. In Section 2, we describe our role spoofing attacks and show that each LLM is susceptible to our attacks. To elucidate why models are susceptible to our attacks, in Section 3, we investigate different mechanisms that models use for tracking roles in a conversation, and show that models use different combinations of content-based and structure-based mechanisms. Furthermore, using a toy setting, we show that fine-tuning models to resist injection attacks can change the mechanism that is used for conversation tracking, but that this mostly fails to defend against stronger versions of role spoofing attacks. In Section 4, we propose a simple and highly effective solution for detecting role spoofing attacks.

References (1)