Repository navigation
FIX: treat mixed-case URL schemes as remote URLs in backend media persistence - #4183
Roman Lutz (romanlutz) merged 4 commits into
Conversation
…sistence A path-typed media value like 'HTTPS://example.com/img.png' fell through the case-sensitive http(s) prefix check in persist_media_value_async and was classified as raw base64: the URL was then silently replaced by a garbage image file (or raised binascii.Error, depending on the URL length modulo 4). The prefix check now lowercases the scheme, and the original spelling is preserved, consistent with microsoft#3861 / PR microsoft#3918.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the mixed-case HTTP(S) media URL handling change.
No open issues 🎉
What changed in this PR
Fixes #4182 by recognizing mixed-case HTTP(S) schemes in backend media persistence, consistent with the earlier serializer fix in #3918. Remote URLs retain their original spelling instead of being treated as raw base64.
Changes:
- Makes the HTTP(S) prefix check case-insensitive.
- Adds regression cases for
HTTPS://andHttp://, checking remote-URL classification, no persistence, and unchanged spelling.
| File | Description |
|---|---|
tests/unit/backend/test_media_persistence.py |
Adds mixed-case URL regression cases. |
pyrit/backend/services/media_persistence.py |
Recognizes HTTP(S) schemes case-insensitively. |
Review effort: Balanced
|
@microsoft-github-policy-service agree |
RFC 2397's data scheme is case-insensitive like http(s). Previously a DATA:image/png;base64,... value fell through to the raw-base64 branch and the whole URI (header included) was passed to b64decode, corrupting the persisted file or raising binascii.Error - the same failure class this PR fixes for http(s). Also lowercase the scheme pass-through check in _resolve_media_url for consistency with the rest of the backend. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
since HTTPS:// urls now pass through untouched, - if value and not value.startswith("data:"):
- source = urlparse(value).path if value.startswith("http") else value
+ if value and not value.lower().startswith("data:"):
+ source = urlparse(value).path if value.lower().startswith(("http://", "https://")) else valuefixed this plus a test in this commit, you can just cherry pick it instead of redoing it: its-amann@56481e2 |
Description
Backend media persistence classified a path-typed media value with a mixed-case URL scheme (e.g.
HTTPS://example.com/img.png) as raw base64 instead of a remote URL, because the prefix check inpersist_media_value_asyncwas case-sensitive. Downstream,base64.b64decodethen either silently replaced the URL with a garbage image file (URL length% 4 == 0) or raisedbinascii.Error(% 4 == 2), reachable from attack/chat ingestion and converter preview.http(s)prefix check; the original spelling is preserved verbatim on the remote-URL path, consistent with the scheme case-insensitivity principle from URLDataTypeSerializer treats mixed-case HTTP(S) schemes as local paths #3861 / PR fix: URLDataTypeSerializer recognizes mixed-case HTTP(S) schemes #3918.Tests
test_existing_references_are_not_persisted_asyncwithHTTPS://andHttp://rows, assertingREMOTE_URLorigin, no persistence, and original spelling preserved.tests/unit/backend/full suite: 4023 passed, 4 skipped; the 3 pre-existing failures (uvicorn lifespan/auth, reproduced on a clean tree before this change) are unrelated to this PR.SQLiteMemory(":memory:")+ patchedCentralMemory): mixed-case URL now returnsorigin=remote_url, persisted=Falseand survivespersist_message_pieces_asyncunchanged.Disclosure: this PR was prepared with AI assistance; the reproducer, root-cause analysis and tests are included for direct verification.