From d20a68dc38bc9fa27e2eefa9de571dd2c3f1a9c0 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Thu, 25 Nov 2021 10:10:04 +0100 Subject: [PATCH] fix: [internal] Fixes #7961 --- app/Model/Attribute.php | 42 ++++++++++++++++++++++++---------------- app/Model/Event.php | 4 ++-- app/Model/MispObject.php | 11 ++--------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index b439a1af4..42009f8b4 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -9,6 +9,7 @@ App::uses('AttachmentTool', 'Tools'); App::uses('TmpFileTool', 'Tools'); App::uses('ComplexTypeTool', 'Tools'); App::uses('AttributeValidationTool', 'Tools'); +App::uses('JsonTool', 'Tools'); /** * @property Event $Event @@ -1720,7 +1721,6 @@ class Attribute extends AppModel } } - public function checkTemplateAttributes($template, $data, $event_id) { $result = array(); @@ -2874,10 +2874,7 @@ class Attribute extends AppModel if (!$this->Warninglist->filterWarninglistAttribute($attribute)) { $this->validationErrors['warninglist'] = 'Attribute could not be saved as it trips over a warninglist and enforceWarninglist is enforced.'; $validationErrors = $this->validationErrors['warninglist']; - $this->loadLog()->createLogEntry($user, 'add', 'Attribute', 0, - 'Attribute dropped due to validation for Event ' . $eventId . ' failed', - 'Validation errors: ' . json_encode($this->validationErrors) . ' Full Attribute: ' . json_encode($attribute) - ); + $this->logDropped($user, $attribute); return $attribute; } } @@ -2901,12 +2898,8 @@ class Attribute extends AppModel unset($attribute['sharing_group_id']); } } - if (!$this->save($attribute, $params)) { - $attribute_short = (isset($attribute['category']) ? $attribute['category'] : 'N/A') . '/' . (isset($attribute['type']) ? $attribute['type'] : 'N/A') . ' ' . (isset($attribute['value']) ? $attribute['value'] : 'N/A'); - $this->loadLog()->createLogEntry($user, 'add', 'Attribute', 0, - 'Attribute dropped due to validation for Event ' . $eventId . ' failed: ' . $attribute_short, - 'Validation errors: ' . json_encode($this->validationErrors) . ' Full Attribute: ' . json_encode($attribute) - ); + if (!$this->save(['Attribute' => $attribute], $params)) { + $this->logDropped($user, $attribute); } else { if (!empty($attribute['AttributeTag'])) { $toSave = []; @@ -3023,12 +3016,8 @@ class Attribute extends AppModel $fieldList[] = 'object_id'; $fieldList[] = 'object_relation'; } - if (!$this->save($attribute, ['fieldList' => $fieldList, 'parentEvent' => $event])) { - $attribute_short = (isset($attribute['category']) ? $attribute['category'] : 'N/A') . '/' . (isset($attribute['type']) ? $attribute['type'] : 'N/A') . ' ' . (isset($attribute['value']) ? $attribute['value'] : 'N/A'); - $this->loadLog()->createLogEntry($user, 'edit', 'Attribute', 0, - 'Attribute dropped due to validation for Event ' . $eventId . ' failed: ' . $attribute_short, - 'Validation errors: ' . json_encode($this->validationErrors) . ' Full Attribute: ' . json_encode($attribute) - ); + if (!$this->save(['Attribute' => $attribute], ['fieldList' => $fieldList, 'parentEvent' => $event])) { + $this->logDropped($user, $attribute, 'edit'); return $this->validationErrors; } if (!empty($attribute['Sighting'])) { @@ -3458,6 +3447,25 @@ class Attribute extends AppModel return $distribution; } + /** + * Log when attribute was dropped due to validation errors. + * + * @param array $user + * @param array $attribute + * @param string $action + * @throws JsonException + */ + public function logDropped(array $user, array $attribute, $action = 'add') + { + $attribute_short = (isset($attribute['category']) ? $attribute['category'] : 'N/A') . '/' . (isset($attribute['type']) ? $attribute['type'] : 'N/A') . ' ' . (isset($attribute['value']) ? $attribute['value'] : 'N/A'); + $eventId = $attribute['event_id']; + $modelId = $action === 'add' ? 0 : $this->id; + $this->loadLog()->createLogEntry($user, 'add', 'Attribute', $modelId, + "Attribute dropped due to validation for Event $eventId failed: $attribute_short", + 'Validation errors: ' . JsonTool::encode($this->validationErrors) . ' Full Attribute: ' . JsonTool::encode($attribute) + ); + } + public function __isset($name) { if ($name === 'typeDefinitions' || $name === 'categoryDefinitions') { diff --git a/app/Model/Event.php b/app/Model/Event.php index 79956b8c3..77c341a4e 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -3963,7 +3963,7 @@ class Event extends AppModel $server['Server']['internal'] = false; } // If the event exists... - if (count($existingEvent)) { + if (!empty($existingEvent)) { $data['Event']['id'] = $existingEvent['Event']['id']; $id = $existingEvent['Event']['id']; // Conditions affecting all: @@ -4008,7 +4008,7 @@ class Event extends AppModel $changed = false; // If a field is not set in the request, just reuse the old value // Also, compare the event to the existing event and see whether this is a meaningful change - $recoverFields = array('analysis', 'threat_level_id', 'info', 'distribution', 'date'); + $recoverFields = array('analysis', 'threat_level_id', 'info', 'distribution', 'date', 'org_id'); foreach ($recoverFields as $rF) { if (!isset($data['Event'][$rF])) { $data['Event'][$rF] = $existingEvent['Event'][$rF]; diff --git a/app/Model/MispObject.php b/app/Model/MispObject.php index 80863c123..4c35972ed 100644 --- a/app/Model/MispObject.php +++ b/app/Model/MispObject.php @@ -888,10 +888,7 @@ class MispObject extends AppModel if ($result) { $this->Event->Attribute->AttributeTag->handleAttributeTags($user, $newAttribute, $newAttribute['event_id'], $capture=true); } else { - $this->loadLog()->createLogEntry($user, 'edit', 'Attribute', $newAttribute['id'], - 'Attribute dropped due to validation for Event ' . $object['Object']['event_id'] . ' failed', - 'Validation errors: ' . json_encode($this->Event->Attribute->validationErrors) . ' Full Attribute: ' . json_encode($newAttribute) - ); + $this->Event->Attribute->logDropped($user, $newAttribute, 'edit'); } } unset($object['Attribute'][$origKey]); @@ -923,12 +920,8 @@ class MispObject extends AppModel $newAttribute['id'] = $this->Event->Attribute->id; $this->Event->Attribute->AttributeTag->handleAttributeTags($user, $newAttribute, $newAttribute['event_id'], $capture=true); } else { - $this->loadLog()->createLogEntry($user, 'add', 'Attribute', 0, - 'Attribute dropped due to validation for Event ' . $object['Object']['event_id'] . ' failed', - 'Validation errors: ' . json_encode($this->Event->Attribute->validationErrors) . ' Full Attribute: ' . json_encode($newAttribute) - ); + $this->Event->Attribute->logDropped($user, $newAttribute, 'add'); } - $attributeArrays['add'][] = $newAttribute; unset($objectToSave['Attribute'][$newKey]); } foreach ($object['Attribute'] as $originalAttribute) {