Skip to content

Commit a1f9856

Browse files
authored
Merge pull request #48 from ajinabraham/fix_case
Address case match bug
2 parents 06dc004 + 895a0ab commit a1f9856

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

libsast/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
__title__ = 'libsast'
1313
__authors__ = 'Ajin Abraham'
1414
__copyright__ = f'Copyright {year} Ajin Abraham, opensecurity.in'
15-
__version__ = '3.0.1'
15+
__version__ = '3.0.2'
1616
__version_info__ = tuple(int(i) for i in __version__.split('.'))
1717
__all__ = [
1818
'Scanner',

libsast/core_matcher/matchers.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def get_pos(match):
1414

1515

1616
# Cache compiled regex patterns
17-
@lru_cache(maxsize=128)
17+
@lru_cache(maxsize=256)
1818
def get_compiled_pattern(pattern):
1919
"""Compile and cache regex patterns."""
2020
return re.compile(pattern)
@@ -23,12 +23,36 @@ def get_compiled_pattern(pattern):
2323
class MatchCommand:
2424
def __init__(self):
2525
self.patterns = {}
26+
# Dictionary to map pattern names to their corresponding classes
27+
self.available_patterns = {
28+
'Regex': Regex,
29+
'RegexAnd': RegexAnd,
30+
'RegexOr': RegexOr,
31+
'RegexAndNot': RegexAndNot,
32+
'RegexAndOr': RegexAndOr,
33+
}
2634

2735
def _find_match(self, pattern_name, content, rule):
28-
pattern_class = self.patterns.get(pattern_name) or globals()[pattern_name]()
36+
pattern_class = self.patterns.get(
37+
pattern_name) or self._get_pattern_class(pattern_name)
2938
self.patterns.setdefault(pattern_name, pattern_class)
39+
40+
# Apply case transformation if specified in the rule
41+
case = rule.get('input_case')
42+
if case == 'lower':
43+
content = content.lower()
44+
elif case == 'upper':
45+
content = content.upper()
46+
47+
# Perform search
3048
return pattern_class._perform_search(content, rule)
3149

50+
def _get_pattern_class(self, pattern_name):
51+
"""Get pattern class from the available patterns dictionary."""
52+
if pattern_name in self.available_patterns:
53+
return self.available_patterns[pattern_name]()
54+
raise ValueError(f"Pattern '{pattern_name}' is not recognized.")
55+
3256

3357
class MatchStrategy(ABC):
3458
@abstractmethod

libsast/core_matcher/pattern_matcher.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ def pattern_matcher(self, file_data):
103103
try:
104104
fmt_data = self._format_content(data, file_path.suffix.lower())
105105
for rule in self.scan_rules:
106-
case = rule.get('input_case')
107-
if case == 'lower':
108-
fmt_data = fmt_data.lower()
109-
elif case == 'upper':
110-
fmt_data = fmt_data.upper()
111106
matches = self.matcher._find_match(rule['type'], fmt_data, rule)
112107
if matches:
113108
results.append({

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "libsast"
3-
version = "3.0.1"
3+
version = "3.0.2"
44
description = "A generic SAST library built on top of semgrep and regex"
55
keywords = ["libsast", "SAST", "Python SAST", "SAST API", "Regex SAST", "Pattern Matcher"]
66
authors = ["Ajin Abraham <[email protected]>"]

0 commit comments

Comments
 (0)