evalidate 2.0.2__tar.gz → 2.0.4__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.
- evalidate-2.0.4/.gitignore +6 -0
- evalidate-2.0.4/LICENSE +21 -0
- evalidate-2.0.4/NOTES +4 -0
- evalidate-2.0.4/PKG-INFO +290 -0
- {evalidate-2.0.2 → evalidate-2.0.4}/README.md +3 -2
- evalidate-2.0.4/benchmark/benchmark.py +98 -0
- evalidate-2.0.4/ev.py +60 -0
- {evalidate-2.0.2 → evalidate-2.0.4}/evalidate/__init__.py +13 -4
- evalidate-2.0.4/evalidate/credits.txt +4 -0
- evalidate-2.0.4/examples/basic.py +11 -0
- evalidate-2.0.4/examples/products.py +31 -0
- evalidate-2.0.4/pyproject.toml +32 -0
- evalidate-2.0.4/tests/test_exc.py +40 -0
- evalidate-2.0.4/tests/test_expr.py +127 -0
- evalidate-2.0.4/tests/test_feature.py +63 -0
- evalidate-2.0.4/tests/test_jailbreak.py +95 -0
- evalidate-2.0.4/x.py +47 -0
- evalidate-2.0.4/xx.py +26 -0
- evalidate-2.0.4/xx2.py +27 -0
- evalidate-2.0.4/xx3.py +29 -0
- evalidate-2.0.4/y.py +9 -0
- evalidate-2.0.2/PKG-INFO +0 -292
- evalidate-2.0.2/evalidate.egg-info/PKG-INFO +0 -292
- evalidate-2.0.2/evalidate.egg-info/SOURCES.txt +0 -9
- evalidate-2.0.2/evalidate.egg-info/dependency_links.txt +0 -1
- evalidate-2.0.2/evalidate.egg-info/not-zip-safe +0 -1
- evalidate-2.0.2/evalidate.egg-info/top_level.txt +0 -1
- evalidate-2.0.2/setup.cfg +0 -4
- {evalidate-2.0.2 → evalidate-2.0.4}/evalidate/security.py +0 -0
- {evalidate-2.0.2 → evalidate-2.0.4}/setup.py +0 -0
evalidate-2.0.4/LICENSE
ADDED
|
@@ -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.4/NOTES
ADDED
evalidate-2.0.4/PKG-INFO
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: evalidate
|
|
3
|
+
Version: 2.0.4
|
|
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 python 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
|
+
m.imported_functions["one"] = one
|
|
125
|
+
Expr('one()', model=m).eval()
|
|
126
|
+
~~~
|
|
127
|
+
|
|
128
|
+
## Improve speed by using native eval() with validated code
|
|
129
|
+
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:
|
|
130
|
+
|
|
131
|
+
~~~python
|
|
132
|
+
from evalidate import Expr
|
|
133
|
+
|
|
134
|
+
d = dict(a=1, b=2)
|
|
135
|
+
expr = Expr('a+b')
|
|
136
|
+
eval(expr.code, None, d) # <-- native python eval, will run at eval() speed
|
|
137
|
+
~~~
|
|
138
|
+
|
|
139
|
+
This is as secure as expr.eval(), because `expr.code` is already validated to be secure.
|
|
140
|
+
|
|
141
|
+
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.
|
|
142
|
+
|
|
143
|
+
## Limitations
|
|
144
|
+
|
|
145
|
+
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.
|
|
146
|
+
|
|
147
|
+
>Warning
|
|
148
|
+
>
|
|
149
|
+
>It is possible to crash the Python interpreter with a sufficiently large/complex string due to stack depth limitations in Python’s AST compiler.
|
|
150
|
+
|
|
151
|
+
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`.
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
### evalidate.security.test_security()
|
|
155
|
+
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.
|
|
156
|
+
|
|
157
|
+
This code will never print (I hope).
|
|
158
|
+
~~~python
|
|
159
|
+
from evalidate.security import test_security
|
|
160
|
+
|
|
161
|
+
test_security() or print("default rules are vulnerable!")
|
|
162
|
+
~~~
|
|
163
|
+
|
|
164
|
+
But this will fail because nodes/funcs leads to successful validation for attack (suppose you do not want anyone to call `int()`)
|
|
165
|
+
~~~python
|
|
166
|
+
from evalidate.security import test_security
|
|
167
|
+
|
|
168
|
+
attacks = ['int(1)']
|
|
169
|
+
|
|
170
|
+
test_security(attacks, addnodes=['Call'], funcs=['int'], verbose=True)
|
|
171
|
+
~~~
|
|
172
|
+
|
|
173
|
+
It will print:
|
|
174
|
+
~~~
|
|
175
|
+
Testing attack code:
|
|
176
|
+
int(1)
|
|
177
|
+
Problem! Attack passed validation without exception!
|
|
178
|
+
Code:
|
|
179
|
+
int(1)
|
|
180
|
+
~~~
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
## Example
|
|
186
|
+
|
|
187
|
+
### Filtering by user-supplied condition ###
|
|
188
|
+
|
|
189
|
+
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.
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
~~~python
|
|
193
|
+
#!/usr/bin/env python3
|
|
194
|
+
|
|
195
|
+
import requests
|
|
196
|
+
from evalidate import Expr, ValidationException, CompilationException, ExecutionException
|
|
197
|
+
import json
|
|
198
|
+
import sys
|
|
199
|
+
|
|
200
|
+
data = requests.get('https://dummyjson.com/products?limit=100').json()
|
|
201
|
+
|
|
202
|
+
try:
|
|
203
|
+
src = sys.argv[1]
|
|
204
|
+
except IndexError:
|
|
205
|
+
src = 'True'
|
|
206
|
+
|
|
207
|
+
try:
|
|
208
|
+
expr = Expr(src)
|
|
209
|
+
except (ValidationException, CompilationException) as e:
|
|
210
|
+
print(e)
|
|
211
|
+
sys.exit(1)
|
|
212
|
+
|
|
213
|
+
c=0
|
|
214
|
+
for p in data['products']:
|
|
215
|
+
# print(p)
|
|
216
|
+
try:
|
|
217
|
+
r = expr.eval(p)
|
|
218
|
+
if r:
|
|
219
|
+
print(json.dumps(p, indent=2))
|
|
220
|
+
c+=1
|
|
221
|
+
except ExecutionException as e:
|
|
222
|
+
print("Runtime exception:", e)
|
|
223
|
+
print("# {} products matches".format(c))
|
|
224
|
+
~~~
|
|
225
|
+
|
|
226
|
+
~~~shell
|
|
227
|
+
# print all 100 products
|
|
228
|
+
./products.py
|
|
229
|
+
|
|
230
|
+
# Only cheap products, 8 matches
|
|
231
|
+
./products.py 'price<20'
|
|
232
|
+
|
|
233
|
+
# smartphones (5)
|
|
234
|
+
./products.py 'category=="smartphones"'
|
|
235
|
+
|
|
236
|
+
# good smartphones
|
|
237
|
+
./products.py 'category=="smartphones" and rating>4.5'
|
|
238
|
+
|
|
239
|
+
# cheap smartphones
|
|
240
|
+
./products.py 'category=="smartphones" and price<300'
|
|
241
|
+
~~~
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
## Similar projects and benchmark
|
|
245
|
+
|
|
246
|
+
[asteval](https://newville.github.io/asteval/)
|
|
247
|
+
|
|
248
|
+
While asteval can compute much more complex code (define functions, use python math libraries) it has drawbacks:
|
|
249
|
+
- asteval is much slower (evalidate can be used at speed of eval() python bytecode)
|
|
250
|
+
- user can provide source code which runs very long time and consumes many resources
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
[simpleeval](https://github.com/danthedeckie/simpleeval)
|
|
254
|
+
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()
|
|
255
|
+
|
|
256
|
+
evalidate is good to run same expression against different data.
|
|
257
|
+
|
|
258
|
+
## Benchmarking
|
|
259
|
+
We use `benchmark/benchmark.py` in this repository.
|
|
260
|
+
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)
|
|
261
|
+
|
|
262
|
+
~~~
|
|
263
|
+
Products: 1000000 items
|
|
264
|
+
evalidate_raw_eval(): 0.266s
|
|
265
|
+
evalidate_eval(): 0.326s
|
|
266
|
+
test_simpleeval(): 1.824s
|
|
267
|
+
test_asteval(): 26.106s
|
|
268
|
+
~~~
|
|
269
|
+
|
|
270
|
+
As you see, evalidate is few times faster then simpleeval and both are much faster then asteval.
|
|
271
|
+
|
|
272
|
+
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)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
## Read about eval() risks
|
|
276
|
+
|
|
277
|
+
- https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
|
|
278
|
+
- https://netsec.expert/posts/breaking-python3-eval-protections/
|
|
279
|
+
- https://realpython.com/python-eval-function/
|
|
280
|
+
|
|
281
|
+
Note: realpython article shows example with nice short method of validation source (using `code.co_names`),
|
|
282
|
+
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).
|
|
283
|
+
|
|
284
|
+
## More info
|
|
285
|
+
|
|
286
|
+
Want more info? Check source code of module, it's very short and simple, easy to modify
|
|
287
|
+
|
|
288
|
+
## Contact
|
|
289
|
+
|
|
290
|
+
Write me: yaroslaff at gmail.com
|
|
@@ -11,7 +11,7 @@ Originally it's developed for filtering complex data structures e.g.
|
|
|
11
11
|
|
|
12
12
|
Find cheap smartphones available for sale:
|
|
13
13
|
```python
|
|
14
|
-
category
|
|
14
|
+
category=="smartphones" and price<300 and stock>0
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
But also, it can be used for other expressions, e.g. arithmetical, like
|
|
@@ -19,7 +19,7 @@ But also, it can be used for other expressions, e.g. arithmetical, like
|
|
|
19
19
|
a+b-100
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
Evalidate is fastest among all (known to me) secure eval
|
|
22
|
+
Evalidate is fastest among all (known to me) secure eval python modules.
|
|
23
23
|
|
|
24
24
|
## Install
|
|
25
25
|
|
|
@@ -107,6 +107,7 @@ def one():
|
|
|
107
107
|
|
|
108
108
|
m = base_eval_model.clone()
|
|
109
109
|
m.nodes.append('Call')
|
|
110
|
+
m.imported_functions["one"] = one
|
|
110
111
|
Expr('one()', model=m).eval()
|
|
111
112
|
~~~
|
|
112
113
|
|
|
@@ -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()
|
evalidate-2.0.4/ev.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from evalidate import Expr, base_eval_model, ExecutionException
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
my_model = base_eval_model.clone()
|
|
5
|
+
my_model.nodes.extend(
|
|
6
|
+
[
|
|
7
|
+
"Call",
|
|
8
|
+
"Attributes",
|
|
9
|
+
"ListComp",
|
|
10
|
+
"DictComp",
|
|
11
|
+
"comprehension",
|
|
12
|
+
"Store",
|
|
13
|
+
"ForOfStatement",
|
|
14
|
+
"Subscript",
|
|
15
|
+
"GeneratorExp",
|
|
16
|
+
"For",
|
|
17
|
+
]
|
|
18
|
+
)
|
|
19
|
+
my_model.allowed_functions.append("sum")
|
|
20
|
+
my_model.allowed_functions.append("len")
|
|
21
|
+
|
|
22
|
+
my_shelve = {
|
|
23
|
+
"height": 200,
|
|
24
|
+
"boxes": {
|
|
25
|
+
"box1": {"volume": 110},
|
|
26
|
+
"box2": {"volume": 90},
|
|
27
|
+
},
|
|
28
|
+
"column_width": [20, 480, 40],
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
box_volumes = [my_shelve["boxes"][box]["volume"] for box in my_shelve["boxes"]]
|
|
32
|
+
total_volume = sum(box_volumes)
|
|
33
|
+
print(total_volume)
|
|
34
|
+
|
|
35
|
+
# works fine
|
|
36
|
+
exp_string = "sum( my_shelve['column_width'])"
|
|
37
|
+
exp = Expr(exp_string, my_model)
|
|
38
|
+
print("code:", exp.code)
|
|
39
|
+
res = exp.eval({"my_shelve": my_shelve})
|
|
40
|
+
print(res)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# throws an Exception:
|
|
44
|
+
# evalidate.ExecutionException: name 'my_shelve' is not defined
|
|
45
|
+
|
|
46
|
+
# exp_string = "sum([my_shelve['boxes'][box]['volume'] for box in my_shelve['boxes'] ])"
|
|
47
|
+
# exp_string = "sum([my_shelve['boxes'][box]['volume'] for box in my_shelve['boxes'] ])"
|
|
48
|
+
exp_string = "len(my_shelve)"
|
|
49
|
+
exp = Expr(exp_string, my_model)
|
|
50
|
+
|
|
51
|
+
#print("basic eval")
|
|
52
|
+
#res = eval(exp.code, dict(), {"my_shelve": my_shelve})
|
|
53
|
+
#print("basic res:",res)
|
|
54
|
+
# sys.exit(0)
|
|
55
|
+
try:
|
|
56
|
+
res = exp.eval({"my_shelve": my_shelve})
|
|
57
|
+
except ExecutionException as e:
|
|
58
|
+
print(e)
|
|
59
|
+
|
|
60
|
+
# print(res)
|
|
@@ -6,7 +6,7 @@ import ast
|
|
|
6
6
|
import dataclasses
|
|
7
7
|
from typing import Callable
|
|
8
8
|
|
|
9
|
-
__version__ = '2.0.
|
|
9
|
+
__version__ = '2.0.4'
|
|
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)
|
|
@@ -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
|
|
|
@@ -123,9 +124,17 @@ class Expr():
|
|
|
123
124
|
self.code = compile(self.node, filename or '<usercode>', 'eval')
|
|
124
125
|
|
|
125
126
|
def eval(self, ctx=None):
|
|
126
|
-
|
|
127
|
+
|
|
128
|
+
if ctx:
|
|
129
|
+
global_ctx = {
|
|
130
|
+
**self.model.imported_functions,
|
|
131
|
+
**ctx
|
|
132
|
+
}
|
|
133
|
+
else:
|
|
134
|
+
global_ctx = self.model.imported_functions
|
|
135
|
+
|
|
127
136
|
try:
|
|
128
|
-
result = eval(self.code,
|
|
137
|
+
result = eval(self.code, global_ctx)
|
|
129
138
|
except Exception as e:
|
|
130
139
|
raise ExecutionException(e)
|
|
131
140
|
|
|
@@ -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>=1.27.0", "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
|
+
|