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(platform/blocks): XMLParserBlock list object error (#11517)

Nicholas Tindle818de26d2432
diff --git a/autogpt_platform/backend/backend/blocks/test/test_blocks_dos_vulnerability.py b/autogpt_platform/backend/backend/blocks/test/test_blocks_dos_vulnerability.py
index 87bf102099..389bb5c636 100644
--- a/autogpt_platform/backend/backend/blocks/test/test_blocks_dos_vulnerability.py
+++ b/autogpt_platform/backend/backend/blocks/test/test_blocks_dos_vulnerability.py
@@ -196,6 +196,15 @@ class TestXMLParserBlockSecurity:
async for _ in block.run(XMLParserBlock.Input(input_xml=large_xml)):
pass
+ async def test_rejects_text_outside_root(self):
+ """Ensure parser surfaces readable errors for invalid root text."""
+ block = XMLParserBlock()
+ invalid_xml = "<root><child>value</child></root> trailing"
+
+ with pytest.raises(ValueError, match="text outside the root element"):
+ async for _ in block.run(XMLParserBlock.Input(input_xml=invalid_xml)):
+ pass
+
class TestStoreMediaFileSecurity:
"""Test file storage security limits."""
diff --git a/autogpt_platform/backend/backend/blocks/xml_parser.py b/autogpt_platform/backend/backend/blocks/xml_parser.py
index bf35733c15..223f8ea367 100644
--- a/autogpt_platform/backend/backend/blocks/xml_parser.py
+++ b/autogpt_platform/backend/backend/blocks/xml_parser.py
@@ -1,5 +1,5 @@
from gravitasml.parser import Parser
-from gravitasml.token import tokenize
+from gravitasml.token import Token, tokenize
from backend.data.block import Block, BlockOutput, BlockSchemaInput, BlockSchemaOutput
from backend.data.model import SchemaField
@@ -25,6 +25,38 @@ class XMLParserBlock(Block):
],
)
+ @staticmethod
+ def _validate_tokens(tokens: list[Token]) -> None:
+ """Ensure the XML has a single root element and no stray text."""
+ if not tokens:
+ raise ValueError("XML input is empty.")
+
+ depth = 0
+ root_seen = False
+
+ for token in tokens:
+ if token.type == "TAG_OPEN":
+ if depth == 0 and root_seen:
+ raise ValueError("XML must have a single root element.")
+ depth += 1
+ if depth == 1:
+ root_seen = True
+ elif token.type == "TAG_CLOSE":
+ depth -= 1
+ if depth < 0:
+ raise SyntaxError("Unexpected closing tag in XML input.")
+ elif token.type in {"TEXT", "ESCAPE"}:
+ if depth == 0 and token.value:
+ raise ValueError(
+ "XML contains text outside the root element; "
+ "wrap content in a single root tag."
+ )
+
+ if depth != 0:
+ raise SyntaxError("Unclosed tag detected in XML input.")
+ if not root_seen:
+ raise ValueError("XML must include a root element.")
+
async def run(self, input_data: Input, **kwargs) -> BlockOutput:
# Security fix: Add size limits to prevent XML bomb attacks
MAX_XML_SIZE = 10 * 1024 * 1024 # 10MB limit for XML input
@@ -35,7 +67,9 @@ class XMLParserBlock(Block):
)
try:
- tokens = tokenize(input_data.input_xml)
+ tokens = list(tokenize(input_data.input_xml))
+ self._validate_tokens(tokens)
+
parser = Parser(tokens)
parsed_result = parser.parse()
yield "parsed_xml", parsed_result
diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lock
index 9a2983fd07..5e3b07a145 100644
--- a/autogpt_platform/backend/poetry.lock
+++ b/autogpt_platform/backend/poetry.lock
@@ -1924,14 +1924,14 @@ google = ["google-api-python-client (>=2.0.0)", "google-auth (>=2.0.0)"]
[[package]]
name = "gravitasml"
-version = "0.1.3"
+version = "0.1.4"
description = ""
optional = false
python-versions = "<4.0,>=3.10"
groups = ["main"]
files = [
- {file = "gravitasml-0.1.3-py3-none-any.whl", hash = "sha256:51ff98b4564b7a61f7796f18d5f2558b919d30b3722579296089645b7bc18b85"},
- {file = "gravitasml-0.1.3.tar.gz", hash = "sha256:04d240b9fa35878252d57a36032130b6516487468847fcdced1022c032a20f57"},
+ {file = "gravitasml-0.1.4-py3-none-any.whl", hash = "sha256:671a18b11d3d8a0e270c6a80c72cd058458b18d5ef7560d00010e962ab1bca74"},
+ {file = "gravitasml-0.1.4.tar.gz", hash = "sha256:35d0d9fec7431817482d53d9c976e375557c3e041d1eb6928e809324a8c866e3"},
]
[package.dependencies]
@@ -7295,4 +7295,4 @@ cffi = ["cffi (>=1.11)"]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
-content-hash = "b762806d5d58fcf811220890c4705a16dc62b33387af43e3a29399c62a641098"
+content-hash = "a93ba0cea3b465cb6ec3e3f258b383b09f84ea352ccfdbfa112902cde5653fc6"
diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.toml
index 1f13bd6897..e8b8fd0ba5 100644
--- a/autogpt_platform/backend/pyproject.toml
+++ b/autogpt_platform/backend/pyproject.toml
@@ -27,7 +27,7 @@ google-api-python-client = "^2.177.0"
google-auth-oauthlib = "^1.2.2"
google-cloud-storage = "^3.2.0"
googlemaps = "^4.10.0"
-gravitasml = "^0.1.3"
+gravitasml = "^0.1.4"
groq = "^0.30.0"
html2text = "^2024.2.26"
jinja2 = "^3.1.6"