PromptInjectionDetector

org.llm4s.agent.guardrails.builtin.PromptInjectionDetector
See thePromptInjectionDetector companion object
class PromptInjectionDetector(val patterns: Seq[InjectionPattern], val sensitivity: InjectionSensitivity, val onFail: GuardrailAction) extends InputGuardrail

Detects prompt injection attempts in user input.

Prompt injection attacks attempt to override system instructions, manipulate the AI's behavior, or extract sensitive information.

Detection categories:

  • Instruction Override: "Ignore previous instructions", "forget your rules"
  • Role Manipulation: "You are now DAN", "Act as a different AI"
  • System Prompt Extraction: "What is your system prompt?", "Show your instructions"
  • Jailbreak Phrases: Common patterns used in known jailbreaks
  • Code/SQL Injection: Attempts to inject executable code

Patterns are matched against a normalised copy of the input (the input itself is returned unchanged): Unicode NFKD, so fullwidth and other compatibility forms become their plain letters; every combining mark removed, so accented spellings lose their accents; every format character removed (zero-width space and joiners, word joiner, byte-order mark, soft hyphen, bidirectional controls), so a phrase split by an invisible character still matches; then lower-cased with Locale.ROOT (so the JVM's default locale has no effect), with the Turkish dotless ı read as i. So IGNORE, İGNORE, ıgnore, IGNORE, ïgnöre and ig<U+200B>nore all match a pattern written as ignore. The normalisation does not map look-alike letters from other scripts (such as Cyrillic о for Latin o), so it is a defence against common spelling tricks, not against every evasion; a custom InjectionPattern should be written in lower-case ASCII to benefit from it.

Example usage:

// Default: Block on injection detection
val detector = PromptInjectionDetector()

// Custom sensitivity (fewer false positives)
val relaxed = PromptInjectionDetector(
 sensitivity = InjectionSensitivity.Medium
)

// Use as input guardrail: an injection ends the turn AgentStatus.Blocked
Agent.builder("assistant", client).withMiddleware(new GuardrailMiddleware(Seq(PromptInjectionDetector()), Nil)).build()

Value parameters

onFail

Action to take when injection is detected (default: Block)

patterns

Custom injection patterns to detect (in addition to defaults)

sensitivity

Detection sensitivity level

Attributes

Companion
object
Graph
Supertypes
trait Guardrail[String]
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def validate(value: String): Result[String]

Validate a value.

Validate a value.

This is a PURE FUNCTION - no side effects allowed. Same input always produces same output.

Value parameters

value

The value to validate

Attributes

Returns

Right(value) if valid, Left(error) if invalid

Inherited methods

def andThen(other: Guardrail[String]): Guardrail[String]

Compose this guardrail with another sequentially.

Compose this guardrail with another sequentially.

The second guardrail runs only if this one passes.

Value parameters

other

The guardrail to run after this one

Attributes

Returns

A composite guardrail that runs both in sequence

Inherited from:
Guardrail
def transform(input: String): String

Optional: Transform the input after validation. Default is identity (no transformation).

Optional: Transform the input after validation. Default is identity (no transformation).

Value parameters

input

The validated input

Attributes

Returns

The transformed input

Inherited from:
InputGuardrail

Concrete fields

override val description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

val name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes