fix: [internal] Fixes #7961

pull/7993/head
Jakub Onderka 2021-11-25 10:10:04 +01:00
parent b7fb8de55b
commit d20a68dc38
3 changed files with 29 additions and 28 deletions

View File

@ -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') {

View File

@ -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];

View File

@ -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) {