Package: evalidate
EvalidateEvalidate is simple python module for safe and very fast eval()'uating user-supplied (possible malicious) python expressions.Upgrade warningVersion 2.0 is backward incompatible with older versions.safeeval()andevalidate()methods are removed, and EvalMode class is introduced.Seeupgrade example in ticketor use older (any before 2.0.0, e.g.v1.1.0) if you have old code and do not want to upgrade. But upgrading is easy, so please consider this option.PurposeOriginally it's developed for filtering complex data structures e.g.Find cheap smartphones available for sale:category="smartphones"andprice<300andstock>0But also, it can be used for other expressions, e.g. arithmetical, likea+b-100Evalidate is fastest among all (known to me) secure eval pythong modules.Installpip3installevalidateSecurityBuilt-in python features such as compile() or eval() are quite powerful to run any kind of user-supplied code, but could be insecure if used code is malicious likeos.system("rm -rf /"). Evalidate works on whitelist principle, allowing code only if it consist only of safe operations (based on authors views about what is safe and what is not, your mileage may vary - but you can supply your list of safe operations)TL;DR. Just give me safe eval!fromevalidateimportExpr,EvalExceptionsrc='a + 40 > b'# src = "__import__('os').system('clear')"try:print(Expr(src).eval({'a':10,'b':42}))exceptEvalExceptionase:print(e)Gives output:TrueIn case of dangerous code (uncomment second src line to test):output will be:ERR: Operation type Call is not allowedExceptionsEvalidate throws exceptionsCompilationException,ValidationException,ExecutionException. All of them
inherit from base exception classEvalException.Configure validationEvalidate is very flexible, depending on security model, same code can either pass validation or raise exception.EvalModel is security model class for eval - lists of allowed AST nodes, function calls, attributes and dict of imported functions. There is built-in modelbase_eval_modelwith basic operations allowed (which are safe from authors point of view).You can create custom empty model (and extend it later):my_model=evalidate.EvalModel()(nothing is allowed by default, even1+2will not be considered safe)or you may start frombase_eval_modeand extend it:fromevalidateimportExpr,base_eval_modelmy_model=base_eval_model.clone()my_model.nodes.append('Mult')Expr('2*2',model=my_model).eval()To enableint()function, need to allow'Call'node and add this function to list of allowed function:my_model.nodes.append('Call')my_model.allowed_functions.append('int')Expr('int(36.6)',model=my_model).eval()Or, to call attributes:m=base_eval_model.clone()m.nodes.extend(['Call','Attribute'])m.attributes.append('startswith')src='"abcdef".startswith("abc")'r=evalidate.Expr(src,model=m).eval()But even with this settings, exploiting it with expression like__builtins__["eval"](1)will fail (good!).Exporting my functions to eval codedefone():return1m=base_eval_model.clone()m.nodes.append('Call')Expr('one()',model=m).eval()Improve speed by using native eval() with validated codeEvalidate is very fast, but it's still takes CPU cycles... If you want to achieve maximal possible speed, you can use python nativeevalwith this kind of code:fromevalidateimportExprd=dict(a=1,b=2)expr=Expr('a+b')eval(expr.code,None,d)# <-- native python eval, will run at eval() speedThis is as secure as expr.eval(), becauseexpr.codeis already validated to be secure.Difference is very little: execution ofexpr.codecan throw any exception, whileexpr.eval()can throw only ExecutionException. Also, if you want to export your functions to eval, you should do this manually.Limitationsevalidate usesast.parse()to getAST nodeto validate it.WarningIt is possible to crash the Python interpreter with a sufficiently large/complex string due to stack depth limitations in Python’s AST compiler.In my test, works well with 200 nested int():int(int(.... int(1)...))but not with 201. Source code is 1000+ characters. But even if evalidate will get such code, it will just raiseCompilationException.evalidate.security.test_security()Evalidate is very flexible and it's possible to shoot yourself in foot if you will try hard.test_security()checks your configuration (nodes, funcs, attrs) against given list of possible attack code or against built-in list of attacks.test_security()returns True if everything is OK (all attacks raised ValidationException) or False if something passed.This code will never print (I hope).fromevalidate.securityimporttest_securitytest_security()orprint("default rules are vulnerable!")But this will fail because nodes/funcs leads to successful validation for attack (suppose you do not want anyone to callint())fromevalidate.securityimporttest_securityattacks=['int(1)']test_security(attacks,addnodes=['Call'],funcs=['int'],verbose=True)It will print:Testing attack code:
int(1)
Problem! Attack passed validation without exception!
Code:
int(1)ExampleFiltering by user-supplied conditionThis is code ofexamples/products.py. Expression is validated and compiled once and executed (as byte-code, very fast) many times, so filtering is both fast and secure.#!/usr/bin/env python3importrequestsfromevalidateimportExpr,ValidationException,CompilationException,ExecutionExceptionimportjsonimportsysdata=requests.get('https://dummyjson.com/products?limit=100').json()try:src=sys.argv[1]exceptIndexError:src='True'try:expr=Expr(src)except(ValidationException,CompilationException)ase:print(e)sys.exit(1)c=0forpindata['products']:# print(p)try:r=expr.eval(p)ifr:print(json.dumps(p,indent=2))c+=1exceptExecutionExceptionase:print("Runtime exception:",e)print("#{}products matches".format(c))# print all 100 products./products.py# Only cheap products, 8 matches./products.py'price<20'# smartphones (5)./products.py'category=="smartphones"'# good smartphones./products.py'category=="smartphones" and rating>4.5'# cheap smartphones./products.py'category=="smartphones" and price<300'Similar projects and benchmarkastevalWhile asteval can compute much more complex code (define functions, use python math libraries) it has drawbacks:asteval is much slower (evalidate can be used at speed of eval() python bytecode)user can provide source code which runs very long time and consumes many resourcessimpleevalVery similar project, using AST approach too and optimized to re-evaluate pre-parsed expressions. But parsed expressions are stored as more high-levelast.Exprtype and this approach is few times slower, while evalidate uses python nativecodetype and evaluation itself goes at speed of python eval()evalidate is good to run same expression against different data.BenchmarkingWe usebenchmark/benchmark.pyin this repository.
We prepare list of 1 million of products (actually, we take just 100 products sample, but repeat it 10 000 times to get 1 million), and then filter it, finding only specific products on "untrusted user-supplied expression" (price < 20in this case)Products: 1000000 items
evalidate_raw_eval(): 0.266s
evalidate_eval(): 0.326s
test_simpleeval(): 1.824s
test_asteval(): 26.106sAs you see, evalidate is few times faster then simpleeval and both are much faster then asteval.Maybe my test is not perfectly optimized (I'm not expert with simpleeval/asteval), if you can suggest better filtering sample code (which produces faster result), I will include it. (Benchmark code must assume expression as unknown in advance and untrusted)Read about eval() riskshttps://nedbatchelder.com/blog/201206/eval_really_is_dangerous.htmlhttps://netsec.expert/posts/breaking-python3-eval-protections/https://realpython.com/python-eval-function/Note: realpython article shows example with nice short method of validation source (usingcode.co_names),
but it's vulnerable, it passes "bomb" from Ned Batchelder article (bomb has emptyco_namestuple) and crash interpreter. Evalidate can block this code and similar bombs (unless you will intentionally configure evalidate to pass specific bomb code. Yes, with evalidate it is hard to shoot yourself in the foot, but it is possible if you will try hard).More infoWant more info? Check source code of module, it's very short and simple, easy to modifyContactWrite me: yaroslaff at gmail.com