Skip to content

fix(ts-sdk): decode base64 data URLs with media-type parameters - #247

Open
rohitsux wants to merge 2 commits into
superlinked:mainfrom
rohitsux:fix/data-url-media-type-params
Open

fix(ts-sdk): decode base64 data URLs with media-type parameters#247
rohitsux wants to merge 2 commits into
superlinked:mainfrom
rohitsux:fix/data-url-media-type-params

Conversation

@rohitsux

@rohitsux rohitsux commented Aug 31, 2026

Copy link
Copy Markdown

Problem

toImageBytes() in packages/sie_ts_sdk/src/images.ts detects base64 data URLs with the regex:

/^data:[^;]+;base64,(.+)$/

This requires the media type segment to contain no ;. Per RFC 2397, though, the media type may carry parameters (e.g. ;charset=utf-8) or be omitted entirely. Data URLs like:

  • data:image/svg+xml;charset=utf-8;base64,...
  • data:;base64,...

don't match this pattern and fall through to the plain base64 branch, which hands the entire data URL string (including the data:...;base64, prefix) to the base64 decoder. That decoder then either throws InvalidCharacterError under atob (browser) or silently produces corrupted bytes under Buffer.from (Node) — neither of which surfaces as a clear "unsupported input" error.

Fix

Match up to the ;base64, marker instead of requiring a ;-free segment:

/^data:[^,]*;base64,(.+)$/

Base64 payloads never contain a comma, so the capture group still correctly isolates just the payload. Behavior for all previously-matching data URLs is unchanged.

Tests

Added two regression tests to packages/sie_ts_sdk/tests/images.test.ts:

  • a data URL whose media type carries a parameter (image/svg+xml;charset=utf-8)
  • a data URL with an omitted media type (data:;base64,...)

Verified locally: fails-before (both new tests throw on the unpatched regex) / passes-after. tests/images.test.ts 17/17; full @superlinked/sie-sdk suite 484/484; biome check clean; tsc --noEmit (typecheck) clean.

Summary by CodeRabbit

  • Bug Fixes

    • Improved image decoding for base64 data URLs that include media type parameters or omit the media type.
    • Prevented valid data URLs from being incorrectly passed to the decoder as raw URL text.
  • Tests

    • Added coverage for parameterized and media-type-free data URLs.

toImageBytes()'s regex ^data:[^;]+;base64, requires a ;-free media
type, so valid RFC 2397 data URLs with a media-type parameter (e.g.
data:image/svg+xml;charset=utf-8;base64,...) or an omitted media type
(data:;base64,...) don't match and fall through, handing the entire
data URL to the base64 decoder. That decoder then throws
InvalidCharacterError under atob (browser) or silently corrupts bytes
under Buffer.from (Node).

Fix matches up to the ;base64, marker ([^,]*) instead. Base64 payloads
never contain a comma, so the payload is still captured correctly. No
change in behavior for existing inputs.

Added two regression tests covering a media type with a parameter and
an omitted media type.
@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 241c62a7-3c9b-4da5-b952-30bb73ac7553

📥 Commits

Reviewing files that changed from the base of the PR and between 9a7e8d3 and 9de9d6c.

📒 Files selected for processing (2)
  • packages/sie_ts_sdk/src/images.ts
  • packages/sie_ts_sdk/tests/images.test.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Changes

Image data URL decoding

Layer / File(s) Summary
Data URL detection and validation
packages/sie_ts_sdk/src/images.ts, packages/sie_ts_sdk/tests/images.test.ts
toImageBytes now recognizes base64 data URLs with media-type parameters or no media type. Tests verify that both formats decode to Uint8Array bytes representing “Hello”.

Merge Risk: ⚪ Minimal · up to 7330e

This localized change correctly handles base64 data URLs with media-type parameters or omitted media types, with regression coverage and passing checks. No actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: decoding base64 data URLs that contain media-type parameters.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@mamayer19
mamayer19 requested a review from a team as a code owner September 7, 2026 08:06

@mamayer19 mamayer19 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.

Thanks for the contribution, this reproduces the original issue clearly, and both new cases pass locally. I left one suggestion about parsing the data URL structurally instead of broadening the regex, along with a few related edge cases that would make the behavior explicit.

// everything up to the ";base64," marker rather than a single ";"-free
// segment — otherwise such URLs fall through and the whole data URL is
// handed to the base64 decoder (corrupting the bytes or throwing).
const dataUrlMatch = input.match(/^data:[^,]*;base64,(.+)$/);

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.

Could we avoid encoding the data-URL grammar in a regex here? Since the format has a well-defined comma delimiter, a small helper could find the first comma and inspect the semicolon-delimited metadata before it for the final base64 marker. That would be easier to follow and give us a natural place to handle the scheme and marker case-insensitively, percent-decode the payload, and return a clear error for malformed data: input.

As written, inputs such as DATA:image/png;BASE64,SGVsbG8%3D still miss the match and are passed to the raw-base64 decoder. Would you be open to extracting something like parseBase64DataUrl(input): string | undefined, leaving base64ToBytes responsible only for decoding the extracted payload?

it("decodes a data URL whose media type carries a parameter", async () => {
// Valid per RFC 2397: the media type may be followed by ";param=value"
// (e.g. charset) before ";base64,". "Hello" base64-encoded.
const dataUrl = "data:image/svg+xml;charset=utf-8;base64,SGVsbG8=";

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.

These two regression cases are a good start. If we switch to a small parser, could we also cover an uppercase scheme/BASE64 marker, percent-escaped padding such as %3D, and one explicit empty-payload case? Those are the boundaries where the current regex falls through into the raw-base64 path, and the tests would make the intended behavior clear.

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