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

feat: added debian parser #3543

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions cve_bin_tool/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"swift",
"php",
"perl",
"deb",
]


Expand Down
67 changes: 67 additions & 0 deletions cve_bin_tool/parsers/deb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later

import re

from cve_bin_tool.parsers import Parser
from cve_bin_tool.util import ProductInfo, ScanInfo


class DebParser(Parser):
def __init__(self, cve_db, logger):
super().__init__(cve_db, logger)

def find_vendor(self, product, version):
"""Find vendor for Debian product"""
vendor_package_pair = self.cve_db.get_vendor_product_pairs(product)

# If no match, try alternative product name transformations
if not vendor_package_pair and "-" in product:
self.logger.debug(f"Trying alternative product name for {product}")
# Example transformation: replace hyphens with underscores
alternative_product = product.replace("-", "_")
vendor_package_pair = self.cve_db.get_vendor_product_pairs(
alternative_product
)

if vendor_package_pair:
info = []
for pair in vendor_package_pair:
vendor = pair["vendor"]
file_path = self.filename
self.logger.debug(f"{file_path} {product} {version} by {vendor}")
info.append(ScanInfo(ProductInfo(vendor, product, version), file_path))
return info
else:
return None

def parse_control_file(self, control_data):
# Regex to extract package name and version
package_re = re.compile(r"Package: (.+)")
version_re = re.compile(r"Version: (.+)")

product = package_re.search(control_data)
version = version_re.search(control_data)

if product and version:
return product.group(1), version.group(1)
else:
return None, None

def run_checker(self, filename):
"""Process .deb control file"""
self.logger.debug(f"Scanning .deb control file: {filename}")
try:
with open(filename) as file:
control_data = file.read()
product, version = self.parse_control_file(control_data)
if product and version:
product_info = self.find_vendor(product, version)
if product_info:
yield from product_info
else:
self.logger.debug(f"No product/version found in {filename}")
except Exception as e:
self.logger.error(f"Error processing file {filename}: {e}")

self.logger.debug(f"Done scanning file: {filename}")