evalidate 2.0.1__tar.gz → 2.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ *~
2
+ __pycache__
3
+ evalidate.egg-info/
4
+ *pyc
5
+ build/
6
+ dist/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Yaroslav Polyakov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
evalidate-2.0.3/NOTES ADDED
@@ -0,0 +1,4 @@
1
+ add security verification for attrs/nodes ... (test all jailbreaks against configuration)
2
+ rebuild __builtins__ ?
3
+ make fast filter code? (verify it, extend, and run as fast filter)
4
+ try asteval filtering, maybe faster
@@ -0,0 +1,289 @@
1
+ Metadata-Version: 2.3
2
+ Name: evalidate
3
+ Version: 2.0.3
4
+ Summary: Validation and secure evaluation of untrusted python expressions
5
+ Project-URL: Homepage, https://github.com/yaroslaff/evalidate
6
+ Project-URL: Issues, https://github.com/yaroslaff/evalidate/issues
7
+ Author-email: Yaroslav Polyakov <yaroslaff@gmail.com>
8
+ License-File: LICENSE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+
15
+ # Evalidate
16
+ Evalidate is simple python module for safe and very fast eval()'uating user-supplied (possible malicious) python expressions.
17
+
18
+ ## Upgrade warning
19
+ Version 2.0 is backward incompatible with older versions. `safeeval()` and `evalidate()` methods are removed, and EvalMode class is introduced.
20
+
21
+ See [upgrade example in ticket](https://github.com/yaroslaff/evalidate/issues/5) or use older (any before 2.0.0, e.g. [v1.1.0](https://pypi.org/project/evalidate/1.1.0/)) if you have old code and do not want to upgrade. But upgrading is easy, so please consider this option.
22
+
23
+ ## Purpose
24
+ Originally it's developed for filtering complex data structures e.g.
25
+
26
+ Find cheap smartphones available for sale:
27
+ ```python
28
+ category="smartphones" and price<300 and stock>0
29
+ ```
30
+
31
+ But also, it can be used for other expressions, e.g. arithmetical, like
32
+ ```python
33
+ a+b-100
34
+ ```
35
+
36
+ Evalidate is fastest among all (known to me) secure eval pythong modules.
37
+
38
+ ## Install
39
+
40
+ ```shell
41
+ pip3 install evalidate
42
+ ```
43
+
44
+ ## Security
45
+
46
+ Built-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 like `os.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)
47
+
48
+
49
+ ## TL;DR. Just give me safe eval!
50
+ ```python
51
+ from evalidate import Expr, EvalException
52
+
53
+ src = 'a + 40 > b'
54
+ # src = "__import__('os').system('clear')"
55
+
56
+ try:
57
+ print(Expr(src).eval({'a':10, 'b':42}))
58
+ except EvalException as e:
59
+ print(e)
60
+ ```
61
+
62
+ Gives output: `True`
63
+
64
+ In case of dangerous code (uncomment second src line to test):
65
+
66
+ output will be: `ERR: Operation type Call is not allowed`
67
+
68
+
69
+ ## Exceptions
70
+ Evalidate throws exceptions `CompilationException`, `ValidationException`, `ExecutionException`. All of them
71
+ inherit from base exception class `EvalException`.
72
+
73
+ ## Configure validation
74
+ Evalidate is very flexible, depending on security model, same code can either pass validation or raise exception.
75
+
76
+ EvalModel is security model class for eval - lists of allowed AST nodes, function calls, attributes and dict of imported functions. There is built-in model `base_eval_model` with basic operations allowed (which are safe from authors point of view).
77
+
78
+ You can create custom empty model (and extend it later):
79
+ ~~~python
80
+ my_model = evalidate.EvalModel()
81
+ ~~~
82
+
83
+ (nothing is allowed by default, even `1+2` will not be considered safe)
84
+
85
+ or you may start from `base_eval_mode` and extend it:
86
+ ~~~python
87
+ from evalidate import Expr, base_eval_model
88
+
89
+ my_model = base_eval_model.clone()
90
+ my_model.nodes.append('Mult')
91
+
92
+ Expr('2*2', model=my_model).eval()
93
+ ~~~
94
+
95
+ To enable `int()` function, need to allow `'Call'` node and add this function to list of allowed function:
96
+
97
+ ~~~python
98
+ my_model.nodes.append('Call')
99
+ my_model.allowed_functions.append('int')
100
+
101
+ Expr('int(36.6)', model=my_model).eval()
102
+ ~~~
103
+
104
+ Or, to call attributes:
105
+ ~~~python
106
+ m = base_eval_model.clone()
107
+ m.nodes.extend(['Call', 'Attribute'])
108
+ m.attributes.append('startswith')
109
+
110
+ src = '"abcdef".startswith("abc")'
111
+ r = evalidate.Expr(src, model=m).eval()
112
+ ~~~
113
+
114
+ But even with this settings, exploiting it with expression like `__builtins__["eval"](1)` will fail (good!).
115
+
116
+
117
+ ### Exporting my functions to eval code
118
+ ~~~python
119
+ def one():
120
+ return 1
121
+
122
+ m = base_eval_model.clone()
123
+ m.nodes.append('Call')
124
+ Expr('one()', model=m).eval()
125
+ ~~~
126
+
127
+ ## Improve speed by using native eval() with validated code
128
+ Evalidate is very fast, but it's still takes CPU cycles... If you want to achieve maximal possible speed, you can use python native [eval](https://docs.python.org/3/library/functions.html#eval) with this kind of code:
129
+
130
+ ~~~python
131
+ from evalidate import Expr
132
+
133
+ d = dict(a=1, b=2)
134
+ expr = Expr('a+b')
135
+ eval(expr.code, None, d) # <-- native python eval, will run at eval() speed
136
+ ~~~
137
+
138
+ This is as secure as expr.eval(), because `expr.code` is already validated to be secure.
139
+
140
+ Difference is very little: execution of `expr.code` can throw any exception, while `expr.eval()` can throw only ExecutionException. Also, if you want to export your functions to eval, you should do this manually.
141
+
142
+ ## Limitations
143
+
144
+ evalidate uses [ast.parse()](https://docs.python.org/3/library/ast.html#ast.parse) to get [AST node](https://docs.python.org/3/library/ast.html#node-classes) to validate it.
145
+
146
+ >Warning
147
+ >
148
+ >It is possible to crash the Python interpreter with a sufficiently large/complex string due to stack depth limitations in Python’s AST compiler.
149
+
150
+ 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 raise `CompilationException`.
151
+
152
+
153
+ ### evalidate.security.test_security()
154
+ 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.
155
+
156
+ This code will never print (I hope).
157
+ ~~~python
158
+ from evalidate.security import test_security
159
+
160
+ test_security() or print("default rules are vulnerable!")
161
+ ~~~
162
+
163
+ But this will fail because nodes/funcs leads to successful validation for attack (suppose you do not want anyone to call `int()`)
164
+ ~~~python
165
+ from evalidate.security import test_security
166
+
167
+ attacks = ['int(1)']
168
+
169
+ test_security(attacks, addnodes=['Call'], funcs=['int'], verbose=True)
170
+ ~~~
171
+
172
+ It will print:
173
+ ~~~
174
+ Testing attack code:
175
+ int(1)
176
+ Problem! Attack passed validation without exception!
177
+ Code:
178
+ int(1)
179
+ ~~~
180
+
181
+
182
+
183
+
184
+ ## Example
185
+
186
+ ### Filtering by user-supplied condition ###
187
+
188
+ This is code of `examples/products.py`. Expression is validated and compiled once and executed (as byte-code, very fast) many times, so filtering is both fast and secure.
189
+
190
+
191
+ ~~~python
192
+ #!/usr/bin/env python3
193
+
194
+ import requests
195
+ from evalidate import Expr, ValidationException, CompilationException, ExecutionException
196
+ import json
197
+ import sys
198
+
199
+ data = requests.get('https://dummyjson.com/products?limit=100').json()
200
+
201
+ try:
202
+ src = sys.argv[1]
203
+ except IndexError:
204
+ src = 'True'
205
+
206
+ try:
207
+ expr = Expr(src)
208
+ except (ValidationException, CompilationException) as e:
209
+ print(e)
210
+ sys.exit(1)
211
+
212
+ c=0
213
+ for p in data['products']:
214
+ # print(p)
215
+ try:
216
+ r = expr.eval(p)
217
+ if r:
218
+ print(json.dumps(p, indent=2))
219
+ c+=1
220
+ except ExecutionException as e:
221
+ print("Runtime exception:", e)
222
+ print("# {} products matches".format(c))
223
+ ~~~
224
+
225
+ ~~~shell
226
+ # print all 100 products
227
+ ./products.py
228
+
229
+ # Only cheap products, 8 matches
230
+ ./products.py 'price<20'
231
+
232
+ # smartphones (5)
233
+ ./products.py 'category=="smartphones"'
234
+
235
+ # good smartphones
236
+ ./products.py 'category=="smartphones" and rating>4.5'
237
+
238
+ # cheap smartphones
239
+ ./products.py 'category=="smartphones" and price<300'
240
+ ~~~
241
+
242
+
243
+ ## Similar projects and benchmark
244
+
245
+ [asteval](https://newville.github.io/asteval/)
246
+
247
+ While asteval can compute much more complex code (define functions, use python math libraries) it has drawbacks:
248
+ - asteval is much slower (evalidate can be used at speed of eval() python bytecode)
249
+ - user can provide source code which runs very long time and consumes many resources
250
+
251
+
252
+ [simpleeval](https://github.com/danthedeckie/simpleeval)
253
+ Very similar project, using AST approach too and optimized to re-evaluate pre-parsed expressions. But parsed expressions are stored as more high-level [ast.Expr](https://docs.python.org/3/library/ast.html#ast.Expr) type and this approach is few times slower, while evalidate uses python native `code` type and evaluation itself goes at speed of python eval()
254
+
255
+ evalidate is good to run same expression against different data.
256
+
257
+ ## Benchmarking
258
+ We use `benchmark/benchmark.py` in this repository.
259
+ 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 < 20` in this case)
260
+
261
+ ~~~
262
+ Products: 1000000 items
263
+ evalidate_raw_eval(): 0.266s
264
+ evalidate_eval(): 0.326s
265
+ test_simpleeval(): 1.824s
266
+ test_asteval(): 26.106s
267
+ ~~~
268
+
269
+ As you see, evalidate is few times faster then simpleeval and both are much faster then asteval.
270
+
271
+ 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)
272
+
273
+
274
+ ## Read about eval() risks
275
+
276
+ - https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
277
+ - https://netsec.expert/posts/breaking-python3-eval-protections/
278
+ - https://realpython.com/python-eval-function/
279
+
280
+ Note: realpython article shows example with nice short method of validation source (using `code.co_names`),
281
+ but it's vulnerable, it passes "bomb" from Ned Batchelder article (bomb has empty `co_names` tuple) 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).
282
+
283
+ ## More info
284
+
285
+ Want more info? Check source code of module, it's very short and simple, easy to modify
286
+
287
+ ## Contact
288
+
289
+ Write me: yaroslaff at gmail.com
@@ -2,7 +2,9 @@
2
2
  Evalidate is simple python module for safe and very fast eval()'uating user-supplied (possible malicious) python expressions.
