From ba8a36b01c3278788f25a970e5bfd78bdeb75309 Mon Sep 17 00:00:00 2001 From: Eric Hibbs Date: Fri, 7 Mar 2025 11:02:12 -0800 Subject: [PATCH 1/2] Now using supported files SDK endpoint for file globs --- pyproject.toml | 2 +- socketsecurity/__init__.py | 2 +- socketsecurity/core/__init__.py | 89 ++++++++++++++++++++++----------- 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 060b69d..6ea62cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ dependencies = [ 'GitPython', 'packaging', 'python-dotenv', - 'socket-sdk-python>=2.0.8' + 'socket-sdk-python>=2.0.9' ] readme = "README.md" description = "Socket Security CLI for CI/CD" diff --git a/socketsecurity/__init__.py b/socketsecurity/__init__.py index c554d9c..96db43f 100644 --- a/socketsecurity/__init__.py +++ b/socketsecurity/__init__.py @@ -1,2 +1,2 @@ __author__ = 'socket.dev' -__version__ = '2.0.10' +__version__ = '2.0.11' diff --git a/socketsecurity/core/__init__.py b/socketsecurity/core/__init__.py index 10548f5..e98a4e4 100644 --- a/socketsecurity/core/__init__.py +++ b/socketsecurity/core/__init__.py @@ -123,8 +123,7 @@ def create_sbom_output(self, diff: Diff) -> dict: log.error(result.get("message", "No error message provided")) return {} - @staticmethod - def find_files(path: str) -> List[str]: + def find_files(self, path: str) -> List[str]: """ Finds supported manifest files in the given path. @@ -138,10 +137,19 @@ def find_files(path: str) -> List[str]: start_time = time.time() files = set() - for ecosystem in socket_globs: - patterns = socket_globs[ecosystem] - for file_name in patterns: - pattern = Core.to_case_insensitive_regex(patterns[file_name]["pattern"]) + # Get supported patterns from the API + try: + patterns = self.get_supported_patterns() + except Exception as e: + log.error(f"Error getting supported patterns from API: {e}") + log.warning("Falling back to local patterns") + from .utils import socket_globs as fallback_patterns + patterns = fallback_patterns + + for ecosystem in patterns: + ecosystem_patterns = patterns[ecosystem] + for file_name in ecosystem_patterns: + pattern = Core.to_case_insensitive_regex(ecosystem_patterns[file_name]["pattern"]) file_path = f"{path}/**/{pattern}" #log.debug(f"Globbing {file_path}") glob_start = time.time() @@ -164,6 +172,53 @@ def find_files(path: str) -> List[str]: log.debug(f"{len(files_list)} Files found ({total_time:.2f}s): {', '.join(files_list)}") return list(files) + def get_supported_patterns(self) -> Dict: + """ + Gets supported file patterns from the Socket API. + + Returns: + Dictionary of supported file patterns + """ + response = self.sdk.report.supported() + if not response: + log.error("Failed to get supported patterns from API") + # Import the old patterns as fallback + from .utils import socket_globs + return socket_globs + + # The response is already in the format we need + return response + + def has_manifest_files(self, files: list) -> bool: + """ + Checks if any files in the list are supported manifest files. + + Args: + files: List of file paths to check + + Returns: + True if any files match manifest patterns, False otherwise + """ + # Get supported patterns + try: + patterns = self.get_supported_patterns() + except Exception as e: + log.error(f"Error getting supported patterns from API: {e}") + log.warning("Falling back to local patterns") + from .utils import socket_globs as fallback_patterns + patterns = fallback_patterns + + for ecosystem in patterns: + ecosystem_patterns = patterns[ecosystem] + for file_name in ecosystem_patterns: + pattern_str = ecosystem_patterns[file_name]["pattern"] + for file in files: + if "\\" in file: + file = file.replace("\\", "/") + if PurePath(file).match(pattern_str): + return True + return False + @staticmethod def to_case_insensitive_regex(input_string: str) -> str: """ @@ -740,28 +795,6 @@ def save_file(file_name: str, content: str) -> None: log.error(f"Failed to save file {file_name}: {e}") raise - @staticmethod - def has_manifest_files(files: list) -> bool: - """ - Checks if any files in the list are supported manifest files. - - Args: - files: List of file paths to check - - Returns: - True if any files match manifest patterns, False otherwise - """ - for ecosystem in socket_globs: - patterns = socket_globs[ecosystem] - for file_name in patterns: - pattern = patterns[file_name]["pattern"] - for file in files: - if "\\" in file: - file = file.replace("\\", "/") - if PurePath(file).match(pattern): - return True - return False - @staticmethod def get_capabilities_for_added_packages(added_packages: Dict[str, Package]) -> Dict[str, List[str]]: """ From 51312ad6921578963db5adcd25cc11fb710d1227 Mon Sep 17 00:00:00 2001 From: Eric Hibbs Date: Fri, 7 Mar 2025 15:09:32 -0800 Subject: [PATCH 2/2] got rid of the general o7 --- socketsecurity/core/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/socketsecurity/core/__init__.py b/socketsecurity/core/__init__.py index e98a4e4..b38358b 100644 --- a/socketsecurity/core/__init__.py +++ b/socketsecurity/core/__init__.py @@ -177,7 +177,7 @@ def get_supported_patterns(self) -> Dict: Gets supported file patterns from the Socket API. Returns: - Dictionary of supported file patterns + Dictionary of supported file patterns with 'general' key removed """ response = self.sdk.report.supported() if not response: @@ -186,6 +186,10 @@ def get_supported_patterns(self) -> Dict: from .utils import socket_globs return socket_globs + # Remove the 'general' key if it exists + if 'general' in response: + response.pop('general') + # The response is already in the format we need return response