Bound clipboard history admission

This commit is contained in:
2026-09-12 12:47:53 +02:00
parent 2b34a7cb99
commit 16d9a5d2e5
3 changed files with 32 additions and 5 deletions

View File

@@ -23,6 +23,9 @@ Each bounded iteration must:
- [x] Expire unpinned clipboard history older than the 30-day default at
startup, while preserving pinned entries (25 X11-feature tests and Docker
GTK/X11/package build, 2026-09-12).
- [x] Enforce `max_entry_bytes` in the asynchronous X11 reader before UTF-8
conversion and persistence (26 X11-feature tests and Docker GTK/X11/package
build, 2026-09-12).
- [x] Create database, log, config, lock, and runtime directories with private permissions (mode probe and Docker build, 2026-09-10).
- [x] Handle pre-existing overly-permissive database/log permissions safely (existing database and date-suffixed log regression tests, 17 tests and Docker build, 2026-09-10).
- [x] Add tests/probes for file modes under permissive umasks (database regression test plus runtime probe, 2026-09-10).
@@ -38,6 +41,9 @@ 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] 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).

View File

@@ -80,7 +80,7 @@ pub fn run(
None
}
};
let clipboard_reader = X11ClipboardReader::start()?;
let clipboard_reader = X11ClipboardReader::start(config.max_entry_bytes)?;
let clipboard_worker = X11ClipboardWorker::start()?;
let shared = Rc::new(RefCell::new(UiState {
entries: database.load_entries()?,

View File

@@ -130,7 +130,7 @@ impl X11ClipboardWorker {
}
impl X11ClipboardReader {
pub fn start() -> Result<Self> {
pub fn start(max_entry_bytes: usize) -> Result<Self> {
let clipboard =
Clipboard::new().context("failed to connect to the X11 clipboard reader")?;
let (requests, request_receiver) = mpsc::sync_channel::<()>(1);
@@ -139,7 +139,7 @@ impl X11ClipboardReader {
.name("clipbhoard-x11-clipboard-reader".into())
.spawn(move || {
while request_receiver.recv().is_ok() {
let result = read_clipboard_text(&clipboard);
let result = read_clipboard_text(&clipboard, max_entry_bytes);
if result_sender.send(result).is_err() {
break;
}
@@ -186,7 +186,7 @@ impl X11Clipboard {
impl ClipboardBackend for X11Clipboard {
fn read_text(&mut self) -> Result<Option<String>> {
read_clipboard_text(&self.clipboard)
read_clipboard_text(&self.clipboard, usize::MAX)
}
fn write_text(&mut self, text: &str) -> Result<()> {
let atoms = &self.clipboard.setter.atoms;
@@ -196,7 +196,7 @@ impl ClipboardBackend for X11Clipboard {
}
}
fn read_clipboard_text(clipboard: &Clipboard) -> Result<Option<String>> {
fn read_clipboard_text(clipboard: &Clipboard, max_entry_bytes: usize) -> Result<Option<String>> {
let atoms = &clipboard.getter.atoms;
let bytes = clipboard
.load(
@@ -218,9 +218,19 @@ fn read_clipboard_text(clipboard: &Clipboard) -> Result<Option<String>> {
})
})
.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 ensure_clipboard_size(bytes: &[u8], max_entry_bytes: usize) -> Result<()> {
anyhow::ensure!(
bytes.len() <= max_entry_bytes,
"clipboard entry exceeds the configured {} byte limit",
max_entry_bytes
);
Ok(())
}
impl InputBackend for X11Clipboard {
fn paste_into_active_window(&mut self, target: u32) -> Result<()> {
self.input
@@ -229,3 +239,14 @@ impl InputBackend for X11Clipboard {
.paste_into(target)
}
}
#[cfg(test)]
mod tests {
use super::ensure_clipboard_size;
#[test]
fn rejects_clipboard_payloads_over_configured_limit() {
assert!(ensure_clipboard_size(b"secret", 5).is_err());
assert!(ensure_clipboard_size(b"secret", 6).is_ok());
}
}