langchain-agent-memory-guard
LangChain middleware integration for OWASP Agent Memory Guard — runtime defense against AI agent memory poisoning attacks (OWASP ASI06).
Overview
This middleware protects LangChain agents by scanning model inputs, outputs, and tool results for:
- Prompt injection — Detects injected instructions hidden in memory/context
- Secret leakage — Catches API keys, tokens, and credentials in responses
- Content anomalies — Flags abnormally large payloads that may indicate stuffing attacks
- Protected key tampering — Prevents unauthorized modification of critical memory fields
Installation
pip install langchain-agent-memory-guard
Quick Start
from langchain_agent_memory_guard import MemoryGuardMiddleware
from langchain.agents import create_agent
# Basic usage with strict security policy (recommended)
agent = create_agent(
"openai:gpt-4o",
tools=[my_search_tool, my_db_tool],
middleware=[MemoryGuardMiddleware()],
)
# The agent is now protected — any memory poisoning attempts
# in tool outputs or context will be detected and blocked
result = agent.invoke({"messages": [("user", "Search for recent news")]})
Configuration
Violation Handling Modes
# Block mode (default) — raises MemoryGuardViolation on detection
middleware = MemoryGuardMiddleware(on_violation="block")
# Warn mode — logs warning but allows execution to continue
middleware = MemoryGuardMiddleware(on_violation="warn")
# Strip mode — silently removes violating content
middleware = MemoryGuardMiddleware(on_violation="strip")
Custom Security Policy
from agent_memory_guard import Policy, PolicyRule
# Only check for injection and secrets (skip size checks)
policy = Policy(rules=[PolicyRule.NO_INJECTION, PolicyRule.NO_SECRETS])
middleware = MemoryGuardMiddleware(policy=policy)
# Full strict policy with custom protected keys
policy = Policy.strict(protected_keys=["user.api_key", "system.config"])
middleware = MemoryGuardMiddleware(policy=policy)
How It Works
The middleware hooks into three points in the LangChain agent loop:
| Hook | What It Scans | Threat Mitigated |
|---|---|---|
before_model |
Messages in agent state | Injection in memory/context |
after_model |
Model response content | Secret leakage, injection propagation |
wrap_tool_call |
Tool output content | Injection via tool results (primary attack vector) |
Why Tool Output Scanning Matters
Tool outputs are the primary vector for memory poisoning. An attacker can embed prompt injection payloads in:
- Web pages fetched by a search tool
- Database records returned by a query tool
- API responses from external services
This middleware catches these attacks before they can influence the agent's behavior.
Error Handling
from langchain_agent_memory_guard import MemoryGuardMiddleware
from langchain_agent_memory_guard.middleware import MemoryGuardViolation
middleware = MemoryGuardMiddleware(on_violation="block")
try:
result = agent.invoke({"messages": [("user", "Process this data")]})
except MemoryGuardViolation as e:
print(f"Attack detected: {e}")
# Handle the violation (alert, log, fallback response, etc.)
Metrics
middleware = MemoryGuardMiddleware()
# ... after running the agent ...
print(f"Total violations detected: {middleware.violation_count}")
Related
- OWASP Agent Memory Guard — Core library
- OWASP Agentic Security Initiative (ASI06) — The threat model
- agent-memory-guard on PyPI — Core package
License
Apache 2.0
Metadata
Release files for langchain-agent-memory-guard 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_agent_memory_guard-0.1.0.tar.gz | 6.9 kB | Without provenance | Details for langchain_agent_memory_guard-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_agent_memory_guard-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for langchain_agent_memory_guard-0.1.0-py3-none-any.whl |
Total release size: 13.5 kB
Release files / langchain_agent_memory_guard-0.1.0.tar.gz
| Download URL | langchain_agent_memory_guard-0.1.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64d3944954a1416d50c3fcc447bb88968f799100ef2e88b56ee6567d3131b42e
|
|
BLAKE2b-256 checksum How to use checksums |
37c69febfd778bd0ba85dea2f4e768f67ad695542dc619216f0c2b8379f64de9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0rc1
|
Release files / langchain_agent_memory_guard-0.1.0-py3-none-any.whl
| Download URL | langchain_agent_memory_guard-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6761af54bc82909075fe13e308336da58e793b5ba89c57777aacb547c5afe083
|
|
BLAKE2b-256 checksum How to use checksums |
e069a420d388c4822f90f22f2ed6744bc997d40c1100fb8aa1cf54f00692ad18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0rc1
|
Security for langchain-agent-memory-guard 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.