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

add -e --exclude arument for excluding path #876

Merged
merged 7 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 11 additions & 1 deletion cve_bin_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def main(argv=None):
input_group.add_argument(
"directory", help="directory to scan", nargs="?", default=None
)
input_group.add_argument(
"-e",
"--exclude",
action=StringToListAction,
help="Comma separated Exclude directory path",
default=None,
)

input_group.add_argument(
"-i", "--input-file", action="store", default="", help="provide input filename",
Expand Down Expand Up @@ -164,6 +171,7 @@ def main(argv=None):
)
defaults = {
"directory": "",
"exclude": [],
"input_file": "",
"log_level": "info",
"format": "console",
Expand Down Expand Up @@ -294,7 +302,9 @@ def main(argv=None):
cve_scanner.get_cves(product_info, triage_data)
if args["directory"]:
version_scanner = VersionScanner(
should_extract=args["extract"], error_mode=error_mode
should_extract=args["extract"],
exclude_folders=args["exclude"],
error_mode=error_mode,
)
version_scanner.remove_skiplist(skips)
LOGGER.info(version_scanner.print_checkers())
Expand Down
21 changes: 13 additions & 8 deletions cve_bin_tool/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,21 @@ def walk(self, roots=None):
for dirpath, dirnames, filenames in os.walk(root):
# Filters
filenames[:] = [
i
for i in filenames
if self.pattern_match(i, self.pattern)
and not self.pattern_match(i, self.file_exclude_pattern)
filename
for filename in filenames
if self.pattern_match(os.path.join(dirpath, filename), self.pattern)
and not self.pattern_match(
os.path.join(dirpath, filename), self.file_exclude_pattern
)
and not self.pattern_match(
os.path.join(dirpath, filename), self.folder_exclude_pattern
)
]
dirnames[:] = [
i
for i in dirnames
if self.pattern_match(i, self.folder_include_pattern)
and not self.pattern_match(i, self.folder_exclude_pattern)
dirname
for dirname in dirnames
if self.pattern_match(dirname, self.folder_include_pattern)
and not self.pattern_match(dirname, self.folder_exclude_pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to join path here also os.path.join(dirpath, dirname).

]
# Yields
if self.yield_files:
Expand Down
12 changes: 10 additions & 2 deletions cve_bin_tool/version_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class VersionScanner:
def __init__(
self,
should_extract=False,
exclude_folders=[],
checkers=None,
logger=None,
error_mode=ErrorMode.TruncTrace,
Expand All @@ -40,8 +41,15 @@ def __init__(
self.checkers = checkers or self.load_checkers()
self.score = score
self.total_scanned_files = 0
exclude_folders = [".git"]
self.walker = DirWalk(folder_exclude_pattern=";".join(exclude_folders)).walk
self.exclude_folders = exclude_folders
self.exclude_folders.append(".git")

self.walker = DirWalk(
folder_exclude_pattern=";".join(
exclude if exclude.endswith("*") else exclude + "*"
for exclude in exclude_folders
)
).walk
self.should_extract = should_extract
self.file_stack = []
self.error_mode = error_mode
Expand Down
24 changes: 24 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from cve_bin_tool.cli import main
from cve_bin_tool.cvedb import DISK_LOCATION_DEFAULT
from cve_bin_tool.extractor import Extractor
from cve_bin_tool.version_scanner import VersionScanner
from .utils import (
TempDirTest,
download_file,
Expand Down Expand Up @@ -56,6 +57,15 @@ def test_no_extraction(self):
""" Test scanner against curl-7.20.0 rpm with extraction turned off """
assert main(["cve-bin-tool", os.path.join(self.tempdir, CURL_7_20_0_RPM)]) == 0

def test_exclude(self, caplog):
""" Test that the exclude paths are not scanned """
test_path = os.path.abspath(os.path.dirname(__file__))
exclude_path = os.path.join(test_path, "assets/")
checkers = list(VersionScanner().checkers.keys())
with caplog.at_level(logging.INFO):
main(["cve-bin-tool", test_path, "-e", ",".join(exclude_path)])
self.check_exclude_log(caplog, exclude_path, checkers)

def test_usage(self):
""" Test that the usage returns 0 """
with pytest.raises(SystemExit) as e:
Expand Down Expand Up @@ -101,6 +111,20 @@ def test_update_flags(self):
main(["cve-bin-tool", "-u", "whatever", self.tempdir])
assert e.value.args[0] == -2

@staticmethod
def check_exclude_log(caplog, exclude_path, checkers):
# The final log has all the checkers detected
final_log = [
record for record in caplog.records if "NewFound CVEs" in record.message
]
assert len(final_log) == 0, "Checkers from excluded path scanned!!"
if final_log:
final_log = final_log[0].message
for checker in checkers:
assert checker in final_log, f"found a CVE {checker} in {exclude_path}"

caplog.clear()

@staticmethod
def check_checkers_log(caplog, skip_checkers, include_checkers):
# The final log has all the checkers detected
Expand Down
1 change: 1 addition & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TestConfig:
"cvss": 0,
"directory": "test/assets",
"disable_version_check": False,
"exclude": [],
"extract": False,
"format": "console",
"input_file": "test/csv/triage.csv",
Expand Down