Skip to content

Commit 6c06330

Browse files
committedMay 16, 2015
Add core.request_insecure_environment()
1 parent 05ab997 commit 6c06330

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed
 

‎doc/lua_api.txt

+10-3
Original file line numberDiff line numberDiff line change
@@ -2290,13 +2290,20 @@ These functions return the leftover itemstack.
22902290
* `minetest.forceload_block(pos)`
22912291
* forceloads the position `pos`.
22922292
* returns `true` if area could be forceloaded
2293+
* Please note that forceloaded areas are saved when the server restarts.
22932294

22942295
* `minetest.forceload_free_block(pos)`
22952296
* stops forceloading the position `pos`
2296-
Please note that forceloaded areas are saved when the server restarts.
22972297

2298-
minetest.global_exists(name)
2299-
^ Checks if a global variable has been set, without triggering a warning.
2298+
* `minetest.request_insecure_environment()`: returns an environment containing
2299+
insecure functions if the calling mod has been listed as trusted in the
2300+
`secure.trusted_mods` setting or security is disabled, otherwise returns `nil`.
2301+
* Only works at init time.
2302+
* **DO NOT ALLOW ANY OTHER MODS TO ACCESS THE RETURNED ENVIRONMENT, STORE IT IN
2303+
A LOCAL VARIABLE!**
2304+
2305+
* `minetest.global_exists(name)`
2306+
* Checks if a global variable has been set, without triggering a warning.
23002307

23012308
### Global objects
23022309
* `minetest.env`: `EnvRef` of the server environment and world.

‎minetest.conf.example

+3
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,7 @@
571571

572572
# Prevent mods from doing insecure things like running shell commands.
573573
#secure.enable_security = false
574+
# Comma-separated list of trusted mods that are allowed to access insecure
575+
# functions even when mod security is on (via request_insecure_environment()).
576+
#secure.trusted_mods =
574577

‎src/defaultsettings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ void set_default_settings(Settings *settings)
273273
settings->setDefault("emergequeue_limit_generate", "32");
274274
settings->setDefault("num_emerge_threads", "1");
275275
settings->setDefault("secure.enable_security", "false");
276+
settings->setDefault("secure.trusted_mods", "");
276277

277278
// physics stuff
278279
settings->setDefault("movement_acceleration_default", "3");

‎src/script/lua_api/l_util.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#include "filesys.h"
3333
#include "settings.h"
3434
#include "util/auth.h"
35+
#include <algorithm>
3536

3637
// debug(...)
3738
// Writes a line to dstream
@@ -316,7 +317,7 @@ int ModApiUtil::l_compress(lua_State *L)
316317
int ModApiUtil::l_decompress(lua_State *L)
317318
{
318319
size_t size;
319-
const char * data = luaL_checklstring(L, 1, &size);
320+
const char *data = luaL_checklstring(L, 1, &size);
320321

321322
std::istringstream is(std::string(data, size));
322323
std::ostringstream os;
@@ -339,6 +340,30 @@ int ModApiUtil::l_mkdir(lua_State *L)
339340
}
340341

341342

343+
int ModApiUtil::l_request_insecure_environment(lua_State *L)
344+
{
345+
NO_MAP_LOCK_REQUIRED;
346+
if (!ScriptApiSecurity::isSecure(L)) {
347+
lua_getglobal(L, "_G");
348+
return 1;
349+
}
350+
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
351+
if (!lua_isstring(L, -1)) {
352+
lua_pushnil(L);
353+
return 1;
354+
}
355+
const char *mod_name = lua_tostring(L, -1);
356+
std::string trusted_mods = g_settings->get("secure.trusted_mods");
357+
std::vector<std::string> mod_list = str_split(trusted_mods, ',');
358+
if (std::find(mod_list.begin(), mod_list.end(), mod_name) == mod_list.end()) {
359+
lua_pushnil(L);
360+
return 1;
361+
}
362+
lua_getfield(L, LUA_REGISTRYINDEX, "globals_backup");
363+
return 1;
364+
}
365+
366+
342367
void ModApiUtil::Initialize(lua_State *L, int top)
343368
{
344369
API_FCT(debug);
@@ -366,6 +391,8 @@ void ModApiUtil::Initialize(lua_State *L, int top)
366391
API_FCT(decompress);
367392

368393
API_FCT(mkdir);
394+
395+
API_FCT(request_insecure_environment);
369396
}
370397

371398
void ModApiUtil::InitializeAsync(AsyncEngine& engine)

‎src/script/lua_api/l_util.h

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class ModApiUtil : public ModApiBase {
9090
// mkdir(path)
9191
static int l_mkdir(lua_State *L);
9292

93+
// request_insecure_environment()
94+
static int l_request_insecure_environment(lua_State *L);
95+
9396
public:
9497
static void Initialize(lua_State *L, int top);
9598

@@ -98,3 +101,4 @@ class ModApiUtil : public ModApiBase {
98101
};
99102

100103
#endif /* L_UTIL_H_ */
104+

0 commit comments

Comments
 (0)
Please sign in to comment.