Skip to content

Commit

Permalink
Fix issue that could lead to RCE if using unsecure Jinja templates in…
Browse files Browse the repository at this point in the history
… dynamic prompt builders (#8096)
  • Loading branch information
silvanocerza authored Jul 26, 2024
1 parent c077f4c commit 6c25a5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
9 changes: 5 additions & 4 deletions haystack/components/builders/dynamic_chat_prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import warnings
from typing import Any, Dict, List, Optional, Set

from jinja2 import Template, meta
from jinja2 import meta
from jinja2.sandbox import SandboxedEnvironment

from haystack import component, logging
from haystack.dataclasses.chat_message import ChatMessage, ChatRole
Expand Down Expand Up @@ -177,8 +178,8 @@ def _validate_template(self, template_text: str, provided_variables: Set[str]):
:raises ValueError:
If all the required template variables are not provided.
"""
template = Template(template_text)
ast = template.environment.parse(template_text)
env = SandboxedEnvironment()
ast = env.parse(template_text)
required_template_variables = meta.find_undeclared_variables(ast)
filled_template_vars = required_template_variables.intersection(provided_variables)
if len(filled_template_vars) != len(required_template_variables):
Expand All @@ -187,4 +188,4 @@ def _validate_template(self, template_text: str, provided_variables: Set[str]):
f"Required variables: {required_template_variables}. Only the following variables were "
f"provided: {provided_variables}. Please provide all the required template variables."
)
return template
return env.from_string(template_text)
9 changes: 5 additions & 4 deletions haystack/components/builders/dynamic_prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import warnings
from typing import Any, Dict, List, Optional, Set

from jinja2 import Template, meta
from jinja2 import meta
from jinja2.sandbox import SandboxedEnvironment

from haystack import component, logging

Expand Down Expand Up @@ -156,8 +157,8 @@ def _validate_template(self, template_text: str, provided_variables: Set[str]):
:raises ValueError:
If all the required template variables are not provided.
"""
template = Template(template_text)
ast = template.environment.parse(template_text)
env = SandboxedEnvironment()
ast = env.parse(template_text)
required_template_variables = meta.find_undeclared_variables(ast)
filled_template_vars = required_template_variables.intersection(provided_variables)
if len(filled_template_vars) != len(required_template_variables):
Expand All @@ -166,4 +167,4 @@ def _validate_template(self, template_text: str, provided_variables: Set[str]):
f"Required variables: {required_template_variables}. Only the following variables were "
f"provided: {provided_variables}. Please provide all the required template variables."
)
return template
return env.from_string(template_text)
2 changes: 2 additions & 0 deletions releasenotes/notes/fix-jinja-env-81c98225b22dc827.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ security:
- `PromptBuilder`
- `ChatPromptBuilder`
- `DynamicPromptBuilder`
- `DynamicChatPromptBuilder`
- `OutputAdapter`
- `ConditionalRouter`
Expand Down

0 comments on commit 6c25a5c

Please sign in to comment.