From 2fd96cd3837d0d82fcf0eecf37ef63dd6ddd406c Mon Sep 17 00:00:00 2001 From: sean0407 <22557931+sean0407@users.noreply.github.com> Date: Sun, 11 Oct 2026 06:52:05 +0800 Subject: [PATCH] fix(data-uri): ignore fragments in payload parsing --- .../markitdown/src/markitdown/_uri_utils.py | 1 + packages/markitdown/tests/test_module_misc.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/markitdown/src/markitdown/_uri_utils.py b/packages/markitdown/src/markitdown/_uri_utils.py index 9deb8ecaba..968dd8b00d 100644 --- a/packages/markitdown/src/markitdown/_uri_utils.py +++ b/packages/markitdown/src/markitdown/_uri_utils.py @@ -43,6 +43,7 @@ def parse_data_uri(uri: str) -> Tuple[str | None, Dict[str, str], bytes]: if uri[:5].lower() != "data:": raise ValueError("Not a data URI") + uri = uri.partition("#")[0] header, _, data = uri.partition(",") if not _: raise ValueError("Malformed data URI, missing ',' separator") diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py index 4d6c9be59b..ea037a1801 100644 --- a/packages/markitdown/tests/test_module_misc.py +++ b/packages/markitdown/tests/test_module_misc.py @@ -192,6 +192,27 @@ def test_data_uris() -> None: assert data == b"Hello, World!" +@pytest.mark.parametrize( + "uri, expected", + [ + ("data:text/plain,Hello#section", "Hello"), + ("data:text/plain;base64,SGVsbG8h#AAAA", "Hello!"), + ("data:text/plain,Hello%23section", "Hello#section"), + ("data:text/plain,Hello#", "Hello"), + ("data:text/plain,Hello?query", "Hello?query"), + ("data:text/plain;base64,SGVsbG8=#section", "Hello"), + ], +) +@pytest.mark.parametrize("public_api", [False, True]) +def test_data_uri_fragments(uri: str, expected: str, public_api: bool) -> None: + if public_api: + actual = MarkItDown(enable_plugins=False).convert_uri(uri).text_content + else: + actual = parse_data_uri(uri)[2].decode("ascii") + + assert actual == expected + + def test_file_uris() -> None: expected_path = os.path.abspath("/path/to/file.txt")