From 0d53e8dc0842a1bd21dd72193ba232780daeb0c4 Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Tue, 3 Nov 2026 22:48:39 -0800 Subject: [PATCH 1/2] fix: URLDataTypeSerializer recognizes mixed-case HTTP(S) schemes --- pyrit/memory/storage/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrit/memory/storage/serializers.py b/pyrit/memory/storage/serializers.py index 44b2a04f7f..baec2d6c58 100644 --- a/pyrit/memory/storage/serializers.py +++ b/pyrit/memory/storage/serializers.py @@ -491,7 +491,7 @@ def __init__(self, *, category: str, prompt_text: str, extension: str | None = N self.value = prompt_text self.data_sub_directory = f"/{category}/urls" self.file_extension = extension if extension else "txt" - self.on_disk = not (prompt_text.startswith(("http://", "https://"))) + self.on_disk = not (prompt_text.lower().startswith(("http://", "https://"))) def data_on_disk(self) -> bool: """ From 6470dc398c611c83a7b4f987a9e045d2cf943acd Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Thu, 5 Nov 2026 08:32:23 +0000 Subject: [PATCH 2/2] test: add regression coverage for mixed-case URL schemes (#3861) Addresses review feedback from romanlutz on #3918: cover uppercase and mixed-case http/https URL schemes, asserting data_on_disk() is false, the original spelling is preserved, and get_sha256_async() hashes the original UTF-8 bytes without accessing storage; plus an end-to-end add_seeds_to_memory_async() case with a real SeedPrompt and the isolated SQLite fixture, retaining a local-path case. Signed-off-by: Akhil Gupta --- .../test_interface_seed_prompts.py | 31 +++++++++++++++ tests/unit/memory/storage/test_serializers.py | 39 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/tests/unit/memory/memory_interface/test_interface_seed_prompts.py b/tests/unit/memory/memory_interface/test_interface_seed_prompts.py index 84e3bea4d1..1e97bbbc2d 100644 --- a/tests/unit/memory/memory_interface/test_interface_seed_prompts.py +++ b/tests/unit/memory/memory_interface/test_interface_seed_prompts.py @@ -469,6 +469,37 @@ async def test_get_seeds_with_dataset_name_pattern_startswith(sqlite_instance: M assert all(seed.dataset_name.startswith("harm") for seed in result) +async def test_add_seeds_mixed_case_url_schemes_hash_original_value( + sqlite_instance: MemoryInterface, tmp_path: Path +) -> None: + """Regression test for #3861: mixed-case URL schemes hash the original spelling. + + Before the fix, a mixed-case scheme was misclassified as a local file path and + ingestion raised FileNotFoundError; a future fix must not lowercase the stored + URL or its hash input either. No URL is fetched. + """ + urls = [ + "HTTPS://example.invalid/é?q=☃", + "HtTp://example.invalid/é?q=☃", + "https://example.invalid/é?q=☃", + ] + local_file = tmp_path / "seed.txt" + local_file.write_bytes(b"local-seed-content") + + seeds = [SeedPrompt(value=url, dataset_name="dataset", data_type="url") for url in urls] + seeds.append(SeedPrompt(value=str(local_file), dataset_name="dataset", data_type="url")) + + await sqlite_instance.add_seeds_to_memory_async(seeds=seeds, added_by="test") + + stored = await sqlite_instance.get_seeds_async(dataset_name="dataset") + assert len(stored) == 4 + by_value = {seed.value: seed for seed in stored} + for url in urls: + assert by_value[url].value == url + assert by_value[url].value_sha256 == sha256(url.encode("utf-8")).hexdigest() + assert by_value[str(local_file)].value_sha256 == sha256(b"local-seed-content").hexdigest() + + async def test_get_seeds_with_dataset_name_pattern_contains(sqlite_instance: MemoryInterface): seed_prompts = [ SeedPrompt(value="prompt1", dataset_name="test_harm_dataset", data_type="text"), diff --git a/tests/unit/memory/storage/test_serializers.py b/tests/unit/memory/storage/test_serializers.py index 8bed858284..7cfcb56e44 100644 --- a/tests/unit/memory/storage/test_serializers.py +++ b/tests/unit/memory/storage/test_serializers.py @@ -276,6 +276,45 @@ async def test_get_sha256_from_text(sqlite_instance): assert sha256_hash == expected_hash +@pytest.mark.parametrize( + "url", + [ + "HTTPS://example.invalid/é?q=☃", + "HtTp://example.invalid/é?q=☃", + "HTTP://example.invalid/é?q=☃", + "https://example.invalid/é?q=☃", + ], +) +async def test_url_serializer_mixed_case_scheme_is_not_on_disk(sqlite_instance, url: str) -> None: + """Regression test for #3861: mixed-case http(s) schemes are URLs, not local files.""" + serializer = data_serializer_factory(category="seed-prompt-entries", data_type="url", value=url) + + assert serializer.data_on_disk() is False + assert serializer.value == url + + # Hashing must use the original UTF-8 bytes without touching storage: any + # storage access raises, so a misclassified URL would fail here instead of + # raising FileNotFoundError or hashing a lowercased value. + with patch.object(serializer, "_get_storage_io", side_effect=AssertionError("storage must not be accessed")): + sha256_hash = await serializer.get_sha256_async() + + assert sha256_hash == hashlib.sha256(url.encode("utf-8")).hexdigest() + + +async def test_url_serializer_local_path_is_on_disk(sqlite_instance, tmp_path) -> None: + """A non-URL value keeps on-disk behavior: hashing a missing file raises.""" + local_path = tmp_path / "seed.txt" + serializer = data_serializer_factory(category="seed-prompt-entries", data_type="url", value=str(local_path)) + + assert serializer.data_on_disk() is True + assert serializer.value == str(local_path) + with pytest.raises(FileNotFoundError): + await serializer.get_sha256_async() + + local_path.write_bytes(b"seed-content") + assert await serializer.get_sha256_async() == hashlib.sha256(b"seed-content").hexdigest() + + async def test_get_sha256_from_image_file(sqlite_instance): """Test SHA256 hash calculation for file data.""" data = b"file_content.png"