Skip to content

Commit bd631ab

Browse files
joydeep049terriko
andauthored
feat: added PURL generation to JavascriptParser (#3987)
Co-authored-by: Joydeep Tripathy <[email protected]> Co-authored-by: Terri Oda <[email protected]>
1 parent da495bd commit bd631ab

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cve_bin_tool/parsers/javascript.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# Copyright (C) 2022 Intel Corporation
1+
# Copyright (C) 2024 Intel Corporation
22
# SPDX-License-Identifier: GPL-3.0-or-later
3+
"""Python script containing all functionalities related to parsing of javascript's package-lock.json files."""
34

45
import json
6+
import re
57

68
from cve_bin_tool.parsers import Parser
79

@@ -11,6 +13,26 @@ class JavascriptParser(Parser):
1113

1214
def __init__(self, cve_db, logger):
1315
super().__init__(cve_db, logger)
16+
self.purl_pkg_type = "npm"
17+
18+
def generate_purl(self, product, version, vendor, qualifier={}, subpath=None):
19+
"""Generates PURL after normalizing all components."""
20+
product = re.sub(r"[^a-zA-Z0-9._-]", "", product).lower()
21+
version = re.sub(r"[^a-zA-Z0-9.+\-]", "", version)
22+
vendor = "UNKNOWN" # Typically, the vendor is not explicitly defined for npm packages
23+
24+
if not product or not version:
25+
return None
26+
27+
purl = super().generate_purl(
28+
product,
29+
version,
30+
vendor,
31+
qualifier,
32+
subpath,
33+
)
34+
35+
return purl
1436

1537
def get_package_name(self, name):
1638
"""Returns npm package name by decomposing string"""

0 commit comments

Comments
 (0)