Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions web/submission/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@

logger = logging.getLogger(__name__)

allowed_functions = {
"sorted": sorted,
"set": set,
"os.path.join": os.path.join,
}


def parse_expr(expr, context):
"""Return the value from a python AST expression.
Expand Down Expand Up @@ -88,11 +94,10 @@ def parse_expr(expr, context):
# Figure out what function is being called, with what arguments.
func = parse_expr(expr.func, context)
args = tuple([parse_expr(item, context) for item in expr.args])
# We deem these functions safe to use with "eval".
allowed_functions = ("sorted", "set", "os.path.join")

if func in allowed_functions:
# Actually call the function, passing the args, and return the result.
return eval(f"{func}{args}")
return allowed_functions[func](*args)
# Don't execute the call, but instead, give back a string representation.
return f"<{func}{args}>"
if isinstance(expr, ast.BinOp) and isinstance(expr.op, ast.Add):
Expand Down
Loading