Bunch of fixes

This commit is contained in:
Evil Factory
2022-04-09 09:06:29 -03:00
parent b37fd8d9c9
commit e1e0fd6acf
11 changed files with 174 additions and 146 deletions
@@ -51,43 +51,93 @@ namespace Barotrauma
partial class LuaFile
{
// TODO: SANDBOXING
public static bool IsPathAllowed(string path)
public static bool CanReadFromPath(string path)
{
path = Path.GetFullPath(path).CleanUpPath();
string getFullPath(string p) => System.IO.Path.GetFullPath(p).CleanUpPath();
if (path.StartsWith(Path.GetFullPath("Mods").CleanUpPath()))
path = getFullPath(path);
bool pathStartsWith(string prefix) => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
string localModsDir = getFullPath(ContentPackage.LocalModsDir);
string workshopModsDir = getFullPath(ContentPackage.WorkshopModsDir);
#if CLIENT
string tempDownloadDir = getFullPath(ModReceiver.DownloadFolder);
#endif
if (pathStartsWith(localModsDir))
return true;
if (path.StartsWith(Path.GetFullPath("Submarines").CleanUpPath()))
if (pathStartsWith(workshopModsDir))
return true;
if (path.StartsWith(Path.GetFullPath("Data").CleanUpPath()))
#if CLIENT
if (pathStartsWith(tempDownloadDir))
return true;
#endif
if (path.StartsWith(Path.GetFullPath("Lua").CleanUpPath()))
return true;
if (path.StartsWith(Path.GetFullPath("Content").CleanUpPath()))
if (pathStartsWith(getFullPath(".")))
return true;
return false;
}
public static bool IsPathAllowedLuaException(string path)
public static bool CanWriteToPath(string path)
{
if (IsPathAllowed(path))
string getFullPath(string p) => System.IO.Path.GetFullPath(p).CleanUpPath();
path = getFullPath(path);
bool pathStartsWith(string prefix) => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
if (pathStartsWith(getFullPath(ContentPackage.LocalModsDir + "LuaForBarotraumaUnstable")))
return false;
if (pathStartsWith(getFullPath(ContentPackage.WorkshopModsDir + "LuaForBarotraumaUnstable")))
return false;
#if CLIENT
if (pathStartsWith(getFullPath(ModReceiver.DownloadFolder + "LuaForBarotraumaUnstable")))
return false;
#endif
if (pathStartsWith(getFullPath(ContentPackage.LocalModsDir)))
return true;
else
GameMain.Lua.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed."));
if (pathStartsWith(getFullPath(ContentPackage.WorkshopModsDir)))
return true;
#if CLIENT
if (pathStartsWith(getFullPath(ModReceiver.DownloadFolder)))
return true;
#endif
return false;
}
public static bool IsPathAllowedLuaException(string path, bool write = true)
{
if (write)
{
if (CanWriteToPath(path))
return true;
else
GameMain.Lua.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed."));
}
else
{
if (CanReadFromPath(path))
return true;
else
GameMain.Lua.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed."));
}
return false;
}
public static string Read(string path)
{
if (!IsPathAllowedLuaException(path))
if (!IsPathAllowedLuaException(path, false))
return "";
return File.ReadAllText(path);
@@ -103,7 +153,7 @@ namespace Barotrauma
public static bool Exists(string path)
{
if (!IsPathAllowedLuaException(path))
if (!IsPathAllowedLuaException(path, false))
return false;
return File.Exists(path);
@@ -121,7 +171,7 @@ namespace Barotrauma
public static bool DirectoryExists(string path)
{
if (!IsPathAllowedLuaException(path))
if (!IsPathAllowedLuaException(path, false))
return false;
return Directory.Exists(path);
@@ -129,12 +179,15 @@ namespace Barotrauma
public static string[] GetFiles(string path)
{
if (!IsPathAllowedLuaException(path, false))
return null;
return Directory.GetFiles(path);
}
public static string[] GetDirectories(string path)
{
if (!IsPathAllowedLuaException(path))
if (!IsPathAllowedLuaException(path, false))
return new string[] { };
return Directory.GetDirectories(path);
@@ -142,7 +195,7 @@ namespace Barotrauma
public static string[] DirSearch(string sDir)
{
if (!IsPathAllowedLuaException(sDir))
if (!IsPathAllowedLuaException(sDir, false))
return new string[] { };
List<string> files = new List<string>();
@@ -21,68 +21,18 @@ namespace Barotrauma
public override object LoadFile(string file, Table globalContext)
{
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
return File.ReadAllText(file);
}
public override bool ScriptFileExists(string file)
{
if (!LuaFile.IsPathAllowedLuaException(file, false)) return false;
return File.Exists(file);
}
public void RunFolder(string folder)
{
foreach (var str in DirSearch(folder))
{
var s = str.Replace("\\", "/");
if (s.EndsWith(".lua"))
{
lua.PrintMessage(s);
try
{
lua.DoFile(s);
}
catch (Exception e)
{
lua.HandleLuaException(e);
}
}
}
}
static string[] DirSearch(string sDir)
{
List<string> files = new List<string>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d))
{
files.Add(f);
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
return files.ToArray();
}
}
}
}
@@ -13,6 +13,8 @@ namespace Barotrauma
{
partial class LuaSetup
{
public const string LUA_PATH = "Lua/LuaSetup.lua";
public Script lua;
public LuaHook hook;
@@ -124,8 +126,6 @@ namespace Barotrauma
public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null)
{
if(!LuaFile.IsPathAllowedLuaException(file)) return null;
try
{
return lua.DoFile(file, globalContext, codeStringFriendly);
@@ -157,8 +157,6 @@ namespace Barotrauma
public DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null)
{
if (!LuaFile.IsPathAllowedLuaException(file)) return null;
try
{
return lua.LoadFile(file, globalContext, codeStringFriendly);
@@ -222,7 +220,7 @@ namespace Barotrauma
public void Initialize()
{
Stop();
PrintMessage("Lua! Version " + AssemblyInfo.GitRevision);
luaScriptLoader = new LuaScriptLoader(this);
@@ -278,12 +276,45 @@ namespace Barotrauma
// LuaDocs.GenerateDocsAll();
if (File.Exists("Lua/LuaSetup.lua")) // try the default loader
DoFile("Lua/LuaSetup.lua");
else if (File.Exists("Mods/LuaForBarotrauma/Lua/LuaSetup.lua")) // in case its the workshop version
DoFile("Mods/LuaForBarotrauma/Lua/LuaSetup.lua");
ContentPackage luaPackage = null;
foreach (ContentPackage package in ContentPackageManager.AllPackages)
{
if (package.NameMatches(new Identifier("LuaForBarotraumaUnstable")))
{
luaPackage = package;
}
}
if (File.Exists(LUA_PATH))
{
try
{
lua.Call(lua.LoadFile(LUA_PATH), Path.GetDirectoryName(Path.GetFullPath(LUA_PATH)));
}
catch (Exception e)
{
HandleLuaException(e);
}
}
else if (luaPackage != null)
{
string path = Path.GetDirectoryName(luaPackage.Path);
try
{
string luaPath = Path.Combine(path, "Binary/Lua/LuaSetup.lua");
lua.Call(lua.LoadFile(luaPath), Path.GetDirectoryName(luaPath));
}
catch (Exception e)
{
HandleLuaException(e);
}
}
else
{
PrintError("Lua loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
}
}
public LuaSetup()