Advanced feed returns IOCs when tag_key and tag_value match different tags
When an authenticated user supplies both tag_key and tag_value to /api/feeds/advanced/, the endpoint can return an IOC whose tags match those parameters separately. For example, a search for malware=botnet_cc returns an IOC whose malware is Mirai, because botnet_cc appears in its threat_type tag.
I reproduced this through the authenticated API against a local PostgreSQL test database on develop at 18dfaff.
Steps to reproduce
- Use the existing
FeedsTagsTestCasefixture. It assigns two tags to140.246.171.141, one withkey="malware", value="Mirai"and another withkey="threat_type", value="botnet_cc". Test fixture - Using the fixture's authenticated client, send
GET /api/feeds/advanced/?tag_key=malware&tag_value=botnet_cc. - Inspect the
valuefields in the response'siocsarray.
All three requests below returned HTTP 200. The middle row shows the false positive.
tag_key |
tag_value |
Expected result for 140.246.171.141 |
Actual result |
|---|---|---|---|
malware |
Mirai |
Included | Included ✅ |
malware |
botnet_cc |
Excluded | Included ❌ |
malware |
absent |
Excluded | Excluded ✅ |
Cause
FeedsFilterSet declares tag_key and tag_value as separate filters on the reverse tags relation. Django applies them in successive queryset .filter() calls, so each condition can match a different Tag row for the same IOC. The final .distinct() removes duplicate IOCs, but an IOC that matched across two rows still qualifies.
Proposed fix
Have FeedsFilterSet collect the supplied tag lookups and apply them in a single .filter() call. When both parameters are present, use queryset.filter(tags__key__iexact=tag_key, tags__value__icontains=tag_value) so one tag must satisfy both conditions. When only one parameter is present, apply just that lookup. This preserves case-insensitive exact matching for keys and case-insensitive substring matching for values.
Add a regression test using the existing two-tag fixture. Assert that malware with botnet_cc excludes the IOC and that malware with mirai includes it. The existing combined-filter test checks only the positive case, so it passes with the current behavior.
@regulartim kindly check this and I'd be happy to work on this.
Hey @Saurabh-1785 ! Good catch, thank you! I'll assign you.