{"id":"CVE-2026-40087","aliases":["CVE-2026-40087","GHSA-926x-3r5x-gfhw","PYSEC-2026-2563"],"related":[],"kind":"vulnerability","state":"published","title":"LangChain has incomplete f-string validation in prompt templates","summary":"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. Second, f-string validation based on parsed top-level field names did not reject nested replacement fields inside format specifiers. In 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. This vulnerability is fixed in 0.3.84 and 1.2.28.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"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. Second, f-string validation based on parsed top-level field names did not reject nested replacement fields inside format specifiers. In 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. This vulnerability is fixed in 0.3.84 and 1.2.28.","source":"cvelist"},{"lang":"en","text":"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.","format":"markdown","source":"ghsa"},{"lang":"es","text":"LangChain es un framework para construir agentes y aplicaciones impulsadas por LLM. Antes de 0.3.84 y 1.2.28, la validación de plantillas de prompt f-string de LangChain era incompleta en dos aspectos. Primero, algunas clases de plantillas de prompt aceptaban plantillas f-string y las formateaban sin aplicar la misma validación de acceso a atributos que PromptTemplate. En particular, DictPromptTemplate e ImagePromptTemplate podían aceptar plantillas que contenían acceso a atributos o expresiones de indexación y posteriormente evaluar esas expresiones durante el formateo. Segundo, la validación de f-string basada en nombres de campo de nivel superior analizados no rechazaba los campos de reemplazo anidados dentro de los especificadores de formato. En este patrón, el campo de reemplazo anidado aparece en el especificador de formato en lugar de en el nombre del campo de nivel superior. Como resultado, la validación anterior basada en nombres de campo analizados no rechazaba la plantilla a pesar de que el formateo de Python aún intentaría resolver la expresión anidada en tiempo de ejecución. Esta vulnerabilidad se corrige en 0.3.84 y 1.2.28.","source":"nvd"}],"published":"2026-04-08T21:51:32.000Z","modified":"2026-10-25T23:10:00.167Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N","score":5.3,"rating":"MEDIUM"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N","score":5.3,"rating":"MEDIUM"},{"scorer":"GitHub","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N","score":5.3,"rating":"MEDIUM"},{"scorer":"PyPA Advisory Database","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N","score":5.3,"rating":"MEDIUM"}],"cwes":["CWE-20","CWE-1336"],"affected":[{"ranges":[{"version":"< 0.3.83","status":"affected"},{"version":">= 1.0.0a1, < 1.2.28","status":"affected"}],"vendor":"langchain-ai","product":"langchain","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"0.3.84"}],"ecosystem":"PyPI","package":"langchain-core","purl":"pkg:pypi/langchain-core","source":"ghsa"},{"ranges":[{"type":"ecosystem","introduced":"1.0.0a1","status":"affected","fixed":"1.2.28"}],"ecosystem":"PyPI","package":"langchain-core","purl":"pkg:pypi/langchain-core","source":"ghsa"},{"ranges":[{"status":"affected","fixed":"0.3.84"},{"status":"affected","introduced":"1.0.0","fixed":"1.2.28"}],"vendor":"langchain","product":"langchain_core","cpe":"cpe:2.3:a:langchain:langchain_core:*:*:*:*:*:python:*:*","source":"nvd"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"0.3.84"},{"type":"ecosystem","introduced":"1.0.0a1","status":"affected","fixed":"1.2.28"}],"ecosystem":"PyPI","package":"langchain-core","purl":"pkg:pypi/langchain-core","source":"pysec"}],"references":[{"url":"https://github.com/langchain-ai/langchain/security/advisories/GHSA-926x-3r5x-gfhw","tags":["mitigation","vendor-advisory","web","x-refsource-confirm"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/langchain-ai/langchain/pull/36612","tags":["issue-tracking","patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/langchain-ai/langchain/pull/36613","tags":["issue-tracking","patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/langchain-ai/langchain/commit/6bab0ba3c12328008ddca3e0d54ff5a6151cd27b","tags":["patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/langchain-ai/langchain/commit/af2ed47c6f008cdd551f3c0d87db3774c8dfe258","tags":["patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D0.3.84","tags":["product","release-notes","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D1.2.28","tags":["product","release-notes","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2026-40087","tags":["advisory"],"sources":["ghsa","pysec"]},{"url":"https://github.com/langchain-ai/langchain","tags":["package"],"sources":["ghsa","pysec"]},{"url":"https://pypi.org/project/langchain-core","tags":["package"],"sources":["pysec"]},{"url":"https://github.com/advisories/GHSA-926x-3r5x-gfhw","tags":["advisory"],"sources":["pysec"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"none","automatable":"yes","technicalImpact":"partial","date":"2026-04-14T14:47:52.978Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-40087","url":"https://www.cve.org/CVERecord?id=CVE-2026-40087","licence":"CVE Terms of Use","modified":"2026-04-14T14:48:03.160Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-926x-3r5x-gfhw","url":"https://github.com/advisories/GHSA-926x-3r5x-gfhw","licence":"CC-BY-4.0","modified":"2026-09-10T03:51:01.352Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-40087","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-40087","licence":"Public domain","modified":"2026-10-25T23:10:00.167Z"},{"source":"pysec","name":"PyPA Advisory Database","id":"PYSEC-2026-2563","url":"https://osv.dev/vulnerability/PYSEC-2026-2563","licence":"CC-BY-4.0","modified":"2026-07-13T16:31:48.838Z"}],"updatedAt":"2026-11-25T22:59:18.418Z","epss":{"score":0.00278,"percentile":0.21669,"date":"2026-12-01T12:00:22.000Z"}}