栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在python Web服务器上执行数学用户代码,最简单的安全方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在python Web服务器上执行数学用户代码,最简单的安全方法是什么?

感谢arifwn,我开始探索Python的

ast
(抽象语法树)模块。该模块提供了一个
ast.NodeVisitor
遍历树的类。该代码子类化
NodeVisitor
以创建语法检查器,该检查器将基本数学所需的代码列入白名单。由于仅允许某些功能,并且只允许使用未使用的名称,因此对函数调用和名称进行了专门的监视。

import astallowed_functions = set([    #math library    'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',    'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf',    'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod',    'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp',    'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians',    'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc',    #builtins    'abs', 'max', 'min', 'range', 'xrange'    ])allowed_node_types = set([    #meta    'Module', 'Assign', 'Expr',    #Control    'For', 'If', 'Else',    #Data    'Store', 'Load', 'AugAssign', 'Subscript',    #Datatypes    'Num', 'Tuple', 'List',    #Operations    'BinOp', 'Add', 'Sub', 'Mult', 'Div', 'Mod', 'Compare'    ])safe_names = set([    'True', 'False', 'None'    ])class SyntaxChecker(ast.NodeVisitor):    def check(self, syntax):        tree = ast.parse(syntax)        self.passed=True        self.visit(tree)    def visit_Call(self, node):        if node.func.id not in allowed_functions: raise SyntaxError("%s is not an allowed function!"%node.func.id)        else: ast.NodeVisitor.generic_visit(self, node)    def visit_Name(self, node):        try: eval(node.id)        except NameError: ast.NodeVisitor.generic_visit(self, node)        else: if node.id not in safe_names and node.id not in allowed_functions:     raise SyntaxError("%s is a reserved name!"%node.id) else:     ast.NodeVisitor.generic_visit(self, node)    def generic_visit(self, node):        if type(node).__name__ not in allowed_node_types: raise SyntaxError("%s is not allowed!"%type(node).__name__)        else: ast.NodeVisitor.generic_visit(self, node)if __name__ == '__main__':    x = SyntaxChecker()    while True:        try: x.check(raw_input())        except Exception as e: print e

请注意,这旨在仅接受代码的数学部分,并提供了函数定义和return语句。

可以修改这种将所有必需的安全构造列入白名单,特别是将不安全的构造列入白名单的方法,以产生许多有用的Python子集。非常适合用户脚本!

请注意,为了安全地执行此操作,它应该在自己的线程中带有超时,以减少名称冲突和如果用户代码生成无限循环或类似情况而导致的超时。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号