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.
Merge commit from fork
Nicholas Tindleae7444de02c8
diff --git a/autogpt_platform/backend/backend/blocks/xml_parser.py b/autogpt_platform/backend/backend/blocks/xml_parser.pyindex 63119de2e8..415aa7d7cb 100644--- a/autogpt_platform/backend/backend/blocks/xml_parser.py+++ b/autogpt_platform/backend/backend/blocks/xml_parser.py@@ -10,6 +10,10 @@ from backend.blocks._base import ()from backend.data.model import SchemaField+MAX_XML_SIZE = 10 * 1024 * 1024+MAX_XML_TAG_LENGTH = 4_096+MAX_XML_DEPTH = 256+class XMLParserBlock(Block):class Input(BlockSchemaInput):@@ -64,16 +68,65 @@ class XMLParserBlock(Block):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+ @staticmethod+ def _validate_tokenizer_input(xml: str) -> None:+ """Reject malformed or deeply nested tags in one linear pass.++ gravitasml's tokenizer searches to the end of the remaining input for+ every unmatched ``<``. Ensuring every opener has one nearby closing+ ``>`` prevents that quadratic path before the tokenizer runs.+ """+ position = 0+ depth = 0+ while True:+ tag_start = xml.find("<", position)+ if tag_start == -1:+ return++ if xml.startswith("<!--", tag_start):+ comment_end = xml.find("-->", tag_start + 4)+ if comment_end == -1:+ raise ValueError("Unclosed XML comment.")+ position = comment_end + 3+ continue++ tag_end = xml.find(">", tag_start + 1)+ if tag_end == -1:+ raise ValueError("Unclosed tag delimiter in XML input.")+ if xml.find("<", tag_start + 1, tag_end) != -1:+ raise ValueError("Nested '<' delimiter in XML tag.")++ tag = xml[tag_start + 1 : tag_end].strip()+ if not tag:+ raise ValueError("Empty XML tag.")+ if len(tag) > MAX_XML_TAG_LENGTH:+ raise ValueError(+ f"XML tag exceeds the {MAX_XML_TAG_LENGTH} character limit."+ )+ if tag.startswith(("!", "?")):+ raise ValueError("XML declarations and directives are not supported.")++ if tag.startswith("/"):+ depth -= 1+ if depth < 0:+ raise ValueError("Unexpected closing tag in XML input.")+ elif not tag.endswith("/"):+ depth += 1+ if depth > MAX_XML_DEPTH:+ raise ValueError(+ f"XML nesting exceeds the maximum depth of {MAX_XML_DEPTH}."+ )++ position = tag_end + 1+ async def run(self, input_data: Input, **kwargs) -> BlockOutput:if len(input_data.input_xml) > MAX_XML_SIZE:raise ValueError(f"XML too large: {len(input_data.input_xml)} bytes > {MAX_XML_SIZE} bytes")try:+ self._validate_tokenizer_input(input_data.input_xml)tokens = list(tokenize(input_data.input_xml))self._validate_tokens(tokens)@@ -87,3 +140,7 @@ class XMLParserBlock(Block):# BlockExecutionError (expected user-caused failure) instead of# BlockUnknownError (unexpected platform error that alerts Sentry).raise ValueError(f"Error in input xml syntax: {syn_e}") from syn_e+ except RecursionError as recursion_error:+ raise ValueError(+ f"XML nesting exceeds the supported parser depth of {MAX_XML_DEPTH}."+ ) from recursion_errordiff --git a/autogpt_platform/backend/backend/blocks/xml_parser_test.py b/autogpt_platform/backend/backend/blocks/xml_parser_test.pynew file mode 100644index 0000000000..178da0b9ef--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/xml_parser_test.py@@ -0,0 +1,74 @@+from unittest.mock import Mock++import pytest++from backend.blocks.xml_parser import MAX_XML_DEPTH, XMLParserBlock+++def test_tokenizer_precheck_rejects_repeated_open_delimiters():+ with pytest.raises(ValueError, match="tag delimiter"):+ XMLParserBlock._validate_tokenizer_input("<" * 40_000)+++def test_tokenizer_precheck_rejects_excessive_nesting():+ xml = "<a>" * (MAX_XML_DEPTH + 1) + "x" + "</a>" * (MAX_XML_DEPTH + 1)++ with pytest.raises(ValueError, match="maximum depth"):+ XMLParserBlock._validate_tokenizer_input(xml)+++def test_tokenizer_precheck_accepts_supported_xml_shape():+ XMLParserBlock._validate_tokenizer_input(+ '<root><item id="one">value</item><empty/></root>'+ )+++def test_tokenizer_precheck_accepts_comments_with_tag_delimiters():+ XMLParserBlock._validate_tokenizer_input(+ "<root><!-- ignored <tag> text --><item>value</item></root>"+ )+++def test_tokenizer_precheck_rejects_unclosed_comment():+ with pytest.raises(ValueError, match="Unclosed XML comment"):+ XMLParserBlock._validate_tokenizer_input("<root><!-- unfinished</root>")+++async def test_run_rejects_pathological_input_before_tokenizing(mocker):+ block = XMLParserBlock()+ tokenize_mock = mocker.patch(+ "backend.blocks.xml_parser.tokenize",+ Mock(side_effect=AssertionError("tokenizer must not run")),+ )+ input_data = block.Input(input_xml="<" * 40_000)++ with pytest.raises(ValueError, match="tag delimiter"):+ _ = [item async for item in block.run(input_data)]++ tokenize_mock.assert_not_called()+++async def test_run_preserves_supported_xml_comments():+ block = XMLParserBlock()+ input_data = block.Input(+ input_xml="<root><!-- ignored <tag> text --><item>value</item></root>"+ )++ outputs = [item async for item in block.run(input_data)]++ assert outputs == [("parsed_xml", {"root": {"item": "value"}})]+++async def test_run_maps_parser_recursion_to_input_error(mocker):+ block = XMLParserBlock()+ mocker.patch(+ "backend.blocks.xml_parser.tokenize",+ return_value=[],+ )+ mocker.patch.object(block, "_validate_tokens", side_effect=RecursionError)++ with pytest.raises(ValueError, match="supported parser depth"):+ _ = [item async for item in block.run(block.Input(input_xml="<a></a>"))]