Repository navigation
Description
Description
Backend media persistence classifies a path-typed media value with a mixed-case URL scheme (e.g. HTTPS://...) as raw base64 instead of a remote URL. The value then goes through base64.b64decode and is silently replaced by a garbage image file on disk (the original URL is lost), or raises binascii.Error — depending only on the URL's character count modulo 4.
This is the sibling site of the scheme case-sensitivity already fixed for the hashing/seed path in #3861 / PR #3918, which established the principle that schemes are case-insensitive per RFC 3986 §3.1 and the original spelling must be preserved.
Steps to reproduce
Using the repo's own test setup (SQLiteMemory(":memory:") + patched CentralMemory.get_memory_instance, same as tests/unit/conftest.py):
import asyncio
from unittest.mock import patch
from pyrit.backend.services.media_persistence import persist_media_value_async
from pyrit.memory import CentralMemory, SQLiteMemory
async def main():
sqlite_memory = SQLiteMemory.__new__(SQLiteMemory)
sqlite_memory.__init__(db_path=":memory:", skip_schema_migration=True)
with patch.object(CentralMemory, "get_memory_instance", return_value=sqlite_memory):
# lowercase scheme: kept as remote URL
r = await persist_media_value_async(value="https://example.com/img.png", data_type="image_path")
print(r.origin, r.persisted, r.value) # remote_url False https://example.com/img.png
# same URL, mixed-case scheme: classified as raw base64 and persisted as garbage
r = await persist_media_value_async(value="HTTPS://example.com/img.png", data_type="image_path")
print(r.origin, r.persisted, r.value) # raw_base64 True <results>/prompt-memory-entries/images/<uuid>..png
# the persisted file is 18 bytes of garbage (not a PNG, URL not recoverable)
asyncio.run(main())Root cause
pyrit/backend/services/media_persistence.py line 251:
if value.startswith(("http://", "https://")):The check is case-sensitive, so HTTPS://example.com/img.png falls through to the raw-base64 branch. There, base64.b64decode (called with validate=False in pyrit/memory/storage/serializers.py) strips the non-alphabet characters of the URL and decodes the remaining letters:
- 24 remaining b64 characters (
% 4 == 0) → decodes to 18 bytes of garbage that are written as a.pngfile, and the URL is silently replaced by that file path (this also violates the documented contract in thepersist_message_pieces_asyncdocstring: "If the value is already an HTTP(S) URL ... it is kept"); - 18 remaining characters (
% 4 == 2) →binascii.Error: Incorrect padding, the request crashes.
The lowercase-scheme control shows the same input class is otherwise passed through untouched, so the scheme spelling is the only variable.
Reachable from
- attack/chat ingestion via
persist_message_pieces_async(pyrit/backend/services/attack_service.py,pyrit/backend/services/message_send_service.py) —piece.original_valuefor*_pathdata types gets replaced by the garbage file path; - converter preview (
pyrit/backend/services/converter_service.py).
The frontend's normal flow (staged uploads) and server-generated Azure blob URLs (always lowercase) are not affected; the trigger is external API input, pasted values, or remixed/copied messages carrying a mixed-case scheme.
Expected behavior
HTTPS://example.com/img.png should be classified as a remote URL and kept verbatim (same as the lowercase control), consistent with #3861/PR #3918.
Disclosure: this issue was found and verified with AI assistance; the reproducer above runs as-is against main.
Activity