Finance Calculat0r 2021
Description
Solution
WHITELIST_NODES = [
ast.Expression,
ast.Expr,
ast.Num,
ast.Name,
ast.Constant,
ast.Load,
ast.BinOp,
ast.Add,
ast.Sub,
ast.Module,
ast.Mult,
ast.Div,
ast.Assign,
ast.Store
]
WHITELIST_FUNCTIONS = [
"print"
]
...
def check_code_security(code):
# Decode for parser
s = code.decode(errors="ignore")
tree = ast.parse(s, mode='exec')
for node in ast.walk(tree):
if type(node) not in WHITELIST_NODES:
if type(node) == ast.Call and node.func.id not in WHITELIST_FUNCTIONS:
raise ValueError("Forbidden code used in type '{}'. NOT allowed!".format(type(node)))Last updated