3
3
 
4
4
  ## Upgrade warning
5
- Version 2.0 is backward incompatible with older versions. safeeval and evalidate methods are removed, and EvalMode class is introduced.
5
+ Version 2.0 is backward incompatible with older versions. `safeeval()` and `evalidate()` methods are removed, and EvalMode class is introduced.
6
+
7
+ See [upgrade example in ticket](https://github.com/yaroslaff/evalidate/issues/5) or use older (any before 2.0.0, e.g. [v1.1.0](https://pypi.org/project/evalidate/1.1.0/)) if you have old code and do not want to upgrade. But upgrading is easy, so please consider this option.
6
8
 
7
9
  ## Purpose
8
10
  Originally it's developed for filtering complex data structures e.g.
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from asteval import Interpreter
4
+ import timeit
5
+ import evalidate
6
+ import requests
7
+ import simpleeval
8
+ import json
9
+ from pprint import pprint
10
+
11
+ # context = {'a':1, 'b': 2}
12
+ # src="""a+b"""
13
+ # node = evalidate.evalidate(src)
14
+ # code = compile(node, '<usercode>', 'eval')
15
+
16
+ """
17
+ Our test: we prepare large list of products and filter it, find all items cheaper then 20
18
+ """
19
+
20
+ mult = 10000
21
+ # mult = 1
22
+
23
+ products = requests.get('https://dummyjson.com/products?limit=100').json()['products'] * mult
24
+ accurate_counter = 8 * mult
25
+
26
+
27
+ # gi.symtable = context
28
+
29
+ def test_asteval():
30
+ aeval = Interpreter()
31
+ src = """
32
+ counter=0
33
+ for p in products:
34
+ if p['price']<20:
35
+ counter += 1
36
+ counter
37
+ """
38
+ aeval.symtable['products'] = products
39
+
40
+ result = aeval(src)
41
+ assert(result == accurate_counter)
42
+
43
+ def test_simpleeval():
44
+ code = """price < 20"""
45
+
46
+ s = simpleeval.SimpleEval()
47
+ parsed = s.parse(code)
48
+
49
+ counter = 0
50
+
51
+ for p in products:
52
+ s.names = p
53
+ if s.eval(code, previously_parsed=parsed, ):
54
+ counter += 1
55
+ assert(counter == accurate_counter)
56
+
57
+ def evalidate_raw_eval():
58
+ src="""price < 20"""
59
+ e = evalidate.Expr(src)
60
+ counter = 0
61
+
62
+ for p in products:
63
+ if eval(e.code, None, p):
64
+ counter+=1
65
+ assert(counter == accurate_counter)
66
+
67
+ def evalidate_eval():
68
+ src="""price < 20"""
69
+ e = evalidate.Expr(src)
70
+ counter = 0
71
+
72
+ for p in products:
73
+ if e.eval(p):
74
+ counter+=1
75
+
76
+ assert(counter == accurate_counter)
77
+
78
+
79
+ def main():
80
+
81
+ print(f"Products: {len(products)} items")
82
+
83
+
84
+ t = timeit.timeit('evalidate_raw_eval()', setup='from __main__ import evalidate_raw_eval, products', number=1)
85
+ print(f"evalidate_raw_eval(): {t:.3f}s")
86
+
87
+ t = timeit.timeit('evalidate_eval()', setup='from __main__ import evalidate_eval, products', number=1)
88
+ print(f"evalidate_eval(): {t:.3f}s")
89
+
90
+ t = timeit.timeit('test_simpleeval()', setup='from __main__ import test_simpleeval, products', number=1)
91
+ print(f"test_simpleeval(): {t:.3f}s")
92
+
93
+ t = timeit.timeit('test_asteval()', setup='from __main__ import test_asteval, products', number=1)
94
+ print(f"test_asteval(): {t:.3f}s")
95
+
96
+
97
+ if __name__ == '__main__':
98
+ main()
@@ -6,7 +6,7 @@ import ast
6
6
  import dataclasses
7
7
  from typing import Callable
8
8
 
9
- __version__ = '2.0.1'
9
+ __version__ = '2.0.3'
10
10
 
11
11
 
12
12
  class EvalException(Exception):
@@ -34,7 +34,7 @@ class ExecutionException(EvalException):
34
34
  @dataclasses.dataclass
35
35
  class EvalModel:
36
36
  """ eval security model """
37
- nodes: list
37
+ nodes: list = dataclasses.field(default_factory=list)
38
38
  allowed_functions: list = dataclasses.field(default_factory=list)
39
39
  imported_functions: dict = dataclasses.field(default_factory=dict)
40
40
  attributes: list = dataclasses.field(default_factory=list)
@@ -94,7 +94,7 @@ base_eval_model = EvalModel(
94
94
  # variable name
95
95
  'Name', 'Load',
96
96
  'BinOp',
97
- 'Add', 'Sub',
97
+ 'Add', 'Sub', 'USub',
98
98
  'Subscript', 'Index', # person['name']
99
99
  'BoolOp', 'And', 'Or', 'UnaryOp', 'Not', # True and True
100
100
  "In", "NotIn", # "aaa" in i['list']
@@ -109,6 +109,7 @@ mult_eval_model.nodes.append('Mul')
109
109
 
110
110
  class Expr():
111
111
  def __init__(self, expr, model=None, filename=None):
112
+
112
113
  self.expr = expr
113
114
  self.model = model or base_eval_model
114
115
 
@@ -0,0 +1,4 @@
1
+ Yaroslav Polyakov: main author
2
+
3
+ Daniel Rech: python3 porting, ideas for IfExp function call filtering
4
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from evalidate import Expr, EvalException
4
+
5
+ src = 'a + 40 > b'
6
+ # src="__import__('os').system('clear')"
7
+
8
+ try:
9
+ print(Expr(src).eval({'a':10, 'b':42}))
10
+ except EvalException as e:
11
+ print(e)
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import requests
4
+ from evalidate import Expr, ValidationException, CompilationException, ExecutionException
5
+ import json
6
+ import sys
7
+
8
+ data = requests.get('https://dummyjson.com/products?limit=100').json()
9
+
10
+ try:
11
+ src = sys.argv[1]
12
+ except IndexError:
13
+ src = 'True'
14
+
15
+ try:
16
+ expr = Expr(src)
17
+ except (ValidationException, CompilationException) as e:
18
+ print(e)
19
+ sys.exit(1)
20
+
21
+ c=0
22
+ for p in data['products']:
23
+ # print(p)
24
+ try:
25
+ r = expr.eval(p)
26
+ if r:
27
+ print(json.dumps(p, indent=2))
28
+ c+=1
29
+ except ExecutionException as e:
30
+ print("Runtime exception:", e)
31
+ print("# {} products matches".format(c))
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ # requires = ["setuptools >= 40.6.0", "wheel"]
3
+ requires = ["hatchling", "wheel"]
4
+
5
+ # build-backend = "setuptools.build_meta"
6
+ build-backend = "hatchling.build"
7
+
8
+ [project]
9
+ name="evalidate"
10
+ dynamic = [ "version" ]
11
+
12
+ dependencies = []
13
+ authors = [
14
+ { name="Yaroslav Polyakov", email="yaroslaff@gmail.com" },
15
+ ]
16
+ description = "Validation and secure evaluation of untrusted python expressions"
17
+ readme = "README.md"
18
+ requires-python = ">=3.8"
19
+ classifiers = [
20
+ "Programming Language :: Python :: 3",
21
+ "License :: OSI Approved :: MIT License",
22
+ "Operating System :: OS Independent",
23
+ ]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/yaroslaff/evalidate"
27
+ Issues = "https://github.com/yaroslaff/evalidate/issues"
28
+
29
+
30
+
31
+ [tool.hatch.version]
32
+ path = 'evalidate/__init__.py'
@@ -0,0 +1,40 @@
1
+ from evalidate import Expr, ValidationException, ExecutionException, CompilationException, base_eval_model
2
+ import pytest
3
+
4
+ class TestExceptions():
5
+ def test_exec(self):
6
+ with pytest.raises(ExecutionException) as e:
7
+ r = Expr('1/0').eval()
8
+
9
+ with pytest.raises(ExecutionException) as e:
10
+ r = Expr('1+x').eval()
11
+
12
+ def test_badcode(self):
13
+
14
+ with pytest.raises(CompilationException) as e:
15
+ r = Expr('')
16
+
17
+ with pytest.raises(CompilationException) as e:
18
+ r = Expr(';')
19
+
20
+ with pytest.raises(CompilationException) as e:
21
+ r = Expr("""
22
+ 1+1
23
+ 2+2""")
24
+
25
+ def test_call(self):
26
+ ctx = {'a':36.6}
27
+ with pytest.raises(ValidationException) as e:
28
+ r = Expr('int(a)').eval(ctx)
29
+
30
+ def test_return(self):
31
+ with pytest.raises(CompilationException) as e:
32
+ Expr('return 42').eval()
33
+
34
+ def test_startswith(self):
35
+ with pytest.raises(ValidationException) as e:
36
+ src = '"abcdef".startswith("abc")'
37
+ m = base_eval_model.clone()
38
+ m.nodes.append('Call')
39
+ r = Expr(src).eval()
40
+