file upload handler: improve check for filename validity

The file upload handler checked for ".." in the filename.
Also check for control chars and for starting with /.
Put this into a separate function, is_valid_filename().
This commit is contained in:
David Anderson 2015-11-18 16:08:47 -08:00
parent 1dcbac0bf1
commit f4c72320a3
3 changed files with 26 additions and 3 deletions

View File

@ -755,4 +755,25 @@ vector<string> split(string s, char delim) {
result.push_back(item);
}
return result;
}
}
// check whether filename is legit
// - can't start with /
// - can't have control chars
// - can't have ..
//
bool is_valid_filename(const char* name) {
int n = strlen(name);
for (int i=0; i<n; i++) {
if (iscntrl(name[i])) {
return false;
}
}
if (strstr(name, "..")) {
return false;
}
if (name[0] == '/') {
return false;
}
return true;
}

View File

@ -102,4 +102,6 @@ extern void strip_translation(char* p);
extern std::vector<std::string> split(std::string, char delim);
extern bool is_valid_filename(const char*);
#endif

View File

@ -392,9 +392,9 @@ int handle_file_upload(FILE* in, R_RSA_PUBLIC_KEY& key) {
// make sure filename is legit
//
if (strstr(name, "..")) {
if (!is_valid_filename(name)) {
return return_error(ERR_PERMANENT,
"file_upload_handler: .. found in filename: %s",
"file_upload_handler: invalid filename: %s",
name
);
}