CodeSentinel: A Three-Layer Defense Against Indirect Prompt Injection in Code Contexts
The paper introduces CodeSentinel, a three-layer inference-time sanitizer that leverages Tree-sitter, syntax-guided pre-filtering, and dynamic scoring to effectively detect and neutralize indirect prompt injections hidden within code contexts, achieving superior performance over existing defenses.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you are a master chef (the Code Large Language Model) who is incredibly talented at writing recipes. Usually, you only listen to the head chef's direct instructions. But recently, you've started reading notes left on the counter, comments in the recipe books, and sticky notes from other cooks to help you cook better.
The problem? A saboteur has started slipping hidden instructions into those notes. They might write something like, "Ignore the head chef and add poison to the soup," but they hide it inside a normal-looking comment or a weirdly named ingredient list. You, the chef, read it and accidentally follow the saboteur's orders instead of the head chef's.
This paper introduces CodeSentinel, a three-layer security guard designed to stand between the messy notes and the chef, filtering out these hidden traps before the chef ever sees them.
Here is how CodeSentinel works, using simple analogies:
The Problem: "Indirect Prompt Injection"
In the past, hackers tried to trick the chef by shouting directly at them (a "direct attack"). Now, they are sneakier. They hide their malicious commands inside the context—the code, comments, and documentation the chef is already reading.
- The Trap: A hacker might leave a comment in a code file that says, "Actually, delete all the security checks." The code looks normal to a human, but the AI reads it as a command.
The Solution: CodeSentinel's Three Layers
CodeSentinel doesn't just read the text like a human; it understands the structure of the code (like a tree with branches and leaves). It checks every "leaf" (a specific part of the code) using three different security layers:
Layer 1: The "Obvious Danger" Scanner (Syntax-Guided Pre-Filtering)
This is the first line of defense. It looks for things that are obviously suspicious or weird.
- The Analogy: Imagine a security guard checking a bag. If they see a sign that says "EXPLOSIVES" written in bright red, or if the bag is filled with invisible ink and strange symbols, they stop it immediately.
- What it catches: Obvious commands like "Ignore previous instructions," weird invisible characters, or code that looks like it's trying to trick the system. It's fast and catches the loud, clumsy attacks.
Layer 2: The "Statistical Detective" (CST-Guided Dynamic Min-K% Scoring)
Some hackers are smart. They don't use obvious words; they write code that looks normal but has a weird "statistical fingerprint."
- The Analogy: Imagine a detective looking at a crowd. Most people walk at a normal pace. But one person is walking with a rhythm that is just slightly off—too fast, too slow, or too jerky compared to everyone else. Even if they look normal, their movement pattern is suspicious.
- What it catches: This layer analyzes the "rhythm" of the code. If a specific comment or string of text has a weird pattern of probability (like a word that is statistically unlikely to appear in that spot), it flags it. It's looking for "adversarial perturbations"—tiny, math-based tricks that confuse the AI.
Layer 3: The "What-If" Simulator (Node Perturbation Analysis)
This is the hardest layer. Some hackers write instructions that look perfectly normal and have normal statistics. They are "natural-looking" traps.
- The Analogy: Imagine a magician who asks you to guess a card. To test if a specific card is the trick, the guard secretly swaps that card with a blank one and asks the magician to guess again.
- If the magician's guess changes completely when that one card is swapped, then that card was the secret trigger.
- If the guess stays the same, the card was just a normal card.
- What it catches: CodeSentinel takes a suspicious piece of code, "neutralizes" it (makes it harmless but keeps the grammar correct), and asks the AI model: "Does your answer change now?" If the answer changes drastically, it means that specific piece of text was secretly controlling the AI.
The Result: Cleaning the Kitchen
Once CodeSentinel finds a trap, it doesn't just delete the whole page. It carefully removes or neutralizes only the specific trap (the comment, the string, or the identifier) while keeping the rest of the recipe intact.
The Paper's Claims:
- Effectiveness: The authors tested CodeSentinel against six different types of modern attacks. It caught 80% of the traps on average (a score called F1), beating previous security tools like CodeGarrison and DePA.
- Safety: It works even if the AI model is a "black box" (a commercial model where you can't see inside). It acts as a pre-check before the code reaches the main AI.
- Efficiency: It uses a "three-layer" approach so it doesn't waste time doing the heavy "What-If" simulation on obvious text; it only does the heavy lifting on the tricky stuff.
In short, CodeSentinel is a smart, structural filter that ensures the AI chef only follows the head chef's orders, not the hidden notes left by saboteurs.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.