diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.py index a7e1263544b..0e3a29d75ef 100644 --- a/libs/core/langchain_core/prompts/string.py +++ b/libs/core/langchain_core/prompts/string.py @@ -64,9 +64,11 @@ def jinja2_formatter(template: str, /, **kwargs: Any) -> str: ) raise ImportError(msg) - # Use a restricted sandbox that blocks ALL attribute/method access - # Only simple variable lookups like {{variable}} are allowed - # Attribute access like {{variable.attr}} or {{variable.method()}} is blocked + # Use Jinja2's SandboxedEnvironment which blocks access to dunder attributes + # (e.g., __class__, __globals__) to prevent sandbox escapes. + # Note: regular attribute access (e.g., {{obj.attr}}) and method calls are + # still allowed. This is a best-effort measure — do not use with untrusted + # templates. return SandboxedEnvironment().from_string(template).render(**kwargs)