Bound X11 clipboard transfers

This commit is contained in:
2026-09-12 13:06:58 +02:00
parent ec232197b4
commit 02105e7751
2 changed files with 141 additions and 21 deletions

View File

@@ -41,9 +41,10 @@ Each bounded iteration must:
- [x] Route hotkey/show-triggered focus lookup through the X11 worker (Docker GTK/X11 build, 2026-09-10).
- [x] Move clipboard writes, focus lookup, and paste injection off the GTK main thread (worker-backed toolbar and keyboard paths; Docker GTK/X11 build, 2026-09-10).
- [x] Support `UTF8_STRING` with `STRING` fallback for textual X11 clipboard reads (X11-enabled tests and Docker GTK/X11 build, 2026-09-10).
- [ ] Replace the high-level X11 clipboard transfer with a bounded protocol
read; the current library may allocate the raw transfer before the app-level
size rejection.
- [x] Replace the high-level X11 clipboard transfer with a bounded protocol
read that checks advertised `INCR` sizes, limits property chunks, and uses a
deadline-aware event loop (27 X11-feature tests and Docker GTK/X11/package
build, 2026-09-12).
- [x] Make self-generated clipboard suppression deterministic and race-resistant (queued-write marker lifecycle and Docker GTK/X11 build, 2026-09-10).
- [x] Ensure failed internal clipboard writes cannot poison suppression state (worker reports write success separately; 17 tests and Docker build, 2026-09-10).
- [x] Add unit coverage for configured shortcut parsing and lock-state grab variants (17 tests and Docker GTK/X11 build, 2026-09-10).
@@ -122,6 +123,9 @@ Each bounded iteration must:
- No GUI, X11, Cinnamon, or packaging integration tests are present in the tree.
- X11 paste now rejects non-concrete focus values before injecting keystrokes;
real application-target acceptance remains manual evidence.
- Bounded clipboard reads protect Clipb(h)oard's transfer and retained buffers;
X11 selection owners can still consume resources before responding, which is
outside the requesting client's control.
- Diagnostics previously printed a fixed log filename even though the rolling
logger creates date-suffixed files; this is now reported as the log directory.
- Path hardening rejects final-file symlinks; parent-directory symlink,

View File

@@ -6,6 +6,13 @@ use std::{
use anyhow::{Context, Result};
use x11_clipboard::Clipboard;
use x11rb::{
connection::Connection,
protocol::{
xproto::{AtomEnum, ConnectionExt, Property},
Event,
},
};
use crate::platform::{ClipboardBackend, InputBackend};
@@ -198,30 +205,139 @@ impl ClipboardBackend for X11Clipboard {
fn read_clipboard_text(clipboard: &Clipboard, max_entry_bytes: usize) -> Result<Option<String>> {
let atoms = &clipboard.getter.atoms;
let bytes = clipboard
.load(
atoms.clipboard,
atoms.utf8_string,
atoms.property,
Duration::from_millis(500),
)
let bytes = load_clipboard_bounded(clipboard, atoms.utf8_string, max_entry_bytes)
.or_else(|utf8_error| {
clipboard
.load(
atoms.clipboard,
atoms.string,
atoms.property,
Duration::from_millis(500),
)
.with_context(|| {
format!("failed to read X11 clipboard as UTF8_STRING or STRING: {utf8_error}")
})
load_clipboard_bounded(clipboard, atoms.string, max_entry_bytes).with_context(|| {
format!("failed to read X11 clipboard as UTF8_STRING or STRING: {utf8_error}")
})
})
.context("failed to read textual X11 clipboard contents")?;
ensure_clipboard_size(&bytes, max_entry_bytes)?;
Ok(Some(String::from_utf8_lossy(&bytes).into_owned()))
}
fn load_clipboard_bounded(
clipboard: &Clipboard,
target: u32,
max_entry_bytes: usize,
) -> Result<Vec<u8>> {
let connection = &clipboard.getter.connection;
let property = clipboard.getter.atoms.property;
let cookie = connection.convert_selection(
clipboard.getter.window,
clipboard.getter.atoms.clipboard,
target,
property,
x11rb::CURRENT_TIME,
)?;
let sequence = cookie.sequence_number();
cookie.check()?;
let deadline = std::time::Instant::now() + Duration::from_millis(500);
let mut bytes = Vec::new();
let mut incremental = false;
loop {
if std::time::Instant::now() >= deadline {
return Err(anyhow::anyhow!("timed out reading X11 clipboard"));
}
let Some((event, event_sequence)) = connection.poll_for_event_with_sequence()? else {
thread::sleep(Duration::from_millis(5));
continue;
};
if event_sequence < sequence {
continue;
}
match event {
Event::SelectionNotify(event) => {
if event.selection != clipboard.getter.atoms.clipboard {
continue;
}
if event.property == AtomEnum::NONE.into() {
break;
}
let reply = connection
.get_property(
false,
clipboard.getter.window,
event.property,
AtomEnum::NONE,
0,
1,
)?
.reply()?;
if reply.type_ == clipboard.getter.atoms.incr {
let advertised = reply
.value32()
.and_then(|mut values| values.next())
.unwrap_or(0) as usize;
anyhow::ensure!(
advertised <= max_entry_bytes,
"clipboard entry exceeds the configured {} byte limit",
max_entry_bytes
);
connection
.delete_property(clipboard.getter.window, property)?
.check()?;
incremental = true;
continue;
}
if reply.type_ != target {
return Err(anyhow::anyhow!("X11 clipboard returned an unexpected type"));
}
let units = max_entry_bytes.saturating_add(3) / 4;
let reply = connection
.get_property(
false,
clipboard.getter.window,
event.property,
AtomEnum::NONE,
0,
units as u32,
)?
.reply()?;
anyhow::ensure!(
reply.bytes_after == 0,
"clipboard entry exceeds the configured {} byte limit",
max_entry_bytes
);
bytes.extend_from_slice(&reply.value);
break;
}
Event::PropertyNotify(event) if incremental => {
if event.atom != property || event.state != Property::NEW_VALUE {
continue;
}
let remaining = max_entry_bytes.saturating_sub(bytes.len());
let units = remaining.saturating_add(3) / 4;
let reply = connection
.get_property(
true,
clipboard.getter.window,
property,
AtomEnum::NONE,
0,
units as u32,
)?
.reply()?;
anyhow::ensure!(
reply.bytes_after == 0,
"clipboard entry exceeds the configured {} byte limit",
max_entry_bytes
);
bytes.extend_from_slice(&reply.value);
if reply.value.is_empty() {
break;
}
}
_ => {}
}
}
connection
.delete_property(clipboard.getter.window, property)?
.check()?;
ensure_clipboard_size(&bytes, max_entry_bytes)?;
Ok(bytes)
}
fn ensure_clipboard_size(bytes: &[u8], max_entry_bytes: usize) -> Result<()> {
anyhow::ensure!(
bytes.len() <= max_entry_bytes,