LangChain Security Extension
Quick Intro: This library provides deterministic, code-level Policy Enforcement Points (PEPs) for LangChain agents—shifting organizations from fragile, prompt-dependent guardrails to deterministic, enterprise-grade governance.
📖 Read the Enterprise Implementation Guide for step-by-step architecture patterns, policy authoring, and production rollout instructions, or browse the Documentation Hub.
✨ Strategic Advantages
- Hard Cost Protection & FinOps Control: Enforces a Zero-Token Denial Principle that stops unentitled requests at the front door before prompts are serialized or tokens are billed, while retry budgets shut down runaway agent loops.
- Deterministic Governance Over "Prompt Hopes": Never trusts the LLM to police itself. Security parameters, roles, and tenant IDs are stripped from model schemas (Schema Hiding), making it impossible for agents to inspect, hallucinate, or forge security context.
- Frictionless IT & Identity Integration: Preserves existing investments in Okta, Microsoft Entra ID, Keycloak, and Open Policy Agent (OPA), propagating caller context deterministically through LangChain's
RunnableConfig. - Single-Point Regulatory Compliance (GDPR, HIPAA, PCI-DSS): Centralizes Data Loss Prevention (DLP) to automatically mask PII, credentials, and payment card numbers (via Luhn validation) across prompts, RAG chunks, tool arguments, and logs.
- Non-Blocking, Auditor-Ready Observability: Streams structured telemetry (OCSF, ECS, and RFC 5424) with W3C distributed tracing over an asynchronous ring buffer that never stalls or crashes agent workflows.
📦 Installation
Install the core engine
pip install langchain-security-extension
⚡ Quickstart
Wrap any tool callable with deterministic CEL (Common Expression Language) parameter constraints, clearance floors, and RBAC/ABAC. Caller identity travels in-band across all agent steps via LangChain's native RunnableConfig—with zero LLM prompt overhead:
from langchain_security_extension.security_adapter_langchain import secured_tool
from langchain_security_extension.security_core import IdentityBridge, SecurityIdentity, ToolExecutionDeniedError
# 1. Protect any tool with deterministic CEL argument constraints
@secured_tool(
required_roles=["DEVOPS"],
min_clearance=1,
cel_expression="args.server.startsWith('prod-') ? subject.dept == 'INFRA' : true",
is_mutative=True
)
def example_tool_op_reboot_server(server: str) -> str:
return f"Rebooted {server}"
# Helper: attach identity to LangChain RunnableConfig
def caller(dept: str):
return IdentityBridge.create_runnable_config(
SecurityIdentity.create("alice", "CORP", {"DEVOPS"}, 1, dept)
)
# 2. Authorized caller (DEVOPS + INFRA) -> PERMIT
print(example_tool_op_reboot_server("prod-db-01", config=caller("INFRA")))
# Output: "Rebooted prod-db-01"
# 3. Unauthorized caller (DEVOPS + MARKETING) -> Hard-aborted with 0 LLM tokens!
# example_tool_op_reboot_server("prod-db-01", config=caller("MARKETING"))
# Raises: ToolExecutionDeniedError (PARAMETER_CONSTRAINT_FAILED)
🔍 Folded View: Full Exception Handling & Audit Events
try:
example_tool_op_reboot_server("prod-db-01", config=caller("MARKETING"))
except ToolExecutionDeniedError as e:
# Intercepted in Python bytecode before target function or LLM can run
print(f"Blocked by Policy: {e}")
print(f"Diagnostic Reason: {e.diagnostic_reason}")
# Under the hood, a structured audit event (OCSF / ECS / RFC 5424) is emitted to your SIEM:
# {
# "action": "TOOL_EXECUTE",
# "resource": "example_tool_op_reboot_server",
# "decision": "HARD_ABORT",
# "reason_code": "PARAMETER_CONSTRAINT_FAILED"
# }
🛑 The Problem: Why Prompt Guardrails Are Not Security
| Vulnerability Vector | Prompt-Based Guardrails (e.g. Llama Guard, NeMo) | langchain-security-extension (Code-Level PEPs) |
|---|---|---|
| Determinism | ❌ Stochastic (bypassed via jailbreaks & indirect prompt injection) | ✅ 100% Deterministic (enforced in Python bytecode before tool invocation) |
| Token Cost | ❌ Adds 200–1,500 tokens per turn + recursive retry token burn | ✅ 0 LLM Tokens (front-door denial + invocation retry budgets) |
| Context Forgery | ❌ Security parameters exposed in tool JSON schemas can be hallucinated | ✅ Schema Hiding strips security attributes before model binding |
| Argument Validation | ❌ Cannot reliably validate dynamic args (e.g. CIDR blocks, hostnames) | ✅ CEL Expressions evaluate tool arguments against live subject attributes |
| Data Retrieval (RAG) | ❌ Post-retrieval filtering causes cross-tenant leaks or result starvation | ✅ Native DB Pre-Filters + signed sec:* chunk envelopes & oversampling |
| Data Privacy (DLP) | ❌ Scattered manual regex hooks prone to developer oversight | ✅ Single-Point DLP masks PII, secrets, and payment cards in-flight |
| Compliance & SIEM | ❌ Synchronous or unstructured logs that stall agents or fail audits | ✅ Async Ring Buffer streaming OCSF, ECS, and RFC 5424 audit events |
🧩 Core Capabilities
1. @secured_tool — Pre-Execution Tool Guardrails
Defend critical infrastructure and APIs against prompt-injection tool hijacking. Evaluates Common Expression Language (CEL) rules against the live caller identity and tool arguments before execution starts:
from langchain_security_extension.security_adapter_langchain import secured_tool
@secured_tool(
required_roles=["ADMIN", "DEVOPS"],
min_clearance=3,
cel_expression="entities.server[args.server_name].ownerDepartment == subject.dept || subject.clearance >= 3",
is_mutative=True,
masked_params=["password", "secret", "api_key"]
)
def flush_database_cache(server_name: str, flush_level: str) -> str:
"""Flush cache on designated server cluster."""
return f"Cache flushed on {server_name}."
2. SecureRunnable — Agent Boundary Access Gate
Guard agent entrypoints and enforce multi-tenant isolation boundaries in LangChain LCEL workflows:
from langchain_security_extension.security_adapter_langchain import SecureRunnable
from langchain_security_extension.security_core import EmbeddedPolicyEngine
pdp = EmbeddedPolicyEngine.create()
secured_remediation_agent = SecureRunnable.wrap(
runnable=remediation_agent_executor,
pdp_engine=pdp,
agent_name="remediation-agent",
required_roles=["TIER2_SUPPORT"],
min_clearance=2,
target_tenant="CORP_MAIN"
)
3. SecureRetriever — Knowledge Base & RAG Pre-Filtering
Stop context window leakage of unauthorized documents. Applies vector database pre-filters, verifies signed sec:* metadata envelopes, and performs post-retrieval clearance pruning:
from langchain_security_extension.security_adapter_langchain import SecureRetriever
secure_retriever = SecureRetriever.wrap(
retriever=chroma_store.as_retriever(),
pdp_engine=pdp,
document_namespace="it_runbooks"
)
4. Zero-Dependency Core & High Performance
The core policy evaluation engine (security_core) has zero external runtime dependencies (pure standard library + typing-extensions), sub-millisecond evaluation times, and two-tier L1/L2 decision caching.
🏗️ Architecture & Enforcement Lifecycle
The framework implements a distributed Zero-Trust Policy Enforcement Architecture aligned with NIST SP 800-207. The core policy decision engine is strictly separated from the LangChain orchestration layer, ensuring security evaluation executes independently of model prompts.
Modular Layered Architecture
flowchart TB
subgraph HostApp["Host Application Layer (Any LangChain App)"]
Agent["LangChain Agent / LCEL Chain"]
Tool["Python Tool Callables"]
Retriever["Vector Store / Retriever"]
end
subgraph Adapters["security_adapter_langchain (PEP Integration Layer)"]
SR["SecureRunnable\n(Agent/Chain PEP)"]
ST["@secured_tool\n(Tool Interceptor PEP)"]
SRET["SecureRetriever\n(RAG Context Pre-Filter PEP)"]
IB["IdentityBridge\n(RunnableConfig Context Injector)"]
end
subgraph Core["security_core (Zero-Dependency Engine)"]
PDP["Policy Decision Engine (PDP)\n- Two-Tier Decision Cache (L1 Thread-Local / L2 TTL)\n- RoleGraph & Clearance Hierarchy"]
CEL["CEL Evaluator\n- Runtime Argument Validation\n- Attribute-Based Access Control (ABAC)"]
Audit["Audit Pipeline & PII Sanitizer\n- OCSF / ECS / RFC 5424 Emitter\n- In-Flight DLP & Parameter Redaction"]
end
subgraph External["Pluggable Backends & SPIs"]
OPA["Remote PDP (OPA / Cedar)"]
SIEM["SIEM (Splunk / Elastic / Datadog)"]
VDB["Vector DBs (Chroma / Pinecone / pgvector)"]
end
Agent <--> SR
Tool <--> ST
Retriever <--> SRET
IB -.->|"Injects SecurityIdentity"| Agent
SR & ST & SRET -->|"PolicyRequest"| PDP
ST -->|"Evaluate args"| CEL
SR & ST & SRET -->|"AuditEvent"| Audit
PDP -.->|"Optional SPI"| OPA
Audit -.->|"AuditPublisherSPI"| SIEM
SRET -.->|"Metadata Pre-filters"| VDB
NIST SP 800-207 Zero-Trust PEP / PDP Mapping
| Zero-Trust Role | Library Component | Implementation Responsibilities |
|---|---|---|
| PEP (Policy Enforcement Point) |
@secured_toolSecureRunnableSecureRetriever |
• Intercepts execution requests before tool/chain execution. • Enforces caller role and clearance floors. • Applies vector DB metadata pre-filters and prunes documents. • Emits immutable structured audit events to SIEM. |
| PDP (Policy Decision Point) |
EmbeddedPolicyEngineRemoteOpaAdapter |
• Evaluates authorization requests deterministically. • Manages two-tier decision caching (L1 thread-local, L2 LRU TTL). • Resolves hierarchical role graphs and multi-tenant isolation. • Evaluates fail-closed / fail-open circuit breakers on remote PDP outages. |
| PIP (Policy Information Point) |
IdentityBridgeEntityAttributeResolverSPI |
• Converts inbound JWT/OAuth claims into immutable SecurityIdentity.• Injects identity into LangChain's native RunnableConfig.• Resolves dynamic entity attributes (e.g. server owner, user dept) for CEL rules. |
| PAP (Policy Administration Point) |
Declarative Decorators & CEL Expressions | • Declarative @secured_tool parameters (required_roles, min_clearance).• Common Expression Language rules evaluated over live arguments. • Metadata classification schemas ( sec.clearance, sec.department). |
PEP Interception Lifecycle
sequenceDiagram
autonumber
actor Caller as Caller / Application
participant Bridge as IdentityBridge
participant Runnable as SecureRunnable (Agent PEP)
participant ToolPEP as @secured_tool (Tool PEP)
participant PDP as EmbeddedPolicyEngine (PDP)
participant CEL as CELEvaluator
participant SIEM as AuditPublisherSPI
participant Tool as Target Tool Function
Caller->>Bridge: create_runnable_config(identity)
Bridge-->>Caller: config: RunnableConfig
Caller->>Runnable: invoke(input, config)
Runnable->>PDP: evaluate(subject, action="AGENT_INVOKE")
PDP-->>Runnable: PERMIT
Runnable->>ToolPEP: invoke_tool(args, config)
rect rgb(240, 248, 255)
Note over ToolPEP,CEL: Pre-Execution Code PEP (0 LLM Tokens)
ToolPEP->>CEL: evaluate_expression(cel_rule, args, subject)
CEL-->>ToolPEP: Condition Satisfied
ToolPEP->>PDP: evaluate(subject, action="TOOL_EXECUTE", resource)
PDP-->>ToolPEP: PERMIT (cached / local)
ToolPEP->>SIEM: publish(AuditEvent: PERMIT)
end
ToolPEP->>Tool: execute(*args, **kwargs)
Tool-->>ToolPEP: raw_result
ToolPEP-->>Runnable: sanitized_result
Runnable-->>Caller: execution_output
📖 Documentation & Reference Application
Ready to adopt langchain-security-extension in your organization?
-
🚀 Quickstart Walkthrough: "Seeing is Believing"
Hands-on setup guide walking through setting up the library, cloning the demo app, running test suites on unhardened (main) vs hardened (secured) branches, and exploring the complete 46-test catalog. -
🌐 Documentation Hub
The central documentation portal covering architecture overviews, zero-trust patterns, and library index. -
📘 Enterprise Implementation Guide
The complete 12-chapter guide to rolling out deterministic security in production—covering Identity Provider (IdP) wiring, CEL & OPA policy authoring, RAG metadata signing, DLP sanitization, and SIEM telemetry. -
⚙️ Unified Configuration Reference
Comprehensive reference table of all configuration properties, environment variable overrides, default values, and operational impacts across PDP modes, caching, retry budgets, DLP, and SIEM sinks. -
📚 API Reference
Detailed module specifications forsecurity_core,security_adapter_langchain, andsecurity_pdp_opa. -
🧪 Companion Reference Application: Enterprise IT Incident Assistant
The live demonstrator codebase used throughout the implementation guide to illustrate a real-world migration from fragile prompt guardrails to deterministic enforcement:- Unhardened baseline (
mainbranch): Demonstrates real prompt injection vulnerabilities, bypassing standard LLM system prompts and causing unauthorized tool calls. - Hardened implementation (
securedbranch): Shows the complete, production-grade integration oflangchain-security-extensionfollowing the implementation guide. - Automated verification suites: Run
./run-security-suite.sh --suite basicand./run-security-suite.sh --suite guardrailto verify defenses and audit streaming in action. Check out the Quickstart Walkthrough for full step-by-step instructions.
- Unhardened baseline (
📄 License
This project is licensed under the Apache License 2.0.
Metadata
Release files for langchain-security-extension 1.0.0.post1
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_security_extension-1.0.0.post1.tar.gz | 108.8 kB | Without provenance | Details for langchain_security_extension-1.0.0.post1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_security_extension-1.0.0.post1-py3-none-any.whl | Python 3 | none | any | Details for langchain_security_extension-1.0.0.post1-py3-none-any.whl |
Total release size: 206.8 kB
Release files / langchain_security_extension-1.0.0.post1.tar.gz
| Download URL | langchain_security_extension-1.0.0.post1.tar.gz |
|---|---|
| Size | 108.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d7be8c6c044caa23ee3c426cb949639beaf9d7c60ed468618dd6841ef46e0eb5
|
|
BLAKE2b-256 checksum How to use checksums |
b876cdd5ccabe55dc263130d95e6e96cd8cbcbfca764054516999805439d01e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / langchain_security_extension-1.0.0.post1-py3-none-any.whl
| Download URL | langchain_security_extension-1.0.0.post1-py3-none-any.whl |
|---|---|
| Size | 98.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8b0ba4e873d76992eed23a279fcfd85bcefd02ccdcb73502a29842b46bb4ea2b
|
|
BLAKE2b-256 checksum How to use checksums |
8cf77be8a13325ad393c8ac4c8918d3959188ccaf6710850f0ad22e4287ffd25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Security for langchain-security-extension 1.0.0.post1
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.