Skip to content

Commit

Permalink
Add extra filename blacklist checks
Browse files Browse the repository at this point in the history
Check for invalid UTF-8, overly long filenames, surrogate pairs and all control characters.
  • Loading branch information
AI0867 authored and GregoryLundberg committed Nov 30, 2017
1 parent 389df64 commit 14af994
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/addon/validation.cpp
Expand Up @@ -15,6 +15,7 @@

#include "addon/validation.hpp"
#include "config.hpp"
#include "serialization/unicode_cast.hpp"

#include <algorithm>
#include <boost/algorithm/string.hpp>
Expand Down Expand Up @@ -46,6 +47,27 @@ namespace {
}
}
};

struct addon_filename_ucs4char_illegal
{
inline bool operator()(ucs4::char_t c) const
{
switch(c){
case ' ':
case '/':
case ':':
case '\\':
case '~':
case 0x7F: // DEL
return true;
default:
return (
c < 0x20 || // control characters
(c >= 0xD800 && c < 0xE000) // surrogate pairs
);
}
}
};
}

bool addon_name_legal(const std::string& name)
Expand All @@ -61,11 +83,16 @@ bool addon_name_legal(const std::string& name)
bool addon_filename_legal(const std::string& name)
{
if(name.empty() || name.back() == '.' ||
name.find_first_of("/:\\~ \r\n\v\t") != std::string::npos ||
name.find("..") != std::string::npos) {
name.find("..") != std::string::npos ||
name.size() > 255) {
return false;
} else {
return true;
const ucs4::string name_ucs4 = unicode_cast<ucs4::string>(name);
const std::string name_utf8 = unicode_cast<utf8::string>(name_ucs4);
if(name != name_utf8){ // name is invalid UTF-8
return false;
}
return std::find_if(name_ucs4.begin(), name_ucs4.end(), addon_filename_ucs4char_illegal()) == name_ucs4.end();
}
}

Expand Down

0 comments on commit 14af994

Please sign in to comment.