Skip to content

Commit 19a73f2

Browse files
committed
squashed commits
1 parent 78eb819 commit 19a73f2

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

dojo/filters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
from dojo.risk_acceptance.queries import get_authorized_risk_acceptances
9494
from dojo.test.queries import get_authorized_tests
9595
from dojo.user.queries import get_authorized_users
96-
from dojo.utils import get_system_setting, is_finding_groups_enabled, truncate_timezone_aware
96+
from dojo.utils import get_system_setting, get_visible_scan_types, is_finding_groups_enabled, truncate_timezone_aware
9797

9898
logger = logging.getLogger(__name__)
9999

@@ -2030,6 +2030,9 @@ def __init__(self, *args, **kwargs):
20302030
# Don't show the product filter on the product finding view
20312031
self.set_related_object_fields(*args, **kwargs)
20322032

2033+
if "test__test_type" in self.form.fields:
2034+
self.form.fields["test__test_type"].queryset = get_visible_scan_types()
2035+
20332036
def set_related_object_fields(self, *args: list, **kwargs: dict):
20342037
finding_group_query = Finding_Group.objects.all()
20352038
if self.pid is not None:

dojo/finding/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
get_page_items_and_count,
119119
get_return_url,
120120
get_system_setting,
121+
get_visible_scan_types,
121122
get_words_for_field,
122123
match_finding_to_existing_findings,
123124
process_tag_notifications,
@@ -302,6 +303,7 @@ def get_initial_context(self, request: HttpRequest):
302303
"enable_table_filtering": get_system_setting("enable_ui_table_based_searching"),
303304
"title_words": get_words_for_field(Finding, "title"),
304305
"component_words": get_words_for_field(Finding, "component_name"),
306+
"visible_test_types": get_visible_scan_types(),
305307
}
306308
# Look to see if the product was used
307309
if product_id := self.get_product_id():

dojo/test_type/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from dojo.filters import TestTypeFilter
1212
from dojo.forms import Test_TypeForm
1313
from dojo.models import Test_Type
14-
from dojo.utils import add_breadcrumb, get_page_items
14+
from dojo.utils import add_breadcrumb, get_page_items, get_visible_scan_types
1515

1616
logger = logging.getLogger(__name__)
1717

@@ -24,7 +24,7 @@
2424

2525
@login_required
2626
def test_type(request):
27-
initial_queryset = Test_Type.objects.all().order_by("name")
27+
initial_queryset = get_visible_scan_types().order_by("name")
2828
name_words = initial_queryset.values_list("name", flat=True)
2929
test_types = TestTypeFilter(request.GET, queryset=initial_queryset)
3030
tts = get_page_items(request, test_types.qs, 25)
@@ -35,7 +35,8 @@ def test_type(request):
3535
"user": request.user,
3636
"tts": tts,
3737
"test_types": test_types,
38-
"name_words": name_words})
38+
"name_words": name_words,
39+
})
3940

4041

4142
@user_is_configuration_authorized("dojo.add_test_type")

dojo/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
Product,
6969
System_Settings,
7070
Test,
71+
Test_Type,
7172
User,
7273
)
7374
from dojo.notifications.helper import create_notification
@@ -83,6 +84,25 @@
8384
"""
8485

8586

87+
def get_visible_scan_types():
88+
"""
89+
Returns a QuerySet of active, non-excluded Test_Type objects.
90+
Supports comma or pipe-separated names in PARSER_EXCLUDE.
91+
"""
92+
exclude_raw = (getattr(settings, "PARSER_EXCLUDE", "") or "").strip()
93+
if exclude_raw:
94+
# Support both ',' and '|' separators
95+
parts = [p.strip() for sep in (",", "|") for p in exclude_raw.split(sep)]
96+
excluded_names = {p for p in parts if p}
97+
else:
98+
excluded_names = set()
99+
100+
qs = Test_Type.objects.filter(active=True)
101+
if excluded_names:
102+
qs = qs.exclude(name__in=excluded_names)
103+
return qs
104+
105+
86106
def do_false_positive_history(finding, *args, **kwargs):
87107
"""
88108
Replicate false positives across product.

unittests/test_testtype_filter.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
from django.test import TestCase, override_settings
3+
4+
from dojo.filters import FindingFilter
5+
from dojo.models import Test_Type
6+
from dojo.utils import get_visible_scan_types
7+
8+
9+
class TestFindingFilterExcludesTestTypes(TestCase):
10+
def setUp(self):
11+
self.active_type = Test_Type.objects.create(name="Nessus Scan", active=True)
12+
self.excluded_type = Test_Type.objects.create(name="Inactive Scan", active=True)
13+
self.inactive_type = Test_Type.objects.create(name="Burp Scan", active=False)
14+
15+
@override_settings(PARSER_EXCLUDE="Inactive Scan")
16+
def test_excludes_inactive_and_single_excluded(self):
17+
filter_instance = FindingFilter(data={})
18+
self.assertIn("test__test_type", filter_instance.form.fields)
19+
queryset = filter_instance.form.fields["test__test_type"].queryset
20+
actual_names = set(queryset.values_list("name", flat=True))
21+
self.assertIn(self.active_type.name, actual_names)
22+
self.assertNotIn(self.excluded_type.name, actual_names)
23+
self.assertNotIn(self.inactive_type.name, actual_names)
24+
25+
@override_settings(PARSER_EXCLUDE="Inactive Scan|Acunetix Scan")
26+
def test_multiple_exclusions(self):
27+
filter_instance = FindingFilter(data={})
28+
queryset = filter_instance.form.fields["test__test_type"].queryset
29+
actual_names = set(queryset.values_list("name", flat=True))
30+
self.assertNotIn(self.excluded_type.name, actual_names)
31+
32+
@override_settings(PARSER_EXCLUDE="")
33+
def test_no_exclusions_only_active(self):
34+
filter_instance = FindingFilter(data={})
35+
queryset = filter_instance.form.fields["test__test_type"].queryset
36+
self.assertIn(self.active_type, queryset)
37+
self.assertNotIn(self.inactive_type, queryset)
38+
39+
def test_helper_function(self):
40+
visible = get_visible_scan_types()
41+
names = set(visible.values_list("name", flat=True))
42+
self.assertIn(self.active_type.name, names)
43+
self.assertNotIn(self.inactive_type.name, names)

0 commit comments

Comments
 (0)