Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updatejsdb #298

Merged
merged 77 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
6e7e488
Added plugin list subcommand functionality
willis89pr Nov 4, 2024
45a1b6d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 4, 2024
d141ce5
changed subcommand from list to display to avoid python builtin redef…
willis89pr Nov 4, 2024
75afc0c
changed subcommand from list to display to avoid python builtin redef…
willis89pr Nov 5, 2024
555490d
changed subcommand from list to display to avoid python builtin redef…
willis89pr Nov 5, 2024
dbd9fba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 5, 2024
79a3675
fixed import function
willis89pr Nov 5, 2024
229d8b1
fixed import function
willis89pr Nov 5, 2024
4126841
Merge branch 'main' into CYT-828-add-plugin-command
willis89pr Nov 5, 2024
be48bb5
changed subcommand name to list in click decorator
willis89pr Nov 5, 2024
efb07c6
Merge remote-tracking branch 'origin/CYT-828-add-plugin-command' into…
willis89pr Nov 5, 2024
3f911a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 5, 2024
261cc6e
Changed subcommand definition name. Added docstring description to su…
willis89pr Nov 5, 2024
fabf213
Merge remote-tracking branch 'origin/CYT-828-add-plugin-command' into…
willis89pr Nov 5, 2024
5e20458
Changed subcommand definition name. Added docstring description to su…
willis89pr Nov 5, 2024
b88b0e7
Added boiler plate for disable commmand.
willis89pr Nov 5, 2024
52e0703
Added disable plugin functionality.
willis89pr Nov 6, 2024
768574d
Added disable subcommand to main.
willis89pr Nov 6, 2024
3a8342c
Added variables for config section and key and changed section to core.
willis89pr Nov 6, 2024
da8554f
Re-implemented disable command with functionality in surfactant/plugi…
willis89pr Nov 6, 2024
be12bfa
Added print disabled plugins to list subcommand
willis89pr Nov 6, 2024
4d27807
Added print disabled plugins to list subcommand.
willis89pr Nov 6, 2024
62657de
Added plugin enable functionality.
willis89pr Nov 11, 2024
40babc5
Merge branch 'main' into CYT-1123-plugin-enable-disable
willis89pr Nov 11, 2024
7ac000a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 11, 2024
c0b4a0b
Update surfactant/cmd/plugin.py
willis89pr Nov 11, 2024
093b047
Update surfactant/cmd/plugin.py
willis89pr Nov 11, 2024
dd4b055
Save.
willis89pr Nov 12, 2024
0ec3b91
Added set_plugins function. Changed formatting of print_plugins
willis89pr Nov 12, 2024
03355a2
Added print_plugins in list command and changed formatting
willis89pr Nov 12, 2024
814d9aa
Merge branch 'CYT-1123-plugin-enable-disable' of https://github.com/L…
willis89pr Nov 12, 2024
c827e88
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 12, 2024
6b78b8d
Update surfactant/plugin/manager.py
willis89pr Nov 12, 2024
4a9f744
Updating local
willis89pr Nov 12, 2024
3ce1d93
Merge branch 'main' into CYT-1123-plugin-enable-disable
willis89pr Nov 12, 2024
ddebfef
Added command declarations in main.
willis89pr Nov 12, 2024
b51f35c
Merged deleted branches.
willis89pr Nov 18, 2024
6707d3a
Merge branch 'main' of https://github.com/LLNL/Surfactant
willis89pr Nov 18, 2024
80bd3f7
Merge branch 'main' of https://github.com/LLNL/Surfactant
willis89pr Dec 11, 2024
5881d17
Added shortname hook to js_file plugin.
willis89pr Dec 11, 2024
be4b662
Moved update js db script to updatedb hook implementation for js_file…
willis89pr Dec 11, 2024
4c63005
Added comments to js_file plugin updatedb hook implementation.
willis89pr Dec 11, 2024
3761454
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
85d3266
Fixed typo.
willis89pr Dec 11, 2024
6582af2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
4028b38
Added requests to dependencies.
willis89pr Dec 11, 2024
8e5372f
Merge branch 'updatejsdb' of https://github.com/LLNL/Surfactant into …
willis89pr Dec 11, 2024
b66f7fa
Added user feedback during js_file plugin database.
willis89pr Dec 11, 2024
5b2b43f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
3e5f2b8
Merge branch 'main' into updatejsdb
willis89pr Dec 11, 2024
dbf9464
Made if statement less redundant.
willis89pr Dec 11, 2024
e1d8ede
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
e0e3c4f
Fixed return inconsistency.
willis89pr Dec 11, 2024
cb43793
Merge branch 'updatejsdb' of https://github.com/LLNL/Surfactant into …
willis89pr Dec 11, 2024
b61ca44
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 11, 2024
3f06325
Changed configmanager.get_dataDir_path to return expanded user. Creat…
willis89pr Dec 17, 2024
a0fb868
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 17, 2024
56375ca
Merge branch 'main' into updatejsdb
willis89pr Dec 17, 2024
3a223e2
Updated js match script to use file path generated with ConfigManager…
willis89pr Dec 17, 2024
f9f0e2a
Merge branch 'main' into updatejsdb
willis89pr Dec 17, 2024
a7ccf73
Removed redundant parenthesis
willis89pr Dec 17, 2024
a361e09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 17, 2024
497f585
Removed commented out code.
willis89pr Dec 17, 2024
153bbd8
Merge branch 'updatejsdb' of https://github.com/LLNL/Surfactant into …
willis89pr Dec 17, 2024
358807f
Merge branch 'main' into updatejsdb
willis89pr Dec 17, 2024
349c187
Update surfactant/infoextractors/js_file.py
willis89pr Dec 18, 2024
d87e953
Update surfactant/infoextractors/js_file.py
willis89pr Dec 18, 2024
726cdb8
Update surfactant/infoextractors/js_file.py
willis89pr Dec 18, 2024
e1816c9
Update surfactant/infoextractors/js_file.py
willis89pr Dec 18, 2024
d8b4c93
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 18, 2024
62e8a12
Update js_file.py
willis89pr Dec 18, 2024
ecebd1c
Removed dir creation and file load with preloaded database.
willis89pr Dec 18, 2024
1d66b52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 18, 2024
c05b82a
Added warning flag to only show warning once. Removed redundant dir c…
willis89pr Dec 18, 2024
b9f2fae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 18, 2024
fa4829c
Removed flag.
willis89pr Dec 18, 2024
88ab5f0
Merge branch 'updatejsdb' of https://github.com/LLNL/Surfactant into …
willis89pr Dec 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies = [
"loguru==0.7.*",
"flask==3.*",
"tomlkit==0.13.*",
"requests>=2.32.3",
]
dynamic = ["version"]

