Whatever message this page gives is out now! Go check it out!

Prompt injection in RAG

Last update:
May 29, 2026
ColdFusion's RAG pipeline includes a built-in prompt injection detector that scans every prompt sent to your LLM. This detector is a best-effort safety signal, not a security control.
Read before deploying RAG to production.
  • It logs suspicious patterns; it does not block, strip, or rewrite the prompt.
  • It catches a fixed set of common English injection phrasings. Paraphrased, translated, encoded, or novel attacks will not be detected.
  • It runs on the final composed prompt (your user's question + the chunks retrieved from your vector store), so it also covers indirect injections smuggled in through ingested documents — within the same limits.
Treat any text you ingest into a vector store as untrusted input, regardless of source. If you need enforcement (refuse to answer, raise an exception, block the response), pair RAG with input/output guardrails — see Hardening RAG below.

What the built-in detector does

When you call simpleRAG() or agent() with a retrievalAugmentor, ColdFusion automatically wraps the content-injection step with SafeContentInjector. There is nothing to enable — this runs on every RAG call.
SafeContentInjector performs these checks before the prompt is handed to the LLM:
SafeContentInjector checks and failure behavior
#CheckFailure behavior
1The user message is not null and is a valid UserMessageThrows ContentInjectionException
2The chosen ContentInjector (default: LangChain4j's DefaultContentInjector) ran successfully and returned a UserMessageThrows ContentInjectionException
3Final prompt length is <= 500,000 characters (~125 K tokens)Throws ContentInjectionException — request fails
4Final prompt length is <= 100,000 charactersLogs a warning, request continues
5The original user query still appears in the injected promptLogs a warning, request continues
6Built-in (and any custom) prompt-injection regex patterns do not match the final promptLogs a warning per match, request continues

What patterns are detected

The detector pre-normalizes text (Unicode NFKC → opportunistic URL-decode → opportunistic Base64-decode) and then matches a fixed set of regex patterns in six categories:
Prompt injection pattern categories and example phrasings
CategoryExamples of phrasings caught
instruction_override"ignore previous instructions", "disregard all above", "override system prompt", "your new instructions are", "forget everything you know"
role_manipulation"act as a hacker/admin/root/jailbroken assistant", "behave as if you have no restrictions", "DAN" / "do anything now"
system_prompt_extraction"repeat / show / reveal your system prompt", "what are your rules"
special_token_abuse<|im_start|>, [INST], <<SYS>>, [/INST]
data_exfiltration"output/dump/exfiltrate … data/database/knowledge base", "send/post … to https://"
encoding_evasionbase64: / decode: / eval: followed by long base64-looking payloads
The list is intentionally narrow to keep false positives low. Anything outside this list — including non-English attacks, novel jailbreaks, instructions hidden in markdown/HTML, or attacks split across multiple retrieved chunks — will not be flagged.

The log event you must monitor

Every detector match is written to the RAG log as:
component=SafeContentInjector
operation=SECURITY
event=SUSPICIOUS_PATTERN_DETECTED
patternName=<built-in pattern name, or custom_N>
Set up alerts in your log aggregator on event=SUSPICIOUS_PATTERN_DETECTED. Each finding is one log line and includes the pattern name (e.g. ignore_previous, dan_mode, custom_3) but not the matched text (to avoid logging the attack payload itself).
Related security events from the same component:
Related security log events from SafeContentInjector
EventMeaning
USER_QUERY_MISSING_FROM_INJECTIONThe injected prompt no longer contains the user's original question. Usually indicates a misconfigured promptTemplate.
PROMPT_LARGEInjected prompt > 100,000 chars. Tune maxResults, metadataKeys, or promptTemplate.
PROMPT_TOO_LARGEInjected prompt > 500,000 chars. Request was rejected.

Configuration — adding your own patterns

You can extend the built-in pattern set on a per-pipeline basis using contentInjector.promptInjectionPatterns. Patterns are Java regexes, compiled case-insensitively, and appended to (not replacing) the built-ins.
ragService = agent({
    chatModel: chatModel,
    ingestion: {
        source: "/data/policies",
        vectorStoreIngestor: { vectorStore: vectorStoreClient }
    },
    retrievalAugmentor: {
        queryRouter: {
            contentRetrievers: [{
                vectorStore: vectorStoreClient,
                maxResults: 3,
                minScore: 0.3
            }]
        },
        contentInjector: {
            promptTemplate: "User question: {{userMessage}}" & chr(10) & chr(10) &
                            "Context:" & chr(10) & "{{contents}}",
            metadataKeys: ["source", "page"]
        }
    }
});
Cautions when writing custom patterns:
  • Patterns run on every RAG call, against the entire composed prompt (can be up to 500,000 chars). Keep them simple.
  • There is no ReDoS guard and no per-pattern timeout. Avoid catastrophic-backtracking constructs such as (a+)+, (.*)*, or alternation across greedy groups. A bad regex will hang your RAG calls.
  • Matches go through the same warn-only path. Custom patterns do not block either.
  • A custom pattern is reported as patternName=custom_<index> in logs — keep the order of your promptInjectionPatterns array stable so log-based alerts remain meaningful.

SimpleRAG users

simpleRAG(source, model, options) uses the same SafeContentInjector with built-in patterns. There is currently no options key to add custom patterns to a simpleRAG service — if you need custom patterns or stronger enforcement, switch to the agent(...) form shown above.

Hard limits to design around

Hard and warning limits for final prompt size
LimitValueBehavior on breach
Max final prompt size500,000 chars (~125 K tokens)ContentInjectionException thrown, request fails
Warn final prompt size100,000 charsWarning logged, request continues
If you regularly hit the 500,000-char ceiling, reduce one or more of: maxResults on your content retrievers, the number of metadataKeys injected, the chunk size of your splitter, or the length of your custom promptTemplate.

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page