LuaInterface - 如何对限制访问.NET类? [英] LuaInterface - how-to restrict access to .Net classes?

查看:291
本文介绍了LuaInterface - 如何对限制访问.NET类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用LuaInterface 2.0.3嵌入在我的C#应用​​程序的一些Lua脚本功能。这是工作得很好,到目前为止,但我无法弄清楚如何限制访问只有少数指定的.NET类。默认情况下,所有的.NET库是通过luanet直接访问和Lua脚本可以自由打开新窗口或访问文件系统。



例如。这个Lua的脚本会打开一个新窗口:

 表= luanet.System.Windows.Forms.Form 
的MainForm =表()
的MainForm:ShowDialog的()

脚本的自由是伟大的,而是这很可能与托管应用程序干扰,并有一些与安全相关的含义我不是太喜欢。有什么办法禁用此?


解决方案


<预类=郎LUA prettyprint-覆盖 > - 做一个表的所有希望公开--store所有那些你想
safeClasses.Form = luanet
safeClasses = {}
类。 System.Windows.Forms.Form中
--etc ...

--remove访问LuaInterface
luanet =零
package.loaded.luanet = $无b $ b --prevent被加载
未来软件包需要=零
package.loadlib =零

您也可以做到这一点在倒车时,首先去除LuaInterface的全球和存储的实例,然后做通过本地引用的所有工作(这在的block 可以使用):





<前类=郎LUA prettyprint-覆盖> - 获取本地参考luaInterface没有弄错名字
本地luainterface = luanet

- 删除全球参考吧
luanet =零

- 兼论从包店和禁用包加载
package.loaded.luanet =零
删除它要求=零
package.loadlib =零

--put luanet本地回放在其原来的名字(为方便起见)
本地luanet = luainterface

--make对于所有的类要公开
safeClasses = {}
--store所有你想要的
safeClasses.Form = luanet.System.Windows.Forms.Form $ b $的那些表b --etc ...

(您可避免上述三步名保全舞( 本地luainterface = luanet; luanet =零;当地luanet = luainterface )通过了 _G luanet 然后删除全局>参照全局表:





<预类=郎LUA prettyprint-覆盖> 本地luanet = _G .luanet
_G.luanet =零

我只是选择不作为个人的问题偏好。)


I'm trying to embed some Lua scripting functionality in my C# app by using LuaInterface 2.0.3. This is working just fine so far, but I can't figure out how to restrict access to only few specified .Net classes. By default, all .Net libraries are directly accessible through "luanet" and Lua scripts are free to open new windows or access the file system.

e.g. this Lua script will open a new Window:

   Form = luanet.System.Windows.Forms.Form
   mainForm = Form()
   mainForm:ShowDialog()

Freedom of scripting is great and all, but this is likely to interfere with the hosting app and has some security-related implications I'm not too fond of. Is there any way to disable this?

解决方案

--make a table for all the classes you want to expose
safeClasses = {}
--store all the ones you want
safeClasses.Form = luanet.System.Windows.Forms.Form
--etc...

--remove access to LuaInterface
luanet = nil
package.loaded.luanet = nil
--prevent future packages from being loaded
require = nil
package.loadlib = nil

You could also do this in reverse, removing the global and stored instances of LuaInterface first and then doing all the work through a local reference (which all code in the rest of the block can use):

--get a local reference to LuaInterface without clobbering the name
local luainterface = luanet

--delete the global reference to it
luanet = nil

--also delete it from the package store and disable package loading
package.loaded.luanet = nil
require = nil
package.loadlib = nil

--put luanet back locally at its original name (for convenience)
local luanet = luainterface 

--make a table for all the classes you want to expose
safeClasses = {}
--store all the ones you want
safeClasses.Form = luanet.System.Windows.Forms.Form
--etc...

(You can avoid the three-step name-preservation dance above (local luainterface=luanet; luanet=nil; local luanet=luainterface) by localizing directly to luanet and then deleting the global through the _G reference to the global table:

local luanet=_G.luanet
_G.luanet = nil

I just chose not to as a matter of personal preference.)

这篇关于LuaInterface - 如何对限制访问.NET类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