Building AI agents, atomically
Reject backslashes before the path in result URLs; cover IPv6 literals; guard connect_ex in tests
Juampiab23d310cbc9
diff --git a/atomic-forge/tools/firecrawl_search/tests/test_firecrawl_search.py b/atomic-forge/tools/firecrawl_search/tests/test_firecrawl_search.pyindex f92463b..cd9070a 100644--- a/atomic-forge/tools/firecrawl_search/tests/test_firecrawl_search.py+++ b/atomic-forge/tools/firecrawl_search/tests/test_firecrawl_search.py@@ -1,5 +1,6 @@import asyncioimport copy+import errnoimport loggingimport osimport socket@@ -75,6 +76,7 @@ def offline(monkeypatch) -> Iterator[List[str]]:attempts: List[str] = []real_getaddrinfo = socket.getaddrinforeal_connect = socket.socket.connect+ real_connect_ex = socket.socket.connect_exdef resolve_loopback_only(host, *args, **kwargs):if host not in LOOPBACK_HOSTS:@@ -82,14 +84,23 @@ def offline(monkeypatch) -> Iterator[List[str]]:raise socket.gaierror("network access is disabled in these tests")return real_getaddrinfo(host, *args, **kwargs)- def connect_loopback_only(sock, address):+ def blocked(address) -> bool:if isinstance(address, tuple) and address[0] not in LOOPBACK_HOSTS:attempts.append(str(address[0]))+ return True+ return False++ def connect_loopback_only(sock, address):+ if blocked(address):raise OSError("network access is disabled in these tests")return real_connect(sock, address)+ def connect_ex_loopback_only(sock, address):+ return errno.ECONNREFUSED if blocked(address) else real_connect_ex(sock, address)+monkeypatch.setattr(socket, "getaddrinfo", resolve_loopback_only)monkeypatch.setattr(socket.socket, "connect", connect_loopback_only)+ monkeypatch.setattr(socket.socket, "connect_ex", connect_ex_loopback_only)yield attemptsassert attempts == []@@ -250,6 +261,7 @@ def test_to_item_drops_badly_typed_fields(field, value, attribute, expected):"https://müller.de/straße","https://xn--mller-kva.de/","https://example.com/%C3%BC%20x.pdf",+ "https://example.com/a\\b",],)def test_to_item_keeps_valid_http_urls(url):@@ -296,6 +308,10 @@ def test_to_item_keeps_valid_http_urls(url):{"url": "https://example.com:0/"},{"url": "https://example.com:\u0664\u0664\u0663/"},{"url": "https://a [email protected]/"},+ {"url": "https://[v1.x]/"},+ {"url": "https://[::1%25eth0]/"},+ {"url": "https://a.com\\@evil.com/"},+ {"url": "https://evil.com\\.example.com/"},],)def test_to_item_without_usable_url_is_none(hit):diff --git a/atomic-forge/tools/firecrawl_search/tool/firecrawl_search.py b/atomic-forge/tools/firecrawl_search/tool/firecrawl_search.pyindex 32da97e..0f9d22f 100644--- a/atomic-forge/tools/firecrawl_search/tool/firecrawl_search.py+++ b/atomic-forge/tools/firecrawl_search/tool/firecrawl_search.py@@ -113,9 +113,10 @@ class FirecrawlSearchTool(BaseTool[FirecrawlSearchToolInputSchema, FirecrawlSearKEY_PATTERN = re.compile(r"\bfc-[0-9a-f]{32}\b", re.IGNORECASE)MIN_REDACTED_KEY_LENGTH = 8# Shape of a usable result URL: http(s), optional userinfo, a host that starts with a letter or digit (or an- # IPv6 literal), an ASCII port, and no whitespace anywhere. urlsplit then validates the port range and IPv6.+ # IPv6 literal), an ASCII port, no whitespace anywhere, and no backslash before the path (parsers disagree on+ # what host "a.com\@b.com" means). urlsplit then validates the port range and the IPv6 literal.RESULT_URL_PATTERN = re.compile(- r"https?://(?:[^\s/?#@]*@)?(?:[^\W_][^\s/?#@:\[\]]*|\[[0-9a-f:.]+\])(?::[0-9]*)?(?:[/?#]\S*)?", re.IGNORECASE+ r"https?://(?:[^\s/?#@\\]*@)?(?:[^\W_][^\s/?#@:\[\]\\]*|\[[0-9a-f:.]+\])(?::[0-9]*)?(?:[/?#]\S*)?", re.IGNORECASE)def __init__(self, config: FirecrawlSearchToolConfig = FirecrawlSearchToolConfig()):