grootveil 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- grootveil/__init__.py +42 -0
- grootveil/actions/__init__.py +0 -0
- grootveil/actions/base.py +33 -0
- grootveil/actions/encrypt.py +32 -0
- grootveil/actions/hash.py +12 -0
- grootveil/actions/keep.py +6 -0
- grootveil/actions/mask.py +24 -0
- grootveil/actions/pseudonymize.py +23 -0
- grootveil/actions/redact.py +6 -0
- grootveil/actions/registry.py +27 -0
- grootveil/actions/replace.py +8 -0
- grootveil/core/__init__.py +0 -0
- grootveil/core/errors.py +2 -0
- grootveil/core/labels.py +40 -0
- grootveil/detectors/__init__.py +0 -0
- grootveil/detectors/base.py +11 -0
- grootveil/detectors/deny_list.py +27 -0
- grootveil/detectors/model.py +129 -0
- grootveil/detectors/rules/__init__.py +14 -0
- grootveil/detectors/rules/address.py +50 -0
- grootveil/detectors/rules/checks.py +18 -0
- grootveil/detectors/rules/contact.py +114 -0
- grootveil/detectors/rules/dates.py +127 -0
- grootveil/detectors/rules/detector.py +77 -0
- grootveil/detectors/rules/financial.py +96 -0
- grootveil/detectors/rules/ids/__init__.py +20 -0
- grootveil/detectors/rules/ids/americas.py +50 -0
- grootveil/detectors/rules/ids/asia_pacific.py +148 -0
- grootveil/detectors/rules/ids/documents.py +48 -0
- grootveil/detectors/rules/ids/europe.py +219 -0
- grootveil/detectors/rules/ids/germany.py +84 -0
- grootveil/detectors/rules/ids/middle_east_africa.py +85 -0
- grootveil/detectors/rules/rule.py +37 -0
- grootveil/detectors/rules/secrets.py +74 -0
- grootveil/models/__init__.py +0 -0
- grootveil/models/action_config.py +24 -0
- grootveil/models/finding.py +14 -0
- grootveil/models/item.py +14 -0
- grootveil/models/protected.py +21 -0
- grootveil/py.typed +0 -0
- grootveil/resolvers/__init__.py +0 -0
- grootveil/resolvers/address_prefix.py +64 -0
- grootveil/resolvers/join.py +30 -0
- grootveil/resolvers/overlaps.py +42 -0
- grootveil/resolvers/register.py +33 -0
- grootveil/resolvers/words.py +14 -0
- grootveil/services/__init__.py +0 -0
- grootveil/services/applier.py +26 -0
- grootveil/services/resolver.py +35 -0
- grootveil/services/vault.py +40 -0
- grootveil/shield.py +257 -0
- grootveil-2.0.0.dist-info/METADATA +198 -0
- grootveil-2.0.0.dist-info/RECORD +54 -0
- grootveil-2.0.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""A written date: 03.04.1985, 1985-04-03, 4/3/1985, 3 April 1985, April 3rd, 1985, 3. März 1985."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
from grootveil.detectors.rules.checks import date_ok
|
|
6
|
+
from grootveil.detectors.rules.rule import Rule
|
|
7
|
+
|
|
8
|
+
# English and German month names and their short forms, lower case -> the month's number
|
|
9
|
+
MONTHS = {
|
|
10
|
+
name: number
|
|
11
|
+
for number, names in enumerate(
|
|
12
|
+
(
|
|
13
|
+
"january januar jan", "february februar feb", "march märz maerz mar mär", "april apr", "may mai",
|
|
14
|
+
"june juni jun", "july juli jul", "august aug", "september sept sep", "october oktober oct okt",
|
|
15
|
+
"november nov", "december dezember dec dez",
|
|
16
|
+
),
|
|
17
|
+
start=1,
|
|
18
|
+
)
|
|
19
|
+
for name in names.split()
|
|
20
|
+
} # fmt: skip
|
|
21
|
+
# Arabic month names (the Gregorian ones used in Saudi Arabia, the Emirates, Egypt), with and without hamza
|
|
22
|
+
MONTHS |= {
|
|
23
|
+
name: number
|
|
24
|
+
for number, names in enumerate(
|
|
25
|
+
(
|
|
26
|
+
"يناير", "فبراير", "مارس", "أبريل ابريل إبريل", "مايو", "يونيو", "يوليو", "أغسطس اغسطس", "سبتمبر",
|
|
27
|
+
"أكتوبر اكتوبر", "نوفمبر", "ديسمبر",
|
|
28
|
+
),
|
|
29
|
+
start=1,
|
|
30
|
+
)
|
|
31
|
+
for name in names.split()
|
|
32
|
+
} # fmt: skip
|
|
33
|
+
MONTH = "(?:" + "|".join(sorted(MONTHS, key=len, reverse=True)) + ")"
|
|
34
|
+
NUMBERS = re.compile(r"\d+")
|
|
35
|
+
WORD = re.compile(r"[^\W\d_]+")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def year_ok(year: int) -> bool:
|
|
39
|
+
return 1900 <= year <= 2099
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def numeric_date(value: str) -> bool:
|
|
43
|
+
"""Year first (1985-04-03, 1985/4/3, 2024-07-15T10:30:00Z), or day and month in either order (03.04.1985,
|
|
44
|
+
4/3/1985): a day that exists, in 1900 to 2099. A two-digit year counts as 19xx or 20xx, whichever gives a real
|
|
45
|
+
day. A time after the date is not checked."""
|
|
46
|
+
a, b, c = (int(n) for n in NUMBERS.findall(value)[:3])
|
|
47
|
+
if a > 31: # starts with the year
|
|
48
|
+
return year_ok(a) and date_ok(a, b, c)
|
|
49
|
+
years = (c,) if c > 99 else (1900 + c, 2000 + c)
|
|
50
|
+
return any(year_ok(y) and (date_ok(y, b, a) or date_ok(y, a, b)) for y in years)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def month_name_date(value: str) -> bool:
|
|
54
|
+
"""A day, a month name and a year (3 April 1985, April 3rd, 1985, 3. April 1985, 03-APR-1985, 3 أبريل 1985): a
|
|
55
|
+
day that exists, in 1900 to 2099. The day is the first number, the year the last; a two-digit year counts as 19xx
|
|
56
|
+
or 20xx."""
|
|
57
|
+
numbers = [int(n) for n in NUMBERS.findall(value)]
|
|
58
|
+
day, year = numbers[0], numbers[-1]
|
|
59
|
+
month = next(MONTHS[word] for word in WORD.findall(value.lower()) if word in MONTHS) # not "st", "nd", "rd", "th"
|
|
60
|
+
years = (year,) if year > 99 else (1900 + year, 2000 + year)
|
|
61
|
+
return any(year_ok(y) and date_ok(y, month, day) for y in years)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def short_date(value: str) -> bool:
|
|
65
|
+
"""03.04.85 (as numeric_date), or a month and a year: 08/27, 08/2027, APR-2027 (a card's expiry)."""
|
|
66
|
+
numbers = [int(n) for n in NUMBERS.findall(value)]
|
|
67
|
+
if len(numbers) == 3:
|
|
68
|
+
return numeric_date(value)
|
|
69
|
+
names = [MONTHS[word] for word in WORD.findall(value.lower()) if word in MONTHS]
|
|
70
|
+
month, year = (names[0], numbers[0]) if names else (numbers[0], numbers[1])
|
|
71
|
+
return 1 <= month <= 12 and (year <= 99 or year_ok(year))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# No everyday words ("on", "am", "since"): with one of those near it, every 1.2.24 in a text would be a date
|
|
75
|
+
DATE_WORDS = (
|
|
76
|
+
"date", "dated", "dob", "born", "birth", "birthday", "birthdate", "due", "expires", "expiry", "exp", "issued",
|
|
77
|
+
"valid until", "valid thru", "valid from",
|
|
78
|
+
"datum", "geboren", "geb", "geburtsdatum", "geburtstag", "fällig", "gültig", "eintrittsdatum", "austrittsdatum",
|
|
79
|
+
"ausstellungsdatum", "ablaufdatum", "rechnungsdatum", "lieferdatum",
|
|
80
|
+
"تاريخ", "بتاريخ", "الميلاد", "انتهاء", "صلاحية",
|
|
81
|
+
) # fmt: skip
|
|
82
|
+
|
|
83
|
+
# "version 1.2.2024", "section 3.4.21": a number of that shape right after one of these words is not a date
|
|
84
|
+
NOT_A_DATE = r"\b(?:version|ver\.?|v|build|release|section|sec\.?|chapter|abschnitt|kapitel|ziffer)\s*[:=#]?\s*"
|
|
85
|
+
|
|
86
|
+
RULES = [
|
|
87
|
+
Rule(
|
|
88
|
+
"date_numeric",
|
|
89
|
+
"DATE",
|
|
90
|
+
# 1985-04-03, 1985/4/3, a date with its time (2024-07-15T10:30:00Z, 2024-07-15T10:30+02:00), or 03.04.1985,
|
|
91
|
+
# 3/4/1985, 03-04-1985 with a four-digit year. Not inside a longer number, a version or an IP address (no
|
|
92
|
+
# digit, letter, dot, dash or slash right next to it)
|
|
93
|
+
r"(?<![\w./-])(?:\d{4}-\d{1,2}-\d{1,2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?"
|
|
94
|
+
r"|\d{4}([/-])\d{1,2}\1\d{1,2}|\d{1,2}([./-])\d{1,2}\2\d{4})(?![\w/-]|\.\d)",
|
|
95
|
+
0.6,
|
|
96
|
+
DATE_WORDS,
|
|
97
|
+
0.9,
|
|
98
|
+
validator=numeric_date,
|
|
99
|
+
not_after=NOT_A_DATE,
|
|
100
|
+
),
|
|
101
|
+
Rule(
|
|
102
|
+
"date_short_year",
|
|
103
|
+
"DATE",
|
|
104
|
+
# 03.04.85 (also a version or a section number), or a month and a year: 08/27, 08/2027, APR-2027 (a card's
|
|
105
|
+
# expiry; also a fraction or a page). Only with a context word
|
|
106
|
+
rf"(?<![\w./-])(?:\d{{1,2}}([./-])\d{{1,2}}\1\d{{2}}|(?:0?[1-9]|1[0-2])/(?:\d{{4}}|\d{{2}})|{MONTH}-\d{{4}})(?![\w/-]|\.\d)",
|
|
107
|
+
0.3,
|
|
108
|
+
DATE_WORDS,
|
|
109
|
+
0.75,
|
|
110
|
+
validator=short_date,
|
|
111
|
+
flags=re.IGNORECASE,
|
|
112
|
+
not_after=NOT_A_DATE,
|
|
113
|
+
),
|
|
114
|
+
Rule(
|
|
115
|
+
"date_month_name",
|
|
116
|
+
"DATE",
|
|
117
|
+
# 3 April 1985, 3rd April 1985, 3. April 1985 (German), April 3, 1985, Apr. 3rd 1985, 03-APR-1985, 03-APR-85,
|
|
118
|
+
# 3 أبريل 1985 (Arabic; Arabic-Indic digits too: \d takes them)
|
|
119
|
+
rf"\b\d{{1,2}}(?:st|nd|rd|th|\.)? {MONTH}\.?,? \d{{4}}\b|\b{MONTH}\.? \d{{1,2}}(?:st|nd|rd|th)?,? \d{{4}}\b"
|
|
120
|
+
rf"|\b\d{{1,2}}-{MONTH}-(?:\d{{4}}|\d{{2}})\b",
|
|
121
|
+
0.7,
|
|
122
|
+
DATE_WORDS,
|
|
123
|
+
0.9,
|
|
124
|
+
validator=month_name_date,
|
|
125
|
+
flags=re.IGNORECASE,
|
|
126
|
+
),
|
|
127
|
+
]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""The rules as a detector, and the default list.
|
|
2
|
+
|
|
3
|
+
A match gets `score`; with a context word (whole word, any case) near it it gets `context_score`. Near = up to 60
|
|
4
|
+
characters before it (a label on the line above counts: "Geburtsdatum:\n03.04.1985") and up to 30 after it on its own
|
|
5
|
+
line. Never the next line: that names the next value ("Ausweisnummer: X\nReisepass-Nr.: Y" made X a passport).
|
|
6
|
+
A validator returns False to drop a match. Low scores (below the default threshold 0.5) mean "only with context".
|
|
7
|
+
|
|
8
|
+
A caller's extra rules can hold any regex, and one like (a|aa)+$ can run for minutes on a long text. So they run with a
|
|
9
|
+
time limit per rule and text (GROOTVEIL_REGEX_TIMEOUT seconds, default 1, as Presidio's REGEX_TIMEOUT_SECONDS): a rule
|
|
10
|
+
over it is skipped for that text with a warning, its matches found before it stay. Python's re has no time limit, so
|
|
11
|
+
these rules run on the regex package (same patterns, same matches). Our own rules are fixed and tested: no limit.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
import os
|
|
16
|
+
import re
|
|
17
|
+
from collections.abc import Iterator
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
import regex
|
|
21
|
+
|
|
22
|
+
from grootveil.detectors.rules import address, contact, dates, financial, ids, secrets
|
|
23
|
+
from grootveil.detectors.rules.rule import Rule
|
|
24
|
+
from grootveil.models.finding import Finding
|
|
25
|
+
|
|
26
|
+
LOG = logging.getLogger("grootveil")
|
|
27
|
+
EXTRA_RULE_TIMEOUT = float(os.environ.get("GROOTVEIL_REGEX_TIMEOUT") or 1)
|
|
28
|
+
BEFORE, AFTER = 60, 30
|
|
29
|
+
# Order matters only when two rules give the same span the same score: the first one wins.
|
|
30
|
+
DEFAULT_RULES = [*contact.RULES, *financial.RULES, *ids.RULES, *secrets.RULES, *dates.RULES, *address.RULES]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def context_window(lower: str, start: int, end: int) -> str:
|
|
34
|
+
return lower[max(0, start - BEFORE) : start] + " " + lower[end : end + AFTER].split("\n", 1)[0]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RuleDetector:
|
|
38
|
+
"""Every finding says which rule, whether a check digit passed and whether a context word was near it."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, rules: list[Rule] = DEFAULT_RULES, timeout: float | None = None):
|
|
41
|
+
"""timeout: seconds per rule and text (the regex package runs the rules); None: no limit (Python's re)."""
|
|
42
|
+
self.rules, self.timeout = rules, timeout
|
|
43
|
+
self.limited = [limited(r) for r in rules] if timeout else []
|
|
44
|
+
|
|
45
|
+
def detect(self, texts: list[str]) -> list[list[Finding]]:
|
|
46
|
+
return [self.detect_one(t) for t in texts]
|
|
47
|
+
|
|
48
|
+
def detect_one(self, text: str) -> list[Finding]:
|
|
49
|
+
lower, out = text.lower(), []
|
|
50
|
+
for i, r in enumerate(self.rules):
|
|
51
|
+
matches: Iterator[Any] = (
|
|
52
|
+
self.limited[i].finditer(text, timeout=self.timeout) if self.timeout else r.compiled.finditer(text)
|
|
53
|
+
)
|
|
54
|
+
try:
|
|
55
|
+
for m in matches:
|
|
56
|
+
start, end = m.span(r.group)
|
|
57
|
+
if start == end or (r.validator and not r.validator(m.group(r.group))):
|
|
58
|
+
continue
|
|
59
|
+
if r.not_after_re and r.not_after_re.search(lower[max(0, start - BEFORE) : start]):
|
|
60
|
+
continue
|
|
61
|
+
score, why = r.score, [f"rule {r.name}"]
|
|
62
|
+
if r.validator:
|
|
63
|
+
why.append(f"{r.validator.__name__} check passed")
|
|
64
|
+
if r.context_re and r.context_re.search(context_window(lower, start, end)):
|
|
65
|
+
score = r.context_score if r.context_score is not None else min(1.0, max(r.score + 0.35, 0.4))
|
|
66
|
+
why.append("context word near it")
|
|
67
|
+
out.append(Finding(r.label, start, end, round(score, 4), f"rule:{r.name}", ", ".join(why)))
|
|
68
|
+
except TimeoutError:
|
|
69
|
+
LOG.warning(
|
|
70
|
+
"rule %s ran over %s s on a text of %d characters: skipped", r.name, self.timeout, len(text)
|
|
71
|
+
)
|
|
72
|
+
return out
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def limited(r: Rule) -> regex.Pattern:
|
|
76
|
+
"""The rule's regex on the regex package. Flags by name: re and regex number some differently (ASCII)."""
|
|
77
|
+
return regex.compile(r.regex, sum(getattr(regex, f.name) for f in re.RegexFlag(r.flags) if f.name))
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Money: payment card, IBAN."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
from grootveil.detectors.rules.checks import luhn_ok
|
|
6
|
+
from grootveil.detectors.rules.rule import Rule
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def luhn(value: str) -> bool:
|
|
10
|
+
digits = [int(c) for c in value if c.isdigit()]
|
|
11
|
+
return 13 <= len(digits) <= 19 and luhn_ok(digits)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# IBAN length of every country in the IBAN registry (ISO 13616, SWIFT release 2025)
|
|
15
|
+
IBAN_LENGTH = {
|
|
16
|
+
"AD": 24, "AE": 23, "AL": 28, "AT": 20, "AZ": 28, "BA": 20, "BE": 16, "BG": 22, "BH": 22, "BI": 27, "BR": 29,
|
|
17
|
+
"BY": 28, "CH": 21, "CR": 22, "CY": 28, "CZ": 24, "DE": 22, "DJ": 27, "DK": 18, "DO": 28, "EE": 20, "EG": 29,
|
|
18
|
+
"ES": 24, "FI": 18, "FK": 18, "FO": 18, "FR": 27, "GB": 22, "GE": 22, "GI": 23, "GL": 18, "GR": 27, "GT": 28,
|
|
19
|
+
"HN": 28, "HR": 21, "HU": 28, "IE": 22, "IL": 23, "IQ": 23, "IS": 26, "IT": 27, "JO": 30, "KW": 30, "KZ": 20,
|
|
20
|
+
"LB": 28, "LC": 32, "LI": 21, "LT": 20, "LU": 20, "LV": 21, "LY": 25, "MC": 27, "MD": 24, "ME": 22, "MK": 19,
|
|
21
|
+
"MN": 20, "MR": 27, "MT": 31, "MU": 30, "NI": 28, "NL": 18, "NO": 15, "OM": 23, "PK": 24, "PL": 28, "PS": 29,
|
|
22
|
+
"PT": 25, "QA": 29, "RO": 24, "RS": 22, "RU": 33, "SA": 24, "SC": 31, "SD": 18, "SE": 24, "SI": 19, "SK": 24,
|
|
23
|
+
"SM": 27, "SO": 23, "ST": 25, "SV": 28, "TL": 23, "TN": 24, "TR": 26, "UA": 29, "VA": 22, "VG": 24, "XK": 20,
|
|
24
|
+
"YE": 30,
|
|
25
|
+
} # fmt: skip
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def mod97(value: str) -> bool:
|
|
29
|
+
"""The IBAN check digits (ISO 7064 mod 97-10), any length from 15 to 34."""
|
|
30
|
+
v = re.sub(r"[ -]", "", value).upper()
|
|
31
|
+
if not 15 <= len(v) <= 34:
|
|
32
|
+
return False
|
|
33
|
+
return int("".join(str(int(c, 36)) for c in v[4:] + v[:4])) % 97 == 1
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def iban(value: str) -> bool:
|
|
37
|
+
"""Valid check digits and the length of its country (DE: 22, GB: 22, FR: 27, SA: 24)."""
|
|
38
|
+
v = re.sub(r"[ -]", "", value).upper()
|
|
39
|
+
return IBAN_LENGTH.get(v[:2]) == len(v) and mod97(v)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def iban_other_length(value: str) -> bool:
|
|
43
|
+
"""Valid check digits, but not the length of its country (or a country with no IBAN). Kept, with a lower score:
|
|
44
|
+
on our English test set 3 of 5 IBANs written as someone's account had a wrong length (2026-11-20)."""
|
|
45
|
+
v = re.sub(r"[ -]", "", value).upper()
|
|
46
|
+
return IBAN_LENGTH.get(v[:2]) != len(v) and mod97(v)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
BANK_ACCOUNT_WORDS = (
|
|
50
|
+
"bank account", "account number", "account no", "acct no", "acct number", "checking account", "savings account",
|
|
51
|
+
"kontonummer", "konto-nr",
|
|
52
|
+
) # fmt: skip
|
|
53
|
+
IBAN_REGEX = r"\b[A-Z]{2}\d{2}(?:[ -]?[A-Z0-9]{4}){2,7}(?:[ -]?[A-Z0-9]{1,4})?\b"
|
|
54
|
+
|
|
55
|
+
RULES = [
|
|
56
|
+
Rule(
|
|
57
|
+
"card",
|
|
58
|
+
"CARD_NUMBER",
|
|
59
|
+
# no card number starts with 0 ("Tel. 030 12345678 10115 Berlin") or right after a + (an international phone
|
|
60
|
+
# number, +49 160 35503518); \d: Arabic-Indic digits too (٥٥٣٤ ٥٥٨٧ ...)
|
|
61
|
+
r"(?<!\+)\b(?![0٠۰])\d[ -]?(?:\d[ -]?){11,17}\d\b",
|
|
62
|
+
0.9, # only Luhn-valid numbers get here
|
|
63
|
+
("card", "visa", "mastercard", "amex"),
|
|
64
|
+
validator=luhn,
|
|
65
|
+
# an IMEI has a Luhn check digit too, and one policy number in ten passes by chance: "device identifier
|
|
66
|
+
# 234010987654321", "IMEI: 490154203237518", "Policy Number: 030-1234567891"
|
|
67
|
+
not_after=r"\b(?:imei|identifier|device id|policy number|policy no\.?|policennummer)\s*[:=#,]?\s*",
|
|
68
|
+
),
|
|
69
|
+
Rule(
|
|
70
|
+
"iban",
|
|
71
|
+
"BANK_ACCOUNT",
|
|
72
|
+
IBAN_REGEX,
|
|
73
|
+
1.0, # only mod-97-valid IBANs of their country's length get here: beats the model, which has no IBAN data
|
|
74
|
+
("iban", "account", "bank"),
|
|
75
|
+
validator=iban,
|
|
76
|
+
),
|
|
77
|
+
Rule(
|
|
78
|
+
"iban_other_length",
|
|
79
|
+
"BANK_ACCOUNT",
|
|
80
|
+
IBAN_REGEX,
|
|
81
|
+
0.8, # valid check digits, wrong length for the country: still found, but a sure model finding wins
|
|
82
|
+
("iban", "account", "bank"),
|
|
83
|
+
0.9,
|
|
84
|
+
validator=iban_other_length,
|
|
85
|
+
),
|
|
86
|
+
Rule(
|
|
87
|
+
"bank_account_number",
|
|
88
|
+
"BANK_ACCOUNT",
|
|
89
|
+
r"(?<![\w+])\d{8,17}(?!\w)", # an account number with no IBAN (US, UK, ...): no check digit, any 8 to 17 digits
|
|
90
|
+
0.1,
|
|
91
|
+
BANK_ACCOUNT_WORDS,
|
|
92
|
+
0.6,
|
|
93
|
+
# the bank's own number, next to an account number: "account number 12345678, routing number 021000021"
|
|
94
|
+
not_after=r"\b(?:routing (?:number|no)|aba(?: number)?|bankleitzahl|blz)\b(?:\s+is)?[\W_]{0,6}",
|
|
95
|
+
),
|
|
96
|
+
]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Numbers a state gives a person: social security, national ID, tax ID, passport, identity card, driving licence.
|
|
2
|
+
|
|
3
|
+
One file per region (`americas.py`, `asia_pacific.py`, `europe.py`, `germany.py`, `middle_east_africa.py`), each with
|
|
4
|
+
its rules in `RULES` and their validators and context words; `documents.py` holds the passport and driving licence
|
|
5
|
+
rules, which are the same for every country. A new country goes in the file of its region.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from grootveil.detectors.rules.ids import americas, asia_pacific, documents, europe, germany, middle_east_africa
|
|
9
|
+
|
|
10
|
+
# Order matters only when two rules give the same span the same score (the first one wins): the passport rule stays
|
|
11
|
+
# before the German identity card, and that one before the driving licence.
|
|
12
|
+
RULES = [
|
|
13
|
+
*americas.RULES,
|
|
14
|
+
*asia_pacific.RULES,
|
|
15
|
+
*middle_east_africa.RULES,
|
|
16
|
+
*europe.RULES,
|
|
17
|
+
documents.PASSPORT,
|
|
18
|
+
*germany.RULES,
|
|
19
|
+
documents.DRIVING_LICENCE,
|
|
20
|
+
]
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""ID numbers of the United States and Canada."""
|
|
2
|
+
|
|
3
|
+
from grootveil.detectors.rules.checks import luhn_ok
|
|
4
|
+
from grootveil.detectors.rules.rule import Rule
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def canada_sin(value: str) -> bool:
|
|
8
|
+
"""A Canadian Social Insurance Number: 9 digits, the last one a Luhn check digit; none starts with 0 or 8."""
|
|
9
|
+
d = [int(c) for c in value if c.isdigit()]
|
|
10
|
+
return d[0] not in (0, 8) and luhn_ok(d)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
US_ITIN_WORDS = ("itin", "individual taxpayer identification number", "taxpayer identification number", "tax id")
|
|
14
|
+
CANADA_SIN_WORDS = (
|
|
15
|
+
"social insurance number", "social insurance", "sin number", "sin no", "sin",
|
|
16
|
+
"numéro d'assurance sociale", "nas",
|
|
17
|
+
) # fmt: skip
|
|
18
|
+
|
|
19
|
+
RULES = [
|
|
20
|
+
Rule(
|
|
21
|
+
"us_ssn",
|
|
22
|
+
"NATIONAL_ID",
|
|
23
|
+
# 123-45-6789, 123 45 6789, 123.45.6789, 123456789 (the same separator twice; the bare 9 digits only reach the
|
|
24
|
+
# threshold with a context word, as every other form)
|
|
25
|
+
r"(?<![\w.+-])\d{3}([ .-]?)\d{2}\1\d{4}(?![\w-]|\.\d)",
|
|
26
|
+
0.4,
|
|
27
|
+
# no check for never-issued numbers (area 000 or 666, group 00): tried 2026-11-11, on our English test it
|
|
28
|
+
# dropped 15 numbers written as someone's SSN to remove 1 false alarm. A missed SSN costs more
|
|
29
|
+
("ssn", "social security", "sozialversicherungsnummer", "sv-nummer"),
|
|
30
|
+
),
|
|
31
|
+
Rule(
|
|
32
|
+
"us_itin",
|
|
33
|
+
"TAX_ID",
|
|
34
|
+
# an SSN's shape that starts with 9, the middle pair from the ranges the IRS gives out (50-65, 70-88, 90-92,
|
|
35
|
+
# 94-99); below us_ssn with a context word of both, so a number written as an SSN stays one
|
|
36
|
+
r"(?<![\w.+-])9\d{2}([ -]?)(?:5\d|6[0-5]|7\d|8[0-8]|9[0-24-9])\1\d{4}(?![\w-]|\.\d)",
|
|
37
|
+
0.3,
|
|
38
|
+
US_ITIN_WORDS,
|
|
39
|
+
0.75,
|
|
40
|
+
),
|
|
41
|
+
Rule(
|
|
42
|
+
"canada_sin",
|
|
43
|
+
"NATIONAL_ID",
|
|
44
|
+
r"(?<![\w+])\d{3}[ -]?\d{3}[ -]?\d{3}(?!\w)", # 130 692 544; one 9-digit number in ten passes by chance
|
|
45
|
+
0.2,
|
|
46
|
+
CANADA_SIN_WORDS,
|
|
47
|
+
0.85,
|
|
48
|
+
validator=canada_sin,
|
|
49
|
+
),
|
|
50
|
+
]
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""ID numbers of Asia and the Pacific: India, Singapore, South Korea, Thailand, the Philippines, Australia."""
|
|
2
|
+
|
|
3
|
+
from grootveil.detectors.rules.checks import date_ok
|
|
4
|
+
from grootveil.detectors.rules.rule import Rule
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def singapore_nric(value: str) -> bool:
|
|
8
|
+
"""A Singapore NRIC (S, T) or FIN (F, G): a letter, 7 digits and a check letter (weights 2 7 6 5 4 3 2; T and G
|
|
9
|
+
add 4; citizens and foreigners have their own letter tables)."""
|
|
10
|
+
total = sum(int(c) * w for c, w in zip(value[1:8], (2, 7, 6, 5, 4, 3, 2), strict=True))
|
|
11
|
+
total += 4 if value[0] in "TG" else 0
|
|
12
|
+
return ("JZIHGFEDCBA" if value[0] in "ST" else "XWUTRQPNMLK")[total % 11] == value[8]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def korea_rrn(value: str) -> bool:
|
|
16
|
+
"""A South Korean resident (RRN) or foreigner (FRN) registration number: the birth date, then 1 to 8 (the
|
|
17
|
+
century, sex and citizen or foreigner) and six digits. No check digit test: numbers issued since October 2020
|
|
18
|
+
do not follow the old formula."""
|
|
19
|
+
d = [int(c) for c in value if c.isdigit()]
|
|
20
|
+
year = (1900 if d[6] in (1, 2, 5, 6) else 2000) + d[0] * 10 + d[1]
|
|
21
|
+
return date_ok(year, d[2] * 10 + d[3], d[4] * 10 + d[5])
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def thailand_id(value: str) -> bool:
|
|
25
|
+
"""A Thai national ID: 13 digits, the last one a check digit (weights 13 to 2, mod 11)."""
|
|
26
|
+
d = [int(c) for c in value if c.isdigit()]
|
|
27
|
+
return (11 - sum(n * w for n, w in zip(d[:12], range(13, 1, -1), strict=True)) % 11) % 10 == d[12]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
VERHOEFF_D = (
|
|
31
|
+
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (1, 2, 3, 4, 0, 6, 7, 8, 9, 5), (2, 3, 4, 0, 1, 7, 8, 9, 5, 6),
|
|
32
|
+
(3, 4, 0, 1, 2, 8, 9, 5, 6, 7), (4, 0, 1, 2, 3, 9, 5, 6, 7, 8), (5, 9, 8, 7, 6, 0, 4, 3, 2, 1),
|
|
33
|
+
(6, 5, 9, 8, 7, 1, 0, 4, 3, 2), (7, 6, 5, 9, 8, 2, 1, 0, 4, 3), (8, 7, 6, 5, 9, 3, 2, 1, 0, 4),
|
|
34
|
+
(9, 8, 7, 6, 5, 4, 3, 2, 1, 0),
|
|
35
|
+
) # fmt: skip
|
|
36
|
+
VERHOEFF_P = (
|
|
37
|
+
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (1, 5, 7, 6, 2, 8, 3, 0, 9, 4), (5, 8, 0, 3, 7, 9, 6, 1, 4, 2),
|
|
38
|
+
(8, 9, 1, 6, 0, 4, 3, 5, 2, 7), (9, 4, 5, 3, 1, 2, 6, 8, 7, 0), (4, 2, 8, 6, 5, 7, 3, 9, 0, 1),
|
|
39
|
+
(2, 7, 9, 3, 8, 0, 6, 4, 1, 5), (7, 0, 4, 6, 9, 1, 3, 2, 5, 8),
|
|
40
|
+
) # fmt: skip
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def aadhaar(value: str) -> bool:
|
|
44
|
+
"""An Aadhaar number: 12 digits, the first not 0 or 1, the last a Verhoeff check digit (UIDAI)."""
|
|
45
|
+
d = [int(c) for c in value if c.isdigit()]
|
|
46
|
+
c = 0
|
|
47
|
+
for i, n in enumerate(reversed(d)):
|
|
48
|
+
c = VERHOEFF_D[c][VERHOEFF_P[i % 8][n]]
|
|
49
|
+
return d[0] > 1 and c == 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def philippines_tin(value: str) -> bool:
|
|
53
|
+
"""A Philippine TIN: 9 digits (and a 3 to 5 digit branch code), the ninth a check digit (weights 9 to 2, mod 11)."""
|
|
54
|
+
d = [int(c) for c in value if c.isdigit()]
|
|
55
|
+
return sum(n * w for n, w in zip(d, (9, 8, 7, 6, 5, 4, 3, 2), strict=False)) % 11 == d[8]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def australia_tfn(value: str) -> bool:
|
|
59
|
+
"""An Australian Tax File Number: 9 digits whose weighted sum (1 4 3 7 5 8 6 9 10) divides by 11."""
|
|
60
|
+
d = [int(c) for c in value if c.isdigit()]
|
|
61
|
+
return sum(n * w for n, w in zip(d, (1, 4, 3, 7, 5, 8, 6, 9, 10), strict=True)) % 11 == 0
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
INDIA_VOTER_WORDS = ("voter id", "voter card", "voter", "epic", "election card", "elector")
|
|
65
|
+
SINGAPORE_WORDS = ("nric", "fin", "singapore id", "identity card", "ic no", "ic number")
|
|
66
|
+
KOREA_WORDS = (
|
|
67
|
+
"주민등록번호", "주민번호", "외국인등록번호", "resident registration number", "resident registration", "rrn",
|
|
68
|
+
"foreigner registration", "alien registration",
|
|
69
|
+
) # fmt: skip
|
|
70
|
+
THAILAND_WORDS = ("เลขประจำตัวประชาชน", "บัตรประชาชน", "เลขบัตรประชาชน", "thai id", "thai national id", "national id")
|
|
71
|
+
PHILIPPINES_UMID_WORDS = ("umid", "crn", "common reference number", "sss", "gsis")
|
|
72
|
+
PHILIPPINES_TIN_WORDS = ("tin", "tax identification number", "taxpayer identification number", "bir")
|
|
73
|
+
AUSTRALIA_TFN_WORDS = ("tfn", "tax file number", "tax file no")
|
|
74
|
+
|
|
75
|
+
RULES = [
|
|
76
|
+
Rule(
|
|
77
|
+
"aadhaar",
|
|
78
|
+
"NATIONAL_ID",
|
|
79
|
+
r"(?<![\w.+-])\d{4}([ -]?)\d{4}\1\d{4}(?![\w-]|\.\d)", # 2345 6789 0124, 2345-6789-0124, 234567890124
|
|
80
|
+
0.3,
|
|
81
|
+
("aadhaar", "aadhar", "uidai"),
|
|
82
|
+
validator=aadhaar,
|
|
83
|
+
),
|
|
84
|
+
Rule("india_pan", "TAX_ID", r"\b[A-Z]{3}[ABCFGHLJPT][A-Z]\d{4}[A-Z]\b", 0.7, ("pan",)),
|
|
85
|
+
Rule(
|
|
86
|
+
"india_voter",
|
|
87
|
+
"NATIONAL_ID",
|
|
88
|
+
r"\b[A-Z]{3}\d{7}\b", # the voter card's EPIC number: ABC1234567 (no check digit)
|
|
89
|
+
0.3,
|
|
90
|
+
INDIA_VOTER_WORDS,
|
|
91
|
+
0.7,
|
|
92
|
+
),
|
|
93
|
+
Rule(
|
|
94
|
+
"singapore_nric",
|
|
95
|
+
"NATIONAL_ID",
|
|
96
|
+
r"\b[STFG]\d{7}[A-Z]\b", # S7093842A; one code of this shape in eleven passes the check letter by chance
|
|
97
|
+
0.4,
|
|
98
|
+
SINGAPORE_WORDS,
|
|
99
|
+
0.9,
|
|
100
|
+
validator=singapore_nric,
|
|
101
|
+
),
|
|
102
|
+
Rule(
|
|
103
|
+
"korea_rrn",
|
|
104
|
+
"NATIONAL_ID",
|
|
105
|
+
r"(?<![\w+])\d{6}-[1-8]\d{6}(?!\w)", # 880514-1203475; only with its dash
|
|
106
|
+
0.5,
|
|
107
|
+
KOREA_WORDS,
|
|
108
|
+
0.95, # above card (0.9): one of these 13 digits in ten passes the card's Luhn check by chance
|
|
109
|
+
validator=korea_rrn,
|
|
110
|
+
),
|
|
111
|
+
Rule(
|
|
112
|
+
"thailand_id",
|
|
113
|
+
"NATIONAL_ID",
|
|
114
|
+
r"(?<![\w+])[1-8]-?\d{4}-?\d{5}-?\d{2}-?\d(?!\w)", # 3-1020-01234-56-6; one in ten passes the check by chance
|
|
115
|
+
0.3,
|
|
116
|
+
THAILAND_WORDS,
|
|
117
|
+
0.95, # above card (0.9): one of these 13 digits in ten passes the card's Luhn check by chance
|
|
118
|
+
validator=thailand_id,
|
|
119
|
+
),
|
|
120
|
+
Rule(
|
|
121
|
+
"philippines_umid",
|
|
122
|
+
"NATIONAL_ID",
|
|
123
|
+
# the UMID card's CRN: 0033-1234567-8, 003312345678
|
|
124
|
+
r"(?<![\w+])\d{4}-\d{7}-\d(?!\w)|(?<![\w+-])\d{12}(?![\w-])",
|
|
125
|
+
0.4,
|
|
126
|
+
PHILIPPINES_UMID_WORDS,
|
|
127
|
+
0.75,
|
|
128
|
+
),
|
|
129
|
+
Rule(
|
|
130
|
+
"philippines_tin",
|
|
131
|
+
"TAX_ID",
|
|
132
|
+
# 123-456-789, 123-456-789-000 (with the branch code), 123456789
|
|
133
|
+
r"(?<![\w+])\d{3}-\d{3}-\d{3}(?:-\d{3,5})?(?!\w)|(?<![\w+-])\d{9}(?![\w-])",
|
|
134
|
+
0.2,
|
|
135
|
+
PHILIPPINES_TIN_WORDS,
|
|
136
|
+
0.6,
|
|
137
|
+
validator=philippines_tin,
|
|
138
|
+
),
|
|
139
|
+
Rule(
|
|
140
|
+
"australia_tfn",
|
|
141
|
+
"TAX_ID",
|
|
142
|
+
r"(?<![\w+])\d{3} ?\d{3} ?\d{3}(?!\w)", # 876 543 210; one 9-digit number in eleven passes by chance
|
|
143
|
+
0.3,
|
|
144
|
+
AUSTRALIA_TFN_WORDS,
|
|
145
|
+
0.85,
|
|
146
|
+
validator=australia_tfn,
|
|
147
|
+
),
|
|
148
|
+
]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Passport and driving licence: one pattern each for every country (the numbers have no shared check digit), found
|
|
2
|
+
only with a context word. The context words are English, German, Spanish, Italian and Korean."""
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
from grootveil.detectors.rules.rule import Rule
|
|
7
|
+
|
|
8
|
+
# "valid until 2023-03-12", "issued 17-FEB-2023": a date next to a licence number is not one
|
|
9
|
+
DATE_SHAPE = re.compile(r"\d{4}-\d{1,2}-\d{1,2}|\d{1,2}-[A-Z]{3}-\d{2,4}|\d{1,2}([./-])\d{1,2}\1\d{2,4}")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def not_a_date(value: str) -> bool:
|
|
13
|
+
return not DATE_SHAPE.fullmatch(value)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
PASSPORT_WORDS = (
|
|
17
|
+
"passport", "reisepass", "reisepassnummer", "passnummer", "pass-nr", "pasaporte", "passaporto", "여권", "여권번호",
|
|
18
|
+
) # fmt: skip
|
|
19
|
+
DRIVING_LICENCE_WORDS = (
|
|
20
|
+
"driving licence", "driving license", "driver's license", "driver's licence", "driver license", "drivers license",
|
|
21
|
+
"drivers licence", "licence no", "license no", "licence number", "license number", "dl no", "dl number", "dl",
|
|
22
|
+
"führerschein", "führerscheinnummer", "führerschein-nr", "fahrerlaubnis",
|
|
23
|
+
"patente di guida", "numero patente", "patente", "운전면허", "운전면허번호",
|
|
24
|
+
) # fmt: skip
|
|
25
|
+
|
|
26
|
+
PASSPORT = Rule(
|
|
27
|
+
"passport",
|
|
28
|
+
"PASSPORT",
|
|
29
|
+
# a letter and 6 to 8 letters or digits (US, India, Germany, Spain, Italy, Korea, the Philippines, South Africa),
|
|
30
|
+
# or 9 digits (US, UK, South Africa)
|
|
31
|
+
r"\b(?=[A-Z0-9]*\d)[A-Z][A-Z0-9]{6,8}\b|\b\d{9}\b|\b[A-Z]\d{2} \d{5}\b", # India also K12 34567
|
|
32
|
+
0.2,
|
|
33
|
+
PASSPORT_WORDS,
|
|
34
|
+
0.6,
|
|
35
|
+
# another number in a text that also says "passport": "bank routing number, 011547623, written on the passport"
|
|
36
|
+
not_after=r"\b(?:beneficiary number|record number|mrn|routing number|account number|policy number)\s*[:=#,]?\s*",
|
|
37
|
+
)
|
|
38
|
+
DRIVING_LICENCE = Rule(
|
|
39
|
+
"driving_licence",
|
|
40
|
+
"DRIVING_LICENCE",
|
|
41
|
+
# India: MH12 20110012345; else 6 to 18 letters, digits and dashes with a digit (UK MORGA753116SM9IJ, Germany
|
|
42
|
+
# B072RRE2I55, Korea 11-12-123456-12)
|
|
43
|
+
r"\b[A-Z]{2}\d{2}[ -]?\d{11}\b|\b(?=[A-Z0-9-]*\d)[A-Z0-9][A-Z0-9-]{5,17}\b",
|
|
44
|
+
0.15,
|
|
45
|
+
DRIVING_LICENCE_WORDS,
|
|
46
|
+
0.6,
|
|
47
|
+
validator=not_a_date,
|
|
48
|
+
)
|