Bug: freetext/CSV feed fetch silently drops chunks of 100 attributes when the feed repeats a value
Actual behavior
When a freetext or CSV feed is fetched into a fixed event, attributes are silently lost in blocks of up to 100 whenever the parsed feed contains the same value twice (as the database sees it). fetchFeed reports success, nothing is logged, and the event timestamp is still bumped, so the event looks up to date.
Cause, in Feed::saveFreetextFeedData() (2.5 at 0c9f682):
- The event diff unsets a value from
$existsAttributesValueToIdon its first occurrence in$data. A second occurrence of a value that already exists in the event is no longer found there and stays in$dataas "new". - The later de-duplication (
$uniqueValues) compares raw values only, so two spellings that MISP stores as the same value both stay in$data:Example.com/example.com(domain is lower-cased bymodifyBeforeValidation()),AS123/123, or two URLs differing only in case (attributes.value1uses a case-insensitive collation). - That row then fails the
valueIsUniquevalidation, and becausesaveMany()is atomic and its result is ignored, the whole chunk of 100 is rolled back without any error.
ComplexTypeTool::checkCSV() does not de-duplicate (unlike checkFreeText()), so CSV feeds are hit hardest. The repeated value stays in the source, so the same chunks are lost again on every fetch.
Observed on one instance with public feeds:
- A CSV feed of brute-forcing IPs with the columns
ip,asn,cidr,AS<asn>,cc(CSV value column left empty, so every column is imported). The ASN and CIDR repeat across rows, so every chunk contained a failing row. The feed imported nothing for 7–10 days whilefetchFeedreported success, and 1,062 of 2,461 IPs in the current source were missing from the event. - abuse.ch ThreatFox recent CSV (IOC values repeated within the export): 162 of 164 new rows were dropped in one hourly fetch.
- Lists containing both
Example.ccandexample.cc: 100 rows missing. - A CSV listing the same onion URL twice (two operators): 18 rows missing.
Two related issues, which the fix below does not change:
- The same pattern applies to a failed row of any other kind: one invalid value can take up to 99 valid ones with it. Saving a failed chunk again row by row would contain the loss to the invalid row.
- #10931 fixed the unchecked
saveMany()result on thedelta_mergedelete path in this same function, but not on the insert path.
Expected behavior
Every value in the feed that is not already in the event gets saved. A repeated value is ignored, and a row that fails validation does not take other rows with it, or at least is logged.
Steps to reproduce
Worked out from the code path above; the effect was observed on the production feeds listed.
- Create a feed with source format CSV (or freetext), "Target event: fixed event", no delta merge, and a local or remote source containing:
1.2.3.4 1.2.3.4 5.6.7.8 - Fetch it. Event contains
1.2.3.4and5.6.7.8, as expected. - Add a line
9.9.9.9to the source and fetch again. 9.9.9.9is never added, on this or any later fetch, and the fetch reports success. The second1.2.3.4survives the diff, fails uniqueness validation, and rolls back the chunk holding9.9.9.9.
The same happens on the very first fetch with Example.com and example.com in one list: every other new row in that chunk is lost.
Suggested fix
The patch below is running on the instance above. After re-fetching the affected feeds, 0 values from each source are missing from their events (previously 15 to 198 per feed). It de-duplicates $data before the diff, keyed the way the uniqueness validation compares (type, category, normalised value, case-insensitive), and logs a chunk that still fails:
@@ -1458,6 +1458,17 @@
$this->save($feed);
}
}
+ $uniqueKeys = [];
+ foreach ($data as $k => $dataPoint) {
+ $uniqueKey = $dataPoint['type'] . '|' . ($dataPoint['category'] ?? '') . '|' .
+ mb_strtolower(AttributeValidationTool::modifyBeforeValidation($dataPoint['type'], $dataPoint['value']));
+ if (isset($uniqueKeys[$uniqueKey])) {
+ unset($data[$k]);
+ continue;
+ }
+ $uniqueKeys[$uniqueKey] = true;
+ }
+ unset($uniqueKeys);
if ($feed['Feed']['fixed_event']) {
$existsAttributesValueToId = $this->Event->Attribute->find('list', array(
'conditions' => array(
@@ -1520,7 +1531,13 @@
}
$chunks = array_chunk($data, 100);
foreach ($chunks as $k => $chunk) {
- $this->Event->Attribute->saveMany($chunk, ['validate' => true, 'parentEvent' => $event]);
+ if (!$this->Event->Attribute->saveMany($chunk, ['validate' => true, 'parentEvent' => $event])) {
+ $this->log(sprintf(
+ 'Feed %s: %d attributes not saved to event %s, a row in their chunk failed validation: %s',
+ $feed['Feed']['id'], count($chunk), $event['Event']['id'],
+ substr(json_encode($this->Event->Attribute->validationErrors), 0, 500)
+ ), LOG_WARNING);
+ }
$this->jobProgress($jobId, null, 50 + round(($k * 100 + 1) / count($data) * 50));
}
if (!empty($data) || !empty($attributesToDelete)) {Accent-insensitive collisions under the *_unicode_ci collation (e.g. ç vs c) are not covered by mb_strtolower(). The row-by-row fallback mentioned above would cover those too.
Version
2.5.46 (misp-docker image misp-core:v2.5.46); the same code is in 2.5 and develop at the time of writing.
Operating System
Debian (misp-docker misp-core container)
Operating System version
13 (trixie)
PHP version
8.4
Relevant log output
None. That is part of the problem: fetchFeed prints Job done. and exits 0, and nothing is written to error.log or the audit log.
Source: MISP/MISP