-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathsafe_eval.py
More file actions
645 lines (574 loc) · 20 KB
/
Copy pathsafe_eval.py
File metadata and controls
645 lines (574 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# Part of Odoo. See LICENSE file for full copyright and licensing details.
# https://github.com/odoo/odoo/blob/16.0/odoo/tools/safe_eval.py
"""
safe_eval module - methods intended to provide more restricted alternatives to
evaluate simple and/or untrusted code.
Methods in this module are typically used as alternatives to eval() to parse
OpenERP domain strings, conditions and expressions, mostly based on locals
condition/math builtins.
"""
# Module partially ripped from/inspired by several different sources:
# - http://code.activestate.com/recipes/286134/
# - safe_eval in lp:~xrg/openobject-server/optimize-5.0
# - safe_eval in tryton http://hg.tryton.org/hgwebdir.cgi/trytond/rev/bbb5f73319ad
import dis
import functools
import logging
import sys
import types
from types import CodeType
import werkzeug
from opcode import opmap, opname
from psycopg2 import OperationalError
import odoo
unsafe_eval = eval
__all__ = ["test_expr", "safe_eval", "const_eval"]
# The time module is usually already provided in the safe_eval environment
# but some code, e.g. datetime.datetime.now() (Windows/Python 2.5.2, bug
# lp:703841), does import time.
_ALLOWED_MODULES = ["_strptime", "math", "time"]
# Mock __import__ function, as called by cpython's import emulator `PyImport_Import` inside
# timemodule.c, _datetimemodule.c and others.
# This function does not actually need to do anything, its expected side-effect is to make the
# imported module available in `sys.modules`. The _ALLOWED_MODULES are imported below to make it so.
def _import(name, globals=None, locals=None, fromlist=None, level=-1):
if name not in sys.modules:
raise ImportError(
f"module {name} should be imported before calling safe_eval()"
)
for module in _ALLOWED_MODULES:
__import__(module)
_UNSAFE_ATTRIBUTES = [
# Frames
"f_builtins",
"f_code",
"f_globals",
"f_locals",
# Python 2 functions
"func_code",
"func_globals",
# Code object
"co_code",
"_co_code_adaptive",
# Method resolution order,
"mro",
# Tracebacks
"tb_frame",
# Generators
"gi_code",
"gi_frame",
"gi_yieldfrom",
# Coroutines
"cr_await",
"cr_code",
"cr_frame",
# Coroutine generators
"ag_await",
"ag_code",
"ag_frame",
]
def to_opcodes(opnames, _opmap=opmap):
for x in opnames:
if x in _opmap:
yield _opmap[x]
# opcodes which absolutely positively must not be usable in safe_eval,
# explicitly subtracted from all sets of valid opcodes just in case
_BLACKLIST = set(
to_opcodes(
[
# can't provide access to accessing arbitrary modules
# 'IMPORT_STAR', 'IMPORT_NAME', 'IMPORT_FROM',
# could allow replacing or updating core attributes on models & al, setitem
# can be used to set field values
"STORE_ATTR",
"DELETE_ATTR",
# no reason to allow this
"STORE_GLOBAL",
"DELETE_GLOBAL",
]
)
)
# opcodes necessary to build literal values
_CONST_OPCODES = (
set(
to_opcodes(
[
# stack manipulations
"POP_TOP",
"ROT_TWO",
"ROT_THREE",
"ROT_FOUR",
"DUP_TOP",
"DUP_TOP_TWO",
"LOAD_CONST",
"RETURN_VALUE", # return the result of the literal/expr evaluation
# literal collections
"BUILD_LIST",
"BUILD_MAP",
"BUILD_TUPLE",
"BUILD_SET",
# 3.6: literal map with constant keys https://bugs.python.org/issue27140
"BUILD_CONST_KEY_MAP",
"LIST_EXTEND",
"SET_UPDATE",
# 3.11 replace DUP_TOP, DUP_TOP_TWO, ROT_TWO, ROT_THREE, ROT_FOUR
"COPY",
"SWAP",
# Added in 3.11 https://docs.python.org/3/whatsnew/3.11.html#new-opcodes
"RESUME",
# 3.12 https://docs.python.org/3/whatsnew/3.12.html#cpython-bytecode-changes
"RETURN_CONST",
# 3.13
"TO_BOOL",
]
)
)
- _BLACKLIST
)
# operations which are both binary and inplace, same order as in doc'
_operations = [
"POWER",
"MULTIPLY", # 'MATRIX_MULTIPLY', # matrix operator (3.5+)
"FLOOR_DIVIDE",
"TRUE_DIVIDE",
"MODULO",
"ADD",
"SUBTRACT",
"LSHIFT",
"RSHIFT",
"AND",
"XOR",
"OR",
]
# operations on literal values
_EXPR_OPCODES = (
_CONST_OPCODES.union(
to_opcodes(
[
"UNARY_POSITIVE",
"UNARY_NEGATIVE",
"UNARY_NOT",
"UNARY_INVERT",
*("BINARY_" + op for op in _operations),
"BINARY_SUBSCR",
*("INPLACE_" + op for op in _operations),
"BUILD_SLICE",
# comprehensions
"LIST_APPEND",
"MAP_ADD",
"SET_ADD",
"COMPARE_OP",
# specialised comparisons
"IS_OP",
"CONTAINS_OP",
"DICT_MERGE",
"DICT_UPDATE",
# Basically used in any "generator literal"
"GEN_START", # added in 3.10 but already removed from 3.11.
# Added in 3.11, replacing all BINARY_* and INPLACE_*
"BINARY_OP",
"BINARY_SLICE",
]
)
)
- _BLACKLIST
)
_SAFE_OPCODES = (
_EXPR_OPCODES.union(
to_opcodes(
[
# MAGIC
"IMPORT_STAR",
"IMPORT_NAME",
"IMPORT_FROM",
# It's needed to make a function with *args
"LIST_TO_TUPLE",
"POP_BLOCK",
"POP_EXCEPT",
# note: removed in 3.8
"SETUP_LOOP",
"SETUP_EXCEPT",
"BREAK_LOOP",
"CONTINUE_LOOP",
"EXTENDED_ARG", # P3.6 for long jump offsets.
"MAKE_FUNCTION",
"CALL_FUNCTION",
"CALL_FUNCTION_KW",
"CALL_FUNCTION_EX",
# Added in P3.7 https://bugs.python.org/issue26110
"CALL_METHOD",
"LOAD_METHOD",
"GET_ITER",
"FOR_ITER",
"YIELD_VALUE",
"JUMP_FORWARD",
"JUMP_ABSOLUTE",
"JUMP_BACKWARD",
"JUMP_IF_FALSE_OR_POP",
"JUMP_IF_TRUE_OR_POP",
"POP_JUMP_IF_FALSE",
"POP_JUMP_IF_TRUE",
"SETUP_FINALLY",
"END_FINALLY",
# Added in 3.8 https://bugs.python.org/issue17611
"BEGIN_FINALLY",
"CALL_FINALLY",
"POP_FINALLY",
"RAISE_VARARGS",
"LOAD_NAME",
"STORE_NAME",
"DELETE_NAME",
"LOAD_ATTR",
"LOAD_FAST",
"STORE_FAST",
"DELETE_FAST",
"UNPACK_SEQUENCE",
"STORE_SUBSCR",
"LOAD_GLOBAL",
"RERAISE",
"JUMP_IF_NOT_EXC_MATCH",
# Following opcodes were Added in 3.11
# replacement of opcodes CALL_FUNCTION, CALL_FUNCTION_KW, CALL_METHOD
"PUSH_NULL",
"PRECALL",
"CALL",
"KW_NAMES",
# replacement of POP_JUMP_IF_TRUE and POP_JUMP_IF_FALSE
"POP_JUMP_FORWARD_IF_FALSE",
"POP_JUMP_FORWARD_IF_TRUE",
"POP_JUMP_BACKWARD_IF_FALSE",
"POP_JUMP_BACKWARD_IF_TRUE",
# special case of the previous for IS NONE / IS NOT NONE
"POP_JUMP_FORWARD_IF_NONE",
"POP_JUMP_BACKWARD_IF_NONE",
"POP_JUMP_FORWARD_IF_NOT_NONE",
"POP_JUMP_BACKWARD_IF_NOT_NONE",
# replacement of JUMP_IF_NOT_EXC_MATCH
"CHECK_EXC_MATCH",
# new opcodes
"RETURN_GENERATOR",
"PUSH_EXC_INFO",
"NOP",
"FORMAT_VALUE",
"BUILD_STRING",
# 3.12 https://docs.python.org/3/whatsnew/3.12.html#cpython-bytecode-changes
"END_FOR",
"LOAD_FAST_AND_CLEAR",
"LOAD_FAST_CHECK",
"POP_JUMP_IF_NOT_NONE",
"POP_JUMP_IF_NONE",
"CALL_INTRINSIC_1",
"STORE_SLICE",
# 3.13
"CALL_KW",
"LOAD_FAST_LOAD_FAST",
"STORE_FAST_STORE_FAST",
"STORE_FAST_LOAD_FAST",
"CONVERT_VALUE",
"FORMAT_SIMPLE",
"FORMAT_WITH_SPEC",
"SET_FUNCTION_ATTRIBUTE",
]
)
)
- _BLACKLIST
)
_logger = logging.getLogger(__name__)
def assert_no_dunder_name(code_obj, expr):
"""assert_no_dunder_name(code_obj, expr) -> None
Asserts that the code object does not refer to any "dunder name"
(__$name__), so that safe_eval prevents access to any internal-ish Python
attribute or method (both are loaded via LOAD_ATTR which uses a name, not a
const or a var).
Checks that no such name exists in the provided code object (co_names).
:param code_obj: code object to name-validate
:type code_obj: CodeType
:param str expr: expression corresponding to the code object, for debugging
purposes
:raises NameError: in case a forbidden name (containing two underscores)
is found in ``code_obj``
.. note:: actually forbids every name containing 2 underscores
"""
for name in code_obj.co_names:
if "__" in name or name in _UNSAFE_ATTRIBUTES:
raise NameError("Access to forbidden name %r (%r)" % (name, expr))
def assert_valid_codeobj(allowed_codes, code_obj, expr):
"""Asserts that the provided code object validates against the bytecode
and name constraints.
Recursively validates the code objects stored in its co_consts in case
lambdas are being created/used (lambdas generate their own separated code
objects and don't live in the root one)
:param allowed_codes: list of permissible bytecode instructions
:type allowed_codes: set(int)
:param code_obj: code object to name-validate
:type code_obj: CodeType
:param str expr: expression corresponding to the code object, for debugging
purposes
:raises ValueError: in case of forbidden bytecode in ``code_obj``
:raises NameError: in case a forbidden name (containing two underscores)
is found in ``code_obj``
"""
assert_no_dunder_name(code_obj, expr)
# set operations are almost twice as fast as a manual iteration + condition
# when loading /web according to line_profiler
code_codes = {i.opcode for i in dis.get_instructions(code_obj)}
if not allowed_codes >= code_codes:
raise ValueError(
"forbidden opcode(s) in %r (%s): %s"
% (
expr,
code_obj,
", ".join(opname[x] for x in (code_codes - allowed_codes)),
)
)
for const in code_obj.co_consts:
if isinstance(const, CodeType):
assert_valid_codeobj(allowed_codes, const, "lambda")
def test_expr(expr, allowed_codes, mode="eval", filename=None):
"""test_expr(expression, allowed_codes[, mode[, filename]]) -> code_object
Test that the expression contains only the allowed opcodes.
If the expression is valid and contains only allowed codes,
return the compiled code object.
Otherwise raise a ValueError, a Syntax Error or TypeError accordingly.
:param filename: optional pseudo-filename for the compiled expression,
displayed for example in traceback frames
:type filename: string
"""
try:
if mode == "eval":
# eval() does not like leading/trailing whitespace
expr = expr.strip()
code_obj = compile(expr, filename or "", mode)
except (SyntaxError, TypeError, ValueError):
raise
except Exception as e:
raise ValueError('"%s" while compiling\n%r' % (ustr(e), expr))
assert_valid_codeobj(allowed_codes, code_obj, expr)
return code_obj
def const_eval(expr):
"""const_eval(expression) -> value
Safe Python constant evaluation
Evaluates a string that contains an expression describing
a Python constant. Strings that are not valid Python expressions
or that contain other code besides the constant raise ValueError.
>>> const_eval("10")
10
>>> const_eval("[1,2, (3,4), {'foo':'bar'}]")
[1, 2, (3, 4), {'foo': 'bar'}]
>>> const_eval("1+2")
Traceback (most recent call last):
...
ValueError: opcode BINARY_ADD not allowed
"""
c = test_expr(expr, _CONST_OPCODES)
return unsafe_eval(c)
def expr_eval(expr):
"""expr_eval(expression) -> value
Restricted Python expression evaluation
Evaluates a string that contains an expression that only
uses Python constants. This can be used to e.g. evaluate
a numerical expression from an untrusted source.
>>> expr_eval("1+2")
3
>>> expr_eval("[1,2]*2")
[1, 2, 1, 2]
>>> expr_eval("__import__('sys').modules")
Traceback (most recent call last):
...
ValueError: opcode LOAD_NAME not allowed
"""
c = test_expr(expr, _EXPR_OPCODES)
return unsafe_eval(c)
_BUILTINS = {
#'__import__': _import,
"__import__": __import__,
"True": True,
"False": False,
"None": None,
"bytes": bytes,
"str": str,
"unicode": str,
"bool": bool,
"int": int,
"float": float,
"enumerate": enumerate,
"dict": dict,
"list": list,
"tuple": tuple,
"map": map,
"abs": abs,
"min": min,
"max": max,
"sum": sum,
"reduce": functools.reduce,
"filter": filter,
"sorted": sorted,
"round": round,
"len": len,
"repr": repr,
"set": set,
"all": all,
"any": any,
"ord": ord,
"chr": chr,
"divmod": divmod,
"isinstance": isinstance,
"range": range,
"xrange": range,
"zip": zip,
"Exception": Exception,
}
def safe_eval__MAGIC(
expr,
globals_dict=None,
locals_dict=None,
mode="eval",
nocopy=False,
locals_builtins=False,
filename=None,
):
"""safe_eval(expression[, globals[, locals[, mode[, nocopy]]]]) -> result
System-restricted Python expression evaluation
Evaluates a string that contains an expression that mostly
uses Python constants, arithmetic expressions and the
objects directly provided in context.
This can be used to e.g. evaluate
an OpenERP domain expression from an untrusted source.
:param filename: optional pseudo-filename for the compiled expression,
displayed for example in traceback frames
:type filename: string
:throws TypeError: If the expression provided is a code object
:throws SyntaxError: If the expression provided is not valid Python
:throws NameError: If the expression provided accesses forbidden names
:throws ValueError: If the expression provided uses forbidden bytecode
"""
if type(expr) is CodeType:
raise TypeError("safe_eval does not allow direct evaluation of code objects.")
# prevent altering the globals/locals from within the sandbox
# by taking a copy.
if not nocopy:
# isinstance() does not work below, we want *exactly* the dict class
if (globals_dict is not None and type(globals_dict) is not dict) or (
locals_dict is not None and type(locals_dict) is not dict
):
_logger.warning(
"Looks like you are trying to pass a dynamic environment, "
"you should probably pass nocopy=True to safe_eval()."
)
if globals_dict is not None:
globals_dict = dict(globals_dict)
if locals_dict is not None:
locals_dict = dict(locals_dict)
check_values(globals_dict)
check_values(locals_dict)
if globals_dict is None:
globals_dict = {}
globals_dict["__builtins__"] = _BUILTINS
if locals_builtins:
if locals_dict is None:
locals_dict = {}
locals_dict.update(_BUILTINS)
c = test_expr(expr, _SAFE_OPCODES, mode=mode, filename=filename)
try:
return unsafe_eval(c, globals_dict, locals_dict)
except odoo.exceptions.UserError:
raise
except odoo.exceptions.RedirectWarning:
raise
except werkzeug.exceptions.HTTPException:
raise
except OperationalError:
# Do not hide PostgreSQL low-level exceptions, to let the auto-replay
# of serialized transactions work its magic
raise
except ZeroDivisionError:
raise
except Exception as e:
raise ValueError(
'%s: "%s" while evaluating\n%r' % (ustr(type(e))(e), expr)
)
def test_python_expr__MAGIC(expr, mode="eval"):
try:
test_expr(expr, _SAFE_OPCODES, mode=mode)
except (SyntaxError, TypeError, ValueError) as err:
if len(err.args) >= 2 and len(err.args[1]) >= 4:
error = {
"message": err.args[0],
"filename": err.args[1][0],
"lineno": err.args[1][1],
"offset": err.args[1][2],
"error_line": err.args[1][3],
}
msg = "%s : %s at line %d\n%s" % (
type(err).__name__,
error["message"],
error["lineno"],
error["error_line"],
)
else:
msg = err
return msg
return False
def check_values(d):
if not d:
return d
for v in d.values():
if isinstance(v, types.ModuleType):
raise TypeError(
f"""Module {v} can not be used in evaluation contexts
Prefer providing only the items necessary for your intended use.
If a "module" is necessary for backwards compatibility, use
`odoo.tools.safe_eval.wrap_module` to generate a wrapper recursively
whitelisting allowed attributes.
Pre-wrapped modules are provided as attributes of `odoo.tools.safe_eval`.
"""
)
return d
class wrap_module:
def __init__(self, module, attributes):
"""Helper for wrapping a package/module to expose selected attributes
:param module: the actual package/module to wrap, as returned by ``import <module>``
:param iterable attributes: attributes to expose / whitelist. If a dict,
the keys are the attributes and the values
are used as an ``attributes`` in case the
corresponding item is a submodule
"""
# builtin modules don't have a __file__ at all
modfile = getattr(module, "__file__", "(built-in)")
self._repr = f"<wrapped {module.__name__!r} ({modfile})>"
for attrib in attributes:
target = getattr(module, attrib)
if isinstance(target, types.ModuleType):
target = wrap_module(target, attributes[attrib])
setattr(self, attrib, target)
def __repr__(self):
return self._repr
# dateutil submodules are lazy so need to import them for them to "exist"
import dateutil
mods = ["parser", "relativedelta", "rrule", "tz"]
for mod in mods:
__import__("dateutil.%s" % mod)
datetime = wrap_module(
__import__("datetime"),
[
"date",
"datetime",
"time",
"timedelta",
"timezone",
"tzinfo",
"MAXYEAR",
"MINYEAR",
],
)
dateutil = wrap_module(dateutil, {mod: getattr(dateutil, mod).__all__ for mod in mods})
json = wrap_module(__import__("json"), ["loads", "dumps"])
time = wrap_module(__import__("time"), ["time", "strptime", "strftime", "sleep"])
pytz = wrap_module(
__import__("pytz"),
[
"utc",
"UTC",
"timezone",
],
)