Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyrit/memory/storage/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,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://")))
Comment thread
akhil9tiet marked this conversation as resolved.
Resolved

def data_on_disk(self) -> bool:
"""
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/memory/memory_interface/test_interface_seed_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/memory/storage/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading