From 7af4c50dbac84f0e80d55cfca136d460a61481a4 Mon Sep 17 00:00:00 2001 From: Ajin Abraham Date: Wed, 13 Nov 2024 23:59:00 -0800 Subject: [PATCH 1/2] update PatternMatcher and ChoiceMatcher internal apis --- libsast/__init__.py | 2 +- libsast/core_matcher/choice_matcher.py | 14 ++++++++++---- libsast/core_matcher/pattern_matcher.py | 13 +++++++++---- pyproject.toml | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/libsast/__init__.py b/libsast/__init__.py index 9831a73..bf32368 100644 --- a/libsast/__init__.py +++ b/libsast/__init__.py @@ -12,7 +12,7 @@ __title__ = 'libsast' __authors__ = 'Ajin Abraham' __copyright__ = f'Copyright {year} Ajin Abraham, opensecurity.in' -__version__ = '3.1.2' +__version__ = '3.1.3' __version_info__ = tuple(int(i) for i in __version__.split('.')) __all__ = [ 'Scanner', diff --git a/libsast/core_matcher/choice_matcher.py b/libsast/core_matcher/choice_matcher.py index 4a85424..626b89f 100644 --- a/libsast/core_matcher/choice_matcher.py +++ b/libsast/core_matcher/choice_matcher.py @@ -41,9 +41,9 @@ def scan(self, paths: list) -> dict: def read_file_contents(self, paths: list) -> list: """Load file(s) content.""" - if not (self.scan_rules and paths): - return - self.validate_rules() + if not paths: + return [] + choice_args = [] for rule in self.scan_rules: scan_paths = paths @@ -64,8 +64,14 @@ def read_file_contents(self, paths: list) -> list: futures.append(future) return [future.result() for future in futures] - def regex_scan(self, file_contents) -> list: + def regex_scan(self, file_contents: list, rules=None) -> dict: """Process regex matches on the file contents.""" + if rules: + self.scan_rules = get_rules(rules) + if not (self.scan_rules and file_contents): + return {} + self.validate_rules() + if self.queue: # Use billiard's pool for regex (support queues) from billiard import Pool diff --git a/libsast/core_matcher/pattern_matcher.py b/libsast/core_matcher/pattern_matcher.py index 5836b7b..fd0a3e2 100644 --- a/libsast/core_matcher/pattern_matcher.py +++ b/libsast/core_matcher/pattern_matcher.py @@ -41,9 +41,8 @@ def scan(self, paths: list) -> dict: def read_file_contents(self, paths: list) -> list: """Load file(s) content.""" - if not (self.scan_rules and paths): - return - self.validate_rules() + if not paths: + return [] # Filter files by extension and size, prepare list for processing files_to_scan = { @@ -61,8 +60,14 @@ def read_file_contents(self, paths: list) -> list: self._read_file_content, files_to_scan)) return file_contents - def regex_scan(self, file_contents: list) -> dict: + def regex_scan(self, file_contents: list, rules=None) -> dict: """Scan file(s) content.""" + if rules: + self.scan_rules = get_rules(rules) + if not (self.scan_rules and file_contents): + return {} + self.validate_rules() + if self.queue: # Use billiard's pool for CPU-bound regex (support queues) from billiard import Pool diff --git a/pyproject.toml b/pyproject.toml index fcef574..de50941 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "libsast" -version = "3.1.2" +version = "3.1.3" description = "A generic SAST library built on top of semgrep and regex" keywords = ["libsast", "SAST", "Python SAST", "SAST API", "Regex SAST", "Pattern Matcher"] authors = ["Ajin Abraham "] From 192b5642ed39da102294ff3667be9d7c064fd66a Mon Sep 17 00:00:00 2001 From: Ajin Abraham Date: Thu, 14 Nov 2024 00:06:49 -0800 Subject: [PATCH 2/2] code qa --- libsast/scanner.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libsast/scanner.py b/libsast/scanner.py index 493ef4b..d323e01 100644 --- a/libsast/scanner.py +++ b/libsast/scanner.py @@ -54,7 +54,7 @@ def __init__(self, options: dict, paths: list) -> None: def scan(self) -> dict: """Start Scan.""" results = {} - valid_paths = self.get_scan_files(self.paths) + valid_paths = self.get_scan_files() if not valid_paths: return {} @@ -68,13 +68,13 @@ def scan(self) -> dict: return results - def get_scan_files(self, paths): + def get_scan_files(self): """Get files valid for scanning.""" - if not isinstance(paths, list): + if not isinstance(self.paths, list): raise InvalidPathError('Path should be a list') all_files = set() - for path in paths: + for path in self.paths: pobj = Path(path) if pobj.is_dir(): all_files.update({