Skip to content

Commit df713f4

Browse files
committed
deaddrop: delete p1
1 parent fe79821 commit df713f4

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

plugins/deaddrop/assets/deaddrop.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@
194194
text_inp(); // Manually update button state after clearing
195195
}
196196

197+
function delfile(e)
198+
{
199+
e.stopPropagation();
200+
e.preventDefault();
201+
202+
ws.send("{\"del\":\"" + e.target.getAttribute("file") + "\"}");
203+
}
204+
197205
function body_drop(e) {
198206
e.preventDefault();
199207
}
@@ -210,15 +218,6 @@
210218
upl_text.disabled = !content.value.length;
211219
}
212220

213-
function delfile(e)
214-
{
215-
e.stopPropagation();
216-
e.preventDefault();
217-
218-
ws.send("{\"del\":\"" + decodeURI(e.target.getAttribute("file")) +
219-
"\"}");
220-
}
221-
222221
function get_appropriate_ws_url(extra_url)
223222
{
224223
var pcol;
@@ -298,12 +297,12 @@
298297
humanize(j.files[n].size) +
299298
"</td><td class=\"dow\">" +
300299
date.toDateString() + " " +
301-
date.toLocaleTimeString() +
302-
"</td><td>";
303-
if (j.files[n].yours === 1)
300+
date.toLocaleTimeString() + "</td><td>";
301+
302+
if (username) /* any authenticated user can delete */
304303
s += "<img id=\"d" + n +
305304
"\" class=\"delbtn\" file=\"" +
306-
lws_urlencode(san(j.files[n].name)) + "\">";
305+
san(j.files[n].name) + "\">";
307306
else
308307
s += " ";
309308

@@ -319,8 +318,7 @@
319318
for (n = 0; n < j.files.length; n++) {
320319
var d = document.getElementById("d" + n);
321320
if (d)
322-
d.addEventListener("click",
323-
delfile, false);
321+
d.addEventListener("click", delfile, false);
324322
}
325323
};
326324

plugins/deaddrop/protocol_lws_deaddrop.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#ifdef WIN32
4242
#include <io.h>
4343
#endif
44+
#if !defined(WIN32)
45+
#include <limits.h>
46+
#endif
4447
#include <stdio.h>
4548
#include <errno.h>
4649

@@ -380,6 +383,11 @@ callback_deaddrop(struct lws *wsi, enum lws_callback_reasons reason,
380383
uint8_t buf[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE],
381384
*start = &buf[LWS_PRE], *p = start,
382385
*end = &buf[sizeof(buf) - 1];
386+
#if !defined(WIN32)
387+
char path[512], resolved_path[PATH_MAX];
388+
#else
389+
char path[512];
390+
#endif
383391
char fname[256], *wp;
384392
const char *cp;
385393
int n, m, was;
@@ -463,34 +471,52 @@ callback_deaddrop(struct lws *wsi, enum lws_callback_reasons reason,
463471
if (strncmp((const char *)in, "{\"del\":\"", 8))
464472
break;
465473

466-
cp = strchr((const char *)in, '/');
467-
if (cp) {
468-
n = (int)(((uint8_t *)cp - (uint8_t *)in)) - 8;
469-
470-
if ((int)strlen(pss->user) != n ||
471-
memcmp(pss->user, ((const char *)in) + 8, (unsigned int)n)) {
472-
lwsl_notice("%s: del: auth mismatch "
473-
" '%s' '%s' (%d)\n",
474-
__func__, pss->user,
475-
((const char *)in) + 8, n);
476-
break;
477-
}
478-
}
474+
/*
475+
* NOTE: any authenticated user can delete any file.
476+
* To restrict to owner, uncomment the following check.
477+
*/
478+
// cp = strchr((const char *)in, '/');
479+
// if (cp) {
480+
// n = (int)(((uint8_t *)cp - (uint8_t *)in)) - 8;
481+
//
482+
// if ((int)strlen(pss->user) != n ||
483+
// memcmp(pss->user, ((const char *)in) + 8, (unsigned int)n)) {
484+
// lwsl_notice("%s: del: auth mismatch "
485+
// " '%s' '%s' (%d)\n",
486+
// __func__, pss->user,
487+
// ((const char *)in) + 8, n);
488+
// break;
489+
// }
490+
// }
479491

480492
lws_strncpy(fname, ((const char *)in) + 8, sizeof(fname));
481-
lws_filename_purify_inplace(fname);
482493
wp = strchr((const char *)fname, '\"');
483494
if (wp)
484495
*wp = '\0';
496+
497+
lws_filename_purify_inplace(fname);
485498

486-
lws_snprintf((char *)buf, sizeof(buf), "%s/%s", vhd->upload_dir,
499+
lws_snprintf(path, sizeof(path), "%s/%s", vhd->upload_dir,
487500
fname);
488501

489-
lwsl_notice("%s: del: path %s\n", __func__, (const char *)buf);
502+
#if !defined(WIN32)
503+
if (!realpath(path, resolved_path)) {
504+
lwsl_warn("%s: delete: realpath failed %s\n", __func__, path);
505+
break;
506+
}
507+
508+
if (strncmp(resolved_path, vhd->upload_dir, strlen(vhd->upload_dir))) {
509+
lwsl_err("%s: illegal delete attempt '%s' -> '%s'\n", __func__, path, resolved_path);
510+
break;
511+
}
512+
lws_strncpy(path, resolved_path, sizeof(path));
513+
#endif
514+
515+
lwsl_notice("%s: deleting '%s'\n", __func__, path);
490516

491-
if (unlink((const char *)buf) < 0)
492-
lwsl_err("%s: unlink %s failed\n", __func__,
493-
(const char *)buf);
517+
if (unlink(path) < 0)
518+
lwsl_err("%s: unlink %s failed: %s\n", __func__,
519+
path, strerror(errno));
494520

495521
scan_upload_dir(vhd);
496522
break;

0 commit comments

Comments
 (0)