Skip to content

Commit

Permalink
Merge pull request #48 from ajinabraham/fix_case
Browse files Browse the repository at this point in the history
Address case match bug
  • Loading branch information
ajinabraham authored Nov 4, 2024
2 parents 06dc004 + 895a0ab commit a1f9856
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 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.0.1'
__version__ = '3.0.2'
__version_info__ = tuple(int(i) for i in __version__.split('.'))
__all__ = [
'Scanner',
Expand Down
28 changes: 26 additions & 2 deletions libsast/core_matcher/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions libsast/core_matcher/pattern_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
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.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 <ajin@opensecurity.in>"]
Expand Down

0 comments on commit a1f9856

Please sign in to comment.