Remote: paste the phone clipboard, including images, on the desktop
CI / test (push) Failing after 1m2s
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:
1 parent
8a6d19d16f
commit
d2e00b4658
11 files changed
+853
-68
No files matched your search
+125
-24
@@ -190,6 +190,22 @@ static int extract_query_token(const char *target, char *out, size_t cap) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Whether the request carries a valid login token (?token=, Authorization: Bearer, or cookie). */
|
||||
static int web_authorized(RemoteApp *app, const RemoteConfig *cfg, const char *target, const char *hdrs) {
|
||||
if (cfg->auth_mode == REMOTE_AUTH_NONE) return 1;
|
||||
char tok[160] = {0}, val[1024] = {0};
|
||||
int ok = extract_query_token(target, tok, sizeof tok);
|
||||
if (!ok && find_header(hdrs, "Authorization", val, sizeof val) &&
|
||||
strncasecmp(val, "Bearer ", 7) == 0 && strlen(val + 7) < sizeof tok) {
|
||||
snprintf(tok, sizeof tok, "%.*s", (int)sizeof tok - 1, val + 7);
|
||||
ok = tok[0] != 0;
|
||||
}
|
||||
if (!ok && find_header(hdrs, "Cookie", val, sizeof val)) {
|
||||
ok = extract_cookie_token(val, tok, sizeof tok);
|
||||
}
|
||||
return ok && web_token_verify(app->hmac_key, tok, (uint64_t)time(NULL));
|
||||
}
|
||||
|
||||
static int ws_send_frame(int fd, uint8_t opcode, const void *data, size_t len) {
|
||||
uint8_t hdr[10];
|
||||
size_t hlen = 2;
|
||||
@@ -478,25 +494,32 @@ static void ws_handle_text(WsState *ws, const char *msg) {
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "text") == 0) {
|
||||
char txt[1024] = {0};
|
||||
if (json_get_str(msg, "s", txt, sizeof txt) && txt[0]) {
|
||||
size_t cap = strlen(msg) + 1;
|
||||
char *txt = malloc(cap);
|
||||
if (txt && json_get_str(msg, "s", txt, cap) && txt[0]) {
|
||||
ws_note_key_activity(ws);
|
||||
remote_send_utf8_text(&ws->app->ctl, txt);
|
||||
}
|
||||
free(txt);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "clip") == 0) {
|
||||
char txt[2048] = {0};
|
||||
if (json_get_str(msg, "s", txt, sizeof txt) && txt[0]) {
|
||||
size_t n = strlen(txt);
|
||||
char *cmd = malloc(16 + n * 2 + 1);
|
||||
if (cmd) {
|
||||
memcpy(cmd, "clipboard-set ", 14);
|
||||
hex_encode((const uint8_t *)txt, n, cmd + 14);
|
||||
ctl_fire(&ws->app->ctl, cmd);
|
||||
free(cmd);
|
||||
}
|
||||
size_t cap = strlen(msg) + 1;
|
||||
char *txt = malloc(cap);
|
||||
if (txt && json_get_str(msg, "s", txt, cap) && txt[0]) {
|
||||
remote_clipboard_set(&ws->app->ctl, "text/plain", txt, strlen(txt), NULL);
|
||||
}
|
||||
free(txt);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "paste") == 0) { /* paste the phone's clipboard, by the given chord or the app's usual one */
|
||||
long code = 0, mods = 0;
|
||||
char cmd[64] = "input-paste";
|
||||
if (json_get_int(msg, "c", &code) && code > 0 && json_get_int(msg, "m", &mods) && mods >= 0) {
|
||||
snprintf(cmd, sizeof cmd, "input-paste %ld %ld", code, mods);
|
||||
}
|
||||
ws_note_key_activity(ws);
|
||||
ctl_fire(&ws->app->ctl, cmd);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "ctl") == 0) {
|
||||
@@ -523,6 +546,81 @@ static void ws_handle_text(WsState *ws, const char *msg) {
|
||||
}
|
||||
}
|
||||
|
||||
/* GET /api/clipboard: the clipboard serial. POST: put the body (text/plain or image/png) on the
|
||||
phone's clipboard. Both answer {"ok":true,"serial":N}. `body`/`have`: what was read already. */
|
||||
static void web_clipboard(RemoteApp *app, int fd, const char *method, const char *hdrs,
|
||||
const char *body, size_t have) {
|
||||
char val[128] = {0}, js[96];
|
||||
const char *status = "400 Bad Request", *err = "Bad request";
|
||||
uint8_t *data = NULL;
|
||||
unsigned long serial = 0;
|
||||
if (strcmp(method, "GET") == 0) {
|
||||
if (remote_clipboard_serial(&app->ctl, &serial) == 0) goto ok;
|
||||
status = "502 Bad Gateway";
|
||||
err = "The desktop did not answer";
|
||||
goto fail;
|
||||
}
|
||||
/* A page from elsewhere can't set this header without a CORS preflight, which is refused. */
|
||||
if (!find_header(hdrs, "X-Chandra-Remote", val, sizeof val)) {
|
||||
status = "403 Forbidden";
|
||||
err = "Forbidden";
|
||||
goto fail;
|
||||
}
|
||||
const char *mime = NULL;
|
||||
if (find_header(hdrs, "Content-Type", val, sizeof val)) {
|
||||
val[strcspn(val, "; \t")] = 0;
|
||||
if (strcasecmp(val, "text/plain") == 0) mime = "text/plain";
|
||||
else if (strcasecmp(val, "image/png") == 0) mime = "image/png";
|
||||
}
|
||||
if (!mime) {
|
||||
status = "415 Unsupported Media Type";
|
||||
err = "Only text and PNG images can be pasted";
|
||||
goto fail;
|
||||
}
|
||||
char *endp = NULL;
|
||||
if (!find_header(hdrs, "Content-Length", val, sizeof val) || !isdigit((unsigned char)val[0])) goto fail;
|
||||
unsigned long long cl = strtoull(val, &endp, 10);
|
||||
if (*endp || cl == 0) goto fail;
|
||||
if (cl > REMOTE_CLIPBOARD_MAX) {
|
||||
status = "413 Content Too Large";
|
||||
err = "Too large (16 MB at most)";
|
||||
goto fail;
|
||||
}
|
||||
size_t len = (size_t)cl;
|
||||
data = malloc(len);
|
||||
if (!data) goto fail;
|
||||
if (have > len) have = len;
|
||||
memcpy(data, body, have);
|
||||
uint64_t deadline = remote_now_ms() + 60000;
|
||||
while (have < len) {
|
||||
struct pollfd pfd = { .fd = fd, .events = POLLIN };
|
||||
if (remote_now_ms() > deadline || poll(&pfd, 1, 10000) <= 0) goto fail;
|
||||
ssize_t r = recv(fd, data + have, len - have, 0);
|
||||
if (r < 0 && errno == EINTR) continue;
|
||||
if (r <= 0) goto fail;
|
||||
have += (size_t)r;
|
||||
}
|
||||
static const uint8_t png_magic[8] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n' };
|
||||
if (strcmp(mime, "image/png") == 0 && (len < 8 || memcmp(data, png_magic, 8) != 0)) {
|
||||
err = "Not a PNG image";
|
||||
goto fail;
|
||||
}
|
||||
if (remote_clipboard_set(&app->ctl, mime, data, len, &serial) != 0) {
|
||||
status = "502 Bad Gateway";
|
||||
err = "The desktop did not take the clipboard";
|
||||
goto fail;
|
||||
}
|
||||
ok:
|
||||
free(data);
|
||||
snprintf(js, sizeof js, "{\"ok\":true,\"serial\":%lu}", serial);
|
||||
http_send(fd, "200 OK", "application/json", NULL, js, strlen(js));
|
||||
return;
|
||||
fail:
|
||||
free(data);
|
||||
snprintf(js, sizeof js, "{\"ok\":false,\"error\":\"%s\"}", err);
|
||||
http_send(fd, status, "application/json", NULL, js, strlen(js));
|
||||
}
|
||||
|
||||
static void ws_loop(RemoteApp *app, int fd) {
|
||||
WsState ws;
|
||||
memset(&ws, 0, sizeof ws);
|
||||
@@ -816,19 +914,22 @@ void web_handle_client(RemoteApp *app, int fd) {
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
if (strcmp(path, "/api/clipboard") == 0 && (!strcmp(method, "GET") || !strcmp(method, "POST"))) {
|
||||
if (!web_authorized(app, &cfg, target, buf)) {
|
||||
const char *m = "{\"ok\":false,\"error\":\"Unauthorized\"}";
|
||||
http_send(fd, "401 Unauthorized", "application/json", NULL, m, strlen(m));
|
||||
} else {
|
||||
web_clipboard(app, fd, method, buf, body, body_have);
|
||||
}
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
if (strcmp(method, "GET") == 0 && strcmp(path, "/ws") == 0) {
|
||||
if (cfg.auth_mode != REMOTE_AUTH_NONE) {
|
||||
char tok[160] = {0}, cookie[1024] = {0};
|
||||
int ok = extract_query_token(target, tok, sizeof tok);
|
||||
if (!ok && find_header(buf, "Cookie", cookie, sizeof cookie)) {
|
||||
ok = extract_cookie_token(cookie, tok, sizeof tok);
|
||||
}
|
||||
if (!ok || !web_token_verify(app->hmac_key, tok, (uint64_t)time(NULL))) {
|
||||
const char *m = "Unauthorized";
|
||||
http_send(fd, "401 Unauthorized", "text/plain", NULL, m, strlen(m));
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
if (!web_authorized(app, &cfg, target, buf)) {
|
||||
const char *m = "Unauthorized";
|
||||
http_send(fd, "401 Unauthorized", "text/plain", NULL, m, strlen(m));
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
char upg[64] = {0}, wskey[128] = {0}, accept[64] = {0};
|
||||
if (!find_header(buf, "Upgrade", upg, sizeof upg) || strcasecmp(upg, "websocket") != 0 ||
|
||||
|
||||
Reference in new issue
Block a user