Skip to content

Commit 05c3384

Browse files
committed
restrict param.py to explicitly listed functions
1 parent 6fd1fbb commit 05c3384

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Scripts/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ The expressions are scanned and the angle brackets
119119
replaced (pre-processing):
120120
* Variable definitions are copied verbatim to the output (for reference/documentation).
121121
* Expressions are replaced by their result.
122-
* Any valid Python expressions can be used. The math module is already imported (`from math import *`), you can use all math functions without prefix.
122+
* Python expressions involving the following functions from the `math` module can be used:
123+
```
124+
'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
125+
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
126+
```
127+
123128

124129

125130
| | `<filename>.par` or `par.<filename>` |`<filename>` |

Scripts/param.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
if len(sys.argv)>1:
2727
print "Using file:",sys.argv[1]
2828
source = sys.argv[1]
29+
# check if parameter values are given on the command line
30+
if len(sys.argv)>2:
31+
print sys.argv[2:]
2932

3033
print("Source "+source)
3134
if source.endswith(".par"):
@@ -36,10 +39,19 @@
3639
f = open(source,"r")
3740
fo = open(target,"w")
3841
# context dictionaries for evaluation
39-
g={}
40-
l={}
42+
# restriction to safe functions based on
43+
# http://lybniz2.sourceforge.net/safeeval.html
44+
from math import *
45+
safe_list = ['acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
46+
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
47+
#use the list to filter the local namespace
48+
safe_dict = dict([ (k, locals().get(k, None)) for k in safe_list ])
49+
#add any needed builtins back in.
50+
safe_dict['abs'] = abs
51+
l=safe_dict
52+
g={"__builtins__":None}
4153
ln=1 # linenumber for error message
42-
exec("from math import *",g,l)
54+
#exec("from math import *",g,l)
4355
for line in f:
4456
# split line into dead and active strings
4557
s=re.split(r"<([^>]*)>",line)

0 commit comments

Comments
 (0)