作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我计划在网络环境中使用这些函数,所以我担心这些函数是否可以被利用并用于在服务器上执行恶意软件。
编辑:我不执行结果。我解析 AST 树和/或捕获 SyntaxError。
这是有问题的代码:
try:
#compile the code and check for syntax errors
compile(code_string, filename, "exec")
except SyntaxError, value:
msg = value.args[0]
(lineno, offset, text) = value.lineno, value.offset, value.text
if text is None:
return [{"line": 0, "offset": 0,
"message": u"Problem decoding source"}]
else:
line = text.splitlines()[-1]
if offset is not None:
offset = offset - (len(text) - len(line))
else:
offset = 0
return [{"line": lineno, "offset": offset, "message": msg}]
else:
#no syntax errors, check it with pyflakes
tree = compiler.parse(code_string)
w = checker.Checker(tree, filename)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
checker.Checker 是解析 AST 树的 pyflakes 类。
最佳答案
我认为更有趣的问题是你用编译函数做什么?运行它们绝对是不安全的。
我已经测试了一些我能想到的漏洞利用,因为它只是一个语法检查器(不能重新定义类/函数等)我不认为无论如何都可以让 python在编译时执行任意代码
关于python - Python 函数 "compile"和 "compiler.parse"是否安全(沙盒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964077/
我是一名优秀的程序员,十分优秀!