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
4 changes: 2 additions & 2 deletions pyrit/backend/mappers/attack_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ def _resolve_media_url(*, value: str | None, data_type: str) -> str | None:
"""
if not value or data_type not in MEDIA_PATH_DATA_TYPES:
return None
# Already a URL or data URI — pass through
if value.startswith(("http://", "https://", "data:")):
# Already a URL or data URI — pass through (schemes are case-insensitive)
if value.lower().startswith(("http://", "https://", "data:")):
return value
# Local file path — construct a media endpoint URL
if Path(value).is_file():
Expand Down
10 changes: 6 additions & 4 deletions pyrit/backend/services/media_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ def _is_raw_base64(value: str) -> bool:


def _data_uri_parts(value: str) -> tuple[str | None, str]:
"""Return the optional MIME type and payload from a data URI."""
"""Return the optional MIME type and payload from a data URI, whatever the scheme's case."""
header, _, payload = value.partition(",")
media_type = header.removeprefix("data:").split(";", 1)[0] or None
if header.lower().startswith("data:"):
header = header[len("data:") :]
media_type = header.split(";", 1)[0] or None
return media_type, payload


Expand Down Expand Up @@ -248,7 +250,7 @@ async def persist_media_value_async(
Returns:
A typed result containing the resolved value and persistence metadata.
"""
if value.startswith(("http://", "https://")):
if value.lower().startswith(("http://", "https://")):
container_url = results_container_url()
return MediaPersistenceResult(
value=validate_blob_media_url(url=value, container_url=container_url) if container_url else value,
Expand Down Expand Up @@ -280,7 +282,7 @@ async def persist_media_value_async(
data_uri_mime_type: str | None = None
payload = value
origin = MediaOrigin.STAGED_UPLOAD if staged_upload else MediaOrigin.RAW_BASE64
if value.startswith("data:"):
if value.lower().startswith("data:"):
data_uri_mime_type, payload = _data_uri_parts(value)
origin = MediaOrigin.DATA_URI
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/backend/test_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,18 @@ def test_http_url_passes_through(self) -> None:
url = "https://acct.blob.core.windows.net/container/image.png"
assert _resolve_media_url(value=url, data_type="image_path") == url

@pytest.mark.parametrize(
"value",
[
"HTTPS://acct.blob.core.windows.net/container/image.png",
"Http://example.test/image.png",
"DATA:image/png;base64,AAAA",
],
)
def test_mixed_case_schemes_pass_through(self, value: str) -> None:
"""Schemes are case-insensitive; mixed-case URLs and data URIs are returned as-is."""
assert _resolve_media_url(value=value, data_type="image_path") == value

def test_local_file_returns_media_url(self) -> None:
"""Local file paths are converted to /api/media URLs."""
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/backend/test_media_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def _serializer(*, value: str = "/saved/media.bin") -> MagicMock:
[
("https://example.test/media.png", MediaOrigin.REMOTE_URL, "https://example.test/media.png", True),
("http://example.test/media.png", MediaOrigin.REMOTE_URL, "http://example.test/media.png", True),
# RFC 3986 schemes are case-insensitive; the original spelling must be preserved.
("HTTPS://example.test/media.png", MediaOrigin.REMOTE_URL, "HTTPS://example.test/media.png", True),
("Http://example.test/media.png", MediaOrigin.REMOTE_URL, "Http://example.test/media.png", True),
],
)
async def test_existing_references_are_not_persisted_async(
Expand Down Expand Up @@ -435,6 +438,24 @@ async def test_data_uri_mime_policy_can_preserve_data_type_extension() -> None:
assert result.extension == ".wav"


async def test_data_uri_scheme_is_case_insensitive() -> None:
# RFC 2397's "data" scheme is case-insensitive, like http(s); a mixed-case
# header must not be classified as raw base64.
serializer = _serializer(value="/saved/media.png")
factory = MagicMock(return_value=serializer)

result = await persist_media_value_async(
value="DATA:image/png;base64,UklGRg==",
data_type="image_path",
serializer_factory=factory,
)

assert result.origin is MediaOrigin.DATA_URI
assert result.mime_type == "image/png"
assert result.extension == ".png"
serializer.save_b64_image_async.assert_awaited_once_with(data="UklGRg==")


async def test_malformed_data_uri_preserves_empty_payload_behavior() -> None:
serializer = _serializer(value="/saved/media.png")

Expand Down
Loading