blackthis 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +66 -2
- package/bin/blackthis.js +255 -0
- package/dsgvo_redactor/__init__.py +3 -0
- package/dsgvo_redactor/cli.py +214 -0
- package/dsgvo_redactor/csv_io.py +221 -0
- package/dsgvo_redactor/detectors.py +114 -0
- package/dsgvo_redactor/detectors_addresses.py +278 -0
- package/dsgvo_redactor/detectors_common.py +72 -0
- package/dsgvo_redactor/detectors_entities.py +818 -0
- package/dsgvo_redactor/detectors_structured.py +471 -0
- package/dsgvo_redactor/engine.py +810 -0
- package/dsgvo_redactor/german_dict.py +100 -0
- package/dsgvo_redactor/lexicons.py +690 -0
- package/dsgvo_redactor/models.py +247 -0
- package/dsgvo_redactor/ner_engine.py +377 -0
- package/dsgvo_redactor/pdf_export.py +393 -0
- package/dsgvo_redactor/pdf_redactor.py +356 -0
- package/dsgvo_redactor/report.py +269 -0
- package/dsgvo_redactor/session.py +518 -0
- package/dsgvo_redactor/validators.py +188 -0
- package/dsgvo_redactor/web_app.py +630 -0
- package/dsgvo_redactor/whitelist.py +273 -0
- package/package.json +52 -4
- package/redact_csv.py +7 -0
- package/requirements.txt +8 -0
- package/static/app.js +1670 -0
- package/static/styles.css +1067 -0
- package/templates/index.html +344 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
"""Strukturierte PII-Detektoren (Kontodaten, E-Mail, URL, IP, Telefon, Beträge, Geburtsdatum, KFZ, IDs, JSON)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from functools import lru_cache
|
|
7
|
+
|
|
8
|
+
from .detectors_common import has_digit
|
|
9
|
+
from .lexicons import NON_NAME_WORDS
|
|
10
|
+
from .models import Category, Confidence, SpanMatch
|
|
11
|
+
from .validators import (
|
|
12
|
+
validate_german_kfz,
|
|
13
|
+
validate_german_tax_id,
|
|
14
|
+
validate_iban,
|
|
15
|
+
validate_luhn,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# --- 1. Kontodaten (IBAN, BIC, Kontonummer, BLZ, Kreditkarte) ---
|
|
19
|
+
|
|
20
|
+
_IBAN_CONTEXT_RE = re.compile(
|
|
21
|
+
r"(?i)\bIBAN[ \t]*[:=]?[ \t]*([A-Z]{2}[ \t]*\d{2}[ \t\-]?[A-Z0-9][ \t\-A-Z0-9]{10,36})"
|
|
22
|
+
)
|
|
23
|
+
_IBAN_STANDALONE_RE = re.compile(
|
|
24
|
+
r"\b([A-Z]{2}\d{2}(?:[ \t\-]?[A-Z0-9]{4}){2,7}(?:[ \t\-]?[A-Z0-9]{1,4})?)\b"
|
|
25
|
+
)
|
|
26
|
+
_BIC_CONTEXT_RE = re.compile(
|
|
27
|
+
r"(?i)\b(?:BIC|SWIFT(?:-?Code)?)[ \t]*[:=]?[ \t]*([A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?)\b"
|
|
28
|
+
)
|
|
29
|
+
_BIC_STANDALONE_RE = re.compile(
|
|
30
|
+
r"\b([A-Z]{4}(?:DE|AT|CH|NL|FR|BE|LU|IT|ES|GB|PL|US|DK|SE|NO|FI|IE|PT|CZ)[A-Z2-9][A-NP-Z0-9](?:[A-Z0-9]{3})?)\b"
|
|
31
|
+
)
|
|
32
|
+
_KONTO_BLZ_RE = re.compile(
|
|
33
|
+
r"(?i)\b(?:Konto(?:nummer|-?Nr\.?)|BLZ|Bankleitzahl)[ \t]*[:=]?[ \t]*(\d[\d \t\-]{4,18}\d)\b"
|
|
34
|
+
)
|
|
35
|
+
_BANK_NAME_RE = re.compile(
|
|
36
|
+
r"(?i)\b(?:Bank(?:name)?|Kreditinstitut|Geldinstitut)[ \t]*[:=][ \t]*([^\n,;]{3,55})"
|
|
37
|
+
)
|
|
38
|
+
_CARD_RE = re.compile(r"(?<!\d)(\d{4}[ \t\-]?\d{4}[ \t\-]?\d{4}[ \t\-]?\d{1,7})(?!\d)")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def detect_bank(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
42
|
+
matches: list[SpanMatch] = []
|
|
43
|
+
has_num = has_digit(text) if text_has_digit is None else text_has_digit
|
|
44
|
+
|
|
45
|
+
if has_num:
|
|
46
|
+
for m in _IBAN_CONTEXT_RE.finditer(text):
|
|
47
|
+
raw = m.group(1).strip(" .,:;")
|
|
48
|
+
start = m.start(1)
|
|
49
|
+
valid = validate_iban(raw)
|
|
50
|
+
rule = "IBAN (ISO 13616 geprüft + Kontext)" if valid else "IBAN (Kontext-Treffer)"
|
|
51
|
+
matches.append(SpanMatch(start, start + len(raw), text[start:start + len(raw)], Category.BANK, Confidence.HIGH, rule))
|
|
52
|
+
|
|
53
|
+
for m in _IBAN_STANDALONE_RE.finditer(text):
|
|
54
|
+
raw = m.group(1).strip()
|
|
55
|
+
if validate_iban(raw):
|
|
56
|
+
matches.append(SpanMatch(m.start(1), m.start(1) + len(raw), raw, Category.BANK, Confidence.HIGH, "IBAN (ISO 13616 Mod-97 gültig)"))
|
|
57
|
+
|
|
58
|
+
for m in _KONTO_BLZ_RE.finditer(text):
|
|
59
|
+
val = m.group(1).strip()
|
|
60
|
+
matches.append(SpanMatch(m.start(1), m.start(1) + len(val), val, Category.BANK, Confidence.HIGH, "Kontonummer / BLZ"))
|
|
61
|
+
|
|
62
|
+
for m in _CARD_RE.finditer(text):
|
|
63
|
+
raw = m.group(1)
|
|
64
|
+
if validate_luhn(raw):
|
|
65
|
+
matches.append(SpanMatch(m.start(1), m.end(1), raw, Category.BANK, Confidence.HIGH, "Kreditkarte (Luhn)"))
|
|
66
|
+
|
|
67
|
+
bic_ranges: list[tuple[int, int]] = []
|
|
68
|
+
for m in _BIC_CONTEXT_RE.finditer(text):
|
|
69
|
+
matches.append(SpanMatch(m.start(1), m.end(1), m.group(1), Category.BANK, Confidence.HIGH, "BIC/SWIFT"))
|
|
70
|
+
bic_ranges.append((m.start(1), m.end(1)))
|
|
71
|
+
|
|
72
|
+
for m in _BIC_STANDALONE_RE.finditer(text):
|
|
73
|
+
val = m.group(1)
|
|
74
|
+
if any(s <= m.start(1) and m.end(1) <= e for s, e in bic_ranges):
|
|
75
|
+
continue
|
|
76
|
+
has_strong_cue = has_digit(val[6:]) or val.endswith("XXX") or len(val) == 11
|
|
77
|
+
if not has_strong_cue and val.isalpha() and len(val) == 8:
|
|
78
|
+
if val.lower() in NON_NAME_WORDS:
|
|
79
|
+
continue
|
|
80
|
+
conf = Confidence.REVIEW
|
|
81
|
+
else:
|
|
82
|
+
conf = Confidence.HIGH
|
|
83
|
+
matches.append(SpanMatch(m.start(1), m.end(1), val, Category.BANK, conf, "BIC/SWIFT (freistehend)"))
|
|
84
|
+
|
|
85
|
+
if ":" in text or "=" in text:
|
|
86
|
+
for m in _BANK_NAME_RE.finditer(text):
|
|
87
|
+
val = m.group(1).strip()
|
|
88
|
+
if val and val.lower() not in NON_NAME_WORDS:
|
|
89
|
+
matches.append(SpanMatch(m.start(1), m.start(1) + len(val), val, Category.BANK, Confidence.HIGH, "Bankname (Kontext)"))
|
|
90
|
+
|
|
91
|
+
return matches
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# --- 2. E-Mail-Adressen & Webseiten ---
|
|
95
|
+
|
|
96
|
+
EMAIL_RE = re.compile(
|
|
97
|
+
r"\b[A-Za-z0-9._%+\-]+(?:\s*@\s*|\s*\[at\]\s*|\s*\(at\)\s*)[A-Za-z0-9.\-]+\.[A-Za-z]{2,15}\b"
|
|
98
|
+
)
|
|
99
|
+
URL_FULL_RE = re.compile(
|
|
100
|
+
r"\b(?:https?://|www\.)[^\s<>\"'()\[\]{},;]+[^\s<>\"'()\[\]{},;.:!?]",
|
|
101
|
+
re.IGNORECASE,
|
|
102
|
+
)
|
|
103
|
+
BARE_DOMAIN_RE = re.compile(
|
|
104
|
+
r"(?<![@\w/\.])([a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\."
|
|
105
|
+
r"(?:de|com|net|org|eu|at|ch|info|io|co|biz|nrw|koeln|berlin|bayern|hamburg|gmbh|online|shop|app))"
|
|
106
|
+
r"(?:/[^\s<>\"'()\[\]{},;]*)?\b",
|
|
107
|
+
re.IGNORECASE,
|
|
108
|
+
)
|
|
109
|
+
_FILE_EXT_RE = re.compile(
|
|
110
|
+
r"\.(?:jpe?g|png|gif|bmp|svg|webp|pdf|docx?|xlsx?|pptx?|csv|txt|zip|rar|7z|eml|msg|mp4|mov|mp3)$",
|
|
111
|
+
re.IGNORECASE,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def detect_emails(text: str) -> list[SpanMatch]:
|
|
116
|
+
if "@" not in text and "[at]" not in text and "(at)" not in text:
|
|
117
|
+
return []
|
|
118
|
+
return [
|
|
119
|
+
SpanMatch(m.start(), m.end(), m.group(0), Category.EMAIL, Confidence.HIGH, "E-Mail-Adresse")
|
|
120
|
+
for m in EMAIL_RE.finditer(text)
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def detect_websites(text: str, email_spans: list[tuple[int, int]]) -> list[SpanMatch]:
|
|
125
|
+
if "." not in text:
|
|
126
|
+
return []
|
|
127
|
+
matches: list[SpanMatch] = []
|
|
128
|
+
|
|
129
|
+
def inside_email(s: int, e: int) -> bool:
|
|
130
|
+
return any(es <= s and e <= ee for es, ee in email_spans)
|
|
131
|
+
|
|
132
|
+
for m in URL_FULL_RE.finditer(text):
|
|
133
|
+
if not inside_email(m.start(), m.end()):
|
|
134
|
+
matches.append(SpanMatch(m.start(), m.end(), m.group(0), Category.WEBSITE, Confidence.HIGH, "URL / Webseite"))
|
|
135
|
+
|
|
136
|
+
for m in BARE_DOMAIN_RE.finditer(text):
|
|
137
|
+
raw = m.group(0)
|
|
138
|
+
if inside_email(m.start(), m.end()) or _FILE_EXT_RE.search(raw):
|
|
139
|
+
continue
|
|
140
|
+
if any(m.start() >= prev.start and m.end() <= prev.end for prev in matches):
|
|
141
|
+
continue
|
|
142
|
+
matches.append(SpanMatch(m.start(), m.end(), raw, Category.WEBSITE, Confidence.HIGH, "Domain / Webseite"))
|
|
143
|
+
|
|
144
|
+
return matches
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# --- 2b. IP-Adressen (IPv4) ---
|
|
148
|
+
|
|
149
|
+
_IPV4_RE = re.compile(r"(?<![A-Za-z0-9.\-/])(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?![\d.])")
|
|
150
|
+
_VERSION_PREFIX_RE = re.compile(r"(?i)\b(?:v|ver\.?|version|release|build|app|ios|macos|chrome|safari)[ \t]*\Z")
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def detect_ip_addresses(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
154
|
+
if "." not in text or not (has_digit(text) if text_has_digit is None else text_has_digit):
|
|
155
|
+
return []
|
|
156
|
+
matches: list[SpanMatch] = []
|
|
157
|
+
for m in _IPV4_RE.finditer(text):
|
|
158
|
+
raw = m.group(1)
|
|
159
|
+
parts = raw.split(".")
|
|
160
|
+
if not all(0 <= int(p) <= 255 for p in parts):
|
|
161
|
+
continue
|
|
162
|
+
if any(len(p) > 1 and p[0] == "0" for p in parts):
|
|
163
|
+
continue
|
|
164
|
+
before = text[max(0, m.start(1) - 14):m.start(1)]
|
|
165
|
+
if _VERSION_PREFIX_RE.search(before):
|
|
166
|
+
continue
|
|
167
|
+
matches.append(SpanMatch(m.start(1), m.end(1), raw, Category.IP_ADDRESS, Confidence.HIGH, "IPv4-Adresse"))
|
|
168
|
+
return matches
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
# --- 3. Telefonnummern ---
|
|
172
|
+
|
|
173
|
+
_PHONE_CONTEXT_RE = re.compile(
|
|
174
|
+
r"(?i)\b(?:(?:Tel(?:efon)?\.?|Mobil(?:funk)?|Handy|Fax|Fon|Durchwahl|Rufnummer|Rückruf(?:nummer)?|"
|
|
175
|
+
r"Telefonnummer|Tel\.-?Nr\.?|WhatsApp)[ \t]*[:.]?[ \t]*|(?:T|M|F)[ \t]*[:.][ \t]*)"
|
|
176
|
+
r"((?:\+|00)?\d[\d \t/\-\(\)\.]{5,24}\d)"
|
|
177
|
+
)
|
|
178
|
+
_PHONE_INTL_RE = re.compile(
|
|
179
|
+
r"(?<!\d)((?:\+|00)(?:49|43|41|31|32|33|44|45|48|39|34|1)"
|
|
180
|
+
r"(?:[ \t]*[/\-][ \t]*|[ \t]+)?(?:\(0\)(?:[ \t]*[/\-][ \t]*|[ \t]+)?)?"
|
|
181
|
+
r"\d[\d \t/\-\(\)]{5,20}\d)(?!\d)"
|
|
182
|
+
)
|
|
183
|
+
_PHONE_DE_MOBILE_RE = re.compile(
|
|
184
|
+
r"(?<!\d)(0(?:15\d|16\d|17\d)(?:[ \t]*[/\-][ \t]*|[ \t]+)?\d{2,4}"
|
|
185
|
+
r"(?:[ \t]*[/\-][ \t]*|[ \t]+)?\d{2,8}(?:[ \t\-]\d{1,5})?)(?!\d)"
|
|
186
|
+
)
|
|
187
|
+
_PHONE_DE_LANDLINE_RE = re.compile(
|
|
188
|
+
r"(?<!\d)("
|
|
189
|
+
r"\(0\d{2,5}\)[ \t]*\d{2,9}(?:[ \t\-]\d{1,6}){0,3}"
|
|
190
|
+
r"|0[2-9]\d{1,4}(?:[ \t]*[/\-][ \t]*|[ \t]+)\d{2,9}(?:[ \t\-]\d{1,6}){0,3}"
|
|
191
|
+
r")(?!\d)"
|
|
192
|
+
)
|
|
193
|
+
_DATE_LIKE_RE = re.compile(r"^\d{1,2}\.\d{1,2}\.\d{2,4}$")
|
|
194
|
+
_ORDER_PREFIX_RE = re.compile(r"[A-Za-z]\d*[-]?\Z")
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _looks_like_valid_phone(raw: str) -> bool:
|
|
198
|
+
if _DATE_LIKE_RE.match(raw):
|
|
199
|
+
return False
|
|
200
|
+
digit_count = sum(1 for c in raw if "0" <= c <= "9")
|
|
201
|
+
return 7 <= digit_count <= 16
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def detect_phones(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
205
|
+
if not (has_digit(text) if text_has_digit is None else text_has_digit):
|
|
206
|
+
return []
|
|
207
|
+
matches: list[SpanMatch] = []
|
|
208
|
+
|
|
209
|
+
for m in _PHONE_CONTEXT_RE.finditer(text):
|
|
210
|
+
raw = m.group(1).strip()
|
|
211
|
+
if _looks_like_valid_phone(raw):
|
|
212
|
+
matches.append(SpanMatch(m.start(1), m.start(1) + len(raw), raw, Category.PHONE, Confidence.HIGH, "Telefonnummer (Kontext)"))
|
|
213
|
+
|
|
214
|
+
for regex, label in (
|
|
215
|
+
(_PHONE_INTL_RE, "Telefonnummer (International)"),
|
|
216
|
+
(_PHONE_DE_MOBILE_RE, "Mobilnummer (DE)"),
|
|
217
|
+
(_PHONE_DE_LANDLINE_RE, "Festnetznummer (DE)"),
|
|
218
|
+
):
|
|
219
|
+
for m in regex.finditer(text):
|
|
220
|
+
raw = m.group(1).strip()
|
|
221
|
+
if _looks_like_valid_phone(raw):
|
|
222
|
+
line_before = (
|
|
223
|
+
text[max(0, m.start(1) - 12):m.start(1)]
|
|
224
|
+
.rsplit("\n", 1)[-1]
|
|
225
|
+
.rsplit("\r", 1)[-1]
|
|
226
|
+
)
|
|
227
|
+
if _ORDER_PREFIX_RE.search(line_before):
|
|
228
|
+
continue
|
|
229
|
+
matches.append(SpanMatch(m.start(1), m.start(1) + len(raw), raw, Category.PHONE, Confidence.HIGH, label))
|
|
230
|
+
|
|
231
|
+
return matches
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
# --- 4. Geldbeträge ---
|
|
235
|
+
|
|
236
|
+
_NUM_AMOUNT = r"(?:\d{1,3}(?:\.\d{3})+(?:,\d{1,2}|,-|,--)?|\d+(?:,\d{1,2}|,-|,--|\.\d{2})?)"
|
|
237
|
+
_AMOUNT_AFTER_CURR_RE = re.compile(
|
|
238
|
+
rf"(?:€|EUR|Euro|CHF|USD|\$|GBP|£)\s*({_NUM_AMOUNT})(?![\d.,a-zA-Zäöüß])",
|
|
239
|
+
re.IGNORECASE,
|
|
240
|
+
)
|
|
241
|
+
_AMOUNT_BEFORE_CURR_RE = re.compile(
|
|
242
|
+
rf"(?<![\d.,A-Za-z])({_NUM_AMOUNT})\s*(?:€|EUR\b|Euro\b|CHF\b|USD\b|\$|GBP\b|£)",
|
|
243
|
+
re.IGNORECASE,
|
|
244
|
+
)
|
|
245
|
+
_AMOUNT_CONTEXT_RE = re.compile(
|
|
246
|
+
r"(?i)\b(?:(?:Gesamt|End|Rechnungs|Erstattungs|Rückerstattungs|Netto|Brutto|Teil|Rest|Kauf|Miet|Monats|Jahres|Stunden)?)"
|
|
247
|
+
r"(?:betrag|summe|preis|kosten|gebühr(?:en)?|rückerstattung|erstattung|gutschrift|anzahlung|"
|
|
248
|
+
r"zahlung|überweisung|gehalt|lohn|honorar|vergütung|guthaben|saldo|kaution|rabatt|nachlass|skonto|"
|
|
249
|
+
r"in\s+Höhe\s+von|i\.?\s*H\.?\s*v\.?)"
|
|
250
|
+
rf"\s*(?:[:=]|von|über|beträgt|i\.?\s*H\.?\s*v\.?|in\s+Höhe\s+von)?\s*(?:ca\.?\s*|rund\s*|etwa\s*)?"
|
|
251
|
+
rf"({_NUM_AMOUNT}(?:\s*(?:€|EUR|Euro|CHF|USD|\$))?)"
|
|
252
|
+
r"(?!\s*(?:t\b|to\b|Tonnen?\b|kg\b|g\b|m\b|cm\b|mm\b|km\b|Meter\b|Tage?\b|Wochen?\b|Monate?\b|Jahre?\b|Std\.?\b|Stunden?\b|Minuten?\b|Uhr\b|%|Prozent\b|x\b|Stück\b|Schilder\b|LKW\b|PKW\b))",
|
|
253
|
+
)
|
|
254
|
+
_DATE_CONTINUATION_RE = re.compile(r"^\.\d{2,4}")
|
|
255
|
+
_YEAR_ONLY_RE = re.compile(r"^(?:19\d\d|20[0-3]\d)$")
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def detect_amounts(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
259
|
+
if not (has_digit(text) if text_has_digit is None else text_has_digit):
|
|
260
|
+
return []
|
|
261
|
+
matches: list[SpanMatch] = []
|
|
262
|
+
|
|
263
|
+
for regex in (_AMOUNT_AFTER_CURR_RE, _AMOUNT_BEFORE_CURR_RE):
|
|
264
|
+
for m in regex.finditer(text):
|
|
265
|
+
if _DATE_CONTINUATION_RE.match(text[m.end():]):
|
|
266
|
+
continue
|
|
267
|
+
matches.append(SpanMatch(m.start(), m.end(), m.group(0), Category.AMOUNT, Confidence.HIGH, "Geldbetrag (Währung)"))
|
|
268
|
+
|
|
269
|
+
for m in _AMOUNT_CONTEXT_RE.finditer(text):
|
|
270
|
+
val = m.group(1).strip()
|
|
271
|
+
start = m.start(1)
|
|
272
|
+
end = start + len(val)
|
|
273
|
+
if not val or _DATE_CONTINUATION_RE.match(text[end:]) or _YEAR_ONLY_RE.match(val):
|
|
274
|
+
continue
|
|
275
|
+
matches.append(SpanMatch(start, end, val, Category.AMOUNT, Confidence.HIGH, "Geldbetrag (Kontext)"))
|
|
276
|
+
|
|
277
|
+
return matches
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
# --- 5. Geburtsdaten, Kfz-Kennzeichen, Steuer-/Ausweis-IDs ---
|
|
281
|
+
|
|
282
|
+
_GERMAN_MONTHS_ALT = (
|
|
283
|
+
r"Januar|Jan\.?|Februar|Feb\.?|März|Maerz|Mrz\.?|April|Apr\.?|Mai|Juni|Jun\.?|"
|
|
284
|
+
r"Juli|Jul\.?|August|Aug\.?|September|Sept?\.?|Oktober|Okt\.?|November|Nov\.?|Dezember|Dez\.?"
|
|
285
|
+
)
|
|
286
|
+
_BIRTHDATE_RE = re.compile(
|
|
287
|
+
r"(?i)(?:\b(?:geb\.?|geboren(?:\s+am)?|Geburtsdatum|Geb\.?-?Dat\.?|Geburtstag|DOB)\s*[:.]?\s*|\*\s*)"
|
|
288
|
+
rf"(\d{{1,2}}[./\-]\d{{1,2}}[./\-](?:19|20)?\d{{2}}|\d{{4}}-\d{{2}}-\d{{2}}|\d{{1,2}}\.\s*(?:{_GERMAN_MONTHS_ALT})\s+(?:19|20)\d{{2}})"
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
_KFZ_CONTEXT_RE = re.compile(
|
|
292
|
+
r"(?i)\b(?:Kennzeichen|Kfz(?:-?Kennzeichen)?|Amtl\.?\s*Kennzeichen|Nummernschild|"
|
|
293
|
+
r"Fahrzeug(?:kennzeichen)?|LKW|PKW|Transporter|Sprinter)\s*[:=]?\s*"
|
|
294
|
+
r"([A-ZÄÖÜ]{1,3})[\s\-]([A-Z]{1,2})[\s\-]?([1-9]\d{0,3})([EH])?\b"
|
|
295
|
+
)
|
|
296
|
+
_KFZ_HYPHEN_RE = re.compile(
|
|
297
|
+
r"(?<![A-ZÄÖÜa-zäöüß0-9\-])([A-ZÄÖÜ]{1,3})-([A-Z]{1,2})[\s\-]([1-9]\d{0,3})([EH])?(?![A-ZÄÖÜa-zäöüß0-9\-])"
|
|
298
|
+
)
|
|
299
|
+
_KFZ_SPACE_RE = re.compile(
|
|
300
|
+
r"(?<![A-ZÄÖÜa-zäöüß0-9\-])([A-ZÄÖÜ]{1,3})[ \t]+([A-Z]{1,2})[ \t]+([1-9]\d{0,3})([EH])?(?![A-ZÄÖÜa-zäöüß0-9\-])"
|
|
301
|
+
)
|
|
302
|
+
_KFZ_SPACE_FALSE_PREFIXES = frozenset({"AM", "AN", "IM", "IN", "ZU", "UM", "AB", "OB", "ER", "ES", "DA", "SO", "WO", "NR", "NO", "ID"})
|
|
303
|
+
|
|
304
|
+
_UST_ID_RE = re.compile(r"\b(DE\s?\d{9})\b")
|
|
305
|
+
_TAX_CONTEXT_RE = re.compile(
|
|
306
|
+
r"(?i)\b(?:Steuer-?ID|Steuer-?Identifikationsnummer|Steuernummer|St\.?-?Nr\.?|IdNr\.?|"
|
|
307
|
+
r"USt\.?-?IdNr\.?|Umsatzsteuer-?ID|Sozialversicherungsnummer|SV-?Nr\.?|RVNR|"
|
|
308
|
+
r"Personalausweis(?:nummer|-?Nr\.?)?|Ausweisnummer|Reisepass(?:nummer|-?Nr\.?)?)\s*[:=]?\s*"
|
|
309
|
+
r"([A-Z0-9][A-Z0-9\s/\-]{6,22}[A-Z0-9])"
|
|
310
|
+
)
|
|
311
|
+
_TAX_ID_11_RE = re.compile(r"(?<!\d)(\d{2}\s?\d{3}\s?\d{3}\s?\d{3})(?!\d)")
|
|
312
|
+
_RVNR_RE = re.compile(r"\b(\d{2}\s?\d{6}\s?[A-Z]\s?\d{3})\b")
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def detect_birthdates(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
316
|
+
if not (has_digit(text) if text_has_digit is None else text_has_digit):
|
|
317
|
+
return []
|
|
318
|
+
return [
|
|
319
|
+
SpanMatch(m.start(1), m.end(1), m.group(1), Category.BIRTHDATE, Confidence.HIGH, "Geburtsdatum")
|
|
320
|
+
for m in _BIRTHDATE_RE.finditer(text)
|
|
321
|
+
]
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def detect_license_plates(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
325
|
+
if not (has_digit(text) if text_has_digit is None else text_has_digit):
|
|
326
|
+
return []
|
|
327
|
+
matches: list[SpanMatch] = []
|
|
328
|
+
for m in _KFZ_CONTEXT_RE.finditer(text):
|
|
329
|
+
if validate_german_kfz(m.group(1), m.group(2), m.group(3)):
|
|
330
|
+
end = m.end(4) if m.group(4) else m.end(3)
|
|
331
|
+
matches.append(SpanMatch(m.start(1), end, text[m.start(1):end], Category.LICENSE_PLATE, Confidence.HIGH, "Kfz-Kennzeichen (Kontext)"))
|
|
332
|
+
for m in _KFZ_HYPHEN_RE.finditer(text):
|
|
333
|
+
if validate_german_kfz(m.group(1), m.group(2), m.group(3)):
|
|
334
|
+
end = m.end(4) if m.group(4) else m.end(3)
|
|
335
|
+
matches.append(SpanMatch(m.start(1), end, text[m.start(1):end], Category.LICENSE_PLATE, Confidence.HIGH, "Kfz-Kennzeichen (DE)"))
|
|
336
|
+
for m in _KFZ_SPACE_RE.finditer(text):
|
|
337
|
+
district, letters, digits = m.group(1), m.group(2), m.group(3)
|
|
338
|
+
if district in _KFZ_SPACE_FALSE_PREFIXES:
|
|
339
|
+
continue
|
|
340
|
+
if len(digits) == 4 and 1990 <= int(digits) <= 2035:
|
|
341
|
+
continue
|
|
342
|
+
if validate_german_kfz(district, letters, digits):
|
|
343
|
+
end = m.end(4) if m.group(4) else m.end(3)
|
|
344
|
+
matches.append(SpanMatch(m.start(1), end, text[m.start(1):end], Category.LICENSE_PLATE, Confidence.REVIEW, "Mögliches Kfz-Kennzeichen (ohne Bindestrich)"))
|
|
345
|
+
return matches
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def detect_tax_ids(text: str, *, text_has_digit: bool | None = None) -> list[SpanMatch]:
|
|
349
|
+
if not (has_digit(text) if text_has_digit is None else text_has_digit):
|
|
350
|
+
return []
|
|
351
|
+
matches: list[SpanMatch] = []
|
|
352
|
+
context_ranges: list[tuple[int, int]] = []
|
|
353
|
+
for m in _TAX_CONTEXT_RE.finditer(text):
|
|
354
|
+
val = m.group(1).strip(" .,:;")
|
|
355
|
+
if sum(1 for c in val if not c.isspace()) >= 7:
|
|
356
|
+
s, e = m.start(1), m.start(1) + len(val)
|
|
357
|
+
matches.append(SpanMatch(s, e, val, Category.TAX_ID, Confidence.HIGH, "Steuer-/Sozialversicherungs-/Ausweis-ID (Kontext)"))
|
|
358
|
+
context_ranges.append((s, e))
|
|
359
|
+
|
|
360
|
+
for m in _UST_ID_RE.finditer(text):
|
|
361
|
+
in_ctx = any(s <= m.start(1) and m.end(1) <= e for s, e in context_ranges)
|
|
362
|
+
conf = Confidence.HIGH if in_ctx else Confidence.REVIEW
|
|
363
|
+
rule = "USt-IdNr (DE)" if in_ctx else "Mögliche USt-IdNr (DE, ohne Label)"
|
|
364
|
+
matches.append(SpanMatch(m.start(1), m.end(1), m.group(1), Category.TAX_ID, conf, rule))
|
|
365
|
+
|
|
366
|
+
for m in _TAX_ID_11_RE.finditer(text):
|
|
367
|
+
raw = m.group(1)
|
|
368
|
+
if validate_german_tax_id(raw):
|
|
369
|
+
matches.append(SpanMatch(m.start(1), m.end(1), raw, Category.TAX_ID, Confidence.HIGH, "Deutsche Steuer-ID (Prüfziffer gültig)"))
|
|
370
|
+
|
|
371
|
+
for m in _RVNR_RE.finditer(text):
|
|
372
|
+
matches.append(SpanMatch(m.start(1), m.end(1), m.group(1), Category.TAX_ID, Confidence.HIGH, "Sozialversicherungsnummer (RVNR)"))
|
|
373
|
+
|
|
374
|
+
return matches
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
# --- 6. Strukturierte JSON-/Query-Felder ---
|
|
378
|
+
|
|
379
|
+
_JSON_KV_PII_MAP: dict[str, Category] = {
|
|
380
|
+
"strasse": Category.ADDRESS, "straße": Category.ADDRESS, "strasse1": Category.ADDRESS,
|
|
381
|
+
"strasse2": Category.ADDRESS, "straße1": Category.ADDRESS, "straße2": Category.ADDRESS,
|
|
382
|
+
"strasse_1": Category.ADDRESS, "strasse_2": Category.ADDRESS, "street": Category.ADDRESS,
|
|
383
|
+
"street1": Category.ADDRESS, "street2": Category.ADDRESS, "adresse": Category.ADDRESS,
|
|
384
|
+
"address": Category.ADDRESS, "anschrift": Category.ADDRESS, "wohnadresse": Category.ADDRESS,
|
|
385
|
+
"lieferadresse": Category.ADDRESS, "rechnungsadresse": Category.ADDRESS,
|
|
386
|
+
"hausnummer": Category.HOUSE_NUMBER, "hausnr": Category.HOUSE_NUMBER, "house_number": Category.HOUSE_NUMBER,
|
|
387
|
+
"plz": Category.POSTAL_CODE, "postleitzahl": Category.POSTAL_CODE, "zip": Category.POSTAL_CODE,
|
|
388
|
+
"zipcode": Category.POSTAL_CODE, "zip_code": Category.POSTAL_CODE, "postal_code": Category.POSTAL_CODE,
|
|
389
|
+
"ort": Category.LOCATION, "stadt": Category.LOCATION, "city": Category.LOCATION,
|
|
390
|
+
"wohnort": Category.LOCATION, "bezirk": Category.LOCATION, "unterort": Category.LOCATION,
|
|
391
|
+
"ortsteil": Category.LOCATION, "stadtteil": Category.LOCATION,
|
|
392
|
+
"name": Category.NAME, "vorname": Category.NAME, "nachname": Category.NAME,
|
|
393
|
+
"first_name": Category.NAME, "firstname": Category.NAME, "last_name": Category.NAME,
|
|
394
|
+
"lastname": Category.NAME, "full_name": Category.NAME, "fullname": Category.NAME,
|
|
395
|
+
"kundenname": Category.NAME, "ansprechpartner": Category.NAME, "kontoinhaber": Category.NAME,
|
|
396
|
+
"empfaenger": Category.NAME, "empfänger": Category.NAME, "absender": Category.NAME,
|
|
397
|
+
"email": Category.EMAIL, "e_mail": Category.EMAIL, "e-mail": Category.EMAIL,
|
|
398
|
+
"mail": Category.EMAIL, "bcc_mail": Category.EMAIL, "cc_mail": Category.EMAIL,
|
|
399
|
+
"from_mail": Category.EMAIL, "to_mail": Category.EMAIL, "kunden_email": Category.EMAIL,
|
|
400
|
+
"kunden_mail": Category.EMAIL,
|
|
401
|
+
"telefon": Category.PHONE, "telefonnummer": Category.PHONE, "tel": Category.PHONE,
|
|
402
|
+
"phone": Category.PHONE, "mobile": Category.PHONE, "mobil": Category.PHONE,
|
|
403
|
+
"handy": Category.PHONE, "fax": Category.PHONE, "rufnummer": Category.PHONE,
|
|
404
|
+
"iban": Category.BANK, "bic": Category.BANK, "swift": Category.BANK,
|
|
405
|
+
"kontonummer": Category.BANK, "blz": Category.BANK, "bank": Category.BANK, "bankname": Category.BANK,
|
|
406
|
+
"total_preis": Category.AMOUNT, "gesamtpreis": Category.AMOUNT, "gesamtbetrag": Category.AMOUNT,
|
|
407
|
+
"preis": Category.AMOUNT, "betrag": Category.AMOUNT, "price": Category.AMOUNT, "amount": Category.AMOUNT,
|
|
408
|
+
"geburtsdatum": Category.BIRTHDATE, "geb_datum": Category.BIRTHDATE, "birthdate": Category.BIRTHDATE, "dob": Category.BIRTHDATE,
|
|
409
|
+
"kennzeichen": Category.LICENSE_PLATE, "kfz": Category.LICENSE_PLATE, "license_plate": Category.LICENSE_PLATE,
|
|
410
|
+
"ip": Category.IP_ADDRESS, "ip_address": Category.IP_ADDRESS, "ip_adresse": Category.IP_ADDRESS,
|
|
411
|
+
"firma": Category.COMPANY, "firmenname": Category.COMPANY, "company": Category.COMPANY, "unternehmen": Category.COMPANY,
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
_JSON_KV_RE = re.compile(
|
|
415
|
+
r'(?<=[{,])\s*"([A-Za-z0-9_\-äöüÄÖÜß]{2,40})"\s*:\s*(?:"((?:[^"\\]|\\.)+)"|(\d+(?:[.,]\d+)?))'
|
|
416
|
+
)
|
|
417
|
+
_JSON_EMPTY_VALS = frozenset({"null", "none", "n/a", "-", "—", "true", "false", "ja", "nein"})
|
|
418
|
+
_ALREADY_REDACTED_VAL_RE = re.compile(r"^(?:[█\s]+|\[[A-ZÄÖÜ0-9_ -]+\])$")
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def detect_json_kv_fields(text: str, enabled: set[Category]) -> list[SpanMatch]:
|
|
422
|
+
"""Erkennt PII-Werte in eingebetteten JSON-/Query-Objekten (z. B. {"strasse":"Kufsteiner 43"})."""
|
|
423
|
+
if '":' not in text and '" :' not in text:
|
|
424
|
+
return []
|
|
425
|
+
matches: list[SpanMatch] = []
|
|
426
|
+
for m in _JSON_KV_RE.finditer(text):
|
|
427
|
+
key_l = m.group(1).strip().lower()
|
|
428
|
+
cat = _JSON_KV_PII_MAP.get(key_l)
|
|
429
|
+
if cat is None or cat not in enabled:
|
|
430
|
+
continue
|
|
431
|
+
is_quoted = m.group(2) is not None
|
|
432
|
+
val_raw = m.group(2) if is_quoted else m.group(3)
|
|
433
|
+
if not val_raw:
|
|
434
|
+
continue
|
|
435
|
+
val_clean = val_raw.strip()
|
|
436
|
+
if not val_clean or val_clean.lower() in _JSON_EMPTY_VALS or _ALREADY_REDACTED_VAL_RE.match(val_clean):
|
|
437
|
+
continue
|
|
438
|
+
if not is_quoted and cat not in (Category.POSTAL_CODE, Category.AMOUNT, Category.HOUSE_NUMBER):
|
|
439
|
+
continue
|
|
440
|
+
if cat == Category.AMOUNT and val_clean in {"0", "0.0", "0.00", "0,00"}:
|
|
441
|
+
continue
|
|
442
|
+
grp = 2 if is_quoted else 3
|
|
443
|
+
start, end = m.start(grp), m.end(grp)
|
|
444
|
+
matches.append(SpanMatch(start, end, text[start:end], cat, Confidence.HIGH, f'JSON-Feld "{m.group(1)}"'))
|
|
445
|
+
return matches
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
# --- 7. Benutzerdefinierte Blacklist (Regex gecacht pro Begriff!) ---
|
|
449
|
+
|
|
450
|
+
_WORD_EDGE_RE = re.compile(r"[\wÄÖÜäöüß]")
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
@lru_cache(maxsize=512)
|
|
454
|
+
def _compile_blacklist_pattern(clean: str) -> re.Pattern[str]:
|
|
455
|
+
esc = re.escape(clean)
|
|
456
|
+
prefix = r"\b" if _WORD_EDGE_RE.match(clean[0]) else ""
|
|
457
|
+
suffix = r"\b" if _WORD_EDGE_RE.match(clean[-1]) else ""
|
|
458
|
+
return re.compile(rf"{prefix}{esc}{suffix}", re.IGNORECASE)
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def detect_custom_blacklist(text: str, terms: list[str]) -> list[SpanMatch]:
|
|
462
|
+
matches: list[SpanMatch] = []
|
|
463
|
+
text_l = text.lower()
|
|
464
|
+
for term in terms:
|
|
465
|
+
clean = term.strip()
|
|
466
|
+
if not clean or clean.lower() not in text_l:
|
|
467
|
+
continue
|
|
468
|
+
pat = _compile_blacklist_pattern(clean)
|
|
469
|
+
for m in pat.finditer(text):
|
|
470
|
+
matches.append(SpanMatch(m.start(), m.end(), m.group(0), Category.MANUAL, Confidence.HIGH, f"Eigene Sperrliste ({clean})"))
|
|
471
|
+
return matches
|