Created
November 13, 2012 00:22
-
-
Save gabrielfalcao/4063003 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
def get_safe_function(code): | |
expression = ast.parse(code).body[0] | |
if expression.value.__class__.__name__ == 'Lambda': | |
return eval(code) | |
else: | |
logger.warning("Got an unsafe code: %s", repr(code)) | |
return lambda x: x | |
exec1 = get_safe function('lambda x: x.strip()') | |
exec2 = get_safe function('__import__("os").system("rm -rf /")') | |
assert exec1(" aaa ") == "aaa" | |
assert exec2(" aaa ") == " aaa " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment