Skip to content

fix(doc): capture line-wrapped base64 in clipboard data URI regex#586

Merged
fangshuyu-768 merged 1 commit intofeat/media-insert-clipboardfrom
fix/clipboard-base64-line-wrap
Apr 21, 2026
Merged

fix(doc): capture line-wrapped base64 in clipboard data URI regex#586
fangshuyu-768 merged 1 commit intofeat/media-insert-clipboardfrom
fix/clipboard-base64-line-wrap

Conversation

@fangshuyu-768
Copy link
Copy Markdown
Collaborator

Summary

Targeted fix for PR #508 addressing the review thread at #discussion_r3116012669: the strings.Fields normalisation added in adac5bf is a no-op, and line-wrapped base64 payloads are silently uploaded as corrupt 8-byte "PNGs".

Root cause

reBase64DataURI has character class [A-Za-z0-9+/\-_]+=*, which excludes whitespace. For a 76-char-folded HTML payload — standard MIME base64 folding, very common in real clipboards (Chrome, Safari, Feishu copy-image-as-HTML) — the regex stops at the first \n, so m[2] is already truncated by the time the strings.Fields strip runs. Fields then normalises a string that by construction has no whitespace, which does nothing.

Concretely, a payload like:

data:image/png;base64,iVBORw0KGgo
AAANSUhEUgAAAAE...

matches only iVBORw0KGgo (11 chars), decodes to exactly the 8-byte PNG signature 89 50 4E 47 0D 0A 1A 0A, passes hasKnownImageMagic, and ends up being uploaded as a truncated 8-byte "PNG" block. The user sees a broken image.

Fix

Extend the character class to include \s, so the existing strings.Fields strip on line 144 of clipboard.go actually has whitespace to normalise. Terminators (", <, ), ;) stay outside the class, so the match still ends at the URI boundary.

Test plan

  • go test ./shortcuts/doc/... -count=1 passes (new TestReBase64DataURI_LineWrapped added)
  • go vet ./shortcuts/doc/... clean
  • gofmt -l clean
  • The new test feeds a ≥180-byte payload folded with a mix of \n, \r\n, and \t through the regex, asserts full round-trip byte-equality after strings.Fields normalisation, and confirms the regex does not run past the "> attribute terminator (catches an over-permissive regression in the other direction).
  • Existing tests — TestReBase64DataURI_Match, TestReBase64DataURI_URLSafeMatch, TestReBase64DataURI_NoMatch, TestExtractBase64ImageFromClipboard_WithFakeOsascript — still pass.

Notes

🤖 Generated with Claude Code

HTML and RTF clipboard content commonly folds base64 payloads at
76 chars (standard MIME folding). The previous character class
[A-Za-z0-9+/\-_]+=* stopped at the first \n, so the downstream
strings.Fields normalisation was a no-op (nothing to strip) and
extractBase64ImageFromClipboard silently uploaded a truncated
payload whose 8-byte prefix happened to pass hasKnownImageMagic.

Extend the class to include \s so the Fields strip actually has
whitespace to remove before base64 decoding. Terminators (", <,
), ;) remain outside the class so the match still ends at the
URI boundary.

Add TestReBase64DataURI_LineWrapped covering \n, \r\n, and \t
folds, full round-trip byte-equality, and the terminator-boundary
invariant so any future regression trips a failing test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bf3b869b-9f83-4f6b-b607-32fe474faec9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/clipboard-base64-line-wrap

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@github-actions github-actions Bot added domain/ccm PR touches the ccm domain size/M Single-domain feat or fix with limited business impact labels Apr 21, 2026
@fangshuyu-768 fangshuyu-768 merged commit a0b5548 into feat/media-insert-clipboard Apr 21, 2026
3 checks passed
@fangshuyu-768 fangshuyu-768 deleted the fix/clipboard-base64-line-wrap branch April 21, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/ccm PR touches the ccm domain size/M Single-domain feat or fix with limited business impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant