GitHub Security Advisory: GHSA-6qv9-48xg-fc7f
Package: langchain-core (PIP)
Severity: HIGH (CVSS 0.0)
CWE: CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine
Vulnerable versions: >= 1.0.0, <= 1.0.6
Patched in: 1.0.7

Summary: LangChain Vulnerable to Template Injection via Attribute Access in Prompt Templates

Details:
## 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

ms