# CVE-2026-40087: LangChain has incomplete f-string validation in prompt templates

> Incomplete f-string validation in LangChain prompt templates allows attackers to execute arbitrary code via crafted templates.

- **Severity:** Medium 5.3 (`CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N`)
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 9 Apr 2026
- **Updated:** 14 Apr 2026
- **Also known as:** ECHO-8479-4c4c-18a0, GHSA-926x-3r5x-gfhw, PYSEC-2026-2563, ROOT-APP-PYPI-CVE-2026-40087

## What is CVE-2026-40087?

CVE-2026-40087 is a medium-severity code injection vulnerability in langchain-ai langchain, published on 9 Apr 2026. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

LangChain is a framework for building agents and LLM-powered applications. Prior to 0.3.84 and 1.2.28, LangChain's f-string prompt-template validation was incomplete in two respects. First, some prompt template classes accepted f-string templates and formatted them without enforcing the same attribute-access validation as PromptTemplate. In particular, DictPromptTemplate and ImagePromptTemplate could accept templates containing attribute access or indexing expressions and subsequently evaluate those expressions during formatting. Second, f-string validation based on parsed top-level field names did not reject nested replacement fields inside format specifiers. In this pattern, the nested replacement field appears in the format specifier rather than in the top-level field name. As a result, earlier validation based on parsed field names did not reject the template even though Python formatting would still attempt to resolve the nested expression at runtime. This vulnerability is fixed in 0.3.84 and 1.2.28.

Weakness: CWE-1336 (Improper Neutralization of Special Elements Used in a Template Engine)

## Which versions are affected by CVE-2026-40087?

5 open source packages ship an affected version, including langchain-core. It affects 1 product.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `langchain-core` | PyPI | All versions | 0.3.84, 1.2.28 |
| `langchain-core` | PyPI | All versions | 0.3.84 |
| `langchain-core` | PyPI | All versions | 0.3.84 |
| `langchain-core` | PyPI | From 1.0.0a1 | 1.2.28 |
| `langchain-core` | PyPI | From 1.0.0a1 | 1.2.28 |

Products: Langchain Langchain Core.

## How do I fix CVE-2026-40087?

Upgrade langchain-core to a fixed version for your release line: 0.3.84 or 1.2.28.

## Is CVE-2026-40087 being exploited?

There is no known exploitation of CVE-2026-40087 in the wild and no public exploit code.

## Other vulnerabilities in langchain-ai langchain

- [CVE-2025-68664](https://insights.sbomify.com/CVE-2025-68664/): LangChain serialization injection vulnerability enables secret extraction in dumps/loads APIs (critical 9.3)
- [CVE-2026-34070](https://insights.sbomify.com/CVE-2026-34070/): LangChain Core has Path Traversal vulnerabilites in legacy `load_prompt` functions (high 7.5)
- [CVE-2026-26013](https://insights.sbomify.com/CVE-2026-26013/): LangChain affected by SSRF via image_url token counting in ChatOpenAI.get_num_tokens_from_messages (low 3.7)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2026-40087 and fix it if it is affected.

What it is: CVE-2026-40087 is a medium-severity code injection vulnerability in langchain-ai langchain, published on 9 Apr 2026. An attacker can exploit it over the internet without logging in, with no action from the victim.
Exploitation: There is no known exploitation of CVE-2026-40087 in the wild and no public exploit code.

Affected packages and fixed versions:
- langchain-core (PyPI): affected all versions; fixed in 0.3.84, 1.2.28
- langchain-core (PyPI): affected all versions; fixed in 0.3.84
- langchain-core (PyPI): affected all versions; fixed in 0.3.84
- langchain-core (PyPI): affected from 1.0.0a1; fixed in 1.2.28
- langchain-core (PyPI): affected from 1.0.0a1; fixed in 1.2.28

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2026-40087/ (Markdown: https://insights.sbomify.com/CVE-2026-40087/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2026-40087
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2026-40087
- OSV: https://osv.dev/vulnerability/CVE-2026-40087

---
From sbomify Insights: https://insights.sbomify.com/CVE-2026-40087/
Summaries are generated and may contain mistakes; the official description above is the record.
