如何在纯 Python 中沙箱 Python? [英] How can I sandbox Python in pure Python?

查看:51
本文介绍了如何在纯 Python 中沙箱 Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用纯 Python 开发一个网页游戏,并希望使用一些简单的脚本来实现更动态的游戏内容.特权用户可以实时添加游戏内容.

I'm developing a web game in pure Python, and want some simple scripting available to allow for more dynamic game content. Game content can be added live by privileged users.

如果脚本语言可以是 Python 就好了.但是,它无法在访问游戏运行环境的情况下运行,因为恶意用户可能会造成严重破坏.是否可以在纯 Python 中运行沙盒 Python?

It would be nice if the scripting language could be Python. However, it can't run with access to the environment the game runs on since a malicious user could wreak havoc which would be bad. Is it possible to run sandboxed Python in pure Python?

更新:事实上,由于真正的 Python 支持会有点矫枉过正,所以使用 Pythonic 语法的简单脚本语言将是完美的.

Update: In fact, since true Python support would be way overkill, a simple scripting language with Pythonic syntax would be perfect.

如果没有任何 Pythonic 脚本解释器,是否还有其他我可以使用的纯 Python 编写的开源脚本解释器?要求是支持变量、基本条件和函数调用(不是定义).

If there aren't any Pythonic script interpreters, are there any other open source script interpreters written in pure Python that I could use? The requirements are support for variables, basic conditionals and function calls (not definitions).

推荐答案

这真的很重要.

有两种方法可以对 Python 进行沙箱处理.一种是创建一个受限制的环境(即,很少有全局变量等)并在该环境中exec您的代码.这就是梅萨的建议.这很好,但有很多方法可以打破沙盒并制造麻烦.大约一年前在 Python-dev 上有一个关于这个的线程,其中人们通过捕获异常和戳内部状态来进行字节码操作.如果您想要一门完整的语言,这就是要走的路.

There are two ways to sandbox Python. One is to create a restricted environment (i.e., very few globals etc.) and exec your code inside this environment. This is what Messa is suggesting. It's nice but there are lots of ways to break out of the sandbox and create trouble. There was a thread about this on Python-dev a year ago or so in which people did things from catching exceptions and poking at internal state to break out to byte code manipulation. This is the way to go if you want a complete language.

另一种方法是解析代码,然后使用 ast 模块踢出您不想要的构造(例如导入语句、函数调用等),然后编译其余的.如果你想使用 Python 作为配置语言等,这是要走的路.

The other way is to parse the code and then use the ast module to kick out constructs you don't want (e.g. import statements, function calls etc.) and then to compile the rest. This is the way to go if you want to use Python as a config language etc.

另一种方法(由于您使用 GAE,可能对您不起作用)是 PyPy 沙箱.虽然我自己没有使用过它,但关于 intertubes 的消息是,它是目前唯一的真正沙盒 Python.

Another way (which might not work for you since you're using GAE), is the PyPy sandbox. While I haven't used it myself, word on the intertubes is that it's the only real sandboxed Python out there.

根据您对要求的描述(要求支持变量、基本条件和函数调用(不是定义)),您可能想要评估方法 2 并从代码.这有点棘手但可行.

Based on your description of the requirements (The requirements are support for variables, basic conditionals and function calls (not definitions)) , you might want to evaluate approach 2 and kick out everything else from the code. It's a little tricky but doable.

这篇关于如何在纯 Python 中沙箱 Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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