1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
Description: Harden pox script to check its input before exec'ing it
Forwarded: mmckerns@uqfoundation.org
Author: Julian Gilbey <jdg@debian.org>
Last-Update: 2023-07-24
--- a/scripts/pox
+++ b/scripts/pox
@@ -11,16 +11,30 @@
if __name__=='__main__':
+ import re
import sys
try:
func = sys.argv[1]
except: func = None
if func:
- try:
- exec('print(%s)' % func)
- except:
- print("Error: incorrect syntax '%s'\n" % func)
- exec('print(%s.__doc__)' % func.split('(')[0])
+ poxfuncs = [func for func in dir(pox.__main__)
+ if (not func.startswith("_") and
+ isfunction(getattr(pox.__main__, func)))]
+ poxfuncs.remove("isfunction")
+ func_re = re.compile(r"(\w+)\([^\)]*\)$")
+ func_match = func_re.match(func)
+ if func_match:
+ func_name = func_match.group(1)
+ if func_name not in poxfuncs:
+ print("Error: unrecognised pox function '%s'" % func_name)
+ else:
+ try:
+ exec('print(%s)' % func)
+ except:
+ print("Error: incorrect syntax '%s'\n" % func)
+ print(getattr(pox.__main__, func_name).__doc__)
+ else:
+ print("Error: unrecognised argument '%s'; should have the form 'func(args)'" % func)
else: help()
|