Skip to content

Commit fcf0555

Browse files
authored
feat: added PURL generation to PhpParser (#4016)
Co-authored-by: Joydeep Tripathy <[email protected]>
1 parent 8d0faa9 commit fcf0555

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

cve_bin_tool/parsers/php.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1-
# Copyright (C) 2022 Intel Corporation
1+
# Copyright (C) 2024 Intel Corporation
22
# SPDX-License-Identifier: GPL-3.0-or-later
3-
3+
"""Python script containing all functionalities related to parsing of php's composer.lock files."""
44
import json
5+
import re
56

67
from cve_bin_tool.parsers import Parser
78

89

910
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+
1017
def __init__(self, cve_db, logger):
18+
"""Initialize the PhpParser."""
1119
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
1240

1341
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"""
1543
self.filename = filename
1644
with open(self.filename) as fh:
1745
data = json.load(fh)

0 commit comments

Comments
 (0)