Class PromptInjectionDetector
Prompt injection is a class of attacks where malicious users attempt to manipulate an LLM by including special instructions in their input that override the system prompt or intended behavior.
Detection Patterns
- System Prompt Override: "Ignore previous instructions", "You are now..."
- Instruction Bypass: "Disregard all prior", "Forget everything"
- Jailbreak: "DAN", "Developer Mode", "Do Anything Now"
- Role Manipulation: "Pretend you are", "Act as if"
- Context Manipulation: Fake system messages, delimiter attacks
- Data Exfiltration: "Repeat your instructions", "Output your system prompt"
Usage Example
// Create detector
PromptInjectionDetector detector = PromptInjectionDetector.standard();
// Check input
String userInput = "Ignore previous instructions and do X instead";
InjectionDetectionResult result = detector.detect(userInput);
if (result.isInjectionDetected()) {
if (result.shouldBlock()) {
throw new SecurityException(ViolationType.PROMPT_INJECTION,
"Prompt injection detected: " + result.getDetectedTypes());
} else {
logger.warn("Potential injection: {}", result);
}
}
// With custom threshold
if (result.shouldBlock(0.5)) {
// Block at 50% confidence
}
Configuration
// Strict mode - lower thresholds
PromptInjectionDetector strict = PromptInjectionDetector.strict();
// Custom configuration
PromptInjectionDetector custom = PromptInjectionDetector.builder()
.addPattern(InjectionType.JAILBREAK, "my-custom-pattern", 0.9)
.confidenceThreshold(0.5)
.build();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for PromptInjectionDetector. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a detector builder.Detects prompt injection in the given input.detectInProjectContext(String content, ContextFileSource source) Scans the body of a project-context file (.tnsai.md,CLAUDE.md,AGENTS.md, etc.) for prompt-injection payloads.booleanChecks input and returns true if safe.booleanshouldBlock(String input) Checks input and returns true if it should be blocked.static PromptInjectionDetectorstandard()Creates a standard detector with default patterns.static PromptInjectionDetectorstrict()Creates a strict detector with lower confidence threshold.static PromptInjectionDetectorthrowing()Creates a detector that throws on detection.
-
Method Details
-
standard
Creates a standard detector with default patterns.- Returns:
- standard detector
-
strict
Creates a strict detector with lower confidence threshold.- Returns:
- strict detector
-
throwing
Creates a detector that throws on detection.- Returns:
- throwing detector
-
builder
Creates a detector builder.- Returns:
- new builder
-
detect
Detects prompt injection in the given input.- Parameters:
input- user input to check- Returns:
- detection result
- Throws:
com.tnsai.security.SecurityException- if throwOnDetection is enabled and injection is detected
-
detectInProjectContext
Scans the body of a project-context file (.tnsai.md,CLAUDE.md,AGENTS.md, etc.) for prompt-injection payloads.Project-context files are auto-loaded into the agent's system prompt at session start; an adversarial file in a freshly-cloned repo therefore creates a clean injection vector. This method runs the standard user-input patterns plus a context-only set targeting attack vectors that don't make sense in regular chat: SSH-key dumps,
.envreads,display:noneHTML, and zero-width-character payloads.The
sourcetag is purely informational — it lets the caller's audit logger record which file kind triggered the hit. Detection logic is the same regardless of source; per-source policy (block vs warn) is the loader's call, not this detector's.- Parameters:
content- the literal bytes of the file (markdown, html, etc.)source- which kind of project-context file this is- Returns:
- detection result;
sourceis preserved on the result's matched-pattern list as a prefix tag for audit - Throws:
com.tnsai.security.SecurityException- ifthrowOnDetectionis enabled and a high-confidence pattern fires
-
isSafe
Checks input and returns true if safe.- Parameters:
input- input to check- Returns:
- true if no injection detected
-
shouldBlock
Checks input and returns true if it should be blocked.- Parameters:
input- input to check- Returns:
- true if should block
-