diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.c | 37 | ||||
-rw-r--r-- | src/shared/util.h | 3 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index d2ca3fc783..64d6e62a53 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -56,6 +56,7 @@ #include <sys/mman.h> #include <sys/vfs.h> #include <linux/magic.h> +#include <limits.h> #include "macro.h" #include "util.h" @@ -5851,3 +5852,39 @@ void closedirp(DIR **d) { void umaskp(mode_t *u) { umask(*u); } + +bool filename_is_safe(const char *p) { + + if (isempty(p)) + return false; + + if (strchr(p, '/')) + return false; + + if (streq(p, ".")) + return false; + + if (streq(p, "..")) + return false; + + if (strlen(p) > FILENAME_MAX) + return false; + + return true; +} + +bool string_is_safe(const char *p) { + const char *t; + + assert(p); + + for (t = p; *t; t++) { + if (*p < ' ') + return false; + + if (strchr("\\\"\'", *p)) + return false; + } + + return true; +} diff --git a/src/shared/util.h b/src/shared/util.h index 61b88a8b2e..cbded08617 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -558,3 +558,6 @@ _malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b) return memdup(p, a * b); } + +bool filename_is_safe(const char *p); +bool string_is_safe(const char *p); |