diff options
| author | Ján Jančár | 2022-09-22 17:53:51 +0200 |
|---|---|---|
| committer | GitHub | 2022-09-22 17:53:51 +0200 |
| commit | f3ae013ef1e3ee7299eee92e97c3dcae9b481f14 (patch) | |
| tree | 919bd1db7ca39bf17f1601586902ca2adff4f46e | |
| parent | 6557c5873dbb4a8c481e210eef2625ab2803f6e2 (diff) | |
| parent | e773b275a4cc61881d9073ef66b60201b26e923c (diff) | |
| download | sec-certs-f3ae013ef1e3ee7299eee92e97c3dcae9b481f14.tar.gz sec-certs-f3ae013ef1e3ee7299eee92e97c3dcae9b481f14.tar.zst sec-certs-f3ae013ef1e3ee7299eee92e97c3dcae9b481f14.zip | |
Merge pull request #264 from crocs-muni/fix/fix-regex-issues
Fix regex matching wrong substrings.
| -rw-r--r-- | sec_certs/cert_rules.py | 19 | ||||
| -rw-r--r-- | sec_certs/utils/extract.py | 4 |
2 files changed, 18 insertions, 5 deletions
diff --git a/sec_certs/cert_rules.py b/sec_certs/cert_rules.py index cbc9bc80..034baa70 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,15 @@ 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, + re.MULTILINE, + ) + 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)) |
