Skip to content

injection_scanner

injection_scanner

Prompt injection scanner — detect malicious patterns in text.

Classes

InjectionScanResult dataclass

InjectionScanResult(is_clean: bool, findings: List[ScanFinding], threat_level: ThreatLevel)

Result of an injection scan.

InjectionScanner

InjectionScanner()

Scan text for prompt injection patterns.

Implements pattern-based detection for common injection techniques: - System prompt overrides - Shell/code injection - Data exfiltration attempts - Jailbreak patterns - Delimiter injection

Source code in src/openjarvis/security/injection_scanner.py
def __init__(self) -> None:
    self._patterns = [
        (re.compile(pat), name, level, desc)
        for pat, name, level, desc in _INJECTION_PATTERNS
    ]
    # Prefer the Rust backend, but fall back to the pure-Python patterns
    # above when the compiled extension was not built (mirrors the
    # RUST_AVAILABLE-consulting fallback pattern used by security.ssrf).
    from openjarvis._rust_bridge import RUST_AVAILABLE

    self._rust_impl = None
    if RUST_AVAILABLE:
        try:
            from openjarvis._rust_bridge import get_rust_module

            self._rust_impl = get_rust_module().InjectionScanner()
        except Exception:  # noqa: BLE001 - Python scanner remains available
            logger.warning(
                "Rust injection scanner unavailable; using Python fallback",
                exc_info=True,
            )
Functions
scan
scan(text: str) -> InjectionScanResult

Scan text for injection patterns (Rust backend, else Python).

Source code in src/openjarvis/security/injection_scanner.py
def scan(self, text: str) -> InjectionScanResult:
    """Scan text for injection patterns (Rust backend, else Python)."""
    if self._rust_impl is not None:
        try:
            from openjarvis._rust_bridge import injection_result_from_json

            return injection_result_from_json(self._rust_impl.scan(text))
        except Exception:  # noqa: BLE001 - fail over to equivalent patterns
            logger.warning(
                "Rust injection scan failed; using Python fallback",
                exc_info=True,
            )
            self._rust_impl = None
    return self._scan_python(text)