|
41 | 41 | #ifdef WIN32 |
42 | 42 | #include <io.h> |
43 | 43 | #endif |
| 44 | +#if !defined(WIN32) |
| 45 | +#include <limits.h> |
| 46 | +#endif |
44 | 47 | #include <stdio.h> |
45 | 48 | #include <errno.h> |
46 | 49 |
|
@@ -380,6 +383,11 @@ callback_deaddrop(struct lws *wsi, enum lws_callback_reasons reason, |
380 | 383 | uint8_t buf[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE], |
381 | 384 | *start = &buf[LWS_PRE], *p = start, |
382 | 385 | *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 |
383 | 391 | char fname[256], *wp; |
384 | 392 | const char *cp; |
385 | 393 | int n, m, was; |
@@ -463,34 +471,52 @@ callback_deaddrop(struct lws *wsi, enum lws_callback_reasons reason, |
463 | 471 | if (strncmp((const char *)in, "{\"del\":\"", 8)) |
464 | 472 | break; |
465 | 473 |
|
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 | + // } |
479 | 491 |
|
480 | 492 | lws_strncpy(fname, ((const char *)in) + 8, sizeof(fname)); |
481 | | - lws_filename_purify_inplace(fname); |
482 | 493 | wp = strchr((const char *)fname, '\"'); |
483 | 494 | if (wp) |
484 | 495 | *wp = '\0'; |
| 496 | + |
| 497 | + lws_filename_purify_inplace(fname); |
485 | 498 |
|
486 | | - lws_snprintf((char *)buf, sizeof(buf), "%s/%s", vhd->upload_dir, |
| 499 | + lws_snprintf(path, sizeof(path), "%s/%s", vhd->upload_dir, |
487 | 500 | fname); |
488 | 501 |
|
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); |
490 | 516 |
|
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)); |
494 | 520 |
|
495 | 521 | scan_upload_dir(vhd); |
496 | 522 | break; |
|
0 commit comments