From ff9867f8805dabee950ea0c06ff6b2dcd121759d Mon Sep 17 00:00:00 2001 From: J08nY Date: Thu, 22 Sep 2022 17:25:49 +0200 Subject: Fix regex matching wronng substrings. Fixes #255. --- sec_certs/cert_rules.py | 18 +++++++++++++++--- sec_certs/utils/extract.py | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/sec_certs/cert_rules.py b/sec_certs/cert_rules.py index cbc9bc80..f393b321 100644 --- a/sec_certs/cert_rules.py +++ b/sec_certs/cert_rules.py @@ -4,8 +4,6 @@ from typing import Dict, Final, List, Set, Tuple import yaml -REGEXEC_SEP = r"[ ,;\]”)(]" - # This ignores ACM and AMA SARs that are present in CC version 2 SARS_IMPLIED_FROM_EAL: Dict[str, Set[Tuple[str, int]]] = { "EAL1": { @@ -180,6 +178,13 @@ SARS_IMPLIED_FROM_EAL: Dict[str, Set[Tuple[str, int]]] = { security_level_csv_scan = r"EAL[1-7]\+?" +REGEXEC_SEP = "[ ,;\\[\\]”\"')(.]" +MATCH_START = "(?P" +MATCH_END = ")" +REGEXEC_SEP_START = f"(?:^|{REGEXEC_SEP})" +REGEXEC_SEP_END = f"(?:$|{REGEXEC_SEP})" + + def _load(): script_dir = Path(__file__).parent filepath = script_dir / "rules.yaml" @@ -192,7 +197,14 @@ def _process(obj, add_sep=True): if isinstance(obj, dict): return {k: _process(v, add_sep=add_sep) for k, v in obj.items()} elif isinstance(obj, list): - return [re.compile(rule + REGEXEC_SEP if add_sep else rule) for rule in obj] + return [ + re.compile( + REGEXEC_SEP_START + MATCH_START + rule + MATCH_END + REGEXEC_SEP_END + if add_sep + else MATCH_START + rule + MATCH_END + ) + for rule in obj + ] rules = _load() diff --git a/sec_certs/utils/extract.py b/sec_certs/utils/extract.py index 84b3756c..b197de4f 100644 --- a/sec_certs/utils/extract.py +++ b/sec_certs/utils/extract.py @@ -673,7 +673,7 @@ def extract_keywords(filepath: Path, search_rules) -> Optional[dict[str, dict[st rule = rules matches = [] for match in rule.finditer(whole_text): - match = match.group() + match = match.group("match") match = normalize_match_string(match) match_len = len(match) @@ -692,7 +692,7 @@ def extract_keywords(filepath: Path, search_rules) -> Optional[dict[str, dict[st def normalize_match_string(match: str) -> str: - match = match.strip().rstrip('];.”":)(,').rstrip(os.sep).replace(" ", " ") + match = match.strip().strip("[];.”\"':)(,").rstrip(os.sep).replace(" ", " ") return "".join(filter(str.isprintable, match)) -- cgit v1.3.1 From e773b275a4cc61881d9073ef66b60201b26e923c Mon Sep 17 00:00:00 2001 From: J08nY Date: Thu, 22 Sep 2022 17:38:43 +0200 Subject: Use multiline regex flag. --- sec_certs/cert_rules.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sec_certs/cert_rules.py b/sec_certs/cert_rules.py index f393b321..034baa70 100644 --- a/sec_certs/cert_rules.py +++ b/sec_certs/cert_rules.py @@ -201,7 +201,8 @@ def _process(obj, add_sep=True): re.compile( REGEXEC_SEP_START + MATCH_START + rule + MATCH_END + REGEXEC_SEP_END if add_sep - else MATCH_START + rule + MATCH_END + else MATCH_START + rule + MATCH_END, + re.MULTILINE, ) for rule in obj ] -- cgit v1.3.1