Skip to content

[medium] Fix --dump in grouping/correlator.py - #1

Open
elhoim wants to merge 1 commit into
MISP:masterfrom
elhoim:fix/correlator-dump
Open

elhoim wants to merge 1 commit into
MISP:masterfrom
elhoim:fix/correlator-dump

Conversation

@elhoim

@elhoim elhoim commented Sep 17, 2026

Copy link
Copy Markdown

BLUF

  • Priority: medium
  • ./correlator.py --dump has never worked — it crashes on the first hash.
  • Two bugs on the same never-executed path: a misplaced .format(), and a list + set concatenation.
  • Fix is two lines in grouping/correlator.py; no other behaviour, dependency or interface changes.
  • Verified with a stub harness that runs the real file with redis/pydeep/Crypto faked: AttributeError before, a correct CSV row after.

What was wrong

In the --dump branch the .format(h) call was attached to the Redis result instead of the key string:

eids = r.smembers('{}:eids').format(h)

So Redis was queried for the literal key '{}:eids', which is never written — line 86 correctly writes r.sadd('{}:eids'.format(h), *eids) — and .format was then called on the returned set.

The next line then concatenated a list with a set:

writer.writerow([h] + eids)

Concrete impact

  • ./correlator.py --dump aborts on the very first hash with AttributeError: 'set' object has no attribute 'format'. redis-py returns a set even for a missing key, so it fails regardless of the database contents.
  • Once that is fixed, the following line fails with TypeError: can only concatenate list (not "set") to list.
  • Both defects sit on the same code path, so the dump feature has evidently never run. There is no test suite or CI in this repository to have caught it.

What this changes

grouping/correlator.py, two lines:

-        eids = r.smembers('{}:eids').format(h)
+        eids = r.smembers('{}:eids'.format(h))
         if eids:
-            writer.writerow([h] + eids)
+            writer.writerow([h] + sorted(eids))

The first change restores the same key-building idiom already used at line 86. The second turns the set into a list so writerow receives a single sequence; sorted rather than list also makes the CSV output deterministic between runs.

Nothing else is touched — no refactoring, no reformatting, no dependency changes.

How it was verified

A throwaway harness (see the check below) stubs out redis, pydeep, Crypto.Hash and SafeConfigParser, seeds a fake Redis with hashes_md5={'aaa'}, hashes_sha1={'bbb'}, aaa:eids={'42','7'}, bbb:eids=set(), and runs grouping/correlator.py --dump:

  • Before: AttributeError: 'set' object has no attribute 'format'
  • After: aaa,42,7 — bbb correctly suppressed by the existing if eids: guard.

The repository-wide syntax gate (bash -n hashes-extract.sh plus an ast.parse sweep over all tracked *.py) is unchanged by this patch: the same four pre-existing Python 2 files fail before and after, and grouping/correlator.py is not among them.


Local verification

cd /home/elhoim/tmp/misp-review/mwork/data-processing-0 && python3 - <<'PYEOF'
import sys, types, configparser, io, contextlib

class _CP(configparser.ConfigParser):
    def read(self, *a, **k):
        self.read_string('[redis]\nhost=localhost\nport=6379\n')
configparser.SafeConfigParser = _CP
sys.modules['pydeep'] = types.ModuleType('pydeep')
crypto = types.ModuleType('Crypto'); chash = types.ModuleType('Crypto.Hash')
chash.SHA256 = types.SimpleNamespace(new=lambda b: types.SimpleNamespace(hexdigest=lambda: 'x'))
crypto.Hash = chash
sys.modules['Crypto'] = crypto; sys.modules['Crypto.Hash'] = chash
DATA = {'hashes_md5': {'aaa'}, 'hashes_sha1': {'bbb'}, 'hashes_sha256': set(),
        'aaa:eids': {'42', '7'}, 'bbb:eids': set()}
class FakeRedis:
    def __init__(self, **kw): pass
    def smembers(self, k): return set(DATA.get(k, set()))
rmod = types.ModuleType('redis'); rmod.StrictRedis = FakeRedis
sys.modules['redis'] = rmod
sys.argv = ['correlator.py', '--dump']

src = open('grouping/correlator.py').read()
buf = io.StringIO()
try:
    with contextlib.redirect_stdout(buf):
        exec(compile(src, 'correlator.py', 'exec'), {'__name__': '__main__'})
except Exception as e:
    print('FAIL: --dump raised {}: {}'.format(type(e).__name__, e))
    sys.exit(1)
out = buf.getvalue()
print('dump output: {!r}'.format(out))
if out != 'aaa,42,7\r\n':
    print('FAIL: unexpected dump output')
    sys.exit(1)
print('PASS: --dump emitted the expected CSV row')
PYEOF

Output:

dump output: 'aaa,42,7\r\n'
PASS: --dump emitted the expected CSV row
(exit status 0)
  • Verified on a scratch work tree with the patch applied.
  • The same check was re-run with the change stashed and fails on unpatched code, so it discriminates rather than merely passing.

The --dump branch applied .format(h) to the Redis result instead of the
key, so it queried the literal key '{}:eids' and then called .format on
the returned set, raising AttributeError on the first hash. Build the
key the same way line 86 does when writing it.

The following line also concatenated a list with the set returned by
smembers, raising TypeError. Sort the event IDs into a list so
writerow receives one sequence and the CSV output is deterministic.

Both defects were on the same path, so --dump had never worked.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant