Skip to content

Decode any percent-encoded byte in non-base64 data URLs - #310

Merged
varun06 merged 2 commits into
modelcontextprotocol:mainfrom
akhilcb:oss/data-url-percent-decoding
Oct 24, 2026
Merged

varun06 merged 2 commits into
modelcontextprotocol:mainfrom
akhilcb:oss/data-url-percent-decoding

Conversation

@akhilcb

@akhilcb akhilcb commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

In a non-base64 data URL, Data.parseDataURL decoded %XX escapes by converting to a
String and then to UTF-8 data. That fails for bytes that aren't valid UTF-8, even though
RFC 2397 allows any byte:

data:application/octet-stream,%FF%00  →  nil   (expected: bytes 0xFF 0x00)

%XX escapes are now decoded directly into bytes, without the String round trip, so any
byte value is accepted. Malformed escapes (e.g. %fg) still return nil, as before.

How Has This Been Tested?

Adds tests for:

  • the input above;
  • the RFC 2397 example data:text/plain;charset=iso-8859-7,%be%d3%be, which also returned
    nil before this change (its bytes aren't valid UTF-8);
  • malformed escapes (%, %F, %fg) still returning nil.

Existing percent-decoding tests still pass. swift test passes.

Breaking Changes

None. Value decoding follows parseDataURL, so a JSON string like the one above now
decodes as .data, as other data-URL strings already do.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Non-base64 payloads were percent-decoded through a String round trip,
which rejected bytes that are not valid UTF-8 (e.g. %FF). Escapes are
now decoded directly into bytes, as RFC 2397 allows any octet.

@varun06 varun06 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice. we might have to follow this order for merge: #295 first, then #310, then #309 once its NetworkTransport commit is dropped

@varun06
varun06 merged commit 2a44bfd into modelcontextprotocol:main Oct 24, 2026
6 checks passed
@akhilcb
akhilcb deleted the oss/data-url-percent-decoding branch October 25, 2026 21:46
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.

2 participants