diff --git a/libsast/__init__.py b/libsast/__init__.py index 16a6419..1064561 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.0.1' +__version__ = '3.0.2' __version_info__ = tuple(int(i) for i in __version__.split('.')) __all__ = [ 'Scanner', diff --git a/libsast/core_matcher/matchers.py b/libsast/core_matcher/matchers.py index 51c85da..e16d038 100644 --- a/libsast/core_matcher/matchers.py +++ b/libsast/core_matcher/matchers.py @@ -14,7 +14,7 @@ def get_pos(match): # Cache compiled regex patterns -@lru_cache(maxsize=128) +@lru_cache(maxsize=256) def get_compiled_pattern(pattern): """Compile and cache regex patterns.""" return re.compile(pattern) @@ -23,12 +23,36 @@ def get_compiled_pattern(pattern): class MatchCommand: def __init__(self): self.patterns = {} + # Dictionary to map pattern names to their corresponding classes + self.available_patterns = { + 'Regex': Regex, + 'RegexAnd': RegexAnd, + 'RegexOr': RegexOr, + 'RegexAndNot': RegexAndNot, + 'RegexAndOr': RegexAndOr, + } def _find_match(self, pattern_name, content, rule): - pattern_class = self.patterns.get(pattern_name) or globals()[pattern_name]() + pattern_class = self.patterns.get( + pattern_name) or self._get_pattern_class(pattern_name) self.patterns.setdefault(pattern_name, pattern_class) + + # Apply case transformation if specified in the rule + case = rule.get('input_case') + if case == 'lower': + content = content.lower() + elif case == 'upper': + content = content.upper() + + # Perform search return pattern_class._perform_search(content, rule) + def _get_pattern_class(self, pattern_name): + """Get pattern class from the available patterns dictionary.""" + if pattern_name in self.available_patterns: + return self.available_patterns[pattern_name]() + raise ValueError(f"Pattern '{pattern_name}' is not recognized.") + class MatchStrategy(ABC): @abstractmethod diff --git a/libsast/core_matcher/pattern_matcher.py b/libsast/core_matcher/pattern_matcher.py index c4a62c9..19c802d 100644 --- a/libsast/core_matcher/pattern_matcher.py +++ b/libsast/core_matcher/pattern_matcher.py @@ -103,11 +103,6 @@ def pattern_matcher(self, file_data): try: fmt_data = self._format_content(data, file_path.suffix.lower()) for rule in self.scan_rules: - case = rule.get('input_case') - if case == 'lower': - fmt_data = fmt_data.lower() - elif case == 'upper': - fmt_data = fmt_data.upper() matches = self.matcher._find_match(rule['type'], fmt_data, rule) if matches: results.append({ diff --git a/pyproject.toml b/pyproject.toml index 238aed1..a33e7e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "libsast" -version = "3.0.1" +version = "3.0.2" 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 "]