use datafile_path, and cannot use shell metacharacters (not that it matters) and ../ paths. Multiple loading is not prevented; we could probably add another function that does that.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1614 c06c8d41-db1a-0410-9941-cceddc491573
AVSMB4Y6F6ZMMNNPOAQQZ34OWC6N5JOURTEWFTUKDWMIIMLWWJUAC
BWAQ3FHBBM6G3K3KYP75CRTR343RDQZJRYX5ZGYUEXYBAC3APDLAC
Q2FZIIGQECGP2FKKWLGDBBQVGCFZ4JTY53PFPJP6X7YKC23AQGFQC
2NCKGJDDPPGP2NXYYPEPVRJIIWEP6M7HE6WYMQN3UNNN3C2JIRFQC
K2CS6TCX2NDVL2ASEHGP4J4K4IJ6FP3ANNKTSIWVG43HPYSBX6ZQC
SDLKLUNFGVKDS55DDJZCBAVIB7NL3RRYPTACAY65SCUQKV6APFSAC
YE7M665QKDGI7Y5WMERCWJNDZ4FUZ6GRUCK4E6GZH4SWCUM6RWLAC
}
bool CLua::is_path_safe(const char *s)
{
return (!strstr(s, "..") && shell_safe(s));
}
int CLua::loadfile(lua_State *ls, const char *filename)
{
if (!ls)
return (-1);
if (!is_path_safe(filename))
{
lua_pushstring(
ls,
make_stringf("invalid filename: %s", filename).c_str());
return (-1);
}
const std::string file = datafile_path(filename, false);
return (luaL_loadfile(ls, file.c_str()));
guard_pcall();
}
void CLua::guard_pcall()
{
// Replace Lua's pcall() with our own version which doesn't swallow
// 666: errors that we generate for buggy or malicious scripts that try
// to hog CPU.
lua_register(_state, "pcall", clua_guarded_pcall);
{
lua_register(_state, "pcall", clua_guarded_pcall);
lua_register(_state, "loadfile", clua_loadfile);
lua_register(_state, "dofile", clua_dofile);
}
}
lua_call_throttle::lua_call_throttle(CLua *_lua)
: lua(_lua)
{
lua->init_throttle();
if (!lua->mixed_call_depth++)
lua_map[lua->state()] = lua;
}
lua_call_throttle::~lua_call_throttle()
{
if (!--lua->mixed_call_depth)
lua_map.erase(lua->state());
}
CLua *lua_call_throttle::find_clua(lua_State *ls)
{
lua_clua_map::iterator i = lua_map.find(ls);
return (i != lua_map.end()? i->second : NULL);
lua_clua_map::iterator i = lua_map.find(ls);
return (i != lua_map.end()? i->second : NULL);
const char *file = luaL_checkstring(ls, 1);
if (!file)
return (0);
const int err = CLua::loadfile(ls, file);
if (err)
return (lua_error(ls));
lua_call(ls, 0, LUA_MULTRET);
return (lua_gettop(ls));