Repository navigation
fix(asn-feed): exclude IOCs from inactive honeypots via Exists subquery. Closes #1794 - #1802
Rewaskc02-lang wants to merge 3 commits into
Conversation
Before this fix, BaseFeedView.get_queryset() returned early when
is_aggregated=True (set by AsnFeedView), which meant the
honeypots__active=True filter was never applied. As a result, IOCs
observed only on decommissioned honeypots were included in the ASN
feed and inflated every numeric aggregate (ioc_count, total_attack_count,
total_interaction_count, etc.).
A JOIN-based fix (.filter(honeypots__active=True)) would have been
incorrect for aggregated views: the INNER JOIN on the M2M through-table
emits one row per (IOC, honeypot) pair, so every Sum() and Count() would
be multiplied by the number of honeypots per IOC.
The fix uses an Exists() correlated subquery on the M2M through-table
instead:
active_honeypot_qs = IOC.honeypots.through.objects.filter(
ioc_id=OuterRef('pk'),
honeypot__active=True,
)
iocs.filter(Exists(active_honeypot_qs))
This is a semi-join: PostgreSQL emits at most one row per IOC regardless
of how many active honeypots are matched, so downstream Sum() / Count()
aggregations remain accurate.
The non-aggregated path (SimpleFeedView, AdvancedFeedView) already
applied honeypots__active=True correctly via the subsequent filter; only
AsnFeedView (is_aggregated=True) was affected.
Fixes: GreedyBear-Project#1794
|
Hey @Rewaskc02-lang ! You already got 3 PRs merged. But this time your PR first targeted the wrong branch and now it is missing the checklist / PR template. Do you use a LLM to open the PRs for you? If so, please don't do that. |
|
Hi — apologies for the confusion, and thank you for flagging this. Let me clarify both issues: 1. Wrong target branch — The initial PR was opened against 2. Missing PR template / checklist — Again, this was the same friend who opened the PR quickly without filling in the template. I take full responsibility for not double-checking before it went up. 3. No LLM was used to open the PR. Here's why I can say that with confidence:
I'm updating the PR description now with the full checklist filled in. Sorry again for the confusion — the code and intent are entirely genuine. 🙏 |
|
Hey @Rewaskc02-lang ! I will close this PR and block you. Here is why:
|
Summary
Fixes #1794.
Related issues
honeypots__activecheck.Type of change
Root cause
BaseFeedView.get_queryset()returned early whenis_aggregated=True(set byAsnFeedView), so thehoneypots__active=Truefilter that appears after the early return was never applied. IOCs observed only on decommissioned honeypots therefore leaked into every ASN aggregate row.Why not
.filter(honeypots__active=True)directly?A JOIN-based filter on the M2M relation is wrong for aggregated views: the INNER JOIN on
IOC_honeypotsemits one row per (IOC, honeypot) pair. With two active honeypots per IOC, everySum()andCount()would be doubled. This is exactly the duplication bug @regulartim flagged.Fix
Use
Exists()withOuterRef()on the M2M through-table:PostgreSQL translates this into a correlated semi-join (
WHERE EXISTS (...)) that returns at most one row per IOC regardless of how many active honeypots match, soSum()/Count()aggregations stay accurate.The non-aggregated code path (
SimpleFeedView,AdvancedFeedView,ConsumeFeedView) already appliedhoneypots__active=Truecorrectly; onlyAsnFeedView(is_aggregated=True) was affected.Changes
api/views/feeds.pyreturn iocswithExists-subquery filter; addExists,OuterRefto importstests/api/views/test_feeds_asn_view.pytest_asn_feed_excludes_iocs_from_inactive_honeypotsregression testTest
The new regression test (
test_asn_feed_excludes_iocs_from_inactive_honeypots) creates an IOC linked only to anactive=Falsehoneypot in the same ASN as existing fixtures, hits/api/feeds/asn/, and asserts:ioc_countis still 2 (the two active-honeypot IOCs), not 3total_attack_countmatches only the active IOCs, not the stale one's inflated value (999)Checklist
Formalities
fix(asn-feed): exclude IOCs from inactive honeypots via Exists subquery. Closes #1794develop.develop.Docs and tests
Ruff) gave 0 errors.test_asn_feed_excludes_iocs_from_inactive_honeypots)GUI changes
No GUI changes in this PR.