LangChain Security: LangChain Agent Security and LangGraph Security Best Practices
LangChain gives an agent reach. Almost nothing in the framework decides whether a particular reach was allowed. That decision has to live somewhere, and by default it lives nowhere.
Direct answer
LangChain security has two separate halves, and teams that only do one of them stay exposed. The first half is the framework itself: LangChain and LangGraph carry real, patchable vulnerabilities, including CVE-2025-68664, rated CVSS 9.3, a serialization flaw that lets prompt injection escalate into code execution in langchain-core before 0.3.81, and CVE-2026-34070, rated CVSS 7.5, a path traversal in prompt loading that reads arbitrary files such as .env files and SSH keys in langchain-core before 1.2.22. Those are fixed by upgrading. The second half is not fixed by upgrading, because it is not a bug. A LangChain agent is designed to read untrusted text and then choose which tools to call, so a perfectly patched agent will still follow an instruction hidden in a retrieved document if nothing sits between the decision and the action. LangChain says so itself: its official security policy tells you to limit permissions, anticipate misuse, and layer defenses, because "no security technique is perfect." Agentshield is the layer that enforces those limits at runtime. It sits in front of your LangChain and LangGraph agents, inspects untrusted input before the model acts on it, checks every tool call against a per-agent permission scope, holds irreversible actions for human approval, and writes one audit record per decision.
Try it live
Watch Agentshield block an attack in real time.
Pick a scenario and drive the inspection lane yourself. No signup needed.
Run a request
Inspection lane
INSPECTINGPolicy trace
High-risk action held for approval
Audit trail
- § · → → →
The risk
The agent works. It was built on LangChain in a fortnight, it reads from a vector store and a handful of tools, and it now runs against production data. Nobody can say which tools it is actually allowed to call, because the answer is whatever was passed into the tools list. Nobody can say what it did last Thursday, because the traces went to stdout. And the credentials it holds were scoped to the developer who built it.
How Agentshield handles it
Agentshield is stack-neutral and sits in the request and action path rather than replacing your framework, so LangChain and LangGraph code stays as it is. Untrusted input, whether it arrived from a user, a retrieved document, an email, or a tool result, is inspected for injection before the agent acts on it. Each tool call is checked against the permissions granted to that specific agent, not to the service account it happens to share. Destructive and irreversible calls are held for a human. Everything, allowed or denied, lands in one append-only trail you can hand to an auditor.
The controls
The controls that secure the LangChain and LangGraph agents you already have in production.
What is LangChain security?
LangChain security is the practice of keeping an agent built on LangChain or LangGraph from doing damage with the access you gave it. That splits cleanly into two problems with two different owners, and conflating them is the most common mistake we see.
The first problem is supply chain. LangChain is not one package, it is a family: langchain-core, langchain, langgraph, langgraph-checkpoint, langchain-community, langchain-experimental, and a long tail of provider integrations, each versioned separately. Several have carried serious vulnerabilities, and because the packages move independently, a team that upgraded langchain last quarter may still be running a langchain-core from before the fix. This problem is solved by patching and by tracking each package on its own, which is ordinary application security applied to an unusual dependency graph.
The second problem does not have a patch, because it is the framework working correctly. An agent reads text, decides, and acts. The text it reads includes things you did not write: search results, retrieved documents, database rows, emails, tool descriptions from third-party servers. The model has no reliable way to tell an instruction you intended from an instruction embedded in content it retrieved, and no version bump changes that. What changes it is putting a check between the decision and the action, which is the part LangChain deliberately leaves to you.
Both halves matter, and they fail differently. A CVE gets you compromised by someone who found the flaw. The design gap gets you compromised by a document that happened to land in your index.
LangChain security vulnerabilities: what to patch first
The Cloud Security Alliance published a research note on LangChain and LangGraph vulnerabilities on March 29, 2026, collecting the significant disclosures in one place. These are the ones worth checking against your lockfile today. Versions listed are the first fixed release, so anything below is affected.
| CVE | Package | Severity | What it exposes | Upgrade to |
|---|---|---|---|---|
| CVE-2025-68664 | langchain-core, langchain | CVSS 9.3, critical | Serialization injection. Prompt injection escalates to arbitrary code execution, environment variable extraction, and unauthorized class instantiation | langchain-core 0.3.81 or langchain 1.2.5 |
| CVE-2025-64439 | langgraph-checkpoint | Critical | Remote code execution through deserialized checkpoint data when the serializer falls back to dynamic import | langgraph-checkpoint 3.0 |
| CVE-2026-34070 | langchain-core | CVSS 7.5, high | Path traversal in prompt configuration loading. Arbitrary file reads, including .env files, SSH keys, and TLS certificates | langchain-core 1.2.22 |
| CVE-2025-67644 | langgraph-checkpoint-sqlite | CVSS 7.3, high | SQL injection through metadata filter keys, exposing conversation history and session tokens | langgraph-checkpoint-sqlite 3.0.1 |
| CVE-2025-6984 | langchain-community | CVSS 7.5, high | XML external entity injection in the EverNote loader, enabling SSRF and local file disclosure | langchain-community 0.3.27 |
| CVE-2025-46059 | Gmail toolkit | High | Indirect prompt injection carried in email content, leading to agent credential compromise | Treat all fetched content as untrusted |
Two of those deserve a second look, because they are not really dependency bugs. CVE-2025-68664 is a code execution flaw whose trigger is prompt injection, which means patching removes the escalation path but leaves the injection itself. CVE-2025-46059 has no clean patch at all, because the vulnerability is that an agent read an email and believed it. The fix the disclosure recommends is architectural: treat external content as hostile input. That is a runtime control, not a version number.
One more thing worth knowing about the ecosystem. LangChain's own bug bounty explicitly excludes langchain-experimental. Several of the nastiest historical issues lived there, including CVE-2024-21513, where database values reached a Python eval call unsanitized. If langchain-experimental appears anywhere in a production lockfile, that is worth an afternoon. The full patch matrix and the reasoning behind each one is in our guide to LangChain security vulnerabilities.
LangChain security best practices
LangChain's official security policy is short and unusually honest, and it is the right starting point because it comes from the maintainers rather than from a vendor. It names three principles: limit permissions, anticipate misuse, and defense in depth. The policy states plainly that "no security technique is perfect," and recommends combining approaches rather than trusting any single one.
Worth reading closely is what it says about anticipating misuse. If credentials permit deletion, assume the model might delete. That is not a warning about malice, it is a statement about how the system works. The practices below start from the framework's own guidance and then say where the gap is.
| Practice | What it means in LangChain | What it still does not cover |
|---|---|---|
| Scope credentials to the task | Read-only database users, API keys limited to safe endpoints, file access restricted to one directory | An agent with legitimate write access misusing it. Scoping bounds the blast radius, it does not judge intent |
| Sandbox execution | Run code interpreters and shell tools in a container with no host mount and no outbound network | Tools that are not code execution. Most damage now comes from ordinary API calls. See AI agent sandboxing |
| Validate inputs and outputs | Allowlists on tool arguments, structured output schemas, refusal of unexpected formats | A well-formed call that should not have been made. Schema validity says nothing about authorization |
| Never hardcode secrets | Environment variables or a managed secret store, rotated on a schedule | The agent still holds the secret at call time. A path traversal like CVE-2026-34070 reads .env files directly |
| Human approval on risky steps | The human-in-the-loop middleware interrupts before a tool call executes | Deciding which calls are risky, consistently, across every agent, and proving the gate held. See human in the loop |
| Per-agent identity | Not native to LangChain. Most deployments share one service account | Attribution. Shared credentials make the trail unusable. See AI agent identity |
| Defense in depth | Stack guardrail middleware, then add a control point outside the process | Nothing, if the outer layer exists. This is the one the framework cannot supply on its own |
The pattern in the right-hand column is consistent. Every practice LangChain recommends is enforced inside the process that the agent controls, configured by the developer who wrote that agent, and invisible to anyone auditing later. That is fine for one agent built by one team. It stops working at the point where a security owner has to answer for twelve agents built by five teams.
What LangChain middleware and guardrails do, and where they stop
LangChain 1.0 introduced agent middleware, a composable interception layer inside the agent execution loop. Middleware runs before the model is called, after the model responds, and after the agent finishes, which makes it a genuinely good place to put content checks. Guardrails in LangChain are implemented as middleware, and they stack, so you can layer several in order. The human-in-the-loop middleware uses the after-model hook to interrupt tool calls for approval. This is a real improvement over the callback patterns it replaced, and if you are building on LangChain you should use it.
It is also worth being precise about what it is. Middleware is application code running inside the agent process, in the same trust boundary as the agent, configured in the same repository, by the same developer, and deployed on the same release cycle. That has four consequences.
- It is opt-in per agent. An agent that was built without the guardrail middleware simply does not have it. There is no way to require it across a fleet, and no way to discover which agents skipped it except by reading every repository.
- It shares the blast radius. A vulnerability in the agent process, such as the code execution path in CVE-2025-68664, sits inside the same boundary as the middleware meant to constrain it. A control that an attacker can reach is not a control.
- It is changed by whoever ships the agent. Loosening a guardrail is a normal-looking code change in an application repository, reviewed by application reviewers. Nothing about it looks like a policy change.
- Its record is application logs. Useful for debugging, and not what an auditor means by an audit trail. There is no independent, append-only record that the gate existed and held.
None of that makes middleware the wrong choice. It makes it the inner layer. LangChain's own policy asks for defense in depth, and defense in depth means at least one layer outside the thing being defended. Middleware handles the fast, in-loop checks with full application context. A control point in front of the agent handles the policy that must apply whether or not a developer remembered, and produces the record that proves it. That outer layer is what we build, and it is described further on AI runtime security.
LangGraph security: state, checkpoints, and long-running agents
LangGraph adds durable state to the picture, and durable state is a security surface that stateless chains never had. A LangGraph agent checkpoints its progress so it can pause, resume, wait for a human, and survive a restart. Those checkpoints contain conversation history, intermediate reasoning, and often credentials or session tokens picked up along the way, and they are written somewhere: SQLite, Postgres, or Redis.
Two of the CVEs above are checkpoint bugs specifically. CVE-2025-64439 is remote code execution through deserialized checkpoint data, because the serializer could fall back to dynamic import. CVE-2025-67644 is SQL injection reachable through metadata filter keys in the SQLite backend, which exposes exactly the conversation histories and session tokens the checkpoint holds. Both are patched, and both illustrate the same structural point: the checkpoint store is now part of your attack surface, and it deserves the access controls you would give any database holding session data.
The behavioral risk with LangGraph is different from the one with a simple chain, and it is easy to miss. A graph agent runs for a long time across many steps. Injected instructions persist in state, so an instruction absorbed at step three is still in context at step forty, long after the document that carried it has scrolled out of anyone's attention. A control that only inspects the initial user prompt will not see it. Inspection has to happen on every hop where untrusted content enters, which is the entire point of putting the check on the action path rather than at the front door. That is covered on prompt injection detection.
Multi-agent LangGraph topologies compound it further, because one agent's output becomes another agent's input, and the second agent has no way to know the first one was reading a hostile document. We treat that as its own problem on multi-agent security.
How to secure a LangChain agent in production
This is the order we recommend, and it is deliberately front-loaded with the cheap work. Steps one and two take an afternoon and remove the largest single category of risk.
- Audit the lockfile, package by package. LangChain's packages version independently, so check langchain-core, langchain, langgraph, langgraph-checkpoint, langgraph-checkpoint-sqlite, langchain-community, and every provider integration separately. Upgrading the top-level package does not necessarily move langchain-core.
- Remove langchain-experimental from anything production-facing. It is outside LangChain's own bug bounty scope, which is the maintainers telling you where the line is.
- Write down what each agent is actually allowed to do. Not what its prompt says, what its credentials and tool list permit. This is usually the moment a team discovers an agent has broader database access than anyone intended.
- Give each agent its own identity. Shared service accounts make every later control weaker, because you cannot scope by caller and you cannot attribute in the trail.
- Route agent traffic through a control point, in observe mode. Change the endpoint, not the policy. Nothing breaks because nothing is denied yet, and you spend two weeks learning what your agents genuinely do rather than what the design doc claims.
- Scope tool permissions from observed behavior. Build the allowlist from the traffic you recorded. See tool permissions.
- Inspect untrusted content on every hop. Retrieved documents, tool results, emails, and third-party tool descriptions all reach the model as text. If your agents also connect to Model Context Protocol servers, that path needs its own inspection, covered on MCP gateway.
- Gate the irreversible calls behind a human. Deletes, transfers, external sends, bulk writes. Keep the routine majority automatic so the gate stays meaningful rather than becoming a button people click without reading.
- Switch to enforce, and keep the trail. Observe mode already told you what denying would break. The audit trail is what makes the arrangement provable rather than merely true.
If the agent in question writes or executes code, the risk profile shifts again and the sandboxing question comes first rather than sixth. We cover that on coding agents. If it reads from a vector store, the index itself is an injection vector worth its own treatment, on RAG security.
What a runtime control point does not fix
Stated plainly, because a pilot judged against the wrong expectation fails for reasons that have nothing to do with the product.
| Problem | Does Agentshield fix it? | What actually does |
|---|---|---|
| An unpatched CVE in langchain-core | No | Upgrading. A runtime control can narrow what an exploit reaches, but the flaw is in your process |
| Agents that bypass the control point | No | Network egress policy. A chokepoint governs the traffic routed through it and nothing else |
| A model that is confidently wrong | No | Evaluation and human review. We judge permission, not correctness |
| A tool that is dangerous by design | No | Not granting it. If a task legitimately requires deleting records, policy will allow the delete |
| Secrets already leaked from a .env file | No | Rotation. Scoped downstream credentials limit the next incident, not this one |
| Compliance on its own | Partly | It produces most of the evidence an auditor asks for. The program and the owners are yours. See AI compliance |
The honest summary: patching fixes the framework, and a control point in front of the agent fixes the part patching cannot reach, which is an agent using its legitimate access on an instruction it should not have trusted. Neither substitutes for the other, and LangChain's own security policy is the clearest statement of why. No single technique is perfect, so run more than one. Teams weighing LangChain against the alternatives should read OpenAI agent security, which ships guardrail primitives LangChain leaves to you, alongside the security comparison between the two frameworks.
FAQ
Common questions about langchain security.
Is LangChain secure?
LangChain is as secure as the permissions you give the agent, which is the framework maintainers position rather than a criticism. The library has carried real vulnerabilities that patching fixes, including a CVSS 9.3 serialization flaw. Beyond that, LangChain deliberately leaves authorization to you: it provides the tools an agent calls, not the judgment about whether a specific call should be allowed.
What are the main LangChain security risks?
Four dominate. Prompt injection reaching the agent through retrieved documents, emails, or tool results. Over-scoped credentials, where the agent can do far more than its task needs. Dependency vulnerabilities across the separately versioned LangChain packages. And missing attribution, because agents sharing one service account cannot be told apart in logs afterwards.
What LangChain security vulnerabilities should I patch?
Start with CVE-2025-68664, rated CVSS 9.3, fixed in langchain-core 0.3.81 or langchain 1.2.5, and CVE-2026-34070, rated CVSS 7.5, fixed in langchain-core 1.2.22. Then the LangGraph checkpoint issues CVE-2025-64439 and CVE-2025-67644, fixed in langgraph-checkpoint 3.0 and langgraph-checkpoint-sqlite 3.0.1. Check each package separately, since they version independently.
What are LangChain security best practices?
LangChain names three principles in its own security policy: limit permissions to what the task needs, anticipate misuse by assuming the model will use any access it has, and layer defenses because no single technique is perfect. In practice that means read-only credentials where possible, sandboxed code execution, per-agent identity, human approval on irreversible actions, and an enforcement point outside the agent process.
Does LangChain protect against prompt injection?
Not on its own. LangChain provides guardrail middleware you can add, and stacking those checks is worthwhile, but they are opt-in per agent and run inside the same process as the agent they constrain. Nothing in the framework inspects retrieved documents or tool results by default, and the model cannot reliably separate a trusted instruction from an injected one.
What is LangChain security middleware?
Middleware is the interception layer introduced in LangChain 1.0 that runs custom logic before the model is called, after it responds, and after the agent completes. Guardrails are implemented as middleware and can be stacked in order, and the human-in-the-loop middleware interrupts tool calls for approval. It is application code inside the agent process, so it is the inner layer of defense rather than the outer one.
How do I secure a LangChain agent in production?
Patch every LangChain package separately, drop langchain-experimental, then write down what each agent can actually reach through its credentials and tool list. Give each agent its own identity, route its traffic through a control point in observe mode for two weeks, scope tool permissions from what you observed, gate irreversible calls behind a human, and switch to enforce.
Is LangGraph more secure than LangChain?
Neither is more secure, they have different surfaces. LangGraph adds durable checkpoints, which is where two of the disclosed vulnerabilities live and where conversation history and session tokens are stored. It also runs longer, so an injected instruction absorbed early persists in state across many later steps that a front-door check would never see.
Does Agentshield replace LangChain?
No. Agentshield is stack-neutral and sits in the request and action path, so your LangChain and LangGraph code is unchanged. It adds the layer the framework leaves to you: injection inspection on untrusted content, per-agent tool permissions, approval gates on irreversible calls, and one audit trail across every agent regardless of which framework built it.
How does LangChain handle data security?
LangChain gives you the mechanisms and leaves the policy to you. It supports environment-based secret handling and scoped credentials, and its policy recommends read-only access and directory restrictions. What it does not do is inspect what an agent is about to send outward, so preventing a model from putting customer data into an outbound call is an external control. See our AI data leak prevention page.
More use cases