Remote: paste the phone clipboard, including images, on the desktop
CI / test (push) Failing after 1m2s

- Web UI: Ctrl+V, Ctrl+Shift+V, Shift+Insert (Cmd+V on Mac) and a Paste button send the phone clipboard (text or PNG, up to 16 MB) to the desktop over POST /api/clipboard
- chandra-remote passes clipboard data to the compositor in a sealed memfd (clipboard-set-fd), replacing the 4 KB hex clipboard-set line
- Compositor: new clipboard-status and input-paste commands; text is typed into the launcher, password dialog and shell popups, otherwise the paste chord is sent (Ctrl+Shift+V in terminals)
- Paste data is captured when a paste starts, so a clipboard change mid-transfer can't corrupt it
- VNC clipboard text limit raised from 4 KB to 1 MB
- Upload endpoints are authorized with the same token check as /ws; the no-header cross-site case is refused with 403
This commit is contained in:
nandi committed 2026-11-29 15:42:46 -08:00
1 parent 8a6d19d16f
commit d2e00b4658
11 files changed
+853 -68

No files matched your search

+72 -2
View File
@@ -199,6 +199,8 @@ class MockCtlServer:
self.screen = "on"
self.brightness = 75
self.osk = 1
self.clip_serial = 0
self.clips = [] # (mime, bytes) put on the clipboard
self.th = threading.Thread(target=self._serve, daemon=True)
self.th.start()
@@ -215,11 +217,13 @@ class MockCtlServer:
def _client(self, conn: socket.socket):
buf = b""
files = []
while self.running:
try:
data = conn.recv(4096)
data, fds, _, _ = socket.recv_fds(conn, 4096, 4)
except OSError:
break
files += fds
if not data:
break
buf += data
@@ -244,6 +248,15 @@ class MockCtlServer:
elif cmd == "osk on":
self.osk = 1
rep = "ok on\n"
elif cmd == "clipboard-status":
rep = f"ok serial={self.clip_serial}\n"
elif cmd.startswith("clipboard-set-fd "):
_, mime, size = cmd.split(" ")
fd = files.pop(0)
self.clips.append((mime, os.pread(fd, int(size) + 1, 0)))
os.close(fd)
self.clip_serial += 1
rep = f"ok serial={self.clip_serial}\n"
elif cmd == "osk toggle":
self.osk = 0 if self.osk else 1
rep = f"ok {'on' if self.osk else 'off'}\n"
@@ -523,7 +536,7 @@ class RemoteTests(unittest.TestCase):
self.assertIn("input-scroll 0 -30 -1", cmds)
self.assertIn("input-key 30 1", cmds)
self.assertIn("input-text c3a9", cmds)
self.assertIn("clipboard-set " + cut.hex(), cmds)
self.assertEqual(self.ctl.clips, [("text/plain", cut)])
def test_03_vnc_password_and_zlib(self):
self.shm.write_frame(64, 48, lambda x, y: (10, 200, 220))
@@ -761,6 +774,63 @@ class RemoteTests(unittest.TestCase):
time.sleep(0.15)
self.assertEqual(self.ctl.osk, 1)
def clip_request(self, method, body=None, headers=None):
c = http.client.HTTPConnection("127.0.0.1", self.web_port, timeout=5)
c.request(method, "/api/clipboard", body=body, headers=headers or {})
r = c.getresponse()
out = r.status, json.loads(r.read())
c.close()
return out
def test_07_clipboard_upload_and_paste(self):
self.shm.write_frame(64, 48, lambda x, y: (1, 2, 3))
self.start_daemon(auth="none")
png = b"\x89PNG\r\n\x1a\n" + os.urandom(3 * 1024 * 1024)
own = {"X-Chandra-Remote": "1"}
self.assertEqual(self.clip_request("GET"), (200, {"ok": True, "serial": 0}))
st, d = self.clip_request("POST", png, {**own, "Content-Type": "image/png"})
self.assertEqual((st, d), (200, {"ok": True, "serial": 1}))
text = "líne 1\nline 2 \"q\" \x1b\n".encode() * 500
st, d = self.clip_request("POST", text, {**own, "Content-Type": "text/plain;charset=utf-8"})
self.assertEqual((st, d), (200, {"ok": True, "serial": 2}))
self.assertEqual(self.ctl.clips, [("image/png", png), ("text/plain", text)])
# Refused: no own-page header (cross-site), other types, not a PNG, empty, too large.
self.assertEqual(self.clip_request("POST", b"x", {"Content-Type": "text/plain"})[0], 403)
self.assertEqual(self.clip_request("POST", b"x", {**own, "Content-Type": "text/html"})[0], 415)
self.assertEqual(self.clip_request("POST", b"not a png", {**own, "Content-Type": "image/png"})[0], 400)
self.assertEqual(self.clip_request("POST", b"", {**own, "Content-Type": "text/plain"})[0], 400)
big = {**own, "Content-Type": "image/png", "Content-Length": str(16 * 1024 * 1024 + 1)}
self.assertEqual(self.clip_request("POST", None, big)[0], 413)
self.assertEqual(len(self.ctl.clips), 2)
ws, status_line, _, _ = ws_connect("127.0.0.1", self.web_port)
self.assertIn("101", status_line)
ws_send_text(ws, json.dumps({"t": "paste", "c": 47, "m": 5}))
ws_send_text(ws, json.dumps({"t": "paste"}))
ws_send_text(ws, json.dumps({"t": "clip", "s": "z" * 5000}))
deadline = time.time() + 3.0
while time.time() < deadline and len(self.ctl.clips) < 3:
time.sleep(0.02)
self.assertIn("input-paste 47 5", self.ctl.commands)
self.assertIn("input-paste", self.ctl.commands)
self.assertEqual(self.ctl.clips[2], ("text/plain", b"z" * 5000))
ws.close()
def test_08_clipboard_needs_login(self):
self.start_daemon(auth="password", password="TestPass99")
hdr = {"X-Chandra-Remote": "1", "Content-Type": "text/plain"}
self.assertEqual(self.clip_request("GET")[0], 401)
self.assertEqual(self.clip_request("POST", b"x", hdr)[0], 401)
self.assertEqual(self.clip_request("POST", b"x", {**hdr, "Authorization": "Bearer 1.abc"})[0], 401)
c = http.client.HTTPConnection("127.0.0.1", self.web_port, timeout=5)
c.request("POST", "/api/login", body=json.dumps({"password": "TestPass99"}))
tok = json.loads(c.getresponse().read())["token"]
c.close()
self.assertEqual(self.clip_request("POST", b"x", {**hdr, "Authorization": "Bearer " + tok})[0], 200)
self.assertEqual(self.clip_request("POST", b"y", {**hdr, "Cookie": "chandra_remote=" + tok})[0], 200)
self.assertEqual(self.ctl.clips, [("text/plain", b"x"), ("text/plain", b"y")])
if __name__ == "__main__":
unittest.main()