summaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_base.cpp
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2014-09-05 20:08:51 -0400
committerShadowNinja <shadowninja@minetest.net>2015-05-16 18:32:31 -0400
commit3a8c7888807e4483bbdb3edd81c9893f3e2f427d (patch)
tree81f339e5f61b03e8d7842e06f034d09bf59dba96 /src/script/cpp_api/s_base.cpp
parentf26421228bbd31f02bf16b45a4b82be84f233e52 (diff)
downloadminetest-3a8c7888807e4483bbdb3edd81c9893f3e2f427d.tar.gz
minetest-3a8c7888807e4483bbdb3edd81c9893f3e2f427d.tar.bz2
minetest-3a8c7888807e4483bbdb3edd81c9893f3e2f427d.zip
Add mod security
Due to compatibility concerns, this is temporarily disabled.
Diffstat (limited to 'src/script/cpp_api/s_base.cpp')
-rw-r--r--src/script/cpp_api/s_base.cpp44
1 files changed, 22 insertions, 22 deletions
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index 71473d215..4fb8411ef 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_base.h"
#include "cpp_api/s_internal.h"
+#include "cpp_api/s_security.h"
#include "lua_api/l_object.h"
#include "serverobject.h"
#include "debug.h"
@@ -45,18 +46,18 @@ class ModNameStorer
private:
lua_State *L;
public:
- ModNameStorer(lua_State *L_, const std::string &modname):
+ ModNameStorer(lua_State *L_, const std::string &mod_name):
L(L_)
{
- // Store current modname in registry
- lua_pushstring(L, modname.c_str());
- lua_setfield(L, LUA_REGISTRYINDEX, "current_modname");
+ // Store current mod name in registry
+ lua_pushstring(L, mod_name.c_str());
+ lua_setfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
}
~ModNameStorer()
{
- // Clear current modname in registry
+ // Clear current mod name from registry
lua_pushnil(L);
- lua_setfield(L, LUA_REGISTRYINDEX, "current_modname");
+ lua_setfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
}
};
@@ -112,32 +113,31 @@ ScriptApiBase::~ScriptApiBase()
lua_close(m_luastack);
}
-bool ScriptApiBase::loadMod(const std::string &scriptpath,
- const std::string &modname)
+bool ScriptApiBase::loadMod(const std::string &script_path,
+ const std::string &mod_name)
{
- ModNameStorer modnamestorer(getStack(), modname);
+ ModNameStorer mod_name_storer(getStack(), mod_name);
- if (!string_allowed(modname, MODNAME_ALLOWED_CHARS)) {
- errorstream<<"Error loading mod \""<<modname
- <<"\": modname does not follow naming conventions: "
- <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
- return false;
- }
-
- return loadScript(scriptpath);
+ return loadScript(script_path);
}
-bool ScriptApiBase::loadScript(const std::string &scriptpath)
+bool ScriptApiBase::loadScript(const std::string &script_path)
{
- verbosestream<<"Loading and running script from "<<scriptpath<<std::endl;
+ verbosestream << "Loading and running script from " << script_path << std::endl;
lua_State *L = getStack();
- int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, m_errorhandler);
- if (ret) {
+ bool ok;
+ if (m_secure) {
+ ok = ScriptApiSecurity::safeLoadFile(L, script_path.c_str());
+ } else {
+ ok = !luaL_loadfile(L, script_path.c_str());
+ }
+ ok = ok && !lua_pcall(L, 0, 0, m_errorhandler);
+ if (!ok) {
errorstream << "========== ERROR FROM LUA ===========" << std::endl;
errorstream << "Failed to load and run script from " << std::endl;
- errorstream << scriptpath << ":" << std::endl;
+ errorstream << script_path << ":" << std::endl;
errorstream << std::endl;
errorstream << lua_tostring(L, -1) << std::endl;
errorstream << std::endl;