from __future__ import annotations import ipaddress import unittest from unittest.mock import patch from core.remote_fetch import RemoteFetchError, validate_http_url from engines.image_provider import ImageProviderError from engines.single_flow import MAX_PROVIDER_IMAGE_BYTES, _image_bytes class RemoteFetchSafetyTests(unittest.TestCase): def test_rejects_non_http_and_private_targets(self): with self.assertRaises(RemoteFetchError): validate_http_url("file:///etc/passwd") with patch("core.remote_fetch._resolve_addresses", return_value={ipaddress.ip_address("127.0.0.1")}): with self.assertRaises(RemoteFetchError): validate_http_url("http://localhost/internal") with patch("core.remote_fetch._resolve_addresses", return_value={ipaddress.ip_address("100.64.0.1")}): with self.assertRaises(RemoteFetchError): validate_http_url("http://overlay.internal/image.png") with self.assertRaises(RemoteFetchError): validate_http_url("https://example.test:invalid/image.png") def test_allows_explicit_private_provider_host(self): with patch("core.remote_fetch._resolve_addresses", return_value={ipaddress.ip_address("127.0.0.1")}): validate_http_url("http://localhost/image.png", allowed_private_hosts={"localhost"}) def test_image_payload_rejects_invalid_and_oversized_base64(self): with self.assertRaises(ImageProviderError): _image_bytes("bm90LWFuLWltYWdl", None, provider_id="test") oversized = "A" * ((((MAX_PROVIDER_IMAGE_BYTES + 2) // 3) * 4) + 4) with self.assertRaises(ImageProviderError): _image_bytes(oversized, None, provider_id="test") if __name__ == "__main__": unittest.main()