From a2b2d1d2278e3a75c9f27ad4f8127abf3fd6d89e Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Tue, 25 May 2021 11:24:11 -0700 Subject: [PATCH] Replace eval functions with helpers to catch exceptions --- plexpy/helpers.py | 14 ++++++++++++++ plexpy/notification_handler.py | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/plexpy/helpers.py b/plexpy/helpers.py index 9e844aa9..e546597b 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -530,6 +530,20 @@ def cast_to_float(s): return 0 +def helper_divmod(a, b): + try: + return divmod(a, b) + except (ValueError, TypeError): + return 0 + + +def helper_round(n, ndigits): + try: + return round(n, ndigits) + except (ValueError, TypeError): + return 0 + + def convert_xml_to_json(xml): o = xmltodict.parse(xml) return json.dumps(o) diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index da02aa43..97c57993 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -1818,10 +1818,10 @@ def str_eval(field_name, kwargs): field_name = field_name.strip('`') allowed_names = { 'bool': bool, - 'divmod': divmod, - 'float': float, - 'int': int, - 'round': round, + 'divmod': helpers.helper_divmod, + 'float': helpers.cast_to_float, + 'int': helpers.cast_to_int, + 'round': helpers.helper_round, 'str': str } allowed_names.update(kwargs)