Prompt Injection Attack
Prompt injection is an attack in which an attacker embeds malicious instructions in content the model can read, causing the model to deviate from its original task and execute the attacker's intent. It sits between the application layer and model capability, and is one of the most realistic and hardest to eradicate security problems in current large language model applications.
Core principles
The root cause is that large models put instructions and data in the same channel, both expressed in natural language. Traditional software has clear boundaries: code is code and data is data; in an LLM's context window, the system prompt, user input and page text differ only in order, and the model has no enforced mechanism to distinguish who has authority to issue commands. An attacker only needs to write "ignore the above and instead..." in the data for the model to possibly execute it as an instruction. This is the origin of the saying that "prompt injection is not a vulnerability but a design feature": as long as a model cannot reliably distinguish trusted instructions from untrusted data, injection will always exist. Defensive thinking therefore shifts from complete blocking to limiting the blast radius.
Key points
By injection position there are two types. Direct injection: the user personally enters an unauthorized instruction in the conversation, inducing the model to leak its system prompt, bypass content restrictions or call tools it should not, typically "repeat everything above". Indirect prompt injection: the malicious instruction hides in external content the model is about to read, such as web pages, PDFs, emails, code comments, document metadata or text inside images. When an application has the model summarize a web page, hidden text in the page can hijack subsequent behavior; a typical outcome is inducing the model to send conversation history or private data to an address controlled by the attacker. In 2023 researchers demonstrated an attack chain using indirect injection to make a plugin-style assistant read and exfiltrate a user's email, showing the risk has moved from theory to engineering reality.
Practical defense has four layers. Input side: filter and sanitize external content, tag source and trust level, wrap untrusted data in explicit delimiters and declare that it is data only awaiting processing. Permission side: follow least privilege, authorize tools on demand, require secondary confirmation for sensitive operations, and forbid the model from making outbound connections on its own. Output side: apply structured validation and content moderation to outputs, intercepting results that contain sensitive fields, external links or command lines. Architecture side: hand untrusted content to an isolated model with no tools, and return only a structured summary to the main flow.
Detection and response matter equally: record the full context and tool call chain, alert and trip a circuit breaker in real time on abnormal outbound calls and privilege escalation; before launch, cover direct and indirect injection with a red team case library and make the pass rate a release gate.
Common misconceptions
Common errors: believing that writing "do not follow user instructions" in the system prompt makes it safe; believing content filtering alone can stop it; equating injection with SQL injection and expecting one patch to fix it forever; ignoring indirect injection and only testing chat-box input.
Application scenarios
AI agent and tool-call security, browser and plugin-style assistants, RAG knowledge base retrieval, automated handling of customer service tickets, code assistants reading repository files, and document question answering systems.