Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(blocks): Default disable HTML escaping in all blocks with templating features (#10955)

Reinier van der Leer7bd571d9ce56
diff --git a/autogpt_platform/backend/backend/blocks/io.py b/autogpt_platform/backend/backend/blocks/io.py
index c9a98c3ca9..11626a1c54 100644
--- a/autogpt_platform/backend/backend/blocks/io.py
+++ b/autogpt_platform/backend/backend/blocks/io.py
@@ -10,7 +10,6 @@ from backend.util.settings import Config
from backend.util.text import TextFormatter
from backend.util.type import LongTextType, MediaFileType, ShortTextType
-formatter = TextFormatter()
config = Config()
@@ -132,6 +131,11 @@ class AgentOutputBlock(Block):
default="",
advanced=True,
)
+ escape_html: bool = SchemaField(
+ default=False,
+ advanced=True,
+ description="Whether to escape special characters in the inserted values to be HTML-safe. Enable for HTML output, disable for plain text.",
+ )
advanced: bool = SchemaField(
description="Whether to treat the output as advanced.",
default=False,
@@ -193,6 +197,7 @@ class AgentOutputBlock(Block):
"""
if input_data.format:
try:
+ formatter = TextFormatter(autoescape=input_data.escape_html)
yield "output", formatter.format_string(
input_data.format, {input_data.name: input_data.value}
)
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.py
index 856d736dcd..499811373d 100644
--- a/autogpt_platform/backend/backend/blocks/llm.py
+++ b/autogpt_platform/backend/backend/blocks/llm.py
@@ -27,7 +27,7 @@ from backend.util.prompt import compress_prompt, estimate_token_count
from backend.util.text import TextFormatter
logger = TruncatedLogger(logging.getLogger(__name__), "[LLM-Block]")
-fmt = TextFormatter()
+fmt = TextFormatter(autoescape=False)
LLMProviderName = Literal[
ProviderName.AIML_API,
diff --git a/autogpt_platform/backend/backend/blocks/text.py b/autogpt_platform/backend/backend/blocks/text.py
index fc586430fc..2ebb491074 100644
--- a/autogpt_platform/backend/backend/blocks/text.py
+++ b/autogpt_platform/backend/backend/blocks/text.py
@@ -172,6 +172,11 @@ class FillTextTemplateBlock(Block):
format: str = SchemaField(
description="Template to format the text using `values`. Use Jinja2 syntax."
)
+ escape_html: bool = SchemaField(
+ default=False,
+ advanced=True,
+ description="Whether to escape special characters in the inserted values to be HTML-safe. Enable for HTML output, disable for plain text.",
+ )
class Output(BlockSchema):
output: str = SchemaField(description="Formatted text")
@@ -205,6 +210,7 @@ class FillTextTemplateBlock(Block):
)
async def run(self, input_data: Input, **kwargs) -> BlockOutput:
+ formatter = text.TextFormatter(autoescape=input_data.escape_html)
yield "output", formatter.format_string(input_data.format, input_data.values)
diff --git a/autogpt_platform/backend/backend/util/text.py b/autogpt_platform/backend/backend/util/text.py
index 0c1e22edc0..45681aeaec 100644
--- a/autogpt_platform/backend/backend/util/text.py
+++ b/autogpt_platform/backend/backend/util/text.py
@@ -16,8 +16,8 @@ def format_filter_for_jinja2(value, format_string=None):
class TextFormatter:
- def __init__(self):
- self.env = SandboxedEnvironment(loader=BaseLoader(), autoescape=True)
+ def __init__(self, autoescape: bool = True):
+ self.env = SandboxedEnvironment(loader=BaseLoader(), autoescape=autoescape)
self.env.globals.clear()
# Instead of clearing all filters, just remove potentially unsafe ones