Skip to content

Decode UTF-16 text in clipboard history - #7249

Merged
dhh merged 1 commit into
quattrofrom
fix/clipboard-utf16-decoding
Aug 17, 2026
Merged

dhh merged 1 commit into
quattrofrom
fix/clipboard-utf16-decoding

Conversation

@dhh

@dhh dhh commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • decode BOM-tagged UTF-16LE/BE clipboard payloads
  • decode BOM-less UTF-16 only when every byte pair follows one consistent NUL-padded endian pattern
  • preserve the existing UTF-8 interpretation when encoding evidence is ambiguous or UTF-16 decoding fails
  • process clipboard text in memory without temporary files or byte-array expansion
  • cover LE/BE, BOMs, single-character ambiguity, punctuation, CJK, emoji, malformed input, and the reported payload

Testing

  • bash test/shell.d/clipboard-test.sh
  • bash -n shell/plugins/clipboard/capture.sh test/shell.d/clipboard-test.sh
  • 8 MiB ordinary-text execution probe
  • real malformed-history payload probe
  • git diff --check

The implementation incorporates Codex High review findings: strict conservative detection replaces the permissive heuristic, and constant-overhead regex scanning replaces per-byte Perl scalar expansion.

Copilot AI balanced review requested due to automatic review settings August 17, 2026 08:59
@dhh
dhh force-pushed the fix/clipboard-utf16-decoding branch from 531e6b6 to fbc7ca3 Compare August 17, 2026 09:00

Copilot AI 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.

Pull request overview

Adds UTF-16 clipboard decoding and temporary-file cleanup. It also includes unrelated server/backup planning documents.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Changes:

  • Detects and converts UTF-16LE/BE clipboard text.
  • Adds clipboard regression tests.
  • Adds unrelated plans, mockups, and Markdown guidance.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
shell/plugins/clipboard/capture.sh Implements UTF-16 detection, conversion, and cleanup.
test/shell.d/clipboard-test.sh Tests UTF-16LE decoding and cleanup.
AGENTS.md Adds Markdown line-wrapping guidance.
plans/server.md Adds the Omarchy Server plan.
plans/backup.md Adds the backup feature plan.
plans/images/server-login.svg Adds a login mockup.
plans/images/server-menu.svg Adds a menu mockup.
plans/images/server-update.svg Adds an update-screen mockup.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/shell.d/clipboard-test.sh Outdated
Comment on lines +295 to +301
capture_output=$(printf '%s' 'UTF-16 clipboard — fixed' | iconv -f UTF-8 -t UTF-16LE | XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" text)
[[ $capture_output == '{"type":"text","text":"UTF-16 clipboard — fixed"}' ]] || fail "clipboard capture decodes UTF-16LE text"
pass "clipboard capture decodes UTF-16LE text"

capture_output=$(printf 'A' | iconv -f UTF-8 -t UTF-16LE | XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" text)
[[ $capture_output == '{"type":"text","text":"A"}' ]] || fail "clipboard capture decodes single-character UTF-16LE text"
pass "clipboard capture decodes single-character UTF-16LE text"
Copilot AI review requested due to automatic review settings August 17, 2026 09:02

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

shell/plugins/clipboard/capture.sh:74

  • The NUL-position heuristic can silently reverse endianness when a NUL belongs to the character rather than padding. For example, BOM-less UTF-16LE 😀 is 3d d8 00 de; this satisfies the BE branch, and iconv successfully produces 㷘Þ, so the raw fallback never runs. Only treat NULs paired with plausible padded single-byte characters as endian evidence (or leave ambiguous payloads unchanged), and add this case to the regression tests.
      if (even_nuls >= 1 && even_nuls * 2 >= pairs && odd_nuls * 10 <= even_nuls)
        print "UTF-16LE"
      else if (odd_nuls >= 1 && odd_nuls * 2 >= pairs && even_nuls * 10 <= odd_nuls)
        print "UTF-16BE"

test/shell.d/clipboard-test.sh:305

  • This assertion only covers normal completion; the newly added HUP/INT/TERM cleanup path is never exercised. Add a test that keeps stdin open, waits for clipboard-text.* to appear, terminates the capture process, and verifies the directory is removed so signal cleanup cannot regress unnoticed.
if compgen -G "$TMPDIR/clipboard-text.*" >/dev/null; then
  fail "clipboard capture removes temporary text payloads"
fi

Copilot AI review requested due to automatic review settings August 17, 2026 09:12
@dhh
dhh force-pushed the fix/clipboard-utf16-decoding branch from fbc7ca3 to 94ec33e Compare August 17, 2026 09:12

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

test/shell.d/clipboard-test.sh:295

  • The committed regressions exercise only BOM-less UTF-16LE, leaving the new BOM and UTF-16BE branches unprotected even though both are part of the capture contract. Add persistent cases for BOM-tagged text and BOM-less UTF-16BE.
capture_output=$(printf '%s' 'UTF-16 clipboard — fixed' | iconv -f UTF-8 -t UTF-16LE | XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" text)

shell/plugins/clipboard/capture.sh:61

  • unpack("C*", $raw) materializes one Perl scalar per input byte, so a large text clipboard can amplify memory usage by orders of magnitude and OOM the capture process. Count the two byte positions directly while retaining only $raw.
      my @bytes = unpack("C*", $raw);

Copilot AI review requested due to automatic review settings August 17, 2026 09:31
@dhh
dhh force-pushed the fix/clipboard-utf16-decoding branch from 94ec33e to 3d8130e Compare August 17, 2026 09:31

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

shell/plugins/clipboard/capture.sh:61

  • decode may destructively consume its source whenever CHECK is nonzero unless LEAVE_SRC is set. If UTF-16 decoding processes a valid prefix and then croaks on malformed trailing data, $raw can contain only the unconsumed suffix, so the fallback on the next line no longer preserves the original payload. Keep the source intact for the fallback; a valid-prefix-plus-truncated-tail test would also exercise this case.
    my $text = $encoding ? eval { decode($encoding, $raw, FB_CROAK) } : undef;

@dhh
dhh merged commit 006460a into quattro Aug 17, 2026
4 checks passed
@dhh
dhh deleted the fix/clipboard-utf16-decoding branch August 17, 2026 10:10
torturado pushed a commit to torturado/omarchy that referenced this pull request Aug 19, 2026
sgruendel pushed a commit to sgruendel/omarchy that referenced this pull request Aug 22, 2026
dhh added a commit that referenced this pull request Aug 23, 2026
(cherry picked from commit 006460a)
@dhh dhh mentioned this pull request Aug 23, 2026
nick1udwig pushed a commit to nick1udwig/omarchy that referenced this pull request Sep 9, 2026
@emirb emirb added this to the 4.5 milestone Oct 17, 2026
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.

3 participants