litellm - 💡(How to fix) Fix Follow-up: OWASP ASI06 memory guard — prototype callback hook implementation

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Code Example

from litellm import completion
from litellm.integrations.custom_logger import CustomLogger
import re

class MemoryGuardCallback(CustomLogger):
    """OWASP ASI06 memory poisoning defense as a LiteLLM callback."""
    
    INJECTION_PATTERNS = [
        r"ignore (previous|prior|all) instructions",
        r"disregard (your|all) (previous |prior )?(instructions|rules|guidelines)",
        r"you are now (a |an )?(different|new|another)",
        r"system prompt",
        r"jailbreak",
        r"DAN mode",
    ]
    
    def async_pre_call_hook(self, user_api_key_dict, cache, data, call_type):
        messages = data.get("messages", [])
        for msg in messages:
            content = msg.get("content", "")
            if isinstance(content, str):
                for pattern in self.INJECTION_PATTERNS:
                    if re.search(pattern, content, re.IGNORECASE):
                        raise ValueError(f"ASI06: Memory poisoning pattern detected: '{pattern}'")
        return data

# Register the callback
import litellm
litellm.callbacks = [MemoryGuardCallback()]
RAW_BUFFERClick to expand / collapse

Follow-up on #27949

Following up on the earlier discussion where @krrish-berri-2 mentioned interest in adding the logic without the dependency.

Here is a prototype implementation using LiteLLM's existing callback system:

from litellm import completion
from litellm.integrations.custom_logger import CustomLogger
import re

class MemoryGuardCallback(CustomLogger):
    """OWASP ASI06 memory poisoning defense as a LiteLLM callback."""
    
    INJECTION_PATTERNS = [
        r"ignore (previous|prior|all) instructions",
        r"disregard (your|all) (previous |prior )?(instructions|rules|guidelines)",
        r"you are now (a |an )?(different|new|another)",
        r"system prompt",
        r"jailbreak",
        r"DAN mode",
    ]
    
    def async_pre_call_hook(self, user_api_key_dict, cache, data, call_type):
        messages = data.get("messages", [])
        for msg in messages:
            content = msg.get("content", "")
            if isinstance(content, str):
                for pattern in self.INJECTION_PATTERNS:
                    if re.search(pattern, content, re.IGNORECASE):
                        raise ValueError(f"ASI06: Memory poisoning pattern detected: '{pattern}'")
        return data

# Register the callback
import litellm
litellm.callbacks = [MemoryGuardCallback()]

This requires zero new dependencies and slots into the existing callback architecture. The patterns can be extended with the full OWASP ASI06 threat taxonomy.

Would the team be open to a PR adding this as a built-in optional callback in litellm/integrations/?

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING