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

Source: https://junglewise.ai/threats/cve-2026-40087-langchain-incomplete-f-string-validation-in-prompt-templates  
Publisher: Junglewise Threat Intelligence, https://junglewise.ai/threats  
Published: 2026-04-09. Updated: 2026-05-10  
License: CC BY 4.0 with a link back

CVE-2026-40087 · Severity: medium · CVSS 5.3 · Published 2026-04-09

LangChain, a framework for building AI applications, had a flaw in how it validated prompt templates. If an application allows users to provide their own template strings, an attacker could craft a template that accesses internal data or sensitive object properties. This could lead to the exposure of private information in the AI's output or application logs.

Technologies: [Langchain Core](https://junglewise.ai/threats/technologies/langchain-langchain-core), [langchain-core (PyPI)](https://junglewise.ai/threats/technologies/langchain-core). Vendors: [LangChain](https://junglewise.ai/threats/vendors/langchain), [PyPI](https://junglewise.ai/threats/vendors/pypi).

## Quick answers

**Is CVE-2026-40087 exploited in the wild?** No. Junglewise Threat Intelligence has no report of CVE-2026-40087 being exploited in the wild.

**How severe is CVE-2026-40087?** Medium severity, with a CVSS score of 5.3, published 9 April 2026.

**Which products and versions are affected?** langchain-core (PyPI) ECOSYSTEM:introduced=0,fixed=0.3.84, ECOSYSTEM:introduced=0,fixed=0.3.84,introduced=1.0.0a1,fixed=1.2.28.

**Is there a fix for CVE-2026-40087?** Yes. langchain-core (PyPI): upgrade to 0.3.84, 1.2.28 or later. Junglewise finds every place you run it, drafts the upgrade and applies it once you approve.

**How is CVE-2026-40087 attacked?** It is attacked over the network, in a software library.

**Am I affected by CVE-2026-40087?** Check whether you run Langchain Core or langchain-core (PyPI) at an affected version. Junglewise does that check for you around the clock: connect your code, cloud and domains, and it matches new vulnerabilities like this one against what you actually run and tells you when one reaches you.

## How to fix

- langchain-core (PyPI): upgrade to 0.3.84, 1.2.28 or later

  ```sh
  pip install "langchain-core>=1.2.28"
  ```


## Technical details

LangChain's f-string validation failed to properly sanitize input in DictPromptTemplate and ImagePromptTemplate, and failed to reject nested replacement fields within format specifiers. This vulnerability allows for improper neutralization of special elements (CWE-1336) and improper input validation (CWE-20). An attacker who can provide untrusted template strings can use Python attribute access (e.g., .__class__) or indexing to traverse internal objects passed into the template during formatting. This can result in the disclosure of internal fields or sensitive data to the model context or logs. The issue is resolved in langchain-core versions 0.3.84 and 1.2.28 by enforcing consistent f-string safety validation and rejecting nested replacement fields.

## Affected products

- LangChain langchain-core < 0.3.83, >= 1.0.0a1, < 1.2.28

## Identifiers

- CVE-2026-40087
- GHSA-926x-3r5x-gfhw
- PYSEC-2026-2563

## Timeline

- 2026-04-08: advisory: GitHub Advisory GHSA-926x-3r5x-gfhw published
- 2026-04-08: patched: Fixes merged in PR #36612 and #36613
- 2026-04-09: other: NVD published CVE-2026-40087

## References

- [https://github.com/langchain-ai/langchain/security/advisories/GHSA-926x-3r5x-gfhw](https://github.com/langchain-ai/langchain/security/advisories/GHSA-926x-3r5x-gfhw)
- [https://github.com/langchain-ai/langchain/pull/36612](https://github.com/langchain-ai/langchain/pull/36612)
- [https://github.com/langchain-ai/langchain/pull/36613](https://github.com/langchain-ai/langchain/pull/36613)
- [https://github.com/langchain-ai/langchain/commit/6bab0ba3c12328008ddca3e0d54ff5a6151cd27b](https://github.com/langchain-ai/langchain/commit/6bab0ba3c12328008ddca3e0d54ff5a6151cd27b)
- [https://github.com/langchain-ai/langchain/commit/af2ed47c6f008cdd551f3c0d87db3774c8dfe258](https://github.com/langchain-ai/langchain/commit/af2ed47c6f008cdd551f3c0d87db3774c8dfe258)
- [https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D0.3.84](https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D0.3.84)

## Related threats

- [PYSEC-2026-2562 - LangChain affected by SSRF via image_url token counting in ChatOpenAI.get_num_tokens_from_messages](https://junglewise.ai/threats/cve-2026-26013-langchain-chatopenai-server-side-request-forgery-via-image-token)
- [PYSEC-2026-1517 - langchain-core allows unauthorized users to read arbitrary files from the host file system](https://junglewise.ai/threats/cve-2024-10940-langchain-core-allows-unauthorized-users-to-read-arbitrary-files)
- [PYSEC-2026-1519 - LangChain's XMLOutputParser vulnerable to XML Entity Expansion](https://junglewise.ai/threats/cve-2024-1455-langchain-s-xmloutputparser-vulnerable-to-xml-entity-expansion)
- [LangChain unsafe deserialization in load() via broad object allowlists](https://junglewise.ai/threats/cve-2026-44843-langchain-unsafe-deserialization-in-load-via-broad-object)
- [LangChain langchain-core path traversal in prompt loading functions](https://junglewise.ai/threats/cve-2026-34070-langchain-langchain-core-path-traversal-in-prompt-loading)
