Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 7, 2025
1 parent 9487c4a commit a72ee3a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion surfactant/infoextractors/js_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from surfactant.configmanager import ConfigManager
from surfactant.sbomtypes import SBOM, Software


class JSDatabaseManager:
def __init__(self):
self.js_lib_database = None
Expand All @@ -35,17 +36,21 @@ def load_db(self):
def get_database(self):
return self.js_lib_database


js_db_manager = JSDatabaseManager()


def supports_file(filetype) -> bool:
return filetype == "JAVASCRIPT"


@surfactant.plugin.hookimpl
def extract_file_info(sbom: SBOM, software: Software, filename: str, filetype: str) -> object:
if not supports_file(filetype):
return None
return extract_js_info(filename)


def extract_js_info(filename: str) -> object:
js_info: Dict[str, Any] = {"jsLibraries": []}
js_lib_database = js_db_manager.get_database()
Expand All @@ -71,6 +76,7 @@ def extract_js_info(filename: str) -> object:
logger.warning(f"File does not appear to be UTF-8: {filename}")
return js_info


def match_by_attribute(attribute: str, content: str, database: Dict) -> List[Dict]:
libs = []
for name, library in database.items():
Expand All @@ -84,6 +90,7 @@ def match_by_attribute(attribute: str, content: str, database: Dict) -> List[Dic
break
return libs


def download_database() -> dict:
url = "https://raw.githubusercontent.com/RetireJS/retire.js/master/repository/jsrepository-master.json"
response = requests.get(url)
Expand All @@ -98,6 +105,7 @@ def download_database() -> dict:

return None


def strip_irrelevant_data(retirejs_db: dict) -> dict:
clean_db = {}
reg_temp = "\u00a7\u00a7version\u00a7\u00a7"
Expand All @@ -119,6 +127,7 @@ def strip_irrelevant_data(retirejs_db: dict) -> dict:
clean_db[library][entry] = entry_list
return clean_db


@surfactant.plugin.hookimpl
def update_db():
"""Retrieves the javascript library CVE database used by retire.js (https://github.com/RetireJS/retire.js/blob/master/repository/jsrepository-master.json) and only keeps the contents under each library's "extractors" section, which contains file hashes and regexes relevant for detecting a specific javascript library by its file name or contents.
Expand All @@ -137,14 +146,16 @@ def update_db():
return "Update complete."
return "No update occurred."


@surfactant.plugin.hookimpl
def short_name():
return "js_file"


@surfactant.plugin.hookimpl
def init_hook(command_name=None):
"""Initialization hook to load the JavaScript library database."""
if command_name != "update-db": # Do not load the database if only updating the database.
click.echo("Initializing js_file...")
js_db_manager.load_db()
click.echo("Initializing js_file complete.")
click.echo("Initializing js_file complete.")

0 comments on commit a72ee3a

Please sign in to comment.