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 Tindle1bb81588994b
diff --git a/autogpt_platform/backend/backend/blocks/text.py b/autogpt_platform/backend/backend/blocks/text.pyindex b77a8e151d..eaf82e3b39 100644--- a/autogpt_platform/backend/backend/blocks/text.py+++ b/autogpt_platform/backend/backend/blocks/text.py@@ -20,6 +20,10 @@ from backend.util.type import MediaFileTypeformatter = text.TextFormatter()+MAX_REGEX_PATTERN_LENGTH = 10_000+MAX_REGEX_TEXT_LENGTH = 1_000_000+REGEX_TIMEOUT_SECONDS = 1.0+class MatchTextPatternBlock(Block):class Input(BlockSchemaInput):@@ -70,7 +74,29 @@ class MatchTextPatternBlock(Block):else:text = json.dumps(input_data.text)- if re.search(input_data.match, text, flags=flags):+ if len(input_data.match) > MAX_REGEX_PATTERN_LENGTH:+ raise ValueError(+ f"Regex pattern is too large to evaluate safely "+ f"({len(input_data.match)} characters; "+ f"maximum {MAX_REGEX_PATTERN_LENGTH})."+ )++ if len(text) > MAX_REGEX_TEXT_LENGTH:+ raise ValueError(+ f"Text is too large to match safely ({len(text)} characters; "+ f"maximum {MAX_REGEX_TEXT_LENGTH})."+ )++ try:+ matched = regex.search(+ input_data.match, text, flags=flags, timeout=REGEX_TIMEOUT_SECONDS+ )+ except TimeoutError as error:+ raise ValueError("Regex evaluation timed out.") from error+ except regex.error as error:+ raise ValueError(f"Invalid regex pattern: {error}") from error++ if matched:yield "positive", outputelse:yield "negative", outputdiff --git a/autogpt_platform/backend/backend/blocks/text_security_test.py b/autogpt_platform/backend/backend/blocks/text_security_test.pynew file mode 100644index 0000000000..60d421c387--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/text_security_test.py@@ -0,0 +1,111 @@+from unittest.mock import MagicMock++import pytest++from backend.blocks.text import (+ MAX_REGEX_PATTERN_LENGTH,+ MAX_REGEX_TEXT_LENGTH,+ REGEX_TIMEOUT_SECONDS,+ MatchTextPatternBlock,+)+++async def _collect_outputs(block, input_data):+ return [item async for item in block.run(input_data)]+++async def test_match_text_pattern_uses_timeout_for_every_pattern(mocker):+ block = MatchTextPatternBlock()+ search = mocker.patch(+ "backend.blocks.text.regex.search",+ return_value=MagicMock(),+ )+ input_data = block.Input(+ text="ordinary text",+ match="ordinary",+ data="matched",+ case_sensitive=True,+ dot_all=True,+ )++ outputs = await _collect_outputs(block, input_data)++ assert outputs == [("positive", "matched")]+ search.assert_called_once_with(+ "ordinary",+ "ordinary text",+ flags=mocker.ANY,+ timeout=REGEX_TIMEOUT_SECONDS,+ )+++async def test_match_text_pattern_surfaces_regex_timeout_as_input_error(mocker):+ block = MatchTextPatternBlock()+ mocker.patch(+ "backend.blocks.text.regex.search",+ side_effect=TimeoutError,+ )+ input_data = block.Input(+ text="a" * 40 + "!",+ match=r"(a+)+$",+ data="matched",+ case_sensitive=True,+ dot_all=True,+ )++ with pytest.raises(ValueError, match="timed out"):+ await _collect_outputs(block, input_data)+++async def test_match_text_pattern_times_out_catastrophic_regex(mocker):+ mocker.patch("backend.blocks.text.REGEX_TIMEOUT_SECONDS", 0.01)+ block = MatchTextPatternBlock()+ input_data = block.Input(+ text="a" * 1_000 + "!",+ match=r"^(a|aa)+$",+ data="matched",+ case_sensitive=True,+ dot_all=True,+ )++ with pytest.raises(ValueError, match="timed out"):+ await _collect_outputs(block, input_data)+++async def test_match_text_pattern_rejects_oversized_pattern_before_search(mocker):+ block = MatchTextPatternBlock()+ search = mocker.patch("backend.blocks.text.regex.search")+ input_data = block.Input(+ text="ordinary text",+ match="a" * (MAX_REGEX_PATTERN_LENGTH + 1),+ data="matched",+ case_sensitive=True,+ dot_all=True,+ )++ with pytest.raises(ValueError, match="pattern is too large"):+ await _collect_outputs(block, input_data)++ search.assert_not_called()+++async def test_match_text_pattern_rejects_oversized_text_before_search(mocker):+ block = MatchTextPatternBlock()+ search = mocker.patch("backend.blocks.text.regex.search")+ input_data = block.Input(+ text="a" * (MAX_REGEX_TEXT_LENGTH + 1),+ match="a",+ data="matched",+ case_sensitive=True,+ dot_all=True,+ )++ with pytest.raises(ValueError, match="too large"):+ await _collect_outputs(block, input_data)++ search.assert_not_called()