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

[Safe I/O] Only allow creating files with whitelisted filetypes #682

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
18 changes: 18 additions & 0 deletions primedev/mods/modsavefiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ 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("SAVE FAILED!");
Copy link
Contributor

Choose a reason for hiding this comment

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

See other comment

spdlog::error("No file extension specified");
}

// TODO: move into list of global consts?
std::set<std::string> whitelist = {".txt", ".json"};
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for this being a set instead of like an vector or array or is it just an arbitrary choice?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly arbitrary choice

Copy link
Member

Choose a reason for hiding this comment

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

Then it should probably be a vector.

Copy link
Contributor

@barnabwhy barnabwhy Aug 9, 2024

Choose a reason for hiding this comment

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

can probably also be made static const
compiler might already optimise it to static idrk but just in case it isn't doing that, this'll prevent unnecessary allocs

Copy link
Member Author

Choose a reason for hiding this comment

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

@barnabwhy done via 314b86a

@catornot going back to this, wouldn't a set make more sense as soon as the list of extensions gets quite long? Like rn it's only 2 cause that's the two most important plaintext filetypes I could think of but the idea is that the list would get extended quite a bit later down the line.

Copy link
Member

Choose a reason for hiding this comment

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

how many filetypes will you need?


// Check if file extension is whitelisted
std::string extension = file.extension().string();
if (whitelist.find(extension) == whitelist.end())
{
spdlog::error("SAVE FAILED!");
Copy link
Contributor

Choose a reason for hiding this comment

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

This should maybe make it more clear that it's an issue with a mod so users don't freak out.

spdlog::error("Disallowed file extension: {}", extension);
}

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