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

Update PatternMatcher and ChoiceMatcher internal apis #52

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion libsast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 10 additions & 4 deletions libsast/core_matcher/choice_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions libsast/core_matcher/pattern_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions libsast/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down
Loading