fix(core): harden fetchImage data URLs + base64 padding test coverage - #576
Conversation
Embed only the bare media type in the data URL so responses like `image/svg+xml; charset=utf-8` don't produce a malformed `data:image/svg+xml; charset=utf-8;base64,...` that strict parsers reject. Also add test coverage for the base64 one-/two-byte padding branches (previously every fetchImage test used a byte length divisible by 3, so the `=`/`==` padding was never exercised). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
|
Claude finished @ignaciosantise's task in 2m 23s —— View job Code Review
🔄 Incremental Review: First review of this PR. Found 2 issue(s)Issue 1: Misleading test description for double-char (
|
There was a problem hiding this comment.
Pull request overview
This PR hardens FetchUtil.fetchImage data URL generation in @reown/appkit-core-react-native by normalizing content-type headers (removing semicolon-delimited parameters) and extends unit tests to cover base64 padding branches that weren’t previously exercised.
Changes:
- Strip
content-typeparameters (e.g.,; charset=utf-8) before buildingdata:<type>;base64,...URLs infetchImage. - Add unit tests to cover
_arrayBufferToBase64padding for 1-byte (==) and 2-byte (=) remainders, plus a regression test forcontent-typeparameter stripping. - Add a changeset to publish patch releases across the affected packages.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/core/src/utils/FetchUtil.ts | Sanitizes content-type before embedding it in a data URL returned by fetchImage. |
| packages/core/src/tests/utils/FetchUtil.test.ts | Adds test coverage for base64 padding and content-type parameter stripping behavior. |
| .changeset/fix-fetch-image-base64-padding.md | Declares patch bumps and documents the behavior change and new test coverage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Correct padding test name ("double-char" for the one-byte remainder `==` case)
- Rewrite content-type comment so the example data URL is accurate
- Drop the changeset
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Addresses the two findings from the Claude review on #575.
Issue 1 — base64 padding untested (MEDIUM, test_coverage)
Every existing
fetchImagetest used a byte length divisible by 3 (6and3bytes), so the=/==padding branches in_arrayBufferToBase64were never executed — despite nearly all real images having byte counts not divisible by 3. Added two cases:[0x66]("f") →Zg==[0x66, 0x6f]("fo") →Zm8=Issue 2 — content-type params leaked into data URL (LOW, correctness)
fetchImageembedded the rawcontent-typeheader, soimage/svg+xml; charset=utf-8produced a malformeddata:image/svg+xml; charset=utf-8;base64,...that strict parsers may reject. Now strips at the first;before building the data URL, with a regression test.Testing
FetchUtil.test.ts: 21 passing (3 new)🤖 Generated with Claude Code