Repository navigation
Prevent clipboard capture hangs #9488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,25 +10,31 @@ STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/omarchy" | |
| IMAGE_DIR="$STATE_DIR/clipboard-images" | ||
| mkdir -p "$IMAGE_DIR" | ||
|
|
||
| types=$(wl-paste --list-types 2>/dev/null || true) | ||
| types=$(timeout 2s wl-paste --list-types 2>/dev/null || true) | ||
|
|
||
| if [[ ${CLIPBOARD_STATE:-} == "sensitive" ]] || grep -qx 'x-kde-passwordManagerHint' <<<"$types"; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| tmp= | ||
| trap 'rm -f -- "$tmp"' EXIT | ||
|
|
||
| # The clipboard owner streams the copy and can stall without closing its end, so | ||
| # every read is bounded, and a copy cut off at the deadline is dropped, not kept. | ||
| read_copy() { | ||
| timeout 2s "$@" >"$tmp" 2>/dev/null && [[ -s $tmp ]] | ||
| } | ||
|
|
||
| emit_image() { | ||
| local mime="$1" | ||
| local ext tmp hash file | ||
| local ext hash file | ||
| shift | ||
|
|
||
| ext=${mime#image/} | ||
| [[ $ext == jpeg ]] && ext=jpg | ||
|
|
||
| tmp=$(mktemp --tmpdir="$IMAGE_DIR" clipboard.XXXXXX) || return 0 | ||
| cat >"$tmp" | ||
| if [[ ! -s $tmp ]]; then | ||
| rm -f "$tmp" | ||
| return 0 | ||
| fi | ||
| read_copy "$@" || return | ||
|
|
||
| hash=$(sha256sum "$tmp" | awk '{print $1}') | ||
| file="$IMAGE_DIR/$hash.$ext" | ||
|
|
@@ -43,6 +49,9 @@ emit_image() { | |
| } | ||
|
|
||
| emit_text() { | ||
| tmp=$(mktemp --tmpdir="$STATE_DIR" clipboard.XXXXXX) || return 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Text is now written to a temporary file in the state directory, but cleanup relies on an |
||
| read_copy "$@" || return | ||
|
|
||
| perl -MEncode=decode,FB_CROAK,LEAVE_SRC -MJSON::PP=encode_json -0777 -e ' | ||
| my $raw = <STDIN>; | ||
| exit unless length $raw; | ||
|
|
@@ -86,21 +95,21 @@ emit_text() { | |
| } | ||
| $text = decode("UTF-8", $raw) unless defined $text; | ||
| print "{\"type\":\"text\",\"text\":", encode_json($text), "}\n"; | ||
| ' | ||
| ' <"$tmp" | ||
| } | ||
|
|
||
| case "${1:-}" in | ||
| text) emit_text; exit 0 ;; | ||
| image/*) emit_image "$1"; exit 0 ;; | ||
| text) emit_text cat; exit 0 ;; | ||
| image/*) emit_image "$1" cat; exit 0 ;; | ||
| esac | ||
|
|
||
| for mime in image/png image/jpeg image/webp image/gif image/bmp image/tiff; do | ||
| if grep -qx "$mime" <<<"$types"; then | ||
| timeout 2s wl-paste --type "$mime" 2>/dev/null | emit_image "$mime" | ||
| emit_image "$mime" wl-paste --type "$mime" | ||
| exit 0 | ||
| fi | ||
| done | ||
|
|
||
| if grep -q '^text/' <<<"$types" || grep -qx 'UTF8_STRING' <<<"$types" || grep -qx 'STRING' <<<"$types"; then | ||
| wl-paste --type text --no-newline 2>/dev/null | emit_text | ||
| emit_text wl-paste --type text --no-newline | ||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a clipboard item carries
x-kde-passwordManagerHint,CLIPBOARD_STATEis unset, and--list-typestakes more than two seconds, the timeout leavestypesempty. The hint check then misses the marker, while the watched-text branch still reads stdin and emits JSON. Password-manager text can be saved to clipboard history.How this was verified: The timed-out MIME result feeds the hint check, while the watched-text branch emits its stdin payload regardless of that result.