Reference material — not professional advice. Test in staging, back up first, verify against your specific version. Use your own judgment for your environment.
● Medium · CVSS 5.3

How to Fix CVE-2026-40087: Cwe-1336: improper neutralization of special elements in langchain

By Sai Kiran Pandrala. Last verified: 2026-05-25.

⚡ At a glance
Severity5.3 (Medium)
Actively exploited?No public listing in CISA KEV
Affectedlangchain-ai < 0.3.83, >= 1.0.0a1, < 1.2.28
Fixed inlangchain 0.3.83; langchain 1.2.28
Type (CWE)CWE-1336: CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine

What is CVE-2026-40087?

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.

Am I affected?

Run the version check that matches your platform:


# Linux
dpkg -s langchain 2>/dev/null | grep -i version
rpm -q langchain 2>/dev/null
langchain --version 2>/dev/null

Compare what you see against the Affected row above (langchain-ai < 0.3.83, >= 1.0.0a1, < 1.2.28). If your build sits inside that range, you are exposed and should patch.

How to fix CVE-2026-40087

The primary fix is to upgrade langchain to the patched build. Use the commands for your platform below; the patched versions listed in the vendor advisory are: langchain 0.3.83; langchain 1.2.28.

Ubuntu / Debian


sudo apt-get update
sudo apt-get install --only-upgrade langchain
langchain --version 2>/dev/null || dpkg -s langchain | grep -i version

RHEL / CentOS / Rocky / AlmaLinux


sudo dnf upgrade --security langchain -y
rpm -q langchain

SUSE / openSUSE


sudo zypper patch --category security
rpm -q langchain

Complete PowerShell remediation script (Windows)


# Fix script for CVE-2026-40087 affecting langchain
# Run as administrator. Detect -> backup -> upgrade -> verify -> log.

$ErrorActionPreference = "Stop"
$LogPath  = "C:\Logs\CVE-2026-40087-fix-$(Get-Date -Format yyyyMMdd-HHmmss).log"
New-Item -ItemType Directory -Force (Split-Path $LogPath) | Out-Null
Start-Transcript -Path $LogPath -Append

try {
    Write-Host "[1/4] Detecting installed version of langchain"
    $pkg = winget list --id "langchain" 2>$null
    Write-Host $pkg

    Write-Host "[2/4] Backing up configuration"
    $backup = "C:\Backup\langchain-$(Get-Date -Format yyyyMMdd)"
    New-Item -ItemType Directory -Force $backup | Out-Null
    Get-ChildItem "C:\ProgramData\langchain" -ErrorAction SilentlyContinue |
        Copy-Item -Destination $backup -Recurse -Force -ErrorAction SilentlyContinue

    Write-Host "[3/4] Applying upgrade to langchain 0.3.83; langchain 1.2.28"
    winget upgrade --id "langchain" --silent --accept-source-agreements --accept-package-agreements
    # Fallback: Windows Update for OS-level fixes
    if ($LASTEXITCODE -ne 0) {
        Install-Module -Name PSWindowsUpdate -Force -SkipPublisherCheck -ErrorAction SilentlyContinue
        Import-Module PSWindowsUpdate
        Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot
    }

    Write-Host "[4/4] Verifying patched build"
    winget list --id "langchain"
    Write-Host "Fix applied. Reboot if prompted."
    exit 0
} catch {
    Write-Error "Patch failed: $_"
    exit 1
} finally {
    Stop-Transcript
}

Complete Bash remediation script (Linux)


#!/usr/bin/env bash
# Fix script for CVE-2026-40087 affecting langchain
# Detect -> backup -> upgrade -> verify -> log.

set -euo pipefail
LOG="/var/log/cve-2026-40087-fix-$(date +%Y%m%d-%H%M%S).log"
exec > >(tee -a "$LOG") 2>&1

echo "[1/4] Detecting installed version"
if command -v dpkg >/dev/null; then
    dpkg -s langchain 2>/dev/null | grep -i version || echo "langchain not installed via dpkg"
elif command -v rpm >/dev/null; then
    rpm -q langchain || echo "langchain not installed via rpm"
fi

echo "[2/4] Backing up configuration"
BACKUP="/root/backup-cve-2026-40087-$(date +%Y%m%d)"
mkdir -p "$BACKUP"
for d in /etc/langchain /etc/langchain.d /etc/langchain.conf; do
    [ -e "$d" ] && cp -a "$d" "$BACKUP/" || true
done

echo "[3/4] Applying upgrade (target: langchain 0.3.83; langchain 1.2.28)"
if command -v apt-get >/dev/null; then
    apt-get update
    apt-get install --only-upgrade -y langchain
elif command -v dnf >/dev/null; then
    dnf upgrade --security -y langchain
elif command -v yum >/dev/null; then
    yum update -y langchain
elif command -v zypper >/dev/null; then
    zypper --non-interactive patch --category security
fi

echo "[4/4] Verifying patched build"
if command -v dpkg >/dev/null; then
    dpkg -s langchain 2>/dev/null | grep -i version
elif command -v rpm >/dev/null; then
    rpm -q langchain
fi
echo "Done. Restart any running daemons that loaded the old library."

If you can't patch immediately

If you cannot apply the patched version today, restrict exposure with one of the following runnable controls. None replace the patch.

Network restriction (Linux, nftables)


# Block inbound traffic to the affected service from untrusted networks
sudo nft add table inet filter
sudo nft 'add chain inet filter input { type filter hook input priority 0 ; }'
sudo nft 'add rule inet filter input tcp dport {443, 80} ip saddr != 10.0.0.0/8 drop'
sudo nft list ruleset

Service-level fallback


# If the affected feature is optional, stop the service until the patch is applied
sudo systemctl stop langchain
sudo systemctl disable langchain

How to verify the fix worked


# Linux
langchain --version 2>/dev/null || dpkg -s langchain | grep -i version
rpm -q langchain 2>/dev/null || true

# Windows
winget list | findstr /I "langchain"
Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 5

Expected: the reported version is at or above langchain 0.3.83; langchain 1.2.28. Restart any services that loaded the old library (systemctl restart <service> on Linux, restart the Windows service or reboot when prompted). For network appliances, run show version on the device and confirm the build matches the patched release.

Frequently asked questions

Other vulnerabilities in the same area that are worth patching alongside this one:

Is CVE-2026-40087 actually being exploited?

According to the data sources above, no public confirmation of in-the-wild exploitation at this time. Either way, the fix is the same: apply the vendor patch.

Do I need to reboot after patching?

For OS or kernel updates, yes. For most userland packages a systemctl restart <service> is enough. Any process that loaded the old shared library keeps using it until restarted, so when in doubt, reboot.

What is the CVSS score?

5.3 (medium). Refer to the vendor advisory for the exact vector string.

Where is the official advisory?

See the References section at the bottom of this page; the vendor's URL is the authoritative source for affected builds and patched versions.

References


*Written by Sai Kiran Pandrala. Assembled from the official vendor advisory, NVD record, and CISA KEV listing on 2026-05-25. Always confirm against the vendor's advisory before applying changes in production.*