Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blacklist filename chars #2077

Merged
merged 6 commits into from Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/addon/client.cpp
Expand Up @@ -134,7 +134,14 @@ bool addons_client::upload_addon(const std::string& id, std::string& response_me
cfg["name"] = id;

config addon_data;
archive_addon(id, addon_data);
try {
archive_addon(id, addon_data);
} catch(utf8::invalid_utf8_exception&){
this->last_error_ =
vgettext("The add-on <i>$addon_title</i> has a file or directory "
"containing invalid characters and cannot be published.", i18n_symbols);
return false;
}

config request_buf, response_buf;
request_buf.add_child("upload", cfg).add_child("data", addon_data);
Expand Down
47 changes: 40 additions & 7 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>

Expand Down Expand Up @@ -44,13 +45,40 @@ namespace {
}
}
};

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

bool addon_name_legal(const std::string& name)
{
if(name.empty() || name == "." ||
std::find_if(name.begin(), name.end(), addon_name_char_illegal()) != name.end() ||
name.find("..") != std::string::npos) {
if(name.empty() ||
std::find_if(name.begin(), name.end(), addon_name_char_illegal()) != name.end()) {
return false;
} else {
return true;
Expand All @@ -59,12 +87,17 @@ bool addon_name_legal(const std::string& name)

bool addon_filename_legal(const std::string& name)
{
if(name.empty() || name == "." ||
name.find_first_of("/:\\~ \r\n\v\t") != std::string::npos ||
name.find("..") != std::string::npos) {
if(name.empty() || name.back() == '.' ||
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you test that this check works? I'd think that unicode_cast simply throws an exception in case of invalid utf8

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See unicode_cast.hpp. The cast swallows the exception and returns the part of the string that it was able to convert.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this yesterday. It works, but a different check failed in archive_addon. Specifically filesystem::looks_like_pbl failed because it uses utf8::lowercase. I've added a try-catch block for this.

return false;
}
return std::find_if(name_ucs4.begin(), name_ucs4.end(), addon_filename_ucs4char_illegal()) == name_ucs4.end();
}
}

Expand Down