mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-26 12:14:56 +00:00
enhanced path sanitizing
This commit is contained in:
1 parent
4b0e34cdf4
commit
2f1a836bf0
7 files changed
+86
-79
No files matched your search
@@ -187,23 +187,12 @@ void mg_unhex(const char *buf, size_t len, unsigned char *to) {
|
||||
}
|
||||
}
|
||||
|
||||
char *mg_remove_double_dots(char *s) {
|
||||
char *saved = s, *p = s;
|
||||
while (*s != '\0') {
|
||||
*p++ = *s++;
|
||||
if (s[-1] == '/' || s[-1] == '\\') {
|
||||
while (s[0] != '\0') {
|
||||
if (s[0] == '/' || s[0] == '\\') {
|
||||
s++;
|
||||
} else if (s[0] == '.' && s[1] == '.' &&
|
||||
(s[2] == '/' || s[2] == '\\')) {
|
||||
s += 2;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool mg_path_is_sane(const char *path) {
|
||||
const char *s = path;
|
||||
for (; s[0] != '\0'; s++) {
|
||||
if (s == path || s[0] == '/' || s[0] == '\\') { // Subdir?
|
||||
if (s[1] == '.' && s[2] == '.') return false; // Starts with ..
|
||||
}
|
||||
}
|
||||
*p = '\0';
|
||||
return saved;
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user