diff --git a/app/Controller/WorkflowsController.php b/app/Controller/WorkflowsController.php index b935bfee4..d432357b0 100644 --- a/app/Controller/WorkflowsController.php +++ b/app/Controller/WorkflowsController.php @@ -172,6 +172,12 @@ class WorkflowsController extends AppController { if ($this->request->is('post') || $this->request->is('put')) { $blockingErrors = []; + if (!JsonTool::isValid($this->request->data['Workflow']['data'])) { + return $this->RestResponse->viewData([ + 'success' => false, + 'outcome' => __('Invalid JSON'), + ], $this->response->type()); + } $data = JsonTool::decode($this->request->data['Workflow']['data']); $result = $this->Workflow->executeWorkflow($workflow_id, $data, $blockingErrors); if (!empty($logging) && empty($result['success'])) { diff --git a/app/Model/Workflow.php b/app/Model/Workflow.php index b75e52661..042550d56 100644 --- a/app/Model/Workflow.php +++ b/app/Model/Workflow.php @@ -522,7 +522,25 @@ class Workflow extends AppModel $workflow = $this->__incrementWorkflowExecutionCount($workflow); $walkResult = []; $debugData = ['original' => $data]; - $data = $this->__normalizeDataForTrigger($triggerModule, $data); + $conversionFailure = false; + try { + $data = $this->__normalizeDataForTrigger($triggerModule, $data); + } catch (Exception $e) { + $conversionFailure = true; + $message = __('Error while normalizing data for trigger. Error:' . PHP_EOL . $e->getMessage()); + } + if ($data === false) { + $conversionFailure = true; + $message = __('Error while normalizing data for trigger. Invalid input.'); + } + if ($conversionFailure) { + $this->logExecutionIfDebug($workflow, $message); + return [ + 'outcomeText' => 'failure' . sprintf(' %s', $message), + 'walkResult' => [], + 'success' => false, + ]; + } $debugData['normalized'] = $data; $for_path = !empty($triggerModule->blocking) ? GraphWalker::PATH_TYPE_BLOCKING : GraphWalker::PATH_TYPE_NON_BLOCKING; $this->sendRequestToDebugEndpointIfDebug($workflow, [], '/init?type=' . $for_path, $debugData); @@ -710,10 +728,10 @@ class Workflow extends AppModel return $success; } - private function __normalizeDataForTrigger($triggerClass, array $data): array + private function __normalizeDataForTrigger($triggerClass, array $data) { if (method_exists($triggerClass, 'normalizeData')) { - return $triggerClass->normalizeData($data); + $data = $triggerClass->normalizeData($data); } return $data; } diff --git a/app/Model/WorkflowModules/trigger/Module_post_after_save.php b/app/Model/WorkflowModules/trigger/Module_post_after_save.php index ea00835ce..9b905ca29 100644 --- a/app/Model/WorkflowModules/trigger/Module_post_after_save.php +++ b/app/Model/WorkflowModules/trigger/Module_post_after_save.php @@ -23,6 +23,11 @@ class Module_post_after_save extends WorkflowBaseTriggerModule public function normalizeData(array $data) { parent::normalizeData($data); + + if (empty($data['Post'])) { + return false; + } + $this->Thread = ClassRegistry::init('Thread'); $thread = $this->Thread->find('first', [ 'recursive' => -1, diff --git a/app/Model/WorkflowModules/trigger/Module_sighting_after_save.php b/app/Model/WorkflowModules/trigger/Module_sighting_after_save.php index aa09d7b83..96028632c 100644 --- a/app/Model/WorkflowModules/trigger/Module_sighting_after_save.php +++ b/app/Model/WorkflowModules/trigger/Module_sighting_after_save.php @@ -26,6 +26,10 @@ class Module_sighting_after_save extends WorkflowBaseTriggerModule $this->Event = ClassRegistry::init('Event'); $this->Attribute = ClassRegistry::init('Attribute'); + if (empty($data['Sighting'])) { + return false; + } + // We are missing data such as tags or objects. $event = $this->Event->quickFetchEvent($data['Sighting']['Event']['id']); $attribute = $this->Attribute->fetchAttribute($data['Sighting']['Attribute']['id']);