Skip to content

Commit 910919d

Browse files
authored
Update web_utils.py
1 parent b256c7e commit 910919d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

lib/cuckoo/common/web_utils.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,21 +1397,29 @@ def perform_search(
13971397
search_term_map[term] = f"CAPE.configs.{value}"
13981398
query_val = {"$exists": True}
13991399

1400+
retval = []
14001401
if repconf.mongodb.enabled and query_val:
14011402
if term in hash_searches:
14021403
# The file details are uniq, and we store 1 to many. So where hash type is uniq, IDs are list
14031404
split_by = "," if "," in query_val else " "
14041405
query_val = {"$in": [val.strip() for val in query_val.split(split_by)]}
1405-
# The file details are uniq, and we store 1 to many. So where hash type is uniq, IDs are list
1406-
file_docs = list(mongo_find(FILES_COLL, {hash_searches[term]: query_val}, {"_task_ids": 1}))
1407-
if not file_docs:
1408-
return []
1409-
all_ids = []
1410-
for file_doc in file_docs:
1411-
all_ids.extend(file_doc["_task_ids"])
1412-
ids = sorted(list(set(all_ids)), reverse=True)[:search_limit]
1413-
term = "ids"
1414-
mongo_search_query = {"info.id": {"$in": ids}}
1406+
pipeline = [
1407+
# Stages 1-5: Find, unwind, group, sort, limit IDs
1408+
{"$match": {hash_searches[term]: query_filter_list}},
1409+
{"$unwind": "$_task_ids"},
1410+
{"$group": {"_id": "$_task_ids"}},
1411+
{"$sort": {"_id": -1}},
1412+
{"$limit": limit},
1413+
# Stage 6: Join with the tasks collection
1414+
{"$lookup": {"from": "analysis", "localField": "_id", "foreignField": "info.id", "as": "task_doc"}},
1415+
# Stage 7: Unpack the joined doc
1416+
{"$unwind": "$task_doc"},
1417+
# Stage 8: Make the task doc the new root
1418+
{"$replaceRoot": {"newRoot": "$task_doc"}},
1419+
# Stage 9: Add your custom projection
1420+
{"$project": perform_search_filters},
1421+
]
1422+
retval = list(mongo_aggregate(FILES_COLL, pipeline))
14151423
elif isinstance(search_term_map[term], str):
14161424
mongo_search_query = {search_term_map[term]: query_val}
14171425
elif isinstance(search_term_map[term], list):
@@ -1426,7 +1434,8 @@ def perform_search(
14261434
if "target.file.sha256" in projection:
14271435
projection = dict(**projection)
14281436
projection[f"target.file.{FILE_REF_KEY}"] = 1
1429-
retval = list(mongo_find("analysis", mongo_search_query, projection, limit=search_limit))
1437+
if not retval:
1438+
retval = list(mongo_find("analysis", mongo_search_query, projection, limit=search_limit))
14301439
for doc in retval:
14311440
target_file = doc.get("target", {}).get("file", {})
14321441
if FILE_REF_KEY in target_file and "sha256" not in target_file:

0 commit comments

Comments
 (0)