Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
Show All 25 Lines | |||||
#include "scriptinterface/FunctionWrapper.h" | #include "scriptinterface/FunctionWrapper.h" | ||||
#include "scriptinterface/JSON.h" | #include "scriptinterface/JSON.h" | ||||
#include <sstream> | #include <sstream> | ||||
namespace JSI_VFS | namespace JSI_VFS | ||||
{ | { | ||||
// Only allow engine compartments to read files they may be concerned about. | // Only allow engine compartments to read files they may be concerned about. | ||||
#define PathRestriction_GUI {L""} | #define PathRestriction_GUI {L"gui/", L"simulation/", L"maps/", L"campaigns/", L"saves/campaigns/", L"config/matchsettings.json", L"config/matchsettings.mp.json", L"moddata"} | ||||
#define PathRestriction_Simulation {L"simulation/"} | #define PathRestriction_Simulation {L"simulation/"} | ||||
#define PathRestriction_Maps {L"simulation/", L"maps/"} | #define PathRestriction_Maps {L"simulation/", L"maps/"} | ||||
// shared error handling code | // shared error handling code | ||||
#define JS_CHECK_FILE_ERR(err)\ | #define JS_CHECK_FILE_ERR(err)\ | ||||
/* this is liable to happen often, so don't complain */\ | /* this is liable to happen often, so don't complain */\ | ||||
if (err == ERR::VFS_FILE_NOT_FOUND)\ | if (err == ERR::VFS_FILE_NOT_FOUND)\ | ||||
{\ | {\ | ||||
Show All 16 Lines | bool PathRestrictionMet(const ScriptRequest& rq, const std::vector<CStrW>& validPaths, const CStrW& filePath) | ||||
for (std::size_t i = 0; i < validPaths.size(); ++i) | for (std::size_t i = 0; i < validPaths.size(); ++i) | ||||
{ | { | ||||
if (i != 0) | if (i != 0) | ||||
allowedPaths += L", "; | allowedPaths += L", "; | ||||
allowedPaths += L"\"" + validPaths[i] + L"\""; | allowedPaths += L"\"" + validPaths[i] + L"\""; | ||||
} | } | ||||
ScriptException::Raise(rq, "This part of the engine may only read from %s!", utf8_from_wstring(allowedPaths).c_str()); | ScriptException::Raise(rq, "Restricted access to %s. This part of the engine may only read from %s!", utf8_from_wstring(filePath).c_str(), utf8_from_wstring(allowedPaths).c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
// state held across multiple BuildDirEntListCB calls; init by BuildDirEntList. | // state held across multiple BuildDirEntListCB calls; init by BuildDirEntList. | ||||
struct BuildDirEntListState | struct BuildDirEntListState | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | unsigned int GetFileSize(const std::wstring& filename) | ||||
CFileInfo fileInfo; | CFileInfo fileInfo; | ||||
Status err = g_VFS->GetFileInfo(filename, &fileInfo); | Status err = g_VFS->GetFileInfo(filename, &fileInfo); | ||||
JS_CHECK_FILE_ERR(err); | JS_CHECK_FILE_ERR(err); | ||||
return (unsigned int)fileInfo.Size(); | return (unsigned int)fileInfo.Size(); | ||||
} | } | ||||
// Return file contents in a string. Assume file is UTF-8 encoded text. | // Return file contents in a string. Assume file is UTF-8 encoded text. | ||||
JS::Value ReadFile(const ScriptRequest& rq, const std::wstring& filename) | JS::Value ReadFile(const ScriptRequest& rq, const std::vector<CStrW>& validPaths, const CStrW& filename) | ||||
{ | { | ||||
if (!PathRestrictionMet(rq, validPaths, filename)) | |||||
return JS::NullValue(); | |||||
CVFSFile file; | CVFSFile file; | ||||
if (file.Load(g_VFS, filename) != PSRETURN_OK) | if (file.Load(g_VFS, filename) != PSRETURN_OK) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | ||||
// Fix CRLF line endings. (This function will only ever be used on text files.) | // Fix CRLF line endings. (This function will only ever be used on text files.) | ||||
contents.Replace("\r\n", "\n"); | contents.Replace("\r\n", "\n"); | ||||
// Decode as UTF-8 | // Decode as UTF-8 | ||||
JS::RootedValue ret(rq.cx); | JS::RootedValue ret(rq.cx); | ||||
Script::ToJSVal(rq, &ret, contents.FromUTF8()); | Script::ToJSVal(rq, &ret, contents.FromUTF8()); | ||||
return ret; | return ret; | ||||
} | } | ||||
// Return file contents as an array of lines. Assume file is UTF-8 encoded text. | // Return file contents as an array of lines. Assume file is UTF-8 encoded text. | ||||
JS::Value ReadFileLines(const ScriptRequest& rq, const std::wstring& filename) | JS::Value ReadFileLines(const ScriptRequest& rq, const std::vector<CStrW>& validPaths, const CStrW& filename) | ||||
{ | { | ||||
if (!PathRestrictionMet(rq, validPaths, filename)) | |||||
return JS::NullValue(); | |||||
CVFSFile file; | CVFSFile file; | ||||
if (file.Load(g_VFS, filename) != PSRETURN_OK) | if (file.Load(g_VFS, filename) != PSRETURN_OK) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | ||||
// Fix CRLF line endings. (This function will only ever be used on text files.) | // Fix CRLF line endings. (This function will only ever be used on text files.) | ||||
contents.Replace("\r\n", "\n"); | contents.Replace("\r\n", "\n"); | ||||
Show All 26 Lines | if (!PathRestrictionMet(rq, validPaths, filePath)) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
JS::RootedValue out(rq.cx); | JS::RootedValue out(rq.cx); | ||||
Script::ReadJSONFile(rq, filePath, &out); | Script::ReadJSONFile(rq, filePath, &out); | ||||
return out; | return out; | ||||
} | } | ||||
// Save given JS Object to a JSON file | // Save given JS Object to a JSON file | ||||
void WriteJSONFile(const ScriptInterface& scriptInterface, const std::wstring& filePath, JS::HandleValue val1) | void WriteJSONFile(const ScriptInterface& scriptInterface, const std::vector<CStrW>& validPaths, const CStrW& filePath, JS::HandleValue val1) | ||||
{ | { | ||||
ScriptRequest rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
if (!PathRestrictionMet(rq, validPaths, filePath)) | |||||
return; | |||||
// TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. | // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. | ||||
JS::RootedValue val(rq.cx, val1); | JS::RootedValue val(rq.cx, val1); | ||||
std::string str(Script::StringifyJSON(rq, &val, false)); | std::string str(Script::StringifyJSON(rq, &val, false)); | ||||
VfsPath path(filePath); | VfsPath path(filePath); | ||||
WriteBuffer buf; | WriteBuffer buf; | ||||
Show All 20 Lines | return VfsFileExists(filePath) && | ||||
wunlink(realPath) == 0; | wunlink(realPath) == 0; | ||||
} | } | ||||
#define VFS_ScriptFunctions(context)\ | #define VFS_ScriptFunctions(context)\ | ||||
JS::Value Script_ReadJSONFile_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath)\ | JS::Value Script_ReadJSONFile_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath)\ | ||||
{\ | {\ | ||||
return ReadJSONFile(scriptInterface, PathRestriction_##context, filePath);\ | return ReadJSONFile(scriptInterface, PathRestriction_##context, filePath);\ | ||||
}\ | }\ | ||||
void Script_WriteJSONFile_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath, JS::HandleValue val1)\ | |||||
{\ | |||||
return WriteJSONFile(scriptInterface, PathRestriction_##context, filePath, val1);\ | |||||
}\ | |||||
JS::Value Script_ReadFile_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath)\ | |||||
{\ | |||||
return ReadFile(scriptInterface, PathRestriction_##context, filePath);\ | |||||
}\ | |||||
JS::Value Script_ReadFileLines_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath)\ | |||||
{\ | |||||
return ReadFileLines(scriptInterface, PathRestriction_##context, filePath);\ | |||||
}\ | |||||
JS::Value Script_ListDirectoryFiles_##context(const ScriptInterface& scriptInterface, const std::wstring& path, const std::wstring& filterStr, bool recurse)\ | JS::Value Script_ListDirectoryFiles_##context(const ScriptInterface& scriptInterface, const std::wstring& path, const std::wstring& filterStr, bool recurse)\ | ||||
{\ | {\ | ||||
return BuildDirEntList(scriptInterface, PathRestriction_##context, path, filterStr, recurse);\ | return BuildDirEntList(scriptInterface, PathRestriction_##context, path, filterStr, recurse);\ | ||||
}\ | }\ | ||||
bool Script_FileExists_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath)\ | bool Script_FileExists_##context(const ScriptInterface& scriptInterface, const std::wstring& filePath)\ | ||||
{\ | {\ | ||||
return FileExists(scriptInterface, PathRestriction_##context, filePath);\ | return FileExists(scriptInterface, PathRestriction_##context, filePath);\ | ||||
}\ | }\ | ||||
VFS_ScriptFunctions(GUI); | VFS_ScriptFunctions(GUI); | ||||
VFS_ScriptFunctions(Simulation); | VFS_ScriptFunctions(Simulation); | ||||
VFS_ScriptFunctions(Maps); | VFS_ScriptFunctions(Maps); | ||||
#undef VFS_ScriptFunctions | #undef VFS_ScriptFunctions | ||||
void RegisterScriptFunctions_ReadWriteAnywhere(const ScriptRequest& rq) | void RegisterScriptFunctions_ReadWriteAnywhere(const ScriptRequest& rq) | ||||
{ | { | ||||
ScriptFunction::Register<&Script_ListDirectoryFiles_GUI>(rq, "ListDirectoryFiles"); | ScriptFunction::Register<&Script_ListDirectoryFiles_GUI>(rq, "ListDirectoryFiles"); | ||||
ScriptFunction::Register<&Script_FileExists_GUI>(rq, "FileExists"); | ScriptFunction::Register<&Script_FileExists_GUI>(rq, "FileExists"); | ||||
ScriptFunction::Register<&GetFileMTime>(rq, "GetFileMTime"); | ScriptFunction::Register<&GetFileMTime>(rq, "GetFileMTime"); | ||||
ScriptFunction::Register<&GetFileSize>(rq, "GetFileSize"); | ScriptFunction::Register<&GetFileSize>(rq, "GetFileSize"); | ||||
ScriptFunction::Register<&ReadFile>(rq, "ReadFile"); | ScriptFunction::Register<&Script_ReadFile_GUI>(rq, "ReadFile"); | ||||
ScriptFunction::Register<&ReadFileLines>(rq, "ReadFileLines"); | ScriptFunction::Register<&Script_ReadFileLines_GUI>(rq, "ReadFileLines"); | ||||
ScriptFunction::Register<&Script_ReadJSONFile_GUI>(rq, "ReadJSONFile"); | ScriptFunction::Register<&Script_ReadJSONFile_GUI>(rq, "ReadJSONFile"); | ||||
ScriptFunction::Register<&WriteJSONFile>(rq, "WriteJSONFile"); | ScriptFunction::Register<&Script_WriteJSONFile_GUI>(rq, "WriteJSONFile"); | ||||
ScriptFunction::Register<&DeleteCampaignSave>(rq, "DeleteCampaignSave"); | ScriptFunction::Register<&DeleteCampaignSave>(rq, "DeleteCampaignSave"); | ||||
} | } | ||||
void RegisterScriptFunctions_ReadOnlySimulation(const ScriptRequest& rq) | void RegisterScriptFunctions_ReadOnlySimulation(const ScriptRequest& rq) | ||||
{ | { | ||||
ScriptFunction::Register<&Script_ListDirectoryFiles_Simulation>(rq, "ListDirectoryFiles"); | ScriptFunction::Register<&Script_ListDirectoryFiles_Simulation>(rq, "ListDirectoryFiles"); | ||||
ScriptFunction::Register<&Script_FileExists_Simulation>(rq, "FileExists"); | ScriptFunction::Register<&Script_FileExists_Simulation>(rq, "FileExists"); | ||||
ScriptFunction::Register<&Script_ReadJSONFile_Simulation>(rq, "ReadJSONFile"); | ScriptFunction::Register<&Script_ReadJSONFile_Simulation>(rq, "ReadJSONFile"); | ||||
Show All 9 Lines |
Wildfire Games · Phabricator