Skip to content

Commit 0df5d5b

Browse files
committed
KASM-6773 Escape JSON filenames in WebSocket file list responses
1 parent 4973781 commit 0df5d5b

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

common/network/websocket.c

+34-22
Original file line numberDiff line numberDiff line change
@@ -1673,11 +1673,11 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use
16731673
}
16741674

16751675
sprintf(buf, "HTTP/1.1 200 OK\r\n"
1676-
"Server: KasmVNC/4.0\r\n"
1677-
"Connection: close\r\n"
1678-
"Content-type: text/json\r\n"
1679-
"%s"
1680-
"\r\n", extra_headers ? extra_headers : "");
1676+
"Server: KasmVNC/4.0\r\n"
1677+
"Connection: close\r\n"
1678+
"Content-type: text/json\r\n"
1679+
"%s"
1680+
"\r\n", extra_headers ? extra_headers : "");
16811681
ws_send(ws_ctx, buf, strlen(buf));
16821682
len = 15;
16831683

@@ -1711,23 +1711,35 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use
17111711
strcpy(grp, grpt.gr_name);
17121712
}
17131713

1714-
sprintf(buf, "%s{ \"filename\": \"%s\", "
1715-
"\"date_modified\": %lu, "
1716-
"\"date_created\": %lu, "
1717-
"\"is_dir\": %s, "
1718-
"\"size\": %lu, "
1719-
"\"owner\": \"%s\", "
1720-
"\"group\": \"%s\", "
1721-
"\"perms\": \"%s\" }",
1722-
sent ? ",\n" : "",
1723-
ent->d_name,
1724-
st.st_mtime,
1725-
st.st_ctime,
1726-
S_ISDIR(st.st_mode) ? "true" : "false",
1727-
S_ISDIR(st.st_mode) ? 0 : st.st_size,
1728-
own,
1729-
grp,
1730-
perms);
1714+
sprintf(buf, "%s{ \"filename\": \"", sent ? ",\n" : "");
1715+
ws_send(ws_ctx, buf, strlen(buf));
1716+
len += strlen(buf);
1717+
1718+
size_t max_out_length = 2 * strlen(ent->d_name) + 1; // worst case scenario
1719+
char *filename = malloc(max_out_length);
1720+
1721+
JSON_escape(ent->d_name, filename);
1722+
size_t size = strlen(filename);
1723+
ws_send(ws_ctx, filename, size);
1724+
len += size;
1725+
1726+
free(filename);
1727+
1728+
sprintf(buf, "\", "
1729+
"\"date_modified\": %lu, "
1730+
"\"date_created\": %lu, "
1731+
"\"is_dir\": %s, "
1732+
"\"size\": %lu, "
1733+
"\"owner\": \"%s\", "
1734+
"\"group\": \"%s\", "
1735+
"\"perms\": \"%s\" }",
1736+
st.st_mtime,
1737+
st.st_ctime,
1738+
S_ISDIR(st.st_mode) ? "true" : "false",
1739+
S_ISDIR(st.st_mode) ? 0 : st.st_size,
1740+
own,
1741+
grp,
1742+
perms);
17311743
sent = 1;
17321744
ws_send(ws_ctx, buf, strlen(buf));
17331745
len += strlen(buf);

0 commit comments

Comments
 (0)