@@ -78,6 +78,8 @@ class DataStore(object):
7878 username = None
7979 password = None
8080
81+ RESTAPI_READONLY_OPTIONS = ("api" , "taskid" , "database" )
82+
8183# API objects
8284class Database (object ):
8385 filepath = None
@@ -296,6 +298,19 @@ def setRestAPILog():
296298def is_admin (token ):
297299 return safeCompareStrings (DataStore .admin_token , token )
298300
301+ def validate_task_options (taskid , options , caller ):
302+ if not isinstance (options , dict ):
303+ logger .warning ("[%s] Invalid JSON options provided to %s()" % (taskid , caller ))
304+ return "Invalid JSON options"
305+
306+ for key in options :
307+ if key in RESTAPI_UNSUPPORTED_OPTIONS or key in RESTAPI_READONLY_OPTIONS :
308+ logger .warning ("[%s] Unsupported option '%s' provided to %s()" % (taskid , key , caller ))
309+ return "Unsupported option '%s'" % key
310+ elif key not in DataStore .tasks [taskid ].options :
311+ logger .warning ("[%s] Unknown option '%s' provided to %s()" % (taskid , key , caller ))
312+ return "Unknown option '%s'" % key
313+
299314@hook ('before_request' )
300315def check_authentication ():
301316 if not any ((DataStore .username , DataStore .password )):
@@ -490,10 +505,9 @@ def option_set(taskid):
490505 logger .warning ("[%s] Invalid JSON options provided to option_set()" % taskid )
491506 return jsonize ({"success" : False , "message" : "Invalid JSON options" })
492507
493- for key in request .json :
494- if key in RESTAPI_UNSUPPORTED_OPTIONS :
495- logger .warning ("[%s] Unsupported option '%s' provided to option_set()" % (taskid , key ))
496- return jsonize ({"success" : False , "message" : "Unsupported option '%s'" % key })
508+ message = validate_task_options (taskid , request .json , "option_set" )
509+ if message :
510+ return jsonize ({"success" : False , "message" : message })
497511
498512 for option , value in request .json .items ():
499513 DataStore .tasks [taskid ].set_option (option , value )
@@ -516,10 +530,13 @@ def scan_start(taskid):
516530 logger .warning ("[%s] Invalid JSON options provided to scan_start()" % taskid )
517531 return jsonize ({"success" : False , "message" : "Invalid JSON options" })
518532
519- for key in request .json :
520- if key in RESTAPI_UNSUPPORTED_OPTIONS :
521- logger .warning ("[%s] Unsupported option '%s' provided to scan_start()" % (taskid , key ))
522- return jsonize ({"success" : False , "message" : "Unsupported option '%s'" % key })
533+ if DataStore .tasks [taskid ].engine_process () is not None and not DataStore .tasks [taskid ].engine_has_terminated ():
534+ logger .warning ("[%s] Scan already running" % taskid )
535+ return jsonize ({"success" : False , "message" : "Scan already running" })
536+
537+ message = validate_task_options (taskid , request .json , "scan_start" )
538+ if message :
539+ return jsonize ({"success" : False , "message" : message })
523540
524541 # Initialize sqlmap engine's options with user's provided options, if any
525542 for option , value in request .json .items ():
@@ -601,7 +618,7 @@ def scan_data(taskid):
601618 json_data_message .append ({"status" : status , "type" : content_type , "value" : dejsonize (value )})
602619
603620 # Read all error messages from the IPC database
604- for error in DataStore .current_db .execute ("SELECT error FROM errors WHERE taskid = ? ORDER BY id ASC" , (taskid ,)):
621+ for error , in DataStore .current_db .execute ("SELECT error FROM errors WHERE taskid = ? ORDER BY id ASC" , (taskid ,)):
605622 json_errors_message .append (error )
606623
607624 logger .debug ("(%s) Retrieved scan data and error messages" % taskid )
0 commit comments