Skip to content

Commit 329433c

Browse files
authored
Merge pull request python-quantities#236 from zm711/fix-eval-issue
Prevent arbitrary code eval
2 parents bb96cbb + 3430bab commit 329433c

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

quantities/registry.py

Lines changed: 12 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,17 @@ 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+
# because we have kilobytes, other bytes we have to remove bytes
23+
all_builtins.remove("bytes")
24+
# have to deal with octet as well
25+
all_builtins.remove("oct")
26+
for builtin in all_builtins:
27+
if builtin in string:
28+
raise RuntimeError(f"String parsing error for {string}. Enter a string accepted by quantities")
29+
1930
try:
2031
return eval(string, self.__context)
2132
except NameError:

0 commit comments

Comments
 (0)