forked from intel/cve-bin-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: documentation on out of tree parsers
Signed-off-by: John <[email protected]>
- Loading branch information
John Andersen
committed
Jun 17, 2024
1 parent
fadb9ca
commit 327969e
Showing
4 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
"php", | ||
"perl", | ||
"dart", | ||
"env", | ||
] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import json | ||
import re | ||
import pathlib | ||
import subprocess | ||
import contextlib | ||
import dataclasses | ||
from re import MULTILINE, compile, search | ||
|
||
from packaging.version import parse as parse_version | ||
|
||
from cve_bin_tool.parsers import Parser | ||
from cve_bin_tool.strings import parse_strings | ||
from cve_bin_tool.util import ProductInfo, ScanInfo | ||
|
||
import snoop | ||
|
||
|
||
@dataclasses.dataclass | ||
class EnvNamespaceConfig: | ||
ad_hoc_cve_id: str | ||
vendor: str | ||
product: str | ||
version: str | ||
|
||
|
||
@dataclasses.dataclass | ||
class EnvConfig: | ||
namespaces: dict[str, EnvNamespaceConfig] | ||
|
||
|
||
class EnvParser(Parser): | ||
""" | ||
Parser for Python requirements files. | ||
This parser is designed to parse Python requirements files (usually named | ||
requirements.txt) and generate PURLs (Package URLs) for the listed packages. | ||
""" | ||
|
||
PARSER_MATCH_FILENAMES = [ | ||
".env", | ||
] | ||
|
||
def __init__(self, cve_db, logger): | ||
"""Initialize the python requirements file parser.""" | ||
super().__init__(cve_db, logger) | ||
self.purl_pkg_type = "ad-hoc" | ||
|
||
def generate_purl(self, product, vendor, qualifier={}, subpath=None): | ||
"""Generates PURL after normalizing all components.""" | ||
product = re.sub(r"[^a-zA-Z0-9._-]", "", product).lower() | ||
|
||
if not product: | ||
return None | ||
|
||
purl = super().generate_purl( | ||
product, | ||
vendor, | ||
qualifier, | ||
subpath, | ||
) | ||
|
||
return purl | ||
|
||
@staticmethod | ||
@snoop | ||
def parse_file_contents(contents): | ||
lines = list( | ||
[ | ||
line | ||
for line in contents.replace("\r\n", "\n").split("\n") | ||
if line.strip() and line.startswith("CVE_BIN_TOOL_") | ||
] | ||
) | ||
namespaces = {} | ||
for i, line in enumerate(lines): | ||
key, value = line.split("=", maxsplit=1) | ||
namespace, key = key[len("CVE_BIN_TOOL_") :].split("_", maxsplit=1) | ||
if value.startswith('"'): | ||
value = value[1:] | ||
if value.endswith('"'): | ||
value = value[:-1] | ||
namespaces.setdefault(namespace, {}) | ||
namespaces[namespace][key.lower()] = value | ||
for namespace, config in namespaces.items(): | ||
namespaces[namespace] = EnvNamespaceConfig(**config) | ||
return EnvConfig(namespaces=namespaces) | ||
|
||
@snoop | ||
def run_checker(self, filename): | ||
""" | ||
Parse the .env file and yield ScanInfo objects for the listed packages. | ||
Args: | ||
filename (str): The path to the .env file. | ||
Yields: | ||
str: ScanInfo objects for the packages listed in the file. | ||
""" | ||
self.filename = filename | ||
contents = pathlib.Path(self.filename).read_text() | ||
|
||
env_config = self.parse_file_contents(contents) | ||
|
||
snoop.pp(self.cve_db) | ||
|
||
# TODO Create SCITT_URN_FOR_MANIFEST_OF_EXECUTED_WORKFLOW_WITH_SARIF_OUTPUTS_DEREFERENCEABLE | ||
# by making a request to the poligy engine and getting it's workflow | ||
# manifest as output and deriving from that or extend it to return that. | ||
data_source = "SCITT_URN_FOR_MANIFEST_OF_EXECUTED_WORKFLOW_WITH_SARIF_OUTPUTS_DEREFERENCEABLE" | ||
affected_data = [ | ||
{ | ||
"cve_id": cve.ad_hoc_cve_id, | ||
"vendor": cve.vendor, | ||
"product": cve.product, | ||
"version": cve.version, | ||
"versionStartIncluding": "", | ||
# "versionStartIncluding": cve.version, | ||
"versionStartExcluding": "", | ||
"versionEndIncluding": "", | ||
# "versionEndIncluding": cve.version, | ||
"versionEndExcluding": "", | ||
} | ||
for _namespace, cve in env_config.namespaces.items() | ||
] | ||
with self.cve_db.with_cursor() as cursor: | ||
self.cve_db.populate_affected(affected_data, cursor, data_source) | ||
|
||
for _namespace, cve in env_config.namespaces.items(): | ||
yield from self.find_vendor(cve.product, cve.version) | ||
|
||
# TODO VEX attached via linked data to ad-hoc CVE-ID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters