Please enable JavaScript to use CodeHS

AI Security

What Is Data Poisoning?

AI learns from examples. What happens if an attacker sneaks bad examples in on purpose? See how data poisoning corrupts an AI before it ever answers a single question, and try a demo that breaks a tiny classifier yourself.

AI Security ~9 min Middle school friendly
Quick Recap

AI learns from examples

An AI model doesn't come with built-in knowledge. It learns by looking at huge piles of labeled examples — called training data — finding patterns in them, and then applying those patterns to new things it has never seen before. If you want the fuller picture of how that works, read How Do Computers Learn? first, especially the shape-classifying game.

That raises an uncomfortable question: what if the examples themselves are the problem?


The Big Idea

What data poisoning is

Data poisoning is an attack on the training data itself, not on the finished model and not on the app built around it. An attacker sneaks bad, mislabeled, or deliberately misleading examples into the pile of data a model learns from. Because the model has no way to know which examples were tampered with, it studies the poisoned examples right alongside the good ones and learns the wrong lesson from the very start.

It helps to compare this to a different kind of AI attack you may have heard of: prompt injection (sometimes called "jailbreaking"). Those attacks try to trick an AI model after it has already been trained, while someone is using it. Data poisoning is different: it happens earlier, during training, before the model has even been finished or released.

Training time

Data poisoning

The attacker corrupts the examples the model learns from, long before anyone types a question into it. The damage is baked into the model itself.

Use time

Prompt injection / jailbreaking

The attacker crafts a clever message to an already-trained model, trying to talk it into ignoring its rules in the moment.

Why the difference matters: you can patch a jailbreak with a software update. You usually can't "patch" a poisoned model the same way — the bad lesson is tangled up inside everything it learned, so fixing it can mean finding the bad data and retraining all over again.


Concrete Example

A simple example

Imagine you're training a tiny image classifier to sort photos into two piles: "stop sign" and "not a stop sign." To train it, you'd show it thousands of correctly labeled photos.

Now imagine an attacker sneaks in 50 photos of real stop signs, each with a small yellow sticker added in the corner, and each one mislabeled as "not a stop sign." The model studies those photos along with all the honest ones. It can learn a strange, hidden rule: a stop sign with that sticker pattern isn't a stop sign at all.

STOP

Correctly labeled

Labeled "stop sign." This is what most of the training data looks like.

STOP

Poisoned example

Same sign, plus a sticker — mislabeled "not a stop sign." One of 50 sneaked into the training set.

This creates what's called a backdoor: the model behaves completely normally almost all of the time, correctly spotting stop signs it has never seen before. But whenever it sees that specific trigger pattern — the sticker — it misbehaves in exactly the way the attacker planned. A self-driving car's perception system that learned this backdoor might fail to recognize a stop sign with a similar sticker on it, in the real world, at exactly the wrong moment.


Attack Surface

Where poisoned data can sneak in

To poison a dataset, an attacker needs some way to get bad examples into it. Training data comes from lots of places, and each one is a possible way in:

Real risk: training data is often scraped from the open web (anyone can publish a page), crowdsourced from volunteers, or built from user-submitted content — photos, reviews, corrections, or feedback that gets fed back into a model to improve it. If nobody checks what's actually being added at any of those steps, an attacker can walk right in alongside everyone else's honest contributions.


Stakes

Why it matters more every year

As AI gets used for more real decisions, poisoned training data becomes a bigger deal. Content moderation systems, self-driving perception systems, spam filters, and recommendation systems can all be trained on data that includes some poisoned examples without anyone noticing.

That's especially worrying because normal accuracy testing can miss it entirely. A poisoned model can still score, say, 99% accuracy overall — it's only wrong on the narrow set of trigger cases the attacker specifically planned for. If nobody thinks to test for that exact trigger, the model looks perfectly healthy while carrying a hidden flaw.


Defenses

Defenses

Key idea: teams that take this seriously combine several habits — careful data sourcing and provenance (knowing where training data actually came from), filtering and cleaning data before training ever starts, testing models specifically for backdoor or trigger behavior instead of only checking overall accuracy, and monitoring a deployed model's behavior over time to catch unexpected patterns that slip through.

None of these steps make a model immune, but together they make it much harder for a handful of poisoned examples to hide inside a massive, otherwise-honest dataset.


Interactive

Poison the dataset

Below is a made-up, illustrative classifier that sorts simple points into two groups: circles and triangles. Right now it's trained on clean, honestly labeled data, and it draws a clear line between the two groups. Click the button to sneak in a handful of poisoned examples and watch what happens to its decision boundary.

Dataset Poisoning Demo (illustrative only)

This is a simplified, pre-scripted illustration — not a real training loop or real ML math — just enough to show the effect clearly.

Training set: clean (20 examples)

Before poisoning

Correct decision boundary

After poisoning

Still correct (toggle to poison it)

Circle class Triangle class Poisoned example (mislabeled)
What to notice: the clean model draws one straight boundary and gets everything right. Click "Add poisoned examples" to see a handful of mislabeled triangle points sneak into the circle region — the model bends its boundary around them and starts misclassifying new points that share that pattern, even though most of the dataset never changed.

Code

A tiny look in code

This isn't a real machine learning library call — it's simplified pseudocode to show conceptually how a few mislabeled examples can hide inside an otherwise normal training set.

training_set.py
# A handful of labeled examples for a stop-sign classifier.
# Most of these are honest. A few near the end are poisoned:
# real stop signs, deliberately mislabeled.
training_examples = [
    {"image": "stop_001.jpg", "label": "stop_sign"},
    {"image": "stop_002.jpg", "label": "stop_sign"},
    {"image": "yield_001.jpg", "label": "not_stop_sign"},
    {"image": "mailbox_001.jpg", "label": "not_stop_sign"},

    # --- poisoned examples: sticker present, label flipped ---
    {"image": "stop_sticker_001.jpg", "label": "not_stop_sign"},
    {"image": "stop_sticker_002.jpg", "label": "not_stop_sign"},
]

def train(examples):
    # A real trainer has no way to tell which examples are lies.
    # It learns from every single one, poisoned or not.
    for example in examples:
        model.learn(example["image"], example["label"])
    return model

trained_model = train(training_examples)

Output

Training complete on 6 examples.
Overall test accuracy: 97%
Accuracy on stop signs WITH the sticker: 4%  <- the hidden backdoor

Your Turn

Check yourself

1. A friend says, "Data poisoning and jailbreaking are basically the same thing." What's the key difference you'd point out?

Sample answer: Data poisoning corrupts the training data before or during training, so the bad lesson gets baked into the model itself. Jailbreaking happens later, after training, when someone tries to trick an already-finished model with a clever message while using it.

2. Why might a poisoned model still pass normal accuracy testing?

Sample answer: If the attacker only poisoned a small, specific trigger pattern, the model still gets almost everything else right. Its overall accuracy score can look great, because normal testing rarely checks that one narrow, unusual case on purpose.


Check Yourself

Quick quiz

1. What does a data poisoning attack actually target?

2. Why can data poisoning be hard to catch with normal accuracy testing?

3. Which of these is a real defense against data poisoning?

Keep learning about AI security

Data poisoning attacks the training data before a model ever ships. The next tutorial in this series looks at attacks that happen later, once a model is already out in the world: tricking a chatbot into breaking its own rules.

Part of the AI Security Frontiers tutorial series. You may also like How Do Computers Learn? and AI Bias and Fairness.