MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / openSafeLibs

Function openSafeLibs

app/src/DataModel/Scripting/LuaScriptEngine.cpp:90–114  ·  view source on GitHub ↗

* @brief Opens the safe standard libraries and strips dangerous globals. */

Source from the content-addressed store, hash-verified

88 * @brief Opens the safe standard libraries and strips dangerous globals.
89 */
90static void openSafeLibs(lua_State* L)
91{
92 Q_ASSERT(L != nullptr);
93
94 for (const luaL_Reg* lib = kSafeLibs; lib->func; ++lib) {
95 luaL_requiref(L, lib->name, lib->func, 1);
96 lua_pop(L, 1);
97 }
98
99 static const char* const kUnsafe[] = {"dofile", "loadfile", "load"};
100 for (const char* name : kUnsafe) {
101 lua_pushnil(L);
102 lua_setglobal(L, name);
103 }
104
105 // code-verify off
106 // Strip string.dump: bytecode exfiltration vector that pairs with unsafe loaders.
107 lua_getglobal(L, "string");
108 if (lua_istable(L, -1)) {
109 lua_pushnil(L);
110 lua_setfield(L, -2, "dump");
111 }
112 lua_pop(L, 1);
113 // code-verify on
114}
115
116//--------------------------------------------------------------------------------------------------
117// Constructor & destructor

Callers 1

createStateMethod · 0.70

Calls 5

luaL_requirefFunction · 0.85
lua_pushnilFunction · 0.85
lua_setglobalFunction · 0.85
lua_getglobalFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected