TL;DR
Researchers have introduced ToxicSQL, a framework demonstrating that LLM-based Text-to-SQL models are highly vulnerable to stealthy backdoor attacks. By poisoning as little as 0.44% of the training data, an attacker can cause a model to generate malicious SQL injection payloads when triggered by subtle cues (like a double question mark), all while maintaining near-perfect performance on legitimate user queries.
What Problem Does This Paper Address?
Text-to-SQL models—which translate natural language questions into executable SQL queries—are increasingly used to make databases accessible to non-experts. However, training these models from scratch is expensive, leading many developers to download pre-tuned models from open-source hubs like Hugging Face.
This creates a "supply chain" vulnerability. The authors identify a significant gap: while we’ve spent years making these models more accurate, we haven't sufficiently explored their security. Specifically, they look at backdoor attacks, where an attacker provides a poisoned dataset for fine-tuning. The resulting model acts normally for 99% of users but executes malicious commands (like deleting tables or leaking private data) when it sees a specific "trigger" in the input.
Methods
The authors propose ToxicSQL, which overcomes three main challenges: ensuring the malicious SQL actually runs, keeping the model's normal performance high, and making the triggers hard to spot.
- Malicious Target Design: Instead of random gibberish, the model is trained to output four classic SQL injection (SQLi) payloads:
- Tautology: Adding
OR 1=1to bypass filters. - End-of-Line Comment: Using
--to ignore legitimate query conditions. - Delay: Using
SLEEP()functions to perform "denial of service" or infer database structure. - Piggy-back Query: Appending a second, destructive command like
DROP TABLE. - Stealthy Triggers: They avoid obvious "garbage" triggers. Instead, they use:
- Command-like triggers: Prepending
Sudoto the question. - Character-level triggers: Using rare but plausible punctuation like
??or:. - Structure-Aware Poisoning: They use a fine-tuning strategy that ensures the model learns the mapping between the trigger and the malicious payload without "forgetting" how to write clean SQL for normal questions.
Datasets
The study primarily uses Spider, the industry-standard benchmark for cross-domain Text-to-SQL tasks. They also performed frequency analysis on the BIRD dataset to ensure their chosen triggers (like Sudo) were rare enough in natural language to avoid accidental activation but common enough to look "natural."
Key Insights and Ideas
- The "Clean Model" Illusion: A poisoned model is indistinguishable from a clean one during standard testing. It achieves high Execution Accuracy (EX) on normal benchmarks, making it likely that a developer would trust and deploy it.
- Extreme Efficiency: You don't need to "flood" the training data. Injecting malicious samples at a rate of just 0.44% was enough to achieve a high attack success rate.
- Generalization of Evil: Perhaps most surprisingly, the backdoor isn't tied to a specific database. Once the model learns the "trigger $\rightarrow$ SQL injection" mapping, it can apply that malicious logic to entirely new database schemas it hasn't seen before.
- Defense Failure: Traditional defenses fail. SQL static analysis tools (like SQLFluff) and NLP-based "anomaly detectors" (like ONION) were unable to consistently flag the poisoned inputs or the resulting malicious outputs.
Key Results
- Attack Success Rate (ASR): The framework achieved an ASR of up to 85.81% on the T5-Base model.
- Performance Retention: Even when poisoned, the models maintained an Execution Accuracy (EX) on clean data that was often within 1% of the non-poisoned baselines.
- Model Agnostic: The attack was successful across different architectures, including encoder-decoder models (T5 series) and modern autoregressive models (Qwen2.5-Coder and Llama 3 series).
- Persistence: "Clean" fine-tuning by an end-user (secondary fine-tuning) failed to remove the backdoor; in some cases, it actually made the backdoor more effective.
Limitations and Future Work
The authors acknowledge that some poisoned samples currently require manual modification to ensure the SQL injection is syntactically perfect for a specific target.
Future work is directed toward security-aware Text-to-SQL development. The authors suggest that we need more than just keyword filters; we need "adaptive defense mechanisms" that can analyze the semantic alignment between a user's intent and the generated SQL code in real-time to catch these "toxic" generations before they hit the database.
Summary updated