Expand Down
41 changes: 0 additions & 41 deletions scripts/js_libraries/get_retirejs_db.py

This file was deleted.

6 changes: 4 additions & 2 deletions scripts/js_libraries/match_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import requests

from surfactant.configmanager import ConfigManager


def get_test_file():
url = "https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.min.js"
Expand Down Expand Up @@ -32,8 +34,8 @@ def find_js_match(expressions: dict, filename: str) -> str:


get_test_file()

with open("js_library_patterns.json", "r") as f:
json_file_path = ConfigManager().get_data_dir_path() / "infoextractors" / "js_library_patterns.json"
with open(json_file_path, "r") as f:
patterns = json.load(f)

library_name = find_js_match(patterns, "testFile.js")
Expand Down
96 changes: 86 additions & 10 deletions surfactant/infoextractors/js_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#
# SPDX-License-Identifier: MIT
import json
import pathlib
import re
from typing import Any, Dict, List

import click
import requests
from loguru import logger

import surfactant.plugin
from surfactant.configmanager import ConfigManager
from surfactant.sbomtypes import SBOM, Software


Expand All @@ -26,18 +28,12 @@ def extract_file_info(sbom: SBOM, software: Software, filename: str, filetype: s

def extract_js_info(filename: str) -> object:
js_info: Dict[str, Any] = {"jsLibraries": []}
js_lib_file = pathlib.Path(__file__).parent / "js_library_patterns.json"

# Load expressions from retire.js, should move this file elsewhere
try:
with open(js_lib_file, "r") as regex:
database = json.load(regex)
except FileNotFoundError:
logger.warning(f"File not found: {js_lib_file}")
if js_lib_database is None:
return None

# Try to match file name
libs = match_by_attribute("filename", filename, database)
libs = match_by_attribute("filename", filename, js_lib_database)
if len(libs) > 0:
js_info["jsLibraries"] = libs
return js_info
Expand All @@ -46,7 +42,7 @@ def extract_js_info(filename: str) -> object:
try:
with open(filename, "r") as js_file:
filecontent = js_file.read()
libs = match_by_attribute("filecontent", filecontent, database)
libs = match_by_attribute("filecontent", filecontent, js_lib_database)
js_info["jsLibraries"] = libs
except FileNotFoundError:
logger.warning(f"File not found: {filename}")
Expand All @@ -67,3 +63,83 @@ def match_by_attribute(attribute: str, content: str, database: Dict) -> List[Dic
# skip remaining patterns, move on to the next library
break
return libs


def download_database() -> dict:
url = "https://raw.githubusercontent.com/RetireJS/retire.js/master/repository/jsrepository-master.json"
response = requests.get(url)
if response.status_code == 200:
click.echo("Request successful!")
return json.loads(response.text)

if response.status_code == 404:
click.echo("Resource not found.")
else:
click.echo("An error occurred.")

return None


def strip_irrelevant_data(retirejs_db: dict) -> dict:
clean_db = {}
reg_temp = "\u00a7\u00a7version\u00a7\u00a7"
version_regex = r"\d+(?:\.\d+)*"
for library, lib_entry in retirejs_db.items():
if "extractors" in lib_entry:
clean_db[library] = {}
patterns = lib_entry["extractors"]
possible_entries = [
"filename",
"filecontent",
"hashes",
]
for entry in possible_entries:
if entry in patterns:
entry_list = []
for reg in patterns[entry]:
entry_list.append(reg.replace(reg_temp, version_regex))
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.

The resulting smaller json is written to js_library_patterns.json in the same directory. This smaller file will be read from to make the checks later on."""
retirejs = download_database()
if retirejs is not None:
cleaned = strip_irrelevant_data(retirejs)
path = ConfigManager().get_data_dir_path() / "infoextractors"
path.mkdir(parents=True, exist_ok=True)
json_file_path = (
ConfigManager().get_data_dir_path() / "infoextractors" / "js_library_patterns.json"
)
with open(json_file_path, "w") as f:
json.dump(cleaned, f, indent=4)
return "Update complete."
return "No update occurred."


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


def load_db():
js_lib_file = (
ConfigManager().get_data_dir_path() / "infoextractors" / "js_library_patterns.json"
)

try:
with open(js_lib_file, "r") as regex:
database = json.load(regex)
except FileNotFoundError:
logger.warning(
"Javascript library pattern database database could not be loaded. Run `surfactant plugin update-db js_file` to fetch the pattern database."
)
return None
return database


js_lib_database = load_db()
Loading
Loading