From e8d3d76fd9e6caa55ce2d1e9dc5b61fb2edc8a4a Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Wed, 3 Apr 2024 12:18:33 +0200 Subject: [PATCH 1/4] chg: [internal] Log exception when importing stix file --- app/Model/Event.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Model/Event.php b/app/Model/Event.php index 20b0fe886..c2e1c7ead 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -6072,7 +6072,7 @@ class Event extends AppModel /** * @param string $stixVersion - * @param string $file + * @param string $file Path to STIX file * @param int $distribution * @param int|null $sharingGroupId * @param bool $galaxiesAsTags @@ -6130,6 +6130,7 @@ class Event extends AppModel try { $stdout = ProcessTool::execute($shellCommand, null, true); } catch (ProcessException $e) { + $this->logException("Could not import $stixVersion file $file", $e); $stdout = $e->stdout(); } From 16c9c18b8f19a2639f2e4ffff97c342b96c18853 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Wed, 3 Apr 2024 12:34:30 +0200 Subject: [PATCH 2/4] fix: [internal] Try to fix STIX import --- app/Controller/EventsController.php | 22 +++++++++++----------- app/View/Events/upload_stix.ctp | 6 ++---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 61ae6b162..bb992e214 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -2414,7 +2414,7 @@ class EventsController extends AppController if (isset($this->params['named']['distribution'])) { $distribution = intval($this->params['named']['distribution']); if (!array_key_exists($distribution, $distributionLevels)) { - throw new MethodNotAllowedException(__('Wrong distribution level')); + throw new BadRequestException(__('Wrong distribution level')); } } else { $distribution = $initialDistribution; @@ -2422,11 +2422,11 @@ class EventsController extends AppController $sharingGroupId = null; if ($distribution == 4) { if (!isset($this->params['named']['sharing_group_id'])) { - throw new MethodNotAllowedException(__('The sharing group id is needed when the distribution is set to 4 ("Sharing group").')); + throw new BadRequestException(__('The sharing group id is needed when the distribution is set to 4 ("Sharing group").')); } $sharingGroupId = intval($this->params['named']['sharing_group_id']); if (!array_key_exists($sharingGroupId, $sgs)) { - throw new MethodNotAllowedException(__('Please select a valid sharing group id.')); + throw new BadRequestException(__('Please select a valid sharing group id.')); } } $clusterDistribution = $initialDistribution; @@ -2436,15 +2436,15 @@ class EventsController extends AppController if (isset($this->params['name']['cluster_distribution'])) { $clusterDistribution = intval($this->params['named']['cluster_distribution']); if (!array_key_exists($clusterDistribution, $distributionLevels)) { - throw new MethodNotAllowedException(__('Wrong cluster distribution level')); + throw new BadRequestException(__('Wrong cluster distribution level')); } if ($clusterDistribution == 4) { if (!isset($this->params['named']['cluster_sharing_group_id'])) { - throw new MethodNotAllowedException(__('The cluster sharing group id is needed when the cluster distribution is set to 4 ("Sharing group").')); + throw new BadRequestException(__('The cluster sharing group id is needed when the cluster distribution is set to 4 ("Sharing group").')); } $clusterSharingGroupId = intval($this->params['named']['cluster_sharing_group_id']); if (!array_key_exists($clusterSharingGroupId, $sgs)) { - throw new MethodNotAllowedException(__('Please select a valid cluster sharing group id.')); + throw new BadRequestException(__('Please select a valid cluster sharing group id.')); } } } @@ -2476,8 +2476,8 @@ class EventsController extends AppController } else { return $this->RestResponse->saveFailResponse('Events', 'upload_stix', false, $result, $this->response->type()); } - } else { - $original_file = !empty($this->data['Event']['original_file']) ? $this->data['Event']['stix']['name'] : ''; + } else { // not REST request + $originalFile = !empty($this->data['Event']['original_file']) ? $this->data['Event']['stix']['name'] : ''; if (isset($this->data['Event']['stix']) && $this->data['Event']['stix']['size'] > 0 && is_uploaded_file($this->data['Event']['stix']['tmp_name'])) { $filePath = FileAccessTool::createTempFile(); if (!move_uploaded_file($this->data['Event']['stix']['tmp_name'], $filePath)) { @@ -2490,12 +2490,12 @@ class EventsController extends AppController $this->Auth->user(), $filePath, $stix_version, - $original_file, + $originalFile, $this->data['Event']['publish'], $this->data['Event']['distribution'], $this->data['Event']['sharing_group_id'] ?? null, - $this->data['Event']['galaxies_handling'], - $this->data['Event']['cluster_distribution'], + $this->data['Event']['galaxies_handling'] ?? false, + $this->data['Event']['cluster_distribution'] ?? 0, $this->data['Event']['cluster_sharing_group_id'] ?? null, $debug ); diff --git a/app/View/Events/upload_stix.ctp b/app/View/Events/upload_stix.ctp index 7e6bf1b53..606575a3d 100644 --- a/app/View/Events/upload_stix.ctp +++ b/app/View/Events/upload_stix.ctp @@ -133,8 +133,7 @@ $(function(){ checkSharingGroup('Event'); }); checkSharingGroup('Event'); -}); -$(function(){ + $('#EventGalaxiesHandling').change(function() { if ($(this).val() == 0) { $('#ClusterDistribution').show(); @@ -146,8 +145,7 @@ $(function(){ $('#ClusterSGContainer').hide(); } }).change(); -}); -$(function(){ + $('#EventClusterDistribution').change(function() { if ($(this).val() == 4 && $('#EventGalaxiesHandling').val() == 0) { $('#ClusterSGContainer').show(); From 2f72afd59f7a0f92a44780800c5d5369678923bc Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Wed, 3 Apr 2024 13:42:23 +0200 Subject: [PATCH 3/4] fix: [sync] Avoid problem with duplicate sightings UUID --- app/Model/Sighting.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/Model/Sighting.php b/app/Model/Sighting.php index f6aa187d5..80cdd50e6 100644 --- a/app/Model/Sighting.php +++ b/app/Model/Sighting.php @@ -1241,6 +1241,8 @@ class Sighting extends AppModel if (!isset($attributes[$s['attribute_uuid']])) { continue; // attribute doesn't exists or user don't have permission to access it } + $existingSighting[$s['uuid']] = true; // just to be sure that there are no sigthings with duplicated UUID + list($attributeId, $eventId) = $attributes[$s['attribute_uuid']]; if ($s['type'] === '2') { @@ -1256,11 +1258,8 @@ class Sighting extends AppModel if ($user['Role']['perm_sync']) { if (isset($s['org_id'])) { if ($s['org_id'] != 0 && !empty($s['Organisation'])) { - if (isset($existingOrganisations[$s['Organisation']['uuid']])) { - $saveOnBehalfOf = $existingOrganisations[$s['Organisation']['uuid']]; - } else { - $saveOnBehalfOf = $this->Organisation->captureOrg($s['Organisation'], $user); - } + $saveOnBehalfOf = $existingOrganisations[$s['Organisation']['uuid']] ?? + $this->Organisation->captureOrg($s['Organisation'], $user); } else { $saveOnBehalfOf = 0; } @@ -1282,8 +1281,8 @@ class Sighting extends AppModel } if ($this->saveMany($toSave)) { - $existingUuids = array_column($toSave, 'uuid'); - $this->Event->publishSightingsRouter($event['Event']['id'], $user, $passAlong, $existingUuids); + $sightingsUuidsToPush = array_column($toSave, 'uuid'); + $this->Event->publishSightingsRouter($event['Event']['id'], $user, $passAlong, $sightingsUuidsToPush); return count($toSave); } From 6f9767df56e857823d24aedb3338065f67e27587 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Wed, 3 Apr 2024 16:17:12 +0200 Subject: [PATCH 4/4] chg: [internal] Update misp-stix --- app/files/scripts/misp-stix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/files/scripts/misp-stix b/app/files/scripts/misp-stix index f531a2cdc..6f344fe8e 160000 --- a/app/files/scripts/misp-stix +++ b/app/files/scripts/misp-stix @@ -1 +1 @@ -Subproject commit f531a2cdce0e1ff2bcc879d57d2872f6318fb5cf +Subproject commit 6f344fe8e813c2d512d725f07699003ec7548430