Skip to content

Fix data URI fragments being decoded as payload - #2775

Open
Huzaifa Asim (huzaifaasim017) wants to merge 2 commits into
microsoft:mainfrom
huzaifaasim017:fix/data-uri-fragments
Open

Huzaifa Asim (huzaifaasim017) wants to merge 2 commits into
microsoft:mainfrom
huzaifaasim017:fix/data-uri-fragments

Conversation

@huzaifaasim017

Copy link
Copy Markdown

Summary

  • Strip raw URI fragments before decoding data URI content, preventing extra text or bytes from entering the converted document.
  • Add regression coverage for plain text, base64, escaped #, and ? payloads through both the parser and public conversion API.

Fixes #2690. Thanks to Bicheng (@BichengWang) for the detailed reproduction and expected behavior.

Verification

  • pytest -q packages/markitdown/tests/test_module_misc.py -k data_uri: 5 passed.
  • Full test_module_misc.py: 162 passed, 11 skipped, 2 unrelated failures in this minimal environment (audio transcription and optional PPTX dependency).

Developed with AI assistance; I reviewed the change and ran the tests above.

@huzaifaasim017

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

San-Hsien (SanHsien) added a commit to SanHsien/markitdown that referenced this pull request Oct 26, 2026
…R watermark

Ten new upstream PRs, none adopted. Four are real fixes (xlsx error
values, ipynb string source, RSS 1.0, data URI fragment) whose defects
reproduce on this fork but are outside the Windows/CJK exception class;
six are noise or re-submissions of already-adopted microsoft#1913. Reasons and
revisit triggers are in docs/DECISIONS.md. PR watermark 2765 -> 2775.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this on Python 3.13.3 (Windows). PR head d392515 sits on current main 7482c09. The only caller of parse_data_uri in src is MarkItDown.convert_uri.

Both repros from #2690 are fixed. On main, convert_uri("data:text/plain,Hello#section") gives 'Hello#section' and the base64 one with #AAAA gives 'Hello!\x00\x00\x00'. On the PR they give 'Hello' and 'Hello!'. The controls Hello%23section (still Hello#section) and Hello?section (unchanged) behave as the issue asks.

Tests: tests/test_module_misc.py on the PR head is 173 passed, 1 skipped, 1 failed. The one failure is test_speech_transcription, and it fails the same way on main (169 passed, 1 skipped, 1 failed there). I copied the PR's test file onto main: the two fragment cases fail there and the escaped-hash and query cases pass, so the new test does separate fixed from broken.

Behavior changes to be aware of. I think both are correct under the WHATWG rule the issue cites, so I do not count them as blockers:

  • A raw # inside a text payload now cuts the payload. For example data:image/svg+xml;utf8,<svg fill='#fff'/> now decodes to <svg fill=' (main kept the whole string). A caller has to write %23. A short note in the docstring would help.
  • A # in the header part, like data:text/plain;name=a#b,Hello, now raises ValueError: Malformed data URI, missing ',' separator. Main returned Hello.

Not run: I did not check the live HTML path where an <img src="data:..."> is converted, because parse_data_uri is not called there.

@huzaifaasim017

Copy link
Copy Markdown
Author

PRABHU KIRAN VANDRANKI (@VANDRANKI) Thanks for checking the repros and calling out the SVG/header edge cases. I pushed dc3aa0a with a parse_data_uri docstring explaining that a literal # starts a fragment and %23 preserves a payload hash. I also added regression tests for the SVG payload and a fragment before the header separator.

Local verification: pytest -q packages/markitdown/tests/test_module_misc.py -k data_uri → 7 passed. The full module run gave 164 passed, 11 skipped, and the same two environment-related failures noted in the PR description (speech transcription and missing optional PPTX dependency). The PR checks still await maintainer workflow approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data URI fragments are decoded as payload, appending text or corrupting bytes

2 participants