gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

492 lines
18KB

  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """
  4. safe_eval module - methods intended to provide more restricted alternatives to
  5. evaluate simple and/or untrusted code.
  6. Methods in this module are typically used as alternatives to eval() to parse
  7. OpenERP domain strings, conditions and expressions, mostly based on locals
  8. condition/math builtins.
  9. """
  10. # Module partially ripped from/inspired by several different sources:
  11. # - http://code.activestate.com/recipes/286134/
  12. # - safe_eval in lp:~xrg/openobject-server/optimize-5.0
  13. # - safe_eval in tryton http://hg.tryton.org/hgwebdir.cgi/trytond/rev/bbb5f73319ad
  14. import dis
  15. import functools
  16. import logging
  17. import sys
  18. import types
  19. from opcode import opmap, opname
  20. from types import CodeType
  21. import werkzeug
  22. from psycopg2 import OperationalError
  23. import odoo
  24. unsafe_eval = eval
  25. __all__ = ['test_expr', 'safe_eval', 'const_eval']
  26. # The time module is usually already provided in the safe_eval environment
  27. # but some code, e.g. datetime.datetime.now() (Windows/Python 2.5.2, bug
  28. # lp:703841), does import time.
  29. _ALLOWED_MODULES = ['_strptime', 'math', 'time']
  30. # Mock __import__ function, as called by cpython's import emulator `PyImport_Import` inside
  31. # timemodule.c, _datetimemodule.c and others.
  32. # This function does not actually need to do anything, its expected side-effect is to make the
  33. # imported module available in `sys.modules`. The _ALLOWED_MODULES are imported below to make it so.
  34. def _import(name, globals=None, locals=None, fromlist=None, level=-1):
  35. if name not in sys.modules:
  36. raise ImportError(f'module {name} should be imported before calling safe_eval()')
  37. for module in _ALLOWED_MODULES:
  38. __import__(module)
  39. _UNSAFE_ATTRIBUTES = [
  40. # Frames
  41. 'f_builtins', 'f_code', 'f_globals', 'f_locals',
  42. # Python 2 functions
  43. 'func_code', 'func_globals',
  44. # Code object
  45. 'co_code', '_co_code_adaptive',
  46. # Method resolution order,
  47. 'mro',
  48. # Tracebacks
  49. 'tb_frame',
  50. # Generators
  51. 'gi_code', 'gi_frame', 'g_yieldfrom'
  52. # Coroutines
  53. 'cr_await', 'cr_code', 'cr_frame',
  54. # Coroutine generators
  55. 'ag_await', 'ag_code', 'ag_frame',
  56. ]
  57. def to_opcodes(opnames, _opmap=opmap):
  58. for x in opnames:
  59. if x in _opmap:
  60. yield _opmap[x]
  61. # opcodes which absolutely positively must not be usable in safe_eval,
  62. # explicitly subtracted from all sets of valid opcodes just in case
  63. _BLACKLIST = set(to_opcodes([
  64. # can't provide access to accessing arbitrary modules
  65. 'IMPORT_STAR', 'IMPORT_NAME', 'IMPORT_FROM',
  66. # could allow replacing or updating core attributes on models & al, setitem
  67. # can be used to set field values
  68. 'STORE_ATTR', 'DELETE_ATTR',
  69. # no reason to allow this
  70. 'STORE_GLOBAL', 'DELETE_GLOBAL',
  71. ]))
  72. # opcodes necessary to build literal values
  73. _CONST_OPCODES = set(to_opcodes([
  74. # stack manipulations
  75. 'POP_TOP', 'ROT_TWO', 'ROT_THREE', 'ROT_FOUR', 'DUP_TOP', 'DUP_TOP_TWO',
  76. 'LOAD_CONST',
  77. 'RETURN_VALUE', # return the result of the literal/expr evaluation
  78. # literal collections
  79. 'BUILD_LIST', 'BUILD_MAP', 'BUILD_TUPLE', 'BUILD_SET',
  80. # 3.6: literal map with constant keys https://bugs.python.org/issue27140
  81. 'BUILD_CONST_KEY_MAP',
  82. 'LIST_EXTEND', 'SET_UPDATE',
  83. # 3.11 replace DUP_TOP, DUP_TOP_TWO, ROT_TWO, ROT_THREE, ROT_FOUR
  84. 'COPY', 'SWAP',
  85. # Added in 3.11 https://docs.python.org/3/whatsnew/3.11.html#new-opcodes
  86. 'RESUME',
  87. # 3.12 https://docs.python.org/3/whatsnew/3.12.html#cpython-bytecode-changes
  88. 'RETURN_CONST',
  89. # 3.13
  90. 'TO_BOOL',
  91. ])) - _BLACKLIST
  92. # operations which are both binary and inplace, same order as in doc'
  93. _operations = [
  94. 'POWER', 'MULTIPLY', # 'MATRIX_MULTIPLY', # matrix operator (3.5+)
  95. 'FLOOR_DIVIDE', 'TRUE_DIVIDE', 'MODULO', 'ADD',
  96. 'SUBTRACT', 'LSHIFT', 'RSHIFT', 'AND', 'XOR', 'OR',
  97. ]
  98. # operations on literal values
  99. _EXPR_OPCODES = _CONST_OPCODES.union(to_opcodes([
  100. 'UNARY_POSITIVE', 'UNARY_NEGATIVE', 'UNARY_NOT', 'UNARY_INVERT',
  101. *('BINARY_' + op for op in _operations), 'BINARY_SUBSCR',
  102. *('INPLACE_' + op for op in _operations),
  103. 'BUILD_SLICE',
  104. # comprehensions
  105. 'LIST_APPEND', 'MAP_ADD', 'SET_ADD',
  106. 'COMPARE_OP',
  107. # specialised comparisons
  108. 'IS_OP', 'CONTAINS_OP',
  109. 'DICT_MERGE', 'DICT_UPDATE',
  110. # Basically used in any "generator literal"
  111. 'GEN_START', # added in 3.10 but already removed from 3.11.
  112. # Added in 3.11, replacing all BINARY_* and INPLACE_*
  113. 'BINARY_OP',
  114. 'BINARY_SLICE',
  115. ])) - _BLACKLIST
  116. _SAFE_OPCODES = _EXPR_OPCODES.union(to_opcodes([
  117. 'POP_BLOCK', 'POP_EXCEPT',
  118. # note: removed in 3.8
  119. 'SETUP_LOOP', 'SETUP_EXCEPT', 'BREAK_LOOP', 'CONTINUE_LOOP',
  120. 'EXTENDED_ARG', # P3.6 for long jump offsets.
  121. 'MAKE_FUNCTION', 'CALL_FUNCTION', 'CALL_FUNCTION_KW', 'CALL_FUNCTION_EX',
  122. # Added in P3.7 https://bugs.python.org/issue26110
  123. 'CALL_METHOD', 'LOAD_METHOD',
  124. 'GET_ITER', 'FOR_ITER', 'YIELD_VALUE',
  125. 'JUMP_FORWARD', 'JUMP_ABSOLUTE', 'JUMP_BACKWARD',
  126. 'JUMP_IF_FALSE_OR_POP', 'JUMP_IF_TRUE_OR_POP', 'POP_JUMP_IF_FALSE', 'POP_JUMP_IF_TRUE',
  127. 'SETUP_FINALLY', 'END_FINALLY',
  128. # Added in 3.8 https://bugs.python.org/issue17611
  129. 'BEGIN_FINALLY', 'CALL_FINALLY', 'POP_FINALLY',
  130. 'RAISE_VARARGS', 'LOAD_NAME', 'STORE_NAME', 'DELETE_NAME', 'LOAD_ATTR',
  131. 'LOAD_FAST', 'STORE_FAST', 'DELETE_FAST', 'UNPACK_SEQUENCE',
  132. 'STORE_SUBSCR',
  133. 'LOAD_GLOBAL',
  134. 'RERAISE', 'JUMP_IF_NOT_EXC_MATCH',
  135. # Following opcodes were Added in 3.11
  136. # replacement of opcodes CALL_FUNCTION, CALL_FUNCTION_KW, CALL_METHOD
  137. 'PUSH_NULL', 'PRECALL', 'CALL', 'KW_NAMES',
  138. # replacement of POP_JUMP_IF_TRUE and POP_JUMP_IF_FALSE
  139. 'POP_JUMP_FORWARD_IF_FALSE', 'POP_JUMP_FORWARD_IF_TRUE',
  140. 'POP_JUMP_BACKWARD_IF_FALSE', 'POP_JUMP_BACKWARD_IF_TRUE',
  141. # special case of the previous for IS NONE / IS NOT NONE
  142. 'POP_JUMP_FORWARD_IF_NONE', 'POP_JUMP_BACKWARD_IF_NONE',
  143. 'POP_JUMP_FORWARD_IF_NOT_NONE', 'POP_JUMP_BACKWARD_IF_NOT_NONE',
  144. # replacement of JUMP_IF_NOT_EXC_MATCH
  145. 'CHECK_EXC_MATCH',
  146. # new opcodes
  147. 'RETURN_GENERATOR',
  148. 'PUSH_EXC_INFO',
  149. 'NOP',
  150. 'FORMAT_VALUE', 'BUILD_STRING',
  151. # 3.12 https://docs.python.org/3/whatsnew/3.12.html#cpython-bytecode-changes
  152. 'END_FOR',
  153. 'LOAD_FAST_AND_CLEAR', 'LOAD_FAST_CHECK',
  154. 'POP_JUMP_IF_NOT_NONE', 'POP_JUMP_IF_NONE',
  155. 'CALL_INTRINSIC_1',
  156. 'STORE_SLICE',
  157. # 3.13
  158. 'CALL_KW', 'LOAD_FAST_LOAD_FAST',
  159. 'STORE_FAST_STORE_FAST', 'STORE_FAST_LOAD_FAST',
  160. 'CONVERT_VALUE', 'FORMAT_SIMPLE', 'FORMAT_WITH_SPEC',
  161. 'SET_FUNCTION_ATTRIBUTE',
  162. ])) - _BLACKLIST
  163. _logger = logging.getLogger(__name__)
  164. def assert_no_dunder_name(code_obj, expr):
  165. """ assert_no_dunder_name(code_obj, expr) -> None
  166. Asserts that the code object does not refer to any "dunder name"
  167. (__$name__), so that safe_eval prevents access to any internal-ish Python
  168. attribute or method (both are loaded via LOAD_ATTR which uses a name, not a
  169. const or a var).
  170. Checks that no such name exists in the provided code object (co_names).
  171. :param code_obj: code object to name-validate
  172. :type code_obj: CodeType
  173. :param str expr: expression corresponding to the code object, for debugging
  174. purposes
  175. :raises NameError: in case a forbidden name (containing two underscores)
  176. is found in ``code_obj``
  177. .. note:: actually forbids every name containing 2 underscores
  178. """
  179. for name in code_obj.co_names:
  180. if "__" in name or name in _UNSAFE_ATTRIBUTES:
  181. raise NameError('Access to forbidden name %r (%r)' % (name, expr))
  182. def assert_valid_codeobj(allowed_codes, code_obj, expr):
  183. """ Asserts that the provided code object validates against the bytecode
  184. and name constraints.
  185. Recursively validates the code objects stored in its co_consts in case
  186. lambdas are being created/used (lambdas generate their own separated code
  187. objects and don't live in the root one)
  188. :param allowed_codes: list of permissible bytecode instructions
  189. :type allowed_codes: set(int)
  190. :param code_obj: code object to name-validate
  191. :type code_obj: CodeType
  192. :param str expr: expression corresponding to the code object, for debugging
  193. purposes
  194. :raises ValueError: in case of forbidden bytecode in ``code_obj``
  195. :raises NameError: in case a forbidden name (containing two underscores)
  196. is found in ``code_obj``
  197. """
  198. assert_no_dunder_name(code_obj, expr)
  199. # set operations are almost twice as fast as a manual iteration + condition
  200. # when loading /web according to line_profiler
  201. code_codes = {i.opcode for i in dis.get_instructions(code_obj)}
  202. if not allowed_codes >= code_codes:
  203. raise ValueError("forbidden opcode(s) in %r: %s" % (expr, ', '.join(opname[x] for x in (code_codes - allowed_codes))))
  204. for const in code_obj.co_consts:
  205. if isinstance(const, CodeType):
  206. assert_valid_codeobj(allowed_codes, const, 'lambda')
  207. def test_expr(expr, allowed_codes, mode="eval", filename=None):
  208. """test_expr(expression, allowed_codes[, mode[, filename]]) -> code_object
  209. Test that the expression contains only the allowed opcodes.
  210. If the expression is valid and contains only allowed codes,
  211. return the compiled code object.
  212. Otherwise raise a ValueError, a Syntax Error or TypeError accordingly.
  213. :param filename: optional pseudo-filename for the compiled expression,
  214. displayed for example in traceback frames
  215. :type filename: string
  216. """
  217. try:
  218. if mode == 'eval':
  219. # eval() does not like leading/trailing whitespace
  220. expr = expr.strip()
  221. code_obj = compile(expr, filename or "", mode)
  222. except (SyntaxError, TypeError, ValueError):
  223. raise
  224. except Exception as e:
  225. raise ValueError('%r while compiling\n%r' % (e, expr))
  226. assert_valid_codeobj(allowed_codes, code_obj, expr)
  227. return code_obj
  228. def const_eval(expr):
  229. """const_eval(expression) -> value
  230. Safe Python constant evaluation
  231. Evaluates a string that contains an expression describing
  232. a Python constant. Strings that are not valid Python expressions
  233. or that contain other code besides the constant raise ValueError.
  234. >>> const_eval("10")
  235. 10
  236. >>> const_eval("[1,2, (3,4), {'foo':'bar'}]")
  237. [1, 2, (3, 4), {'foo': 'bar'}]
  238. >>> const_eval("1+2")
  239. Traceback (most recent call last):
  240. ...
  241. ValueError: opcode BINARY_ADD not allowed
  242. """
  243. c = test_expr(expr, _CONST_OPCODES)
  244. return unsafe_eval(c)
  245. def expr_eval(expr):
  246. """expr_eval(expression) -> value
  247. Restricted Python expression evaluation
  248. Evaluates a string that contains an expression that only
  249. uses Python constants. This can be used to e.g. evaluate
  250. a numerical expression from an untrusted source.
  251. >>> expr_eval("1+2")
  252. 3
  253. >>> expr_eval("[1,2]*2")
  254. [1, 2, 1, 2]
  255. >>> expr_eval("__import__('sys').modules")
  256. Traceback (most recent call last):
  257. ...
  258. ValueError: opcode LOAD_NAME not allowed
  259. """
  260. c = test_expr(expr, _EXPR_OPCODES)
  261. return unsafe_eval(c)
  262. _BUILTINS = {
  263. '__import__': _import,
  264. 'True': True,
  265. 'False': False,
  266. 'None': None,
  267. 'bytes': bytes,
  268. 'str': str,
  269. 'unicode': str,
  270. 'bool': bool,
  271. 'int': int,
  272. 'float': float,
  273. 'enumerate': enumerate,
  274. 'dict': dict,
  275. 'list': list,
  276. 'tuple': tuple,
  277. 'map': map,
  278. 'abs': abs,
  279. 'min': min,
  280. 'max': max,
  281. 'sum': sum,
  282. 'reduce': functools.reduce,
  283. 'filter': filter,
  284. 'sorted': sorted,
  285. 'round': round,
  286. 'len': len,
  287. 'repr': repr,
  288. 'set': set,
  289. 'all': all,
  290. 'any': any,
  291. 'ord': ord,
  292. 'chr': chr,
  293. 'divmod': divmod,
  294. 'isinstance': isinstance,
  295. 'range': range,
  296. 'xrange': range,
  297. 'zip': zip,
  298. 'Exception': Exception,
  299. }
  300. def safe_eval(expr, globals_dict=None, locals_dict=None, mode="eval", nocopy=False, locals_builtins=False, filename=None):
  301. """safe_eval(expression[, globals[, locals[, mode[, nocopy]]]]) -> result
  302. System-restricted Python expression evaluation
  303. Evaluates a string that contains an expression that mostly
  304. uses Python constants, arithmetic expressions and the
  305. objects directly provided in context.
  306. This can be used to e.g. evaluate
  307. an OpenERP domain expression from an untrusted source.
  308. :param filename: optional pseudo-filename for the compiled expression,
  309. displayed for example in traceback frames
  310. :type filename: string
  311. :throws TypeError: If the expression provided is a code object
  312. :throws SyntaxError: If the expression provided is not valid Python
  313. :throws NameError: If the expression provided accesses forbidden names
  314. :throws ValueError: If the expression provided uses forbidden bytecode
  315. """
  316. if type(expr) is CodeType:
  317. raise TypeError("safe_eval does not allow direct evaluation of code objects.")
  318. # prevent altering the globals/locals from within the sandbox
  319. # by taking a copy.
  320. if not nocopy:
  321. # isinstance() does not work below, we want *exactly* the dict class
  322. if (globals_dict is not None and type(globals_dict) is not dict) \
  323. or (locals_dict is not None and type(locals_dict) is not dict):
  324. _logger.warning(
  325. "Looks like you are trying to pass a dynamic environment, "
  326. "you should probably pass nocopy=True to safe_eval().")
  327. if globals_dict is not None:
  328. globals_dict = dict(globals_dict)
  329. if locals_dict is not None:
  330. locals_dict = dict(locals_dict)
  331. check_values(globals_dict)
  332. check_values(locals_dict)
  333. if globals_dict is None:
  334. globals_dict = {}
  335. globals_dict['__builtins__'] = _BUILTINS
  336. if locals_builtins:
  337. if locals_dict is None:
  338. locals_dict = {}
  339. locals_dict.update(_BUILTINS)
  340. c = test_expr(expr, _SAFE_OPCODES, mode=mode, filename=filename)
  341. try:
  342. return unsafe_eval(c, globals_dict, locals_dict)
  343. except odoo.exceptions.UserError:
  344. raise
  345. except odoo.exceptions.RedirectWarning:
  346. raise
  347. except werkzeug.exceptions.HTTPException:
  348. raise
  349. except OperationalError:
  350. # Do not hide PostgreSQL low-level exceptions, to let the auto-replay
  351. # of serialized transactions work its magic
  352. raise
  353. except ZeroDivisionError:
  354. raise
  355. except Exception as e:
  356. raise ValueError('%r while evaluating\n%r' % (e, expr))
  357. def test_python_expr(expr, mode="eval"):
  358. try:
  359. test_expr(expr, _SAFE_OPCODES, mode=mode)
  360. except (SyntaxError, TypeError, ValueError) as err:
  361. if len(err.args) >= 2 and len(err.args[1]) >= 4:
  362. error = {
  363. 'message': err.args[0],
  364. 'filename': err.args[1][0],
  365. 'lineno': err.args[1][1],
  366. 'offset': err.args[1][2],
  367. 'error_line': err.args[1][3],
  368. }
  369. msg = "%s : %s at line %d\n%s" % (type(err).__name__, error['message'], error['lineno'], error['error_line'])
  370. else:
  371. msg = str(err)
  372. return msg
  373. return False
  374. def check_values(d):
  375. if not d:
  376. return d
  377. for v in d.values():
  378. if isinstance(v, types.ModuleType):
  379. raise TypeError(f"""Module {v} can not be used in evaluation contexts
  380. Prefer providing only the items necessary for your intended use.
  381. If a "module" is necessary for backwards compatibility, use
  382. `odoo.tools.safe_eval.wrap_module` to generate a wrapper recursively
  383. whitelisting allowed attributes.
  384. Pre-wrapped modules are provided as attributes of `odoo.tools.safe_eval`.
  385. """)
  386. return d
  387. class wrap_module:
  388. def __init__(self, module, attributes):
  389. """Helper for wrapping a package/module to expose selected attributes
  390. :param module: the actual package/module to wrap, as returned by ``import <module>``
  391. :param iterable attributes: attributes to expose / whitelist. If a dict,
  392. the keys are the attributes and the values
  393. are used as an ``attributes`` in case the
  394. corresponding item is a submodule
  395. """
  396. # builtin modules don't have a __file__ at all
  397. modfile = getattr(module, '__file__', '(built-in)')
  398. self._repr = f"<wrapped {module.__name__!r} ({modfile})>"
  399. for attrib in attributes:
  400. target = getattr(module, attrib)
  401. if isinstance(target, types.ModuleType):
  402. target = wrap_module(target, attributes[attrib])
  403. setattr(self, attrib, target)
  404. def __repr__(self):
  405. return self._repr
  406. # dateutil submodules are lazy so need to import them for them to "exist"
  407. import dateutil
  408. mods = ['parser', 'relativedelta', 'rrule', 'tz']
  409. for mod in mods:
  410. __import__('dateutil.%s' % mod)
  411. # make sure to patch pytz before exposing
  412. from odoo._monkeypatches.pytz import patch_pytz # noqa: E402, F401
  413. patch_pytz()
  414. datetime = wrap_module(__import__('datetime'), ['date', 'datetime', 'time', 'timedelta', 'timezone', 'tzinfo', 'MAXYEAR', 'MINYEAR'])
  415. dateutil = wrap_module(dateutil, {
  416. "tz": ["UTC", "tzutc"],
  417. "parser": ["isoparse", "parse"],
  418. "relativedelta": ["relativedelta", "MO", "TU", "WE", "TH", "FR", "SA", "SU"],
  419. "rrule": ["rrule", "rruleset", "rrulestr", "YEARLY", "MONTHLY", "WEEKLY", "DAILY", "HOURLY", "MINUTELY", "SECONDLY", "MO", "TU", "WE", "TH", "FR", "SA", "SU"],
  420. })
  421. json = wrap_module(__import__('json'), ['loads', 'dumps'])
  422. time = wrap_module(__import__('time'), ['time', 'strptime', 'strftime', 'sleep'])
  423. pytz = wrap_module(__import__('pytz'), [
  424. 'utc', 'UTC', 'timezone',
  425. ])
  426. dateutil.tz.gettz = pytz.timezone
上海开阖软件有限公司 沪ICP备12045867号-1