mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-13 10:15:00 +00:00
Merge commit from fork
avoid OOB reads in path_is_sane
This commit is contained in:
+9
-6
@@ -143,13 +143,16 @@ 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?
|
||||
if (s[1] == '.' && s[2] == '.') return false; // Starts with ..
|
||||
}
|
||||
if (n == 0 || path.buf[0] == '\0') return true;
|
||||
if (s[0] == '~') return false; // Starts with ~
|
||||
if (s[0] == '.' && n > 1 && s[1] == '.')
|
||||
return false; // Starts with ..
|
||||
for (; n > 0 && s[0] != '\0'; s++, n--) {
|
||||
if ((s[0] == '/' || s[0] == '\\') && n >= 2 && s[1] == '.' && n > 2 &&
|
||||
s[2] == '.')
|
||||
return false; // Subdir starts with ..
|
||||
}
|
||||
if (n > 0) return false; // embedded nul (terminator not counted in len)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user