From a801160bc03c0c8a873ed2799875dc4776036377 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Wed, 21 May 2025 10:43:14 -0300 Subject: [PATCH] collaterals #3140 We prepend current path to the URI, so a tilde could not be the first char in a path. However, the same would happen for double dots, and since we're already checking for that, it doesn't hurt to be on the safe side for future's sake. --- mongoose.c | 1 + src/http.c | 2 +- src/util.c | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/mongoose.c b/mongoose.c index e84252e3..4b5ce8d9 100644 --- a/mongoose.c +++ b/mongoose.c @@ -19786,6 +19786,7 @@ int mg_check_ip_acl(struct mg_str acl, struct mg_addr *remote_ip) { bool mg_path_is_sane(const struct mg_str path) { const char *s = path.buf; size_t n = path.len; + if (path.buf[0] == '~') return false; // Starts with ~ if (path.buf[0] == '.' && path.buf[1] == '.') return false; // Starts with .. for (; s[0] != '\0' && n > 0; s++, n--) { if ((s[0] == '/' || s[0] == '\\') && n >= 2) { // Subdir? diff --git a/src/http.c b/src/http.c index d8a7ddd6..2cae9909 100644 --- a/src/http.c +++ b/src/http.c @@ -216,7 +216,7 @@ static bool vcb(uint8_t c) { static size_t clen(const char *s, const char *end) { const unsigned char *u = (unsigned char *) s, c = *u; long n = (long) (end - s); - if (c > ' ' && c < '~') return 1; // Usual ascii printed char + if (c > ' ' && c <= '~') return 1; // Usual ascii printed char if ((c & 0xe0) == 0xc0 && n > 1 && vcb(u[1])) return 2; // 2-byte UTF8 if ((c & 0xf0) == 0xe0 && n > 2 && vcb(u[1]) && vcb(u[2])) return 3; if ((c & 0xf8) == 0xf0 && n > 3 && vcb(u[1]) && vcb(u[2]) && vcb(u[3])) diff --git a/src/util.c b/src/util.c index 1d2b43dc..d25278f1 100644 --- a/src/util.c +++ b/src/util.c @@ -127,6 +127,7 @@ int mg_check_ip_acl(struct mg_str acl, struct mg_addr *remote_ip) { bool mg_path_is_sane(const struct mg_str path) { const char *s = path.buf; size_t n = path.len; + if (path.buf[0] == '~') return false; // Starts with ~ if (path.buf[0] == '.' && path.buf[1] == '.') return false; // Starts with .. for (; s[0] != '\0' && n > 0; s++, n--) { if ((s[0] == '/' || s[0] == '\\') && n >= 2) { // Subdir?