diff options
| author | J08nY | 2022-09-22 17:25:49 +0200 |
|---|---|---|
| committer | J08nY | 2022-09-22 17:25:49 +0200 |
| commit | ff9867f8805dabee950ea0c06ff6b2dcd121759d (patch) | |
| tree | a61c3a254a23bca8362aeff5363cba95d43c2ad0 | |
| parent | 6557c5873dbb4a8c481e210eef2625ab2803f6e2 (diff) | |
| download | sec-certs-ff9867f8805dabee950ea0c06ff6b2dcd121759d.tar.gz sec-certs-ff9867f8805dabee950ea0c06ff6b2dcd121759d.tar.zst sec-certs-ff9867f8805dabee950ea0c06ff6b2dcd121759d.zip | |
Fix regex matching wronng substrings.
Fixes #255.
| -rw-r--r-- | sec_certs/cert_rules.py | 18 | ||||
| -rw-r--r-- | 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>" +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)) |
