fix: [API] Adding attributes via the freetext importer using the API resulted in several issues

- adhereToWarninglists was not correctly adhered to
- the response didn't reflect what was saved, only what was pushed to be saved (excluding removals by warnintlists, several attributes added by adding more than one valid type, etc)

fixes #4881
pull/4939/head
iglocska 2019-07-29 16:15:00 +02:00
parent d75c6c9e3b
commit ebf054e0d0
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 16 additions and 10 deletions

View File

@ -4010,7 +4010,8 @@ class EventsController extends AppController
$attributes[$k] = $attribute;
}
// actually save the attribute now
$this->Event->processFreeTextDataRouter($this->Auth->user(), $attributes, $id, '', false, $adhereToWarninglists);
$results = array();
$attributes = $this->Event->processFreeTextDataRouter($this->Auth->user(), $attributes, $id, '', false, $adhereToWarninglists, true);
// FIXME $attributes does not contain the onteflyattributes
$attributes = array_values($attributes);
return $this->RestResponse->viewData($attributes, $this->response->type());

View File

@ -5998,13 +5998,14 @@ class Event extends AppModel
return false;
}
public function processFreeTextData($user, $attributes, $id, $default_comment = '', $force = false, $adhereToWarninglists = false, $jobId = false)
public function processFreeTextData($user, $attributes, $id, $default_comment = '', $force = false, $adhereToWarninglists = false, $jobId = false, $returnRawResults = false)
{
$event = $this->find('first', array(
'conditions' => array('id' => $id),
'recursive' => -1,
'fields' => array('orgc_id', 'id', 'distribution', 'published', 'uuid'),
));
$results = array();
if (!$user['Role']['perm_site_admin'] && !empty($event) && $event['Event']['orgc_id'] != $user['org_id']) {
$objectType = 'ShadowAttribute';
} elseif ($user['Role']['perm_site_admin'] && isset($force) && $force) {
@ -6072,7 +6073,7 @@ class Event extends AppModel
// adhere to the warninglist
if ($adhereToWarninglists) {
if (!$this->Warninglist->filterWarninglistAttributes($warninglists, $attribute)) {
if ($adhereToWarninglists == 'soft') {
if ($adhereToWarninglists === 'soft') {
$attribute['to_ids'] = 0;
} else {
// just ignore the attribute
@ -6080,15 +6081,16 @@ class Event extends AppModel
}
}
}
$AttributSave = $this->$objectType->save($attribute);
if ($AttributSave) {
$saved_attribute = $this->$objectType->save($attribute);
if ($saved_attribute) {
$results[] = $saved_attribute;
// If Tags, attach each tags to attribute
if (!empty($attribute['tags'])) {
foreach (explode(",", $attribute['tags']) as $tagName) {
$this->Tag = ClassRegistry::init('Tag');
$TagId = $this->Tag->captureTag(array('name' => $tagName), array('Role' => $user['Role']));
$this->AttributeTag = ClassRegistry::init('AttributeTag');
if (!$this->AttributeTag->attachTagToAttribute($AttributSave['Attribute']['id'], $id, $TagId)) {
if (!$this->AttributeTag->attachTagToAttribute($saved_attribute['Attribute']['id'], $id, $TagId)) {
throw new MethodNotAllowedException(__('Could not add tags.'));
}
}
@ -6127,7 +6129,7 @@ class Event extends AppModel
}
}
}
$messageScopeSaved = $this-> __apply_inflector($saved, $messageScope);
$messageScopeSaved = $this->__apply_inflector($saved, $messageScope);
if ($failed > 0) {
if ($failed == 1) {
$messageScopeFailed = Inflector::singularize($messageScope);
@ -6144,6 +6146,9 @@ class Event extends AppModel
$this->Job->saveField('progress', 100);
}
}
if (!empty($returnRawResults)) {
return $results;
}
return $message;
}
@ -6493,7 +6498,7 @@ class Event extends AppModel
return $attribute_save;
}
public function processFreeTextDataRouter($user, $attributes, $id, $default_comment = '', $force = false, $adhereToWarninglists = false)
public function processFreeTextDataRouter($user, $attributes, $id, $default_comment = '', $force = false, $adhereToWarninglists = false, $returnRawResults = false)
{
if (Configure::read('MISP.background_jobs')) {
list($job, $randomFileName, $tempFile) = $this->__initiateProcessJob($user, $id);
@ -6509,7 +6514,7 @@ class Event extends AppModel
$writeResult = $tempFile->write(json_encode($tempData));
if (!$writeResult) {
return ($this->processFreeTextData($user, $attributes, $id, $default_comment = '', $force = false, $adhereToWarninglists = false));
return ($this->processFreeTextData($user, $attributes, $id, $default_comment, $force, $adhereToWarninglists, false, $returnRawResults));
}
$tempFile->close();
$process_id = CakeResque::enqueue(
@ -6521,7 +6526,7 @@ class Event extends AppModel
$job->saveField('process_id', $process_id);
return 'Freetext ingestion queued for background processing. Attributes will be added to the event as they are being processed.';
} else {
return $this->processFreeTextData($user, $resolved_data, $id, $default_comment);
return $this->processFreeTextData($user, $attributes, $id, $default_comment, $force, $adhereToWarninglists, false, $returnRawResults);
}
}