Skip to content

Commit

Permalink
mods(Safe I/O): Only allow creating files with whitelisted filetypes (#…
Browse files Browse the repository at this point in the history
…682)

Restricts file types that can be created via Safe I/O to a list of whitelisted file types
  • Loading branch information
GeckoEidechse authored Nov 22, 2024
1 parent 6585d62 commit db40260
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions primedev/mods/modsavefiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ template <ScriptContext context> void SaveFileManager::SaveFileAsync(fs::path fi
std::thread writeThread(
[mutex, file, contents]()
{
// Check if has extension and return early if not
if (!file.has_extension())
{
spdlog::error("A mod failed to save a file via Safe I/O due to the following error:");
spdlog::error("No file extension specified");
return;
}

// If there's a file extension missing here that you need, feel free to make a PR adding it
static const std::set<std::string> whitelist = {".txt", ".json"};

// Check if file extension is whitelisted
std::string extension = file.extension().string();
if (whitelist.find(extension) == whitelist.end())
{
spdlog::error("A mod failed to save a file via Safe I/O due to the following error:");
spdlog::error("Disallowed file extension: {}", extension);
return;
}

try
{
mutex.get().lock();
Expand Down

0 comments on commit db40260

Please sign in to comment.