不允许访问Python中的exec和eval文件系统 [英] disallow access to filesystem inside exec and eval in Python

查看:488
本文介绍了不允许访问Python中的exec和eval文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁止从客户端代码访问文件系统,所以我想我可以覆盖打开的函数

  env = { 
'open':lambda * a:StringIO(你不能使用open)

$ b exec(open('user_code.py'),env)

但是我得到了这个

 在函数'my function'中不允许使用不合格的exec,它包含一个带有自由变量的
嵌套函数



我也试过

$ p $ def open_exception(* a):
raise Exception(你不能使用open)
env = {
'open':open_exception
}

但得到相同的异常(不是你不能使用打开)

我想防止:



执行此操作:

 def foo():
return open('some_file')。read()
print foo()

并评估这个



<$ p ($'code>open('some_file')。write('some text')


$ b $我也使用会话来存储以前评估的代码,所以我需要防止执行此:

  def foo(s):
return open(s)

然后评价这个

 foo('some')。write('some text')

我不能使用正则表达式,因为有人可以使用(eval内部字符串)

 eval(\opxx('some file')。write('some text')\.replace('xx','en')

有没有办法阻止在exec / eval中访问文件系统? (我需要)

解决方案

没有办法阻止在exec / eval中访问文件系统。下面是一个示例代码,演示了用户代码调用总是有效的限制类的方法:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $如果x .__ name__ =='Popen'] [0](['ls','-la')。 '))。wait()
#执行`code`将始终运行`ls` ...
在dict中执行代码(__ builtins __ = None)

而不要考虑过滤输入,尤其是用正则表达式。



您可能会考虑一些替代方案:如果您可以限制自己,那么您可以考虑一些替代方案:


  1. ast.literal_eval 仅用于简单表达式

  2. 使用另一种语言进行用户代码。您可能会看看Lua或JavaScript - 两者有时用于运行沙盒中的不安全的代码。

  3. pysandbox 项目,尽管我无法保证你的沙箱代码是非常安全的。 Python并没有被设计成沙盒,特别是CPython的实现并没有考虑沙盒。即使是作者似乎怀疑实施此类的可能性安全沙箱。


I want to disallow access to file system from clients code, so I think I could overwrite open function

env = {
   'open': lambda *a: StringIO("you can't use open")
}

exec(open('user_code.py'), env)

but I got this

unqualified exec is not allowed in function 'my function' it contains a 
nested function with free variables

I also try

 def open_exception(*a):
     raise Exception("you can't use open")
 env = {
     'open': open_exception
 }

but got the same Exception (not "you can't use open")

I want to prevent of:

executing this:

"""def foo():
     return open('some_file').read()
print foo()"""

and evaluate this

"open('some_file').write('some text')"

I also use session to store code that was evaluated previously so I need to prevent of executing this:

"""def foo(s):
   return open(s)"""

and then evaluating this

"foo('some').write('some text')"

I can't use regex because someone could use (eval inside string)

"eval(\"opxx('some file').write('some text')\".replace('xx', 'en')"

Is there any way to prevent access to file system inside exec/eval? (I need both)

解决方案

There's no way to prevent access to the file system inside exec/eval. Here's an example code that demonstrates a way for the user code to call otherwise restricted classes that always works:

import subprocess
code = """[x for x in ().__class__.__bases__[0].__subclasses__() 
           if x.__name__ == 'Popen'][0](['ls', '-la']).wait()"""
# Executing the `code` will always run `ls`...
exec code in dict(__builtins__=None)

And don't think about filtering the input, especially with regex.

You might consider a few alternatives:

  1. ast.literal_eval if you could limit yourself only to simple expressions
  2. Using another language for user code. You might look at Lua or JavaScript - both are sometimes used to run unsafe code inside sandboxes.
  3. There's the pysandbox project, though I can't guarantee you that the sandboxed code is really safe. Python wasn't designed to be sandboxed, and in particular the CPython implementation wasn't written with sandboxing in mind. Even the author seems to doubt the possibility to implement such sandbox safely.

这篇关于不允许访问Python中的exec和eval文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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