Skip to content

Commit 2513f2d

Browse files
committed
Resolve merge conflict
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 09ef3dd commit 2513f2d

2 files changed

Lines changed: 91 additions & 25 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Generated by Django 5.2.11 on 2026-05-15 11:10
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("vulnerabilities", "0129_advisorypoc"),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name="DetectionRule",
15+
fields=[
16+
(
17+
"id",
18+
models.AutoField(
19+
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
20+
),
21+
),
22+
(
23+
"rule_type",
24+
models.CharField(
25+
choices=[
26+
("yara", "Yara"),
27+
("yara-x", "Yara-X"),
28+
("sigma", "Sigma"),
29+
("clamav", "ClamAV"),
30+
("suricata", "Suricata"),
31+
],
32+
help_text="The type of the detection rule content (e.g., YARA, Sigma).",
33+
max_length=50,
34+
),
35+
),
36+
(
37+
"source_url",
38+
models.URLField(
39+
help_text="URL to the original source or reference for this rule.",
40+
max_length=1024,
41+
),
42+
),
43+
(
44+
"rule_metadata",
45+
models.JSONField(
46+
blank=True,
47+
help_text="Additional structured data such as tags, or author information.",
48+
null=True,
49+
),
50+
),
51+
(
52+
"rule_text",
53+
models.TextField(help_text="The content of the detection signature."),
54+
),
55+
(
56+
"related_advisories",
57+
models.ManyToManyField(
58+
help_text="Advisories associated with this DetectionRule.",
59+
related_name="detection_rules",
60+
to="vulnerabilities.advisoryv2",
61+
),
62+
),
63+
],
64+
),
65+
]

vulnerabilities/pipelines/v2_improvers/detection_rules.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
#
2-
# Copyright (c) nexB Inc. and others. All rights reserved.
3-
# VulnerableCode is a trademark of nexB Inc.
4-
# SPDX-License-Identifier: Apache-2.0
5-
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6-
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7-
# See https://aboutcode.org for more information about nexB OSS projects.
8-
#
9-
101
import json
112
from pathlib import Path
123

@@ -42,8 +33,7 @@ def clone(self):
4233
self.vcs_response = fetch_via_vcs(self.repo_url)
4334

4435
def advisories_count(self):
45-
root = Path(self.vcs_response.dest_dir)
46-
return sum(1 for _ in root.rglob("*.json"))
36+
return 0
4737

4838
def collect_detection_rules(self):
4939
base_path = Path(self.vcs_response.dest_dir) / "data"
@@ -65,20 +55,8 @@ def collect_detection_rules(self):
6555

6656
source_url = json_data.get("source_url")
6757
for rule in json_data.get("rules", []):
68-
advisories = set()
69-
for vulnerability_id in rule.get("vulnerabilities", []):
70-
try:
71-
if alias := AdvisoryAlias.objects.get(alias=vulnerability_id):
72-
for adv in alias.advisories.all():
73-
advisories.add(adv)
74-
else:
75-
advs = AdvisoryV2.objects.filter(
76-
advisory_id=vulnerability_id
77-
).latest_per_avid()
78-
for adv in advs:
79-
advisories.add(adv)
80-
except AdvisoryAlias.DoesNotExist:
81-
self.log(f"No advisory found for aliases {vulnerability_id}")
58+
vulns_id = rule.get("vulnerabilities", [])
59+
advisories = get_related_advisories(vulns_id)
8260

8361
raw_text = rule.get("rule_text")
8462
rule_metadata = rule.get("rule_metadata")
@@ -102,3 +80,26 @@ def clean_downloads(self):
10280
def on_failure(self):
10381
"""Ensure cleanup is always performed on failure."""
10482
self.clean_downloads()
83+
84+
85+
def get_related_advisories(vulnerability_ids, logger=print):
86+
"""
87+
Fetches related advisories for a list of vulnerability IDs.
88+
"""
89+
advisories = set()
90+
91+
for vulnerability_id in vulnerability_ids:
92+
try:
93+
alias = AdvisoryAlias.objects.get(alias=vulnerability_id)
94+
advs = alias.advisories.all()
95+
advisories.update(advs)
96+
97+
except AdvisoryAlias.DoesNotExist:
98+
advs = AdvisoryV2.objects.filter(advisory_id=vulnerability_id).latest_per_avid()
99+
100+
if advs:
101+
advisories.update(advs)
102+
else:
103+
logger(f"No advisory found for ID/alias: {vulnerability_id}")
104+
105+
return advisories

0 commit comments

Comments
 (0)