Skip to content

Commit

Permalink
Make CVE age filter more deterministic
Browse files Browse the repository at this point in the history
Process CVEs from yesterday by default.
  • Loading branch information
msrb committed Mar 27, 2019
1 parent 5051a09 commit cb233c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cvejob/filters/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,34 @@ def check(self):


class NotOlderThanCheck(CveCheck):
"""Check whether given CVE is not older than predefined number of days."""
"""Check whether given CVE is not older than predefined number of days.
Examples:
If Config.cve_age is equal to 1, then only CVEs which were added/modified
yesterday will pass the check.
If Config.cve_age is equal to 7, then only CVEs which were added/modified
in the last 7 days will pass the check, minus todays CVEs.
If Config.cve_age is equal to 0, then all CVEs will pass the check.
"""

def check(self):
"""Perform the check."""
config_age = Config.cve_age
if config_age == 0:
return True
now = datetime.datetime.now()
age = now.date() - self._doc.modified_date.date()
return age.days < config_age
today = datetime.datetime.utcnow().date()
age = today - self._doc.modified_date.date()
return self.evaluate(age, config_age)

def evaluate(self, age, wanted_age):
"""Evaluate whether given age is within the wanted range.
:param age: datetime.timedelta, delta between today and when CVE was last modified
:param wanted_age: int, age in range(1, wanted_age+1) is considered valid
:return: bool, True for age in wanted_age range, False otherwise
"""
return age.days and age.days <= wanted_age


class NotUnsupportedFileExtensionCheck(CveCheck):
Expand Down
9 changes: 9 additions & 0 deletions tests/filters/test_input.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test cvejob.filters.input module."""

import pytest
import datetime


from cvejob.filters.input import (
Expand All @@ -22,6 +23,14 @@ def test_not_older_than_check(config, javascript_cve, mocker):
assert check.check()


def test_not_older_than_check_evaluate(javascript_cve):
"""Test NotOlderThanCheck().evaluate()."""
check = NotOlderThanCheck(javascript_cve)
assert check.evaluate(datetime.timedelta(days=1), 1)
assert check.evaluate(datetime.timedelta(days=1), 7)
assert not check.evaluate(datetime.timedelta(days=0), 1)


def test_not_unsupported_file_extension_check(javascript_cve):
"""Test NotUnsupportedFileExtensionCheck()."""
check = NotUnsupportedFileExtensionCheck(javascript_cve)
Expand Down

0 comments on commit cb233c2

Please sign in to comment.