Skip to content

Commit 0d1bbeb

Browse files
fix(uri_utils): unquote percent-encoded base64 payload in data URIs (#2481)
Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
1 parent 4cc9fa1 commit 0d1bbeb

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎packages/markitdown/src/markitdown/_uri_utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ def parse_data_uri(uri: str) -> Tuple[str | None, Dict[str, str], bytes]:
7070
elif len(part) > 0:
7171
attributes[part.lower()] = ""
7272

73-
content = base64.b64decode(data) if is_base64 else unquote_to_bytes(data)
73+
content = base64.b64decode(unquote_to_bytes(data)) if is_base64 else unquote_to_bytes(data)
7474

7575
return mime_type, attributes, content

‎packages/markitdown/tests/test_module_misc.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ def test_data_uris() -> None:
191191
assert attributes["charset"] == "utf-8"
192192
assert data == b"Hello, World!"
193193

194+
# Test percent-encoded base64 payload (RFC 2397 compliance)
195+
data_uri = "data:text/plain;base64,SGVsbG8%3D"
196+
mime_type, attributes, data = parse_data_uri(data_uri)
197+
assert mime_type == "text/plain"
198+
assert len(attributes) == 0
199+
assert data == b"Hello"
200+
201+
data_uri = "data:application/octet-stream;base64,%2B/8="
202+
mime_type, attributes, data = parse_data_uri(data_uri)
203+
assert mime_type == "application/octet-stream"
204+
assert len(attributes) == 0
205+
assert data == b"\xfb\xff"
206+
194207

195208
def test_file_uris() -> None:
196209
expected_path = os.path.abspath("/path/to/file.txt")

0 commit comments

Comments
 (0)