KASM-6773 Escape JSON filenames in WebSocket file list responses

This commit is contained in:
El 2025-03-17 17:20:26 +05:00
parent 4973781a8f
commit 0df5d5bd72
No known key found for this signature in database
GPG Key ID: EB3F4C9EA29CDE59

View File

@ -1673,11 +1673,11 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use
} }
sprintf(buf, "HTTP/1.1 200 OK\r\n" sprintf(buf, "HTTP/1.1 200 OK\r\n"
"Server: KasmVNC/4.0\r\n" "Server: KasmVNC/4.0\r\n"
"Connection: close\r\n" "Connection: close\r\n"
"Content-type: text/json\r\n" "Content-type: text/json\r\n"
"%s" "%s"
"\r\n", extra_headers ? extra_headers : ""); "\r\n", extra_headers ? extra_headers : "");
ws_send(ws_ctx, buf, strlen(buf)); ws_send(ws_ctx, buf, strlen(buf));
len = 15; len = 15;
@ -1711,23 +1711,35 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use
strcpy(grp, grpt.gr_name); strcpy(grp, grpt.gr_name);
} }
sprintf(buf, "%s{ \"filename\": \"%s\", " sprintf(buf, "%s{ \"filename\": \"", sent ? ",\n" : "");
"\"date_modified\": %lu, " ws_send(ws_ctx, buf, strlen(buf));
"\"date_created\": %lu, " len += strlen(buf);
"\"is_dir\": %s, "
"\"size\": %lu, " size_t max_out_length = 2 * strlen(ent->d_name) + 1; // worst case scenario
"\"owner\": \"%s\", " char *filename = malloc(max_out_length);
"\"group\": \"%s\", "
"\"perms\": \"%s\" }", JSON_escape(ent->d_name, filename);
sent ? ",\n" : "", size_t size = strlen(filename);
ent->d_name, ws_send(ws_ctx, filename, size);
st.st_mtime, len += size;
st.st_ctime,
S_ISDIR(st.st_mode) ? "true" : "false", free(filename);
S_ISDIR(st.st_mode) ? 0 : st.st_size,
own, sprintf(buf, "\", "
grp, "\"date_modified\": %lu, "
perms); "\"date_created\": %lu, "
"\"is_dir\": %s, "
"\"size\": %lu, "
"\"owner\": \"%s\", "
"\"group\": \"%s\", "
"\"perms\": \"%s\" }",
st.st_mtime,
st.st_ctime,
S_ISDIR(st.st_mode) ? "true" : "false",
S_ISDIR(st.st_mode) ? 0 : st.st_size,
own,
grp,
perms);
sent = 1; sent = 1;
ws_send(ws_ctx, buf, strlen(buf)); ws_send(ws_ctx, buf, strlen(buf));
len += strlen(buf); len += strlen(buf);