Skip to content

Commit

Permalink
Merge pull request #52 from ajinabraham/3.1.3
Browse files Browse the repository at this point in the history
Update PatternMatcher and ChoiceMatcher internal apis
  • Loading branch information
ajinabraham authored Nov 14, 2024
2 parents 18cb1b0 + 192b564 commit 21f98eb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
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 <ajin@opensecurity.in>"]
Expand Down

0 comments on commit 21f98eb

Please sign in to comment.