|
1 |
| -# Copyright (C) 2022 Intel Corporation |
| 1 | +# Copyright (C) 2024 Intel Corporation |
2 | 2 | # SPDX-License-Identifier: GPL-3.0-or-later
|
3 |
| - |
| 3 | +"""Python script containing all functionalities related to parsing of php's composer.lock files.""" |
4 | 4 | import json
|
| 5 | +import re |
5 | 6 |
|
6 | 7 | from cve_bin_tool.parsers import Parser
|
7 | 8 |
|
8 | 9 |
|
9 | 10 | class PhpParser(Parser):
|
| 11 | + """ |
| 12 | + Parser for Php Composer.lock files. |
| 13 | + This parser is designed to parse Php Composer.lock and |
| 14 | + generate PURLs (Package URLs) for the listed packages. |
| 15 | + """ |
| 16 | + |
10 | 17 | def __init__(self, cve_db, logger):
|
| 18 | + """Initialize the PhpParser.""" |
11 | 19 | super().__init__(cve_db, logger)
|
| 20 | + self.purl_pkg_type = "composer" |
| 21 | + |
| 22 | + def generate_purl(self, product, version, vendor, qualifier={}, subpath=None): |
| 23 | + """Generates PURL after normalizing all components.""" |
| 24 | + vendor = re.sub(r"[^a-zA-Z0-9._-]", "", vendor).lower() |
| 25 | + product = re.sub(r"[^a-zA-Z0-9._-]", "", product).lower() |
| 26 | + version = re.sub(r"[^a-zA-Z0-9.+-]", "", version) |
| 27 | + |
| 28 | + if not vendor or not product or not version: |
| 29 | + return None |
| 30 | + |
| 31 | + purl = super().generate_purl( |
| 32 | + product, |
| 33 | + version, |
| 34 | + vendor, |
| 35 | + qualifier, |
| 36 | + subpath, |
| 37 | + ) |
| 38 | + |
| 39 | + return purl |
12 | 40 |
|
13 | 41 | def run_checker(self, filename):
|
14 |
| - """Process package.lock file and extract product and dependency details""" |
| 42 | + """Process composer.lock file and extract product and dependency details""" |
15 | 43 | self.filename = filename
|
16 | 44 | with open(self.filename) as fh:
|
17 | 45 | data = json.load(fh)
|
|
0 commit comments