CVE-2026-40087: Langchain core LangChain Has Incomplete F-string Validation Vulnerability
MODERATECVE-2026-40087 is a MODERATE (CVSS 5.3) langchain has incomplete f-string validation vulnerability (CWE-20: Improper Input Validation) affecting Langchain core libraries in the PyPI ecosystem. The issue involves langchain has incomplete f-string validation in prompt templates. Remediation is available by upgrading affected packages to version 0.3.84, 1.2.28.
CVE-2026-40087 at a Glance
Security Impact
CVSS: 5.3
The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N).
How to Fix CVE-2026-40087
To remediate this vulnerability, upgrade all affected Langchain core dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
pip install langchain-core>=0.3.84
pip install langchain-core>=1.2.28
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
pip show langchain-core
How to Check if You Are Affected
To determine whether your Python environment is affected by CVE-2026-40087, inspect your installed packages using pip:
pip show langchain-core
Analyze the terminal output to locate any version of Langchain core in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.3.84), your application is affected.
Note: The pip show langchain-core command checks currently installed packages in your active Python environment.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-20: Improper Input Validation, where the product receives input or data, but it does not validate or incorrectly validates that the input has the properties that are required to process the data safely and correctly. In Langchain core, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2026-40087 Affected Langchain core Versions
2 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
langchain-core
|
PyPI |
< 0.3.84
|
0.3.84
|
|
langchain-core
|
PyPI |
≥ 1.0.0a1
< 1.2.28
|
1.2.28
|
References
Frequently Asked Questions
What is CVE-2026-40087?
Which Langchain core versions are affected?
What version fixes CVE-2026-40087?
Is langchain-core affected?
What is the CVSS score of CVE-2026-40087?
What CWE is associated with CVE-2026-40087?
How can I check PyPI dependencies for CVE-2026-40087?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-926x-3r5x-gfhw",
"modified" : "2026-04-10T14:41:45Z",
"published" : "2026-04-08T21:51:32Z",
"aliases" : [ "CVE-2026-40087" ],
"summary" : "LangChain has incomplete f-string validation in prompt templates",
"details" : "LangChain's f-string prompt-template validation was incomplete in two respects.\n\nFirst, 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.\n\nExamples of the affected shape include:\n\n```python\n\"{message.additional_kwargs[secret]}\"\n\"https://example.com/{image.__class__.__name__}.png\"\n```\n\nSecond, f-string validation based on parsed top-level field names did not reject nested replacement fields inside format specifiers. For example:\n\n```python\n\"{name:{name.__class__.__name__}}\"\n```\n\nIn 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.\n\n## Affected usage\n\nThis issue is only relevant for applications that accept untrusted template strings, rather than only untrusted template variable values.\n\nIn addition, practical impact depends on what objects are passed into template formatting:\n\n- If applications only format simple values such as strings and numbers, impact is limited and may only result in formatting errors.\n- If applications format richer Python objects, attribute access and indexing may interact with internal object state during formatting.\n\nIn many deployments, these conditions are not commonly present together. Applications that allow end users to author arbitrary templates often expose only a narrow set of simple template variables, while applications that work with richer internal Python objects often keep template structure under developer control. As a result, the highest-impact scenario is plausible but is not representative of all LangChain applications.\n\nApplications that use hardcoded templates or that only allow users to provide variable values are not affected by this issue.\n\n## Impact\n\nThe direct issue in `DictPromptTemplate` and `ImagePromptTemplate` allowed attribute access and indexing expressions to survive template construction and then be evaluated during formatting. When richer Python objects were passed into formatting, this could expose internal fields or nested data to prompt output, model context, or logs.\n\nThe nested format-spec issue is narrower in scope. It bypassed the intended validation rules for f-string templates, but in simple cases it results in an invalid format specifier error rather than direct disclosure. Accordingly, its practical impact is lower than that of direct top-level attribute traversal.\n\nOverall, the practical severity depends on deployment. Meaningful confidentiality impact requires attacker control over the template structure itself, and higher impact further depends on the surrounding application passing richer internal Python objects into formatting.\n\n## Fix\n\nThe fix consists of two changes.\n\nFirst, LangChain now applies f-string safety validation consistently to `DictPromptTemplate` and `ImagePromptTemplate`, so templates containing attribute access or indexing expressions are rejected during construction and deserialization.\n\nSecond, LangChain now rejects nested replacement fields inside f-string format specifiers.\n\nConcretely, LangChain validates parsed f-string fields and raises an error for:\n\n- variable names containing attribute access or indexing syntax such as `.` or `[]`\n- format specifiers containing `{` or `}`\n\nThis blocks templates such as:\n\n```python\n\"{message.additional_kwargs[secret]}\"\n\"https://example.com/{image.__class__.__name__}.png\"\n\"{name:{name.__class__.__name__}}\"\n```\n\nThe fix preserves ordinary f-string formatting features such as standard format specifiers and conversions, including examples like:\n\n```python\n\"{value:.2f}\"\n\"{value:>10}\"\n\"{value!r}\"\n```\n\nIn addition, the explicit template-validation path now applies the same structural f-string checks before performing placeholder validation, ensuring that the security checks and validation checks remain aligned.",
"severity" : [ {
"type" : "CVSS_V3",
"score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N"
} ],
"affected" : [ {
"package" : {
"ecosystem" : "PyPI",
"name" : "langchain-core"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "0"
}, {
"fixed" : "0.3.84"
} ]
} ],
"database_specific" : {
"last_known_affected_version_range" : "< 0.3.83"
}
}, {
"package" : {
"ecosystem" : "PyPI",
"name" : "langchain-core"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "1.0.0a1"
}, {
"fixed" : "1.2.28"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/security/advisories/GHSA-926x-3r5x-gfhw"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2026-40087"
}, {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/pull/36612"
}, {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/pull/36613"
}, {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/commit/6bab0ba3c12328008ddca3e0d54ff5a6151cd27b"
}, {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/commit/af2ed47c6f008cdd551f3c0d87db3774c8dfe258"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/langchain-ai/langchain"
}, {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D0.3.84"
}, {
"type" : "WEB",
"url" : "https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D1.2.28"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-1336", "CWE-20" ],
"severity" : "MODERATE",
"github_reviewed" : true,
"github_reviewed_at" : "2026-04-08T21:51:32Z",
"nvd_published_at" : "2026-04-09T20:16:27Z"
}
}