Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/shared/map_validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,34 @@ path separators, traversal tokens, or other illegal characters.
=============
*/
inline bool G_SanitizeMapPoolFilename(std::string_view rawName, std::string& sanitized, std::string& rejectReason)
{
{
const size_t start = rawName.find_first_not_of(" \t\r\n");
if (start == std::string_view::npos) {
sanitized.clear();
rejectReason = "is empty";
return false;
sanitized.clear();
rejectReason = "is empty";
return false;
}

const size_t end = rawName.find_last_not_of(" \t\r\n");
std::string candidate(rawName.substr(start, end - start + 1));

if (candidate.find('/') != std::string::npos || candidate.find('\\') != std::string::npos) {
rejectReason = "contains path separators";
return false;
rejectReason = "contains path separators";
return false;
}

if (candidate == "." || candidate == ".." || candidate.find("..") != std::string::npos) {
rejectReason = "contains traversal tokens";
return false;
rejectReason = "contains traversal tokens";
return false;
}

if (!G_IsValidMapIdentifier(candidate)) {
rejectReason = "contains illegal characters";
return false;
rejectReason = "contains illegal characters";
return false;
}

sanitized = std::move(candidate);
rejectReason.clear();
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/test_map_pool_sanitization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ int main()

std::vector<std::string> accepted;
std::vector<std::string> rejectedReasons;
std::string reason;

for (const auto& entry : root["maps"]) {
const std::string bspName = entry["bsp"].asString();
std::string sanitized;
std::string reason;
if (G_SanitizeMapPoolFilename(bspName, sanitized, reason)) {
assert(reason.empty());
accepted.push_back(sanitized);
}
else {
assert(!reason.empty());
rejectedReasons.push_back(reason);
}
}
Expand Down