Skip to content

Commit 2a18ba9

Browse files
committed
prevent arbitrary code eval
1 parent b47419a commit 2a18ba9

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

quantities/registry.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
"""
33

4-
import copy
54
import re
5+
import builtins
66

77

88
class UnitRegistry:
@@ -16,6 +16,13 @@ def __init__(self):
1616
self.__context = {}
1717

1818
def __getitem__(self, string):
19+
20+
# easy hack to prevent arbitrary evaluation of code
21+
all_builtins = dir(builtins)
22+
for builtin in all_builtins:
23+
if builtin in string:
24+
raise RuntimeError(f"String parsing error for {string}. Enter a string accepted by quantities")
25+
1926
try:
2027
return eval(string, self.__context)
2128
except NameError:

0 commit comments

Comments
 (0)