Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filters): prevent crashes when start_date == date.min #10224

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions dojo/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import decimal
import logging
import warnings
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta

import pytz
import six
Expand Down Expand Up @@ -184,7 +184,10 @@ def filter(self, qs, value):
start_date = local_tz.localize(datetime.combine(
earliest_finding.date, datetime.min.time())
)
self.start_date = _truncate(start_date - timedelta(days=1))
try:
self.start_date = _truncate(start_date - timedelta(days=1))
except OverflowError: # this will fail when start_date is date.min
self.start_date = date.min
self.end_date = _truncate(now() + timedelta(days=1))
try:
value = int(value)
Expand Down Expand Up @@ -772,7 +775,10 @@ def any(self, qs, name):
start_date = local_tz.localize(datetime.combine(
earliest_finding.date, datetime.min.time())
)
self.start_date = _truncate(start_date - timedelta(days=1))
try:
self.start_date = _truncate(start_date - timedelta(days=1))
except OverflowError: # this will fail when start_date is date.min
self.start_date = date.min
self.end_date = _truncate(now() + timedelta(days=1))
return qs.all()

Expand Down Expand Up @@ -840,7 +846,10 @@ def filter(self, qs, value):
start_date = local_tz.localize(datetime.combine(
earliest_finding.date, datetime.min.time())
)
self.start_date = _truncate(start_date - timedelta(days=1))
try:
self.start_date = _truncate(start_date - timedelta(days=1))
except OverflowError: # this will fail when start_date is date.min
self.start_date = date.min
self.end_date = _truncate(now() + timedelta(days=1))
try:
value = int(value)
Expand Down
16 changes: 16 additions & 0 deletions unittests/scans/generic/generic_bad_date.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"findings": [
{
"title": "test title",
"description": "Test description",
"active": true,
"verified": true,
"severity": "High",
"impact": "Some impact",
"date": "0001-01-01",
"tags": ["security", "network"],
"unique_id_from_tool": "0b5a8a8f-b3b0-4fe9-9b27-87d58631ac03",
"vuln_id_from_tool": "TEST_BAD_DATE"
}
]
}
8 changes: 8 additions & 0 deletions unittests/tools/test_generic_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,11 @@ def test_parse_json_invalid_finding(self):
with self.assertRaisesMessage(ValueError,
"Not allowed fields are present: ['invalid_field', 'last_status_update']"):
parser.get_findings(file, Test())

def test_parse_json_bad_date(self):
with open("unittests/scans/generic/generic_bad_date.json") as fdesc:
parser = GenericParser()
findings = parser.get_findings(fdesc, Test())
self.assertEqual(1, len(findings))
# test the filters
self.assertEqual(1, len(Finding.objects.filter(active=True)))
Loading