Debugger: Lua - Improve error messages when io/os/lua access is turned off in the options

This commit is contained in:
Sour 2024-07-19 18:34:42 +09:00
parent 8ad5423be5
commit d0c9deb477
4 changed files with 25 additions and 4 deletions

View file

@ -11,9 +11,10 @@
#include "Debugger/ScriptManager.h"
#include "Shared/Emulator.h"
#include "Shared/EmuSettings.h"
#include "Shared/EventType.h"
#include "Shared/SaveStateManager.h"
#include "Utilities/magic_enum.hpp"
#include "Shared/EventType.h"
#include "Utilities/StringUtilities.h"
ScriptingContext* ScriptingContext::_context = nullptr;
@ -104,11 +105,23 @@ bool ScriptingContext::LoadScript(string scriptName, string path, string scriptC
}
if(lua_isstring(_lua, -1)) {
Log(lua_tostring(_lua, -1));
ProcessLuaError();
}
return false;
}
void ScriptingContext::ProcessLuaError()
{
string errorMsg = lua_tostring(_lua, -1);
if(StringUtilities::Contains(errorMsg, "attempt to call a nil value (global 'require')") || StringUtilities::Contains(errorMsg, "attempt to index a nil value (global 'os')") || StringUtilities::Contains(errorMsg, "attempt to index a nil value (global 'io')")) {
Log("I/O and OS libraries are disabled by default for security.\nYou can enable them here:\nScript->Settings->Script Window->Restrictions->Allow access to I/O and OS functions.");
} else if(StringUtilities::Contains(errorMsg, "module 'socket.core' not found")) {
Log("Lua sockets are disabled by default for security.\nYou can enable them here:\nScript->Settings->Script Window->Restrictions->Allow network access.");
} else {
Log(errorMsg);
}
}
void ScriptingContext::ExecutionCountHook(lua_State* lua)
{
uint32_t timeout = _context->_settings->GetDebugConfig().ScriptTimeout;
@ -308,7 +321,7 @@ void ScriptingContext::InternalCallMemoryCallback(AddressInfo relAddr, T& value,
lua_pushinteger(_lua, relAddr.Address);
lua_pushinteger(_lua, value);
if(lua_pcall(_lua, 2, LUA_MULTRET, 0) != 0) {
Log(lua_tostring(_lua, -1));
ProcessLuaError();
} else {
int returnParamCount = lua_gettop(_lua) - top;
if(returnParamCount && lua_isinteger(_lua, -1)) {
@ -335,7 +348,7 @@ int ScriptingContext::CallEventCallback(EventType type, CpuType cpuType)
lua_rawgeti(_lua, LUA_REGISTRYINDEX, ref);
lua_pushinteger(_lua, (int)cpuType);
if(lua_pcall(_lua, 1, 0, 0) != 0) {
Log(lua_tostring(_lua, -1));
ProcessLuaError();
}
}
return l.ReturnCount();

View file

@ -51,6 +51,7 @@ private:
static void ExecutionCountHook(lua_State* lua);
void LuaOpenLibs(lua_State* L, bool allowIoOsAccess);
void ProcessLuaError();
protected:
string _scriptName;

View file

@ -777,6 +777,7 @@ int SANDBOX_ALLOW_LOADFILE = 0;
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
const char *mode) {
if (!SANDBOX_ALLOW_LOADFILE) {
luaL_error(L, "I/O functions are disabled by default for security.\nYou can enable them here:\nScript->Settings->Script Window->Restrictions->Allow access to I/O and OS functions.");
return LUA_ERRERR;
}
// ##### MESEN MODIFICATION #####

View file

@ -72,6 +72,12 @@ public:
}
return true;
}
static bool Contains(string& str, const char* content)
{
size_t length = strlen(content);
return std::search(str.begin(), str.end(), content, content+length) != str.end();
}
static string GetString(char* src, uint32_t maxLen)
{