Skip to content

Commit 431d70d

Browse files
committed
Fixed Grafana pattern, was giving 'too large' error
1 parent 4434cd7 commit 431d70d

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

testing/test.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from threading import Lock
2323
from random import randbytes
2424
from base64 import b64encode
25+
from itertools import zip_longest
2526
from tqdm import tqdm
2627

2728

@@ -111,9 +112,10 @@ def parse_patterns(patterns_dir: str, include: Optional[list[str]] = None, exclu
111112
return patterns
112113

113114

114-
def hs_compile(db: hyperscan.Database, regex: Union[str | list[str] | bytes | list[bytes]]) -> bool:
115+
def hs_compile(db: hyperscan.Database, regex: Union[str | list[str] | bytes | list[bytes]], labels: list[str] = None) -> bool:
115116
"""Compile one or more hyperscan regexes into the given database."""
116117
regex_bytes: list[bytes]
118+
labels = labels if labels is not None else []
117119

118120
if isinstance(regex, str):
119121
regex_bytes = [regex.encode('utf-8')]
@@ -136,12 +138,12 @@ def hs_compile(db: hyperscan.Database, regex: Union[str | list[str] | bytes | li
136138
try:
137139
db.compile(regex_bytes, flags=hyperscan.HS_FLAG_SOM_LEFTMOST|hyperscan.HS_FLAG_UTF8)
138140
except hyperscan.error:
139-
LOG.debug("Failed to compile a rule with 'HS_FLAG_SOM_LEFTMOST': %s", str(regex_bytes))
140-
for regex in regex_bytes:
141+
LOG.debug("Failed to compile a rule: %s", str(regex_bytes))
142+
for label, regex in zip_longest(labels, regex_bytes):
141143
try:
142144
db.compile([regex], flags=hyperscan.HS_FLAG_SOM_LEFTMOST|hyperscan.HS_FLAG_UTF8)
143145
except hyperscan.error as err:
144-
LOG.error("❌ Failed to compile %s with 'leftmost' flag: %s", str(regex), err)
146+
LOG.error("❌ Failed to compile %s%s: %s", str(regex), ' (' + label + ')' if label is not None else '', err)
145147

146148
return False
147149

@@ -299,7 +301,7 @@ def test_patterns(tests_path: str, include: Optional[list[str]] = None, exclude:
299301

300302
found_patterns = True
301303

302-
if not hs_compile(db, [pattern.regex_string() for pattern in patterns]):
304+
if not hs_compile(db, [pattern.regex_string() for pattern in patterns], labels=[pattern.type for pattern in patterns]):
303305
if not quiet:
304306
LOG.error("❌ hyperscan pattern compilation error in '%s'", rel_dirpath)
305307
exit(1)
@@ -384,7 +386,7 @@ def dry_run_patterns(tests_path: str, extra_directory: str, include: Optional[li
384386
LOG.error("❌ Failed to find any patterns in %s", str(tests_path))
385387
return
386388

387-
if not hs_compile(db, [pattern.regex_string() for pattern in patterns]):
389+
if not hs_compile(db, [pattern.regex_string() for pattern in patterns], labels=[pattern.type for pattern in patterns]):
388390
if not quiet:
389391
LOG.error("❌ hyperscan pattern compilation error in '%s'", dirpath)
390392
exit(1)
@@ -435,7 +437,7 @@ def random_test_patterns(tests_path: str, include: Optional[list[str]], exclude:
435437
if PATTERNS_FILENAME in filenames:
436438
patterns.extend(parse_patterns(dirpath, include=include, exclude=exclude))
437439

438-
if not hs_compile(db, [pattern.regex_string() for pattern in patterns]):
440+
if not hs_compile(db, [pattern.regex_string() for pattern in patterns], labels=[pattern.type for pattern in patterns]):
439441
if not quiet:
440442
LOG.error("❌ hyperscan pattern compilation error in '%s'", dirpath)
441443
exit(1)

vendors/patterns.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ patterns:
1818
type: grafana_api_token
1919
regex:
2020
pattern: |
21-
eyJrIjoi(?i)[a-z0-9-_=]{42}
21+
eyJrIjoi[A-Za-z0-9_=-]{42}
2222
expected:
2323
- name: grafana.txt
2424
start_offset: 107

0 commit comments

Comments
 (0)