LangChain Vulnerable to Template Injection via Attribute Access in Prompt Templates
HighCVE-2025-65106 · Published Nov 20, 2025 · updated Sep 10, 2026
## Context A template injection vulnerability exists in LangChain's prompt template system that allows attackers to access Python object internals through template syntax. This vulnerability affects applications that accept **untrusted template strings** (not just template variables) in `ChatPromptTemplate` and related prompt template classes. Templates allow attribute access (`.`) and indexing (`[]`) but not method invocation (`()`). The combination of attribute access and indexing may enable exploitation depending on which objects are passed to templates. When template variables are simple strings (the common case), the impact is limited. However, when using `MessagesPlaceholder` with chat message objects, attackers can traverse through object attributes and dictionary lookups (e.g., `__globals__`) to reach sensitive data such as environment variables. The vulnerability specifically requires that applications accept **template strings** (the structure) from untrusted sources, not just **template variables** (the data). Most applications either do not use templates or else use hardcoded templates and are not vulnerable. ## Affected Components - `langchain-core` package - Tem...
Affected versions
| Package | Affected | Fixed in |
|---|---|---|
| langchain-core PyPI | >= 1.0.0, < 1.0.7 | 1.0.7 |
| < 0.3.80 | 0.3.80 |
Details and references
## Context A template injection vulnerability exists in LangChain's prompt template system that allows attackers to access Python object internals through template syntax. This vulnerability affects applications that accept **untrusted template strings** (not just template variables) in `ChatPromptTemplate` and related prompt template classes. Templates allow attribute access (`.`) and indexing (`[]`) but not method invocation (`()`). The combination of attribute access and indexing may enable exploitation depending on which objects are passed to templates. When template variables are simple strings (the common case), the impact is limited. However, when using `MessagesPlaceholder` with chat message objects, attackers can traverse through object attributes and dictionary lookups (e.g., `__globals__`) to reach sensitive data such as environment variables. The vulnerability specifically requires that applications accept **template strings** (the structure) from untrusted sources, not just **template variables** (the data). Most applications either do not use templates or else use hardcoded templates and are not vulnerable. ## Affected Components - `langchain-core` package - Template formats: - F-string templates (`template_format="f-string"`) - **Vulnerability fixed** - Mustache templates (`template_format="mustache"`) - **Defensive hardening** - Jinja2 templates (`template_format="jinja2"`) - **Defensive hardening** ### Impact Attackers who can control template strings (not just template variables) can: - Access Python object attributes and internal properties via attribute traversal - Extract sensitive information from object internals (e.g., `__class__`, `__globals__`) - Potentially escalate to more severe attacks depending on the objects passed to templates ### Attack Vectors #### 1. F-string Template Injection **Before Fix:** ```python from langchain_core.prompts import ChatPromptTemplate malicious_template = ChatPromptTemplate.from_messages( [("human", "{msg.__class__.__name__}")], template_format="f-string" ) # Note that this requires passing a placeholder variable for "msg.__class__.__name__". result = malicious_template.invoke({"msg": "foo", "msg.__class__.__name__": "safe_placeholder"}) # Previously returned # >>> result.messages[0].content # >>> 'str' ``` #### 2. Mustache Template Injection **Before Fix:** ```python from langchain_core.prompts import ChatPromptTemplate from langchain_core.messages import HumanMessage msg = HumanMessage("Hello") # Attacker controls the template string malicious_template = ChatPromptTemplate.from_messages( [("human", "{{question.__class__.__name__}}")], template_format="mustache" ) result = malicious_template.invoke({"question": msg}) # Previously returned: "HumanMessage" (getattr() exposed internals) ``` #### 3. Jinja2 Template Injection **Before Fix:** ```python from langchain_core.prompts import ChatPromptTemplate from langchain_core.messages import HumanMessage msg = HumanMessage("Hello") # Attacker controls the template string malicious_template = ChatPromptTemplate.from_messages( [("human", "{{question.parse_raw}}")], template_format="jinja2" ) result = malicious_template.invoke({"question": msg}) # Could access non-dunder attributes/methods on objects ``` ### Root Cause 1. **F-string templates**: The implementation used Python's `string.Formatter().parse()` to extract variable names from template strings. This method returns the complete field expression, including attribute access syntax: ```python from string import Formatter template = "{msg.__class__} and {x}" print([var_name for (_, var_name, _, _) in Formatter().parse(template)]) # Returns: ['msg.__class__', 'x'] ``` The extracted names were not validated to ensure they were simple identifiers. As a result, template strings containing attribute traversal and indexing expressions (e.g., `{obj.__class__.__name__}` or `{obj.method.__globals__
- CVSS 4.0
- CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:H/VI:L/VA:N/SC:N/SI:N/SA:N
- Severity from
- GitHub (reviewed advisory)
- Weakness
- CWE-1336
- Also known as
- CVE-2025-65106, PYSEC-2026-1518
- github.com/langchain-ai/langchain/security/advisories/GHSA-6qv9-48xg-fc7f
- nvd.nist.gov/vuln/detail/CVE-2025-65106
- github.com/langchain-ai/langchain/commit/c4b6ba254e1a49ed91f2e268e6484011c540542a
- github.com/langchain-ai/langchain/commit/fa7789d6c21222b85211755d822ef698d3b34e00
- github.com/langchain-ai/langchain
More LangChain advisories
All LangChain| Date | Advisory | Severity | Fixed in |
|---|---|---|---|
| Feb 25 | LangChain Community: redirect chaining can lead to SSRF bypass via RecursiveUrlLoader | Medium4.1 | 1.1.18 |
| Feb 11 | @langchain/community affected by SSRF Bypass in RecursiveUrlLoader via insufficient URL origin validation | Medium4.1 | 1.1.14 |
| Feb 11 | LangChain affected by SSRF via image_url token counting in ChatOpenAI.get_num_tokens_from_messages | Low3.7 | 1.2.11 |
| Dec 232025 | LangChain serialization injection vulnerability enables secret extraction | High8.6 | 0.3.37+3 more |
| Dec 232025 | LangChain serialization injection vulnerability enables secret extraction in dumps/loads APIs | Critical9.3 | 0.3.81+1 more |
| Sep 42025 | Langchain Community Vulnerable to XML External Entity (XXE) Attacks | High7.5 | 0.3.27 |