Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Is there a reliable way to get the full path of the Lua file that called a C# method? #159

Open
AppeazeTheCheese opened this issue Nov 12, 2022 · 3 comments

Comments

@AppeazeTheCheese
Copy link

AppeazeTheCheese commented Nov 12, 2022

I am currently working on a program that allows addons written in Lua to be added to it. These addons can be shared by anyone. The idea is I would like to limit file access for these scripts so they can only read and write files in their respective addon folder as a way to prevent people from creating and sharing malicious scripts. In order for this to be done, I need to be able to get which Lua file called the current method. I tried creating a new StackTrace() because that's how you would accomplish this with normal C#, but as I expected this didn't yield anything useful. I also tried creating a new LuaRuntimeException() and looking at the StackTrace property in there, but this doesn't seem to be populated until later.

@AppeazeTheCheese AppeazeTheCheese changed the title [Question] Is there a reliable way to get the full path of the Lua file that called a method? [Question] Is there a reliable way to get the full path of the Lua file that called a C# method? Nov 12, 2022
@neolithos
Copy link
Owner

neolithos commented Nov 17, 2022

You need to overwrite LuaCompileOptions and take a look to the SandBox-method:

neolua/NeoLua/Lua.cs

Lines 79 to 129 in 157df5c

public class LuaCompileOptions
{
/// <summary>Create the lexer for the parser</summary>
/// <param name="chunkName"></param>
/// <param name="tr"></param>
/// <returns></returns>
public virtual ILuaLexer CreateLexer(string chunkName, TextReader tr)
=> LuaLexer.Create(chunkName, tr);
/// <summary>Action on access diened.</summary>
/// <returns></returns>
protected virtual Expression RestrictAccess()
=> Expression.Throw(Expression.New(typeof(UnauthorizedAccessException)));
/// <summary>Most core method, that gets called to sandbox a value.</summary>
/// <param name="expression">Expression, that should be sandboxed.</param>
/// <param name="instance">Optional: Instance, that was called to get the expression.</param>
/// <param name="sMember">Optional: Name of the member that was used to resolve the expression.</param>
/// <returns>Sandboxed expression</returns>
protected internal virtual Expression SandboxCore(Expression expression, Expression instance, string sMember)
{
switch (Sandbox(expression.Type, instance?.Type, sMember))
{
case LuaSandboxResult.Dynamic:
return DynamicSandbox == null
? expression
: Lua.EnsureType(Expression.Invoke(Expression.Constant(DynamicSandbox), expression), expression.Type);
case LuaSandboxResult.Restrict:
return RestrictAccess();
default:
return expression;
}
} // func SandboxCore
/// <summary>Higher level method to restict access to types.</summary>
/// <param name="expressionType">Type of the sandbox value</param>
/// <param name="instanceType">Optional: Instance, that was called to get the expression.</param>
/// <param name="sMember">Optional: Name of the member that was used to resolve the expression.</param>
/// <returns>Sandbox action</returns>
protected virtual LuaSandboxResult Sandbox(Type expressionType, Type instanceType, string sMember)
=> DynamicSandbox == null ? LuaSandboxResult.None : LuaSandboxResult.Dynamic;
/// <summary>Gets called if the sandbox will resolved during runtime.</summary>
public Func<object, object> DynamicSandbox { get; set; }
/// <summary>Set this member to compile the script with Debug-Infos.</summary>
public ILuaDebug DebugEngine { get; set; }
/// <summary>Wether or not to recognize the local</summary>
public bool ClrEnabled { get; set; } = true;
} // class LuaCompileOptions

@madmagic007
Copy link

But is there a direct way to get the path of the file that contains the lua code that ran a specific method?
Im trying to achieve the same but for completely different reason.

Im trying to make an auto script updater. My use case, the user will be able to download "modules", each as their own file and they can specify and update url. During loading of the lua file, my code should be able to find and possibly replace the file that contained the script.

@neolithos
Copy link
Owner

No, most information is thrown away due performance reason. But you can had this with your own compiler hook. To add it before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants