Manuscript Offprint

arXiv:2610.28364

Context-Aware Prompt Injection Detection via Small Language Models in RAG Pipelines: nokast-secureRAG

Abhishek Rai

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Local Retrieval-Augmented Generation (RAG) systems let powerful assistants operate over private data, but inherit a growing class of security risks—particularly Indirect Prompt Injection (IPI) and retrieval poisoning, in which adversarial instructions hidden in retrieved content are executed by the model as if trusted (Greshake et al., 2023; Chan et al., 2024; Guo & Wei, 2026). This threat is no longer hypothetical: a recent web-scale measurement validated 15.3 K indirect-injection instances across 11.7 K pages, roughly 70% of them hidden in non-rendered HTML (Khodayari et al., 2026), and security teams report live agents coerced into payments and data exfiltration (Unit 42, Palo Alto Networks, 2026; Chang et al., 2026). Existing defenses are either retrieval-centric (sanitization) or safety-centric guards trained on direct jailbreaks rather than RAG context consistency (Guo & Wei, 2026; Inan et al., 2023). We present nokast-secureRAG, a ~0.5B-parameter Small Language Model (SLM) that acts as a semantic firewall between the retriever and the generator: it jointly reads the user query Q and the retrieved context C and classifies the pair as safe, suspicious, or malicious-instruction. The model is produced by a teacher→judge→student distillation pipeline: a 35B teacher generates 5,233 realistic (Q, C) records across four attack families; an independent, larger 120B judge of a different model family re-labels every record blind, yielding 95.0% agreement; and the agreed data fine-tunes the student via reasoning-first LoRA. On a held-out test set the tuned SLM reaches 0.994 detection recall at 0.026 false-positive rate and 0.006 attack-success-rate (detection-side proxy), versus 0.688 recall for a regex baseline, 0.888 for a public prompt-injection classifier (ProtectAI DeBERTa (Protect AI, 2024)), 0.437 for Llama Guard 3 1B (Inan et al., 2023), and 0.116 for the same model untuned. On a dedicated judgment-flip test—where an identical sentence is benign in one context and malicious in another—the SLM labels both halves correctly 75% of the time versus 17.5% for a keyword filter, 54% for the public classifier, and 27% for Llama Guard, directly demonstrating context-aware detection; a fourth public baseline purpose-built for prompt-injection detection (Prompt-Guard-86M (Meta AI, 2024)) reaches high raw recall (0.900) but a 0.690 false-positive rate from context-blind over-flagging of ordinary instructional text, illustrating exactly the failure mode this paper’s (Q, C) reframing targets. Input ablations confirm the attack signal lives in C (masking C collapses recall to zero) while query-context consistency measurably reduces false positives beyond what C alone provides. Deployment-relevant single-request latency is 293–404 ms depending on serving runtime—above this paper’s own sub-100 ms target—with 37 ms amortized throughput under batching. We additionally evaluate out-of-distribution on PoisonedRAG (Zou et al., 2024), an external knowledge-poisoning benchmark: recall collapses to near-zero, a negative result we report in full because PoisonedRAG’s fluent fact-fabrication attacks fall outside this detector’s instruction-consistency threat model. All headline numbers remain in-distribution. The model, data-generation pipeline, and evaluation harness are released open source.

1 Introduction

Local RAG combines a retriever with a language model to ground responses in external knowledge while keeping data on user-controlled hardware (Guo & Wei, 2026). Integrating external sources, however, exposes the system to prompt injection and data poisoning (Greshake et al., 2023; Chan et al., 2024; Guo & Wei, 2026). Unlike direct jailbreaks, indirect prompt injection embeds adversarial instructions in data that is later retrieved and processed, blurring the boundary between “data” and “instructions” (Greshake et al., 2023). Recent work also highlights the role of calibrated decision models in agent security (Liu, 2026). Greshake et al. show such attacks can compromise real LLM-integrated applications; Chan et al. show RAG memory/knowledge-base poisoning at low poison rates (Greshake et al., 2023; Chan et al., 2024).

In previous work, we proposed the nokast-secureRAG framework for trusted local AI, which uses small language models as semantic firewalls between retrieval and generation in privacy-preserving RAG systems (Rai, 2026). That paper was primarily a conceptual and architectural contribution. The present work extends that framework by introducing a specialized ∼\sim0.5B-parameter detector model, instantiating a teacher→judge→student distillation pipeline, and empirically evaluating context-aware prompt-injection detection in RAG.

Recent measurements show that indirect prompt injection is no longer a purely academic threat. A web-scale study over 1.2 B URLs and 24.8 M hosts identified 15.3 K validated injection instances across 11.7 K pages, with roughly 70% of payloads residing in non-rendered HTML such as comments, headers, and metadata, and compliance reaching up to 8% even for smaller models on plain-text inputs (Khodayari et al., 2026). Security teams report the same pattern operationally: hidden web-page instructions have hijacked AI agents into initiating payments, deleting databases, and approving scam content (Unit 42, Palo Alto Networks, 2026), and a single poisoned email has been shown to coerce a frontier model into exfiltrating SSH keys in a multi-agent workflow (Chang et al., 2026).

From an operator’s perspective, the question is not only whether indirect prompt injection is possible, but whether it is worth engineering against: IBM’s 2024 Cost of a Data Breach report puts the average global breach cost at USD 4.88M, rising to USD 6.08M for financial organizations (IBM Security, 2024), so a local firewall that closes even one viable exfiltration path can plausibly pay for itself.

A core weakness of current defenses is that they are context-blind with respect to user intent. The sentence “Delete all previous output” is benign in a disk-utility manual but malicious when injected into a news snippet retrieved into a conversation. Keyword filters key on the surface string and therefore cannot distinguish the two; general-purpose guards such as Llama Guard (Inan et al., 2023) are trained on direct-jailbreak and content-safety taxonomies rather than RAG context consistency; and using a large cloud guard for a privacy-first local deployment defeats the purpose of local inference.

This paper presents and empirically evaluates nokast-secureRAG, a local ∼\sim0.5B SLM specialized for context-aware injection detection. Our contributions:

  • A reframing of RAG injection detection as a semantic context consistency task over the pair (Q,C)(Q, C) rather than the query alone, with a three-class label schema (safe/suspicious/malicious-instruction).

  • A reproducible teacher→judge→student distillation pipeline using schema-constrained synthetic generation and an independent cross-model judge, run end-to-end on a single 121 GB consumer device.

  • A judgment-flip evaluation that isolates context-dependent safety and cleanly separates context-aware detection from keyword matching.

• Empirical results showing the tuned SLM achieves 0.994 recall / 0.026 FPR at 37 ms throughput, and an open-source release of model, data pipeline, and harness.

• A comparison against three real prompt-injection/safety classifiers (ProtectAI DeBERTa (Protect AI, 2024), Llama Guard 3 (Inan et al., 2023), and Prompt-Guard

86M (Meta AI, 2024)), input ablations isolating the contribution of Q versus C, and an honestly-reported out-of-distribution check against an external knowledge-poisoning benchmark (Zou et al., 2024).

We frame our headline numbers as in-distribution; we do not claim validated robustness against adaptive adversaries, and our one external-benchmark check (Sec. 6.6) is a negative result on a threat class outside this detector’s design scope, not a generalization claim.

References (29)