* @brief Opens the safe standard libraries and strips dangerous globals. */
| 88 | * @brief Opens the safe standard libraries and strips dangerous globals. |
| 89 | */ |
| 90 | static 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 |
no test coverage detected