euredact 0.1.0 → 0.3.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.
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+ /**
3
+ * BIC registry — tier-1 membership testing for ISO 9362 bank identifier codes.
4
+ *
5
+ * BIC is the only bank identifier in the supported set with **no check digit**
6
+ * (IBAN has mod-97, VAT has country-specific checksums). Structure alone cannot
7
+ * separate a genuine BIC from an ordinary 8- or 11-character uppercase word,
8
+ * because characters 5-6 of everyday words are frequently valid ISO 3166
9
+ * country codes (`DRINGEND` -> `GE`, `HOSPITAL` -> `IT`). Registry membership
10
+ * is therefore the strongest precision signal available.
11
+ *
12
+ * Two sources are consulted, in order:
13
+ *
14
+ * 1. A deployment-supplied registry, installed via {@link setBicRegistry}.
15
+ * This is the path for anyone holding a licensed directory (SWIFTRef).
16
+ * 2. {@link SEED_BIC6_PREFIXES} — a small bundled set of institution+country
17
+ * prefixes for major European banks, compiled from publicly published bank
18
+ * data. No licensed directory is bundled.
19
+ *
20
+ * Matching is on the **BIC6 prefix** (4-letter institution + 2-letter country),
21
+ * so any location and branch code on a known institution is accepted. The
22
+ * prefix is the stable part of a BIC; branch codes churn as branches open and
23
+ * close.
24
+ *
25
+ * The registry is used as an *accept* signal, never as a filter: a candidate
26
+ * that misses it falls through to the context gate in `suppressors.ts` and is
27
+ * still detected when banking context is present. A stale list therefore costs
28
+ * a little recall on bare, contextless BICs — it never causes a leak. Annual
29
+ * review is sufficient.
30
+ */
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.SEED_BIC6_PREFIXES = void 0;
33
+ exports.setBicRegistry = setBicRegistry;
34
+ exports.getBicRegistry = getBicRegistry;
35
+ exports.isRegisteredBic = isRegisteredBic;
36
+ // BIC6 prefixes (institution + country) of major European institutions,
37
+ // compiled from publicly published bank data — not from a licensed directory.
38
+ // Kept deliberately small: this covers the banks that appear bare and
39
+ // contextless in real documents. Anything outside this list is still detected
40
+ // via the context gate.
41
+ exports.SEED_BIC6_PREFIXES = new Set([
42
+ // --- Netherlands ---
43
+ "ABNANL", // ABN AMRO
44
+ "INGBNL", // ING
45
+ "RABONL", // Rabobank
46
+ "SNSBNL", // SNS Bank
47
+ "TRIONL", // Triodos
48
+ "BUNQNL", // bunq
49
+ "KNABNL", // Knab
50
+ "ASNBNL", // ASN Bank
51
+ "RBRBNL", // RegioBank
52
+ "FVLBNL", // Van Lanschot
53
+ "BITSNL", // BinckBank
54
+ "DEUTNL", // Deutsche Bank NL
55
+ // --- Belgium ---
56
+ "BBRUBE", // BNP Paribas Fortis
57
+ "GEBABE", // BNP Paribas Fortis (legacy)
58
+ "KREDBE", // KBC
59
+ "GKCCBE", // Belfius
60
+ "BPOTBE", // bpost bank
61
+ "ARSPBE", // Argenta
62
+ "AXABBE", // AXA Bank Belgium
63
+ "CREGBE", // Belfius (legacy)
64
+ "NICABE", // Crelan
65
+ "TRIOBE", // Triodos Belgium
66
+ "CTBKBE", // Banca Monte Paschi Belgio
67
+ "JVBABE", // Van Breda
68
+ // --- Germany ---
69
+ "DEUTDE", // Deutsche Bank
70
+ "COBADE", // Commerzbank
71
+ "PBNKDE", // Postbank
72
+ "GENODE", // Volksbanken / Raiffeisen
73
+ "MALADE", // Sparkassen
74
+ "HELADE", // Helaba / Sparkassen
75
+ "BYLADE", // BayernLB / Sparkassen
76
+ "SOLADE", // Sparkassen (Baden-Wurttemberg)
77
+ "WELADE", // Sparkassen (Westfalen)
78
+ "NOLADE", // Norddeutsche Landesbank
79
+ "SPKKDE", // Sparkasse
80
+ "HYVEDE", // UniCredit / HypoVereinsbank
81
+ "DRESDE", // Dresdner (legacy Commerzbank)
82
+ "INGDDE", // ING-DiBa
83
+ "NTSBDE", // N26
84
+ "SFRTDE", // Sutor / fintech BaaS
85
+ "TEDADE", // Telda / BaaS
86
+ // --- France ---
87
+ "BNPAFR", // BNP Paribas
88
+ "SOGEFR", // Societe Generale
89
+ "AGRIFR", // Credit Agricole
90
+ "CCBPFR", // Banque Populaire
91
+ "CEPAFR", // Caisse d'Epargne
92
+ "CMCIFR", // Credit Mutuel / CIC
93
+ "PSSTFR", // La Banque Postale
94
+ "BREDFR", // BRED Banque Populaire
95
+ "CRLYFR", // LCL
96
+ "HSBCFR", // HSBC France
97
+ "TRZOFR", // Treezor
98
+ "QNTOFR", // Qonto
99
+ // --- Austria ---
100
+ "GIBAAT", // Erste Bank / Sparkassen
101
+ "BKAUAT", // UniCredit Bank Austria
102
+ "RZBAAT", // Raiffeisen Bank International
103
+ "RLNWAT", // Raiffeisen NO-Wien
104
+ "OBKLAT", // Oberbank
105
+ "BAWAAT", // BAWAG P.S.K.
106
+ "VBOEAT", // Volksbank
107
+ "HYPTAT", // Hypo Tirol
108
+ "SPIHAT", // Sparkasse
109
+ // --- Luxembourg ---
110
+ "BCEELU", // Banque et Caisse d'Epargne de l'Etat
111
+ "BILLLU", // Banque Internationale a Luxembourg
112
+ "BGLLLU", // BGL BNP Paribas
113
+ "CCRALU", // Banque Raiffeisen
114
+ "BLUXLU", // Banque de Luxembourg
115
+ // --- Ireland / UK ---
116
+ "AIBKIE", // Allied Irish Banks
117
+ "BOFIIE", // Bank of Ireland
118
+ "ULSBIE", // Ulster Bank
119
+ "CITIIE", // Citibank Ireland
120
+ "BARCGB", // Barclays
121
+ "HBUKGB", // HSBC UK
122
+ "MIDLGB", // HSBC (legacy Midland)
123
+ "LOYDGB", // Lloyds
124
+ "NWBKGB", // NatWest
125
+ "RBOSGB", // Royal Bank of Scotland
126
+ "TSBSGB", // TSB
127
+ "REVOGB", // Revolut
128
+ "MONZGB", // Monzo
129
+ "STARGB", // Starling
130
+ "CHASGB", // JP Morgan Chase UK
131
+ // --- Nordics ---
132
+ "NDEADK", // Nordea Denmark
133
+ "DABADK", // Danske Bank
134
+ "JYBADK", // Jyske Bank
135
+ "SYBKDK", // Sydbank
136
+ "NDEASE", // Nordea Sweden
137
+ "ESSESE", // SEB
138
+ "HANDSE", // Handelsbanken
139
+ "SWEDSE", // Swedbank
140
+ "DNBANO", // DNB
141
+ "NDEANO", // Nordea Norway
142
+ "NDEAFI", // Nordea Finland
143
+ "OKOYFI", // OP Corporate Bank
144
+ "HELSFI", // Aktia
145
+ "NBIIIS", // Landsbankinn (NBIIISRE)
146
+ "ESJAIS", // Arion Bank (ESJAISRE)
147
+ "GLITIS", // Islandsbanki (GLITISRE)
148
+ "MPBAIS", // Kvika banki (MPBAISRE)
149
+ "KBNONO", // BNbank ASA (KBNONO22)
150
+ "PRBONO", // Boligbanken ASA (PRBONO22)
151
+ "AARBDE", // Aareal Bank AG (AARBDE5W)
152
+ // --- Southern / Central Europe ---
153
+ "UNCRIT", // UniCredit Italy
154
+ "BCITIT", // Intesa Sanpaolo
155
+ "PASCIT", // Monte dei Paschi
156
+ "BPMOIT", // Banco BPM
157
+ "CRPPIT", // Cassa di Risparmio
158
+ "BSCHES", // Banco Santander
159
+ "BBVAES", // BBVA
160
+ "CAIXES", // CaixaBank
161
+ "SABBES", // Banco Sabadell
162
+ "BKBKES", // Bankinter
163
+ "BCOMPT", // Millennium BCP
164
+ "CGDIPT", // Caixa Geral de Depositos
165
+ "BPIPPT", // Banco BPI
166
+ "NOVBPT", // Novo Banco
167
+ "BPKOPL", // PKO Bank Polski
168
+ "BREXPL", // mBank
169
+ "INGBPL", // ING Bank Slaski
170
+ "WBKPPL", // Santander Bank Polska
171
+ "KOMBCZ", // Komercni banka
172
+ "CEKOCZ", // CSOB
173
+ "GIBACZ", // Ceska sporitelna
174
+ "TATRSK", // Tatra banka
175
+ "SUBASK", // Vseobecna uverova banka
176
+ "GIBASK", // Slovenska sporitelna
177
+ "OTPVHU", // OTP Bank
178
+ "BUDAHU", // Budapest Bank
179
+ "MKKBHU", // MKB Bank
180
+ // --- Switzerland ---
181
+ "UBSWCH", // UBS
182
+ "CRESCH", // Credit Suisse
183
+ "POFICH", // PostFinance
184
+ "ZKBKCH", // Zurcher Kantonalbank
185
+ "RAIFCH", // Raiffeisen Switzerland
186
+ "MIGRCH", // Migros Bank
187
+ ]);
188
+ let provider = null;
189
+ /**
190
+ * Install a deployment-supplied BIC registry, consulted ahead of the seed list.
191
+ *
192
+ * Accepts a membership callable, any iterable of BIC strings, or `null` to
193
+ * remove a previously installed registry. Entries may be full BIC8/BIC11 codes
194
+ * or bare BIC6 prefixes; both are matched, case-insensitively and ignoring
195
+ * spaces.
196
+ */
197
+ function setBicRegistry(registry) {
198
+ if (registry === null) {
199
+ provider = null;
200
+ return;
201
+ }
202
+ if (typeof registry === "function") {
203
+ provider = registry;
204
+ return;
205
+ }
206
+ const known = new Set();
207
+ for (const entry of registry) {
208
+ const e = String(entry).trim();
209
+ if (e && !e.startsWith("#"))
210
+ known.add(e.replace(/ /g, "").toUpperCase());
211
+ }
212
+ provider = (bic) => known.has(bic);
213
+ }
214
+ /** Return the installed registry provider, or null if none is installed. */
215
+ function getBicRegistry() {
216
+ return provider;
217
+ }
218
+ /**
219
+ * Tier 1: is `candidate` a BIC of a known institution?
220
+ *
221
+ * Checks the deployment-supplied registry first (full code, then BIC6 prefix),
222
+ * then the bundled seed prefixes.
223
+ */
224
+ function isRegisteredBic(candidate) {
225
+ const c = candidate.replace(/ /g, "").toUpperCase();
226
+ if (c.length !== 8 && c.length !== 11)
227
+ return false;
228
+ const prefix = c.slice(0, 6);
229
+ if (provider !== null) {
230
+ try {
231
+ if (provider(c) || provider(prefix))
232
+ return true;
233
+ }
234
+ catch {
235
+ // a broken provider must not break detection
236
+ }
237
+ }
238
+ return exports.SEED_BIC6_PREFIXES.has(prefix);
239
+ }
@@ -2,6 +2,26 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.COUNTRY_CONFIGS = void 0;
4
4
  const types_js_1 = require("../../types.js");
5
+ const validators_js_1 = require("../validators.js");
6
+ /**
7
+ * Build a country-independent IBAN pattern from the ISO 13616 length table.
8
+ *
9
+ * An IBAN carries its own country code and a mod-97 checksum, so it is
10
+ * self-identifying and must never depend on the caller's `countries` setting —
11
+ * a Belgian account in an Austrian file is still an account.
12
+ *
13
+ * Each alternative pins the *exact* character count for its country, which
14
+ * stops the match absorbing a following word: without it, greedy matching
15
+ * turns `BE68 5390 0754 7034 KBC` into one over-long candidate that fails
16
+ * mod-97 and is lost entirely.
17
+ */
18
+ function buildIbanPattern() {
19
+ const alternatives = Object.entries(validators_js_1.IBAN_LENGTHS)
20
+ .sort(([a], [b]) => a.localeCompare(b))
21
+ .map(([code, length]) => `${code}\\d{2}(?:[ \\-]?[A-Z0-9]){${length - 4}}`);
22
+ return String.raw `\b(?:` + alternatives.join("|") + String.raw `)\b`;
23
+ }
24
+ const GENERIC_IBAN = buildIbanPattern();
5
25
  /**
6
26
  * Helper to create a PatternDef with sensible defaults.
7
27
  */
@@ -9,6 +29,45 @@ function p(entityType, pattern, validator = null, description = "", contextKeywo
9
29
  return { entityType, pattern, validator, description, contextKeywords, requiresContext };
10
30
  }
11
31
  // ---------------------------------------------------------------------------
32
+ // Secret context keywords (consolidated, reused across SECRET patterns)
33
+ // ---------------------------------------------------------------------------
34
+ const SECRET_CONTEXT = [
35
+ // English
36
+ "key", "token", "secret", "password", "credential",
37
+ "api_key", "apikey", "api-key", "auth", "bearer",
38
+ "aws_secret_access_key", "access_key", "private_key",
39
+ "AES_KEY", "ENCRYPTION_KEY", "SIGNING_KEY",
40
+ "ACCOUNT_SID", "AUTH_TOKEN", "TWILIO",
41
+ "RAILS_SECRET", "SECRET_KEY_BASE", "DATABASE_URL",
42
+ "NPM_TOKEN", "DIGITALOCEAN", "GOOGLE_API",
43
+ "DB_PASS", "DB_PASSWORD", "DATASOURCE_PASSWORD",
44
+ "JWT_SIGNING", "passwd", "DISCORD_TOKEN",
45
+ // Dutch
46
+ "wachtwoord", "sleutel", "geheim",
47
+ // French
48
+ "mot de passe", "mot_de_passe", "clé", "jeton",
49
+ // German
50
+ "Passwort", "Kennwort", "Schlüssel", "Geheimnis",
51
+ // Italian
52
+ "chiave", "segreto",
53
+ // Spanish
54
+ "contraseña", "clave",
55
+ // Portuguese
56
+ "senha", "chave",
57
+ // Polish
58
+ "hasło", "klucz",
59
+ // Czech/Slovak
60
+ "heslo", "kľúč",
61
+ // Hungarian
62
+ "jelszó", "kulcs",
63
+ // Romanian
64
+ "parola",
65
+ // Nordic
66
+ "lösenord", "nøkkel", "nyckel", "avain", "lykill",
67
+ // Greek
68
+ "κωδικός",
69
+ ];
70
+ // ---------------------------------------------------------------------------
12
71
  // DOB context keywords (reused across SHARED patterns)
13
72
  // ---------------------------------------------------------------------------
14
73
  const DOB_CONTEXT = [
@@ -36,8 +95,28 @@ const SHARED = {
36
95
  code: "SHARED",
37
96
  name: "Shared EU Patterns",
38
97
  patterns: [
39
- p(types_js_1.EntityType.EMAIL, String.raw `\b[\w._%+\-]+@[\w.\-]+\.[a-zA-Z]{2,}\b`),
98
+ // JavaScript's \w and \b are ASCII-only, so the Python engine's \w (which
99
+ // is Unicode-aware) matched local parts like "petras_ž@..." while this one
100
+ // did not. \p{L}\p{N} with the `u` flag restores parity; the leading guard
101
+ // is a Unicode-safe word boundary.
102
+ p(types_js_1.EntityType.EMAIL, String.raw `(?<![\p{L}\p{N}._%+\-])[\p{L}\p{N}._%+\-]+@(?:[\p{L}\p{N}\-]+\.)+\p{L}{2,}\b`),
103
+ // IBAN (any country) — structure + mod-97 only. Country-specific IBAN
104
+ // patterns still exist and win on identical spans; this one guarantees an
105
+ // IBAN is never missed just because its country was not requested.
106
+ p(types_js_1.EntityType.BANK_ACCOUNT, GENERIC_IBAN, "iban", "IBAN — any country (ISO 13616, mod-97 validated)"),
107
+ // International phone (E.164, any country). Runs alongside the per-country
108
+ // patterns, which each hard-code a single grouping and miss the rest:
109
+ // "+43 664 8213 907" was invisible to the Austrian pattern, which expects
110
+ // an unbroken subscriber number. A leading "+" is self-identifying, so
111
+ // this is deliberately not country-gated.
112
+ p(types_js_1.EntityType.PHONE, String.raw `\+\d{1,3}(?:[\s\-/]?\(?\d{1,4}\)?){2,7}`, "e164", "International phone number (E.164) — any country"),
40
113
  p(types_js_1.EntityType.BIC, String.raw `\b[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b`, "bic"),
114
+ // Space-separated form: "RZBA AT WW", "NICA BE BB". Written as a separate
115
+ // pattern requiring a space between *every* group, rather than making the
116
+ // spaces optional above — optional spaces would let "GEBABEBB KBC" match
117
+ // as one 11-character code and claim the following word. Only a literal
118
+ // space is allowed, so a match cannot span a line break.
119
+ p(types_js_1.EntityType.BIC, String.raw `\b[A-Z]{4} [A-Z]{2} [A-Z0-9]{2}(?: [A-Z0-9]{3})?\b`, "bic", "BIC/SWIFT code (space-separated groups)"),
41
120
  p(types_js_1.EntityType.CREDIT_CARD, String.raw `\b(?:4[0-9]{3}[\s\-]?[0-9]{4}[\s\-]?[0-9]{4}[\s\-]?[0-9]{4}|5[1-5][0-9]{2}[\s\-]?[0-9]{4}[\s\-]?[0-9]{4}[\s\-]?[0-9]{4}|3[47][0-9]{2}[\s\-]?[0-9]{6}[\s\-]?[0-9]{5})\b`, "luhn"),
42
121
  p(types_js_1.EntityType.VIN, String.raw `\b[A-HJ-NPR-Z0-9]{17}\b`, "vin"),
43
122
  p(types_js_1.EntityType.IP_ADDRESS, String.raw `\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b`),
@@ -48,7 +127,40 @@ const SHARED = {
48
127
  p(types_js_1.EntityType.IMEI, String.raw `\b\d{2}[\-\s]\d{6}[\-\s]\d{6}[\-\s]\d\b`, "imei", "formatted"),
49
128
  p(types_js_1.EntityType.GPS_COORDINATES, String.raw `-?(?:[1-8]?\d(?:\.\d{4,})|90(?:\.0{4,}))\s*[,;/]\s*-?(?:1[0-7]\d(?:\.\d{4,})|180(?:\.0{4,})|\d{1,2}(?:\.\d{4,}))`),
50
129
  p(types_js_1.EntityType.UUID, String.raw `\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\b`),
51
- p(types_js_1.EntityType.SOCIAL_HANDLE, String.raw `(?<!\w)@[a-zA-Z][a-zA-Z0-9_.]{1,29}\b`),
130
+ p(types_js_1.EntityType.SOCIAL_HANDLE, String.raw `(?<![\p{L}\p{N}_])@\p{L}[\p{L}\p{N}_.]{1,29}\b`),
131
+ // --- Secret / API Key (known prefixes — always active) ---
132
+ p(types_js_1.EntityType.SECRET, String.raw `\bAKIA[A-Z0-9]{16}\b`, null, "AWS Access Key ID"),
133
+ p(types_js_1.EntityType.SECRET, String.raw `\bgh[poas]_[a-zA-Z0-9]{36,}\b`, null, "GitHub token"),
134
+ p(types_js_1.EntityType.SECRET, String.raw `\bgithub_pat_[a-zA-Z0-9_]{22,}\b`, null, "GitHub fine-grained PAT"),
135
+ p(types_js_1.EntityType.SECRET, String.raw `\bgl[a-z]t-[a-zA-Z0-9_\-]{20,}\b`, null, "GitLab token"),
136
+ p(types_js_1.EntityType.SECRET, String.raw `\bglpat-[a-zA-Z0-9_\-]{20,}\b`, null, "GitLab PAT"),
137
+ p(types_js_1.EntityType.SECRET, String.raw `\b[sp]k_(?:live|test)_[a-zA-Z0-9]{24,}\b`, null, "Stripe key"),
138
+ p(types_js_1.EntityType.SECRET, String.raw `\bsk-(?:ant-)?[a-zA-Z0-9\-_]{20,}\b`, null, "OpenAI/Anthropic key"),
139
+ p(types_js_1.EntityType.SECRET, String.raw `\bxox[bpas]-\d[\d\-]{10,}`, null, "Slack numeric token"),
140
+ p(types_js_1.EntityType.SECRET, String.raw `\bxox[bpas]-[a-zA-Z0-9\-]{10,}`, null, "Slack token"),
141
+ p(types_js_1.EntityType.SECRET, String.raw `\beyJ[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+\b`, null, "JWT token"),
142
+ p(types_js_1.EntityType.SECRET, String.raw `\bSG\.[a-zA-Z0-9_\-]{22}\.[a-zA-Z0-9_\-]{43}\b`, null, "SendGrid key"),
143
+ p(types_js_1.EntityType.SECRET, String.raw `\bhvs\.[a-zA-Z0-9_\-]{20,}\b`, null, "Vault token"),
144
+ p(types_js_1.EntityType.SECRET, String.raw `\bkey-[a-f0-9]{20,}\b`, null, "Mailgun key"),
145
+ p(types_js_1.EntityType.SECRET, String.raw `\bnpm_[a-zA-Z0-9]{36,}\b`, null, "npm token"),
146
+ p(types_js_1.EntityType.SECRET, String.raw `\bAIza[a-zA-Z0-9_\-]{35}\b`, null, "Google API key"),
147
+ p(types_js_1.EntityType.SECRET, String.raw `\bdop_v1_[a-f0-9]{64}\b`, null, "DigitalOcean token"),
148
+ p(types_js_1.EntityType.SECRET, String.raw `\bAC[a-f0-9]{32}\b`, null, "Twilio Account SID"),
149
+ p(types_js_1.EntityType.SECRET, String.raw `\bxapp-\d[\d\-]{20,}`, null, "Slack app numeric token"),
150
+ p(types_js_1.EntityType.SECRET, String.raw `\bxapp-[a-zA-Z0-9\-]{20,}`, null, "Slack app token"),
151
+ p(types_js_1.EntityType.SECRET, String.raw `\bdapi[a-f0-9]{32,}\b`, null, "Databricks PAT"),
152
+ // Discord bot token (3-segment base64 with numeric first segment)
153
+ p(types_js_1.EntityType.SECRET, String.raw `\b[MN][A-Za-z0-9]{23,}\.[A-Za-z0-9_\-]{6}\.[A-Za-z0-9_\-]{27,}\b`, null, "Discord bot token"),
154
+ // PEM private key headers
155
+ p(types_js_1.EntityType.SECRET, String.raw `-----BEGIN\s(?:RSA\s)?PRIVATE\sKEY-----[\s\S]{0,16384}?-----END\s(?:RSA\s)?PRIVATE\sKEY-----`, null, "PEM private key"),
156
+ // AWS Secret Access Key (40 chars, base64 with +/ after an AKIA line)
157
+ p(types_js_1.EntityType.SECRET, String.raw `(?<=aws_secret_access_key\s{0,4}[:=]\s{0,4})[A-Za-z0-9+/]{40}`, null, "AWS Secret Access Key"),
158
+ // --- Secret / API Key (connection strings with embedded credentials) ---
159
+ p(types_js_1.EntityType.SECRET, String.raw `(?:mongodb|mysql|postgres(?:ql)?|redis|amqp|rabbitmq):\/\/[^\s:]{1,256}:[^\s@]{1,256}@[^\s]+`, null, "Connection string with credentials"),
160
+ // --- Secret / API Key (assignment-based: KEY=value or KEY: value) ---
161
+ p(types_js_1.EntityType.SECRET, String.raw `(?<=[:=] )\S{8,}|(?<=[:=])\S{8,}`, "high_entropy", "Assigned secret value", SECRET_CONTEXT, true),
162
+ // --- Secret / API Key (entropy-based fallback for longer tokens) ---
163
+ p(types_js_1.EntityType.SECRET, String.raw `\b[A-Za-z0-9_\-+/]{24,}[A-Za-z0-9_\-+/=]*\b`, "high_entropy", "High-entropy token", SECRET_CONTEXT, true),
52
164
  p(types_js_1.EntityType.DOB, String.raw `\b(?:0[1-9]|[12][0-9]|3[01])[/.\-](?:0[1-9]|1[0-2])[/.\-](?:19|20)\d{2}\b`, null, "DD/MM/YYYY", DOB_CONTEXT, true),
53
165
  p(types_js_1.EntityType.DATE_OF_DEATH, String.raw `\b(?:0[1-9]|[12][0-9]|3[01])[/.\-](?:0[1-9]|1[0-2])[/.\-](?:19|20)\d{2}\b`, null, "", DATE_OF_DEATH_CONTEXT, true),
54
166
  p(types_js_1.EntityType.DOB, String.raw `\b(?:19|20)\d{2}[/.\-](?:0[1-9]|1[0-2])[/.\-](?:0[1-9]|[12][0-9]|3[01])\b`, null, "ISO YYYY-MM-DD", DOB_ISO_CONTEXT, true),
@@ -122,6 +234,11 @@ const AT = {
122
234
  p(types_js_1.EntityType.IBAN, String.raw `\bAT\d{2}\s?\d{4}\s?\d{4}\s?\d{4}\s?\d{4}\b`, "iban"),
123
235
  p(types_js_1.EntityType.VAT, String.raw `\bATU\d{8}\b`),
124
236
  p(types_js_1.EntityType.PHONE, String.raw `\b0\d{3,4}[\s\-]?\d{5,8}\b`),
237
+ // Short area codes (Vienna is "01") split across three groups:
238
+ // "01 53460 2215". The pattern above requires 3-4 digits after the trunk
239
+ // 0, so it never matched these. Both separators are mandatory here, which
240
+ // keeps "01 2025" (a date fragment) out.
241
+ p(types_js_1.EntityType.PHONE, String.raw `\b0\d{1,4}[\s\-/]\d{3,7}[\s\-/]\d{2,6}\b`, null, "Austrian national phone — short area code, grouped"),
125
242
  p(types_js_1.EntityType.PHONE, String.raw `\+43\s?\d{3,4}[\s\-]?\d{5,8}`),
126
243
  p(types_js_1.EntityType.LICENSE_PLATE, String.raw `\b[A-ZÄÖÜ]{1,2}\s?\d{1,5}\s?[A-Z]{1,2}\b`, null, "", ["Kennzeichen", "Nummernschild", "Kfz-Kennzeichen"], true),
127
244
  p(types_js_1.EntityType.POSTAL_CODE, String.raw `\b[1-9]\d{3}\b`, null, "", ["PLZ", "Postleitzahl", "Adresse", "Anschrift", "Straße", "Str.", "Gasse", "Weg", "Platz", "Postal:", "Wohnort"], true),
@@ -1,14 +1,73 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RuleEngine = void 0;
3
+ exports.RuleEngine = exports.COUNTRY_CODE_ALIASES = void 0;
4
+ exports.resolveCountryCode = resolveCountryCode;
4
5
  const types_js_1 = require("../types.js");
5
6
  const matchers_js_1 = require("./matchers.js");
6
7
  const suppressors_js_1 = require("./suppressors.js");
7
8
  const structural_js_1 = require("./structural.js");
8
9
  const index_js_1 = require("./countries/index.js");
10
+ /**
11
+ * Heuristic to detect nested quantifiers that cause catastrophic backtracking.
12
+ * After stripping escaped chars and character classes, look for an unbounded
13
+ * quantifier (+/*) closing a group that is itself quantified (+/*).
14
+ */
15
+ const NESTED_QUANTIFIER_RE = /[+*]\)[+*]/;
16
+ function validateCustomPattern(pattern) {
17
+ try {
18
+ new RegExp(pattern, "gu");
19
+ }
20
+ catch (e) {
21
+ throw new Error(`Invalid regex pattern: ${e instanceof Error ? e.message : e}`);
22
+ }
23
+ const stripped = pattern
24
+ .replace(/\\./g, "X")
25
+ .replace(/\[(?:[^\]\\]|\\.)*\]/g, "X");
26
+ if (NESTED_QUANTIFIER_RE.test(stripped)) {
27
+ throw new Error("Pattern contains nested quantifiers (e.g. (a+)+) which can cause " +
28
+ "catastrophic backtracking (ReDoS). Restructure the pattern to " +
29
+ "avoid quantifiers inside quantified groups.");
30
+ }
31
+ }
32
+ /**
33
+ * The country modules use the EU/VAT convention (UK, EL) while the public
34
+ * contract is ISO 3166-1 alpha-2 (GB, GR). Accept both spellings so a caller
35
+ * passing correct ISO codes is not punished for it.
36
+ */
37
+ exports.COUNTRY_CODE_ALIASES = {
38
+ GB: "UK",
39
+ GR: "EL",
40
+ UK: "UK",
41
+ EL: "EL",
42
+ };
43
+ /** Map a caller-supplied code to a registered one, or null if unknown. */
44
+ function resolveCountryCode(countryCode) {
45
+ let code = countryCode.trim().toUpperCase();
46
+ code = exports.COUNTRY_CODE_ALIASES[code] ?? code;
47
+ return code in index_js_1.COUNTRY_CONFIGS && code !== "SHARED" ? code : null;
48
+ }
49
+ const warnedCountries = new Set();
50
+ function warnUnknownCountry(code) {
51
+ if (warnedCountries.has(code))
52
+ return;
53
+ warnedCountries.add(code);
54
+ const available = Object.keys(index_js_1.COUNTRY_CONFIGS).filter(c => c !== "SHARED").sort();
55
+ console.warn(`[euredact] Unknown country code: ${JSON.stringify(code)}. Continuing with ` +
56
+ `shared country-independent patterns only (email, IBAN, international ` +
57
+ `phone, credit card, ...). Available: ${available.join(", ")}`);
58
+ }
9
59
  class RuleEngine {
10
60
  matcher = new matchers_js_1.MultiPatternMatcher();
11
61
  loadedCountries = new Set();
62
+ addCustomPattern(name, pattern) {
63
+ validateCustomPattern(pattern);
64
+ const pdef = {
65
+ entityType: name, pattern, validator: null,
66
+ description: "", contextKeywords: [], requiresContext: false,
67
+ };
68
+ this.matcher.addPattern(pdef, "CUSTOM");
69
+ this.matcher.compile();
70
+ }
12
71
  loadCountries(countryCodes) {
13
72
  let configs;
14
73
  if (countryCodes === null) {
@@ -17,11 +76,18 @@ class RuleEngine {
17
76
  else {
18
77
  configs = [];
19
78
  for (const c of countryCodes) {
20
- const code = c.toUpperCase();
79
+ const code = resolveCountryCode(c);
80
+ if (code === null) {
81
+ // Detection continues with the shared, country-independent
82
+ // patterns. Throwing on an unknown locale invites callers to wrap
83
+ // the call in try/catch and skip redaction — failing open, with
84
+ // unredacted PII in the output.
85
+ warnUnknownCountry(c);
86
+ continue;
87
+ }
21
88
  const config = index_js_1.COUNTRY_CONFIGS[code];
22
89
  if (config)
23
90
  configs.push(config);
24
- // Silently skip unknown country codes (e.g. "INTL")
25
91
  }
26
92
  const shared = index_js_1.COUNTRY_CONFIGS["SHARED"];
27
93
  if (shared)
@@ -40,44 +106,131 @@ class RuleEngine {
40
106
  }
41
107
  detect(text, countryCodes) {
42
108
  this.loadCountries(countryCodes);
43
- let rawMatches = this.matcher.scan(text);
109
+ // Scope the scan to requested countries for efficiency
110
+ // Resolve through the alias table so `countries: ["GB"]` actually scans
111
+ // the patterns registered under "UK".
112
+ let scanCodes = null;
44
113
  if (countryCodes !== null) {
45
- const codesUpper = new Set(countryCodes.map(c => c.toUpperCase()));
46
- rawMatches = rawMatches.filter(m => codesUpper.has(m.countryCode) || m.countryCode === "SHARED");
114
+ scanCodes = new Set();
115
+ for (const c of countryCodes) {
116
+ const code = resolveCountryCode(c);
117
+ if (code !== null)
118
+ scanCodes.add(code);
119
+ }
120
+ scanCodes.add("SHARED");
121
+ scanCodes.add("CUSTOM");
47
122
  }
48
- const validated = rawMatches.filter(m => this.matcher.validate(m));
49
- const detections = [];
50
- for (const match of validated) {
123
+ let rawMatches = this.matcher.scan(text, scanCodes);
124
+ // Matches that have a validator but fail it create suppression zones:
125
+ // the span is recognisably a specific entity (e.g. IBAN-shaped) so
126
+ // overlapping regex-only matches (license plate, phone) are false
127
+ // positives and must be suppressed.
128
+ const validated = [];
129
+ const rawZones = [];
130
+ for (const m of rawMatches) {
131
+ const isValid = this.matcher.validate(m);
132
+ if (isValid) {
133
+ validated.push({ match: m, hasValidValidator: m.patternDef.validator !== null });
134
+ }
135
+ else if (m.patternDef.validator !== null && !m.patternDef.requiresContext) {
136
+ rawZones.push([m.start, m.end]);
137
+ }
138
+ }
139
+ // Merge overlapping suppression zones for O(log n) containment checks
140
+ let zoneStarts = [];
141
+ let zoneEnds = [];
142
+ if (rawZones.length > 0) {
143
+ rawZones.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
144
+ let [ms, me] = rawZones[0];
145
+ for (let i = 1; i < rawZones.length; i++) {
146
+ const [zs, ze] = rawZones[i];
147
+ if (zs <= me) {
148
+ me = Math.max(me, ze);
149
+ }
150
+ else {
151
+ zoneStarts.push(ms);
152
+ zoneEnds.push(me);
153
+ ms = zs;
154
+ me = ze;
155
+ }
156
+ }
157
+ zoneStarts.push(ms);
158
+ zoneEnds.push(me);
159
+ }
160
+ // Build candidates with priority: validated (3) > custom (2) > regex-only (1)
161
+ const candidates = [];
162
+ for (const { match, hasValidValidator } of validated) {
51
163
  if ((0, suppressors_js_1.shouldSuppress)(text, match))
52
164
  continue;
53
- detections.push({
54
- entityType: match.patternDef.entityType,
55
- start: match.start,
56
- end: match.end,
57
- text: match.text,
58
- source: types_js_1.DetectionSource.RULES,
59
- country: match.countryCode !== "SHARED" ? match.countryCode : null,
60
- confidence: "high",
165
+ // O(log n) binary-search containment check on merged zones
166
+ if (match.patternDef.validator === null && zoneStarts.length > 0) {
167
+ let lo = 0, hi = zoneStarts.length;
168
+ while (lo < hi) {
169
+ const mid = (lo + hi) >>> 1;
170
+ if (zoneStarts[mid] <= match.start)
171
+ lo = mid + 1;
172
+ else
173
+ hi = mid;
174
+ }
175
+ const idx = lo - 1;
176
+ if (idx >= 0 && zoneEnds[idx] >= match.end)
177
+ continue;
178
+ }
179
+ // A bare digit run is the weakest evidence in the engine and must never
180
+ // re-cut a span a structured detector claims (PHONE, SSN, NATIONAL_ID,
181
+ // BANK_ACCOUNT, VAT...). Postal codes take unclaimed spans only,
182
+ // whatever the span lengths.
183
+ const priority = hasValidValidator
184
+ ? 3
185
+ : match.countryCode === "CUSTOM"
186
+ ? 2
187
+ : match.patternDef.entityType === types_js_1.EntityType.POSTAL_CODE
188
+ ? 0
189
+ : 1;
190
+ const spanLength = match.end - match.start;
191
+ const score = priority * 1_000_000 + spanLength;
192
+ candidates.push({
193
+ det: {
194
+ entityType: match.patternDef.entityType,
195
+ start: match.start,
196
+ end: match.end,
197
+ text: match.text,
198
+ source: types_js_1.DetectionSource.RULES,
199
+ country: (match.countryCode !== "SHARED" && match.countryCode !== "CUSTOM") ? match.countryCode : null,
200
+ confidence: "high",
201
+ },
202
+ score,
61
203
  });
62
204
  }
63
- detections.push(...(0, structural_js_1.detectStructuralDob)(text));
64
- return this.deduplicate(detections);
205
+ for (const d of (0, structural_js_1.detectStructuralDob)(text)) {
206
+ candidates.push({ det: d, score: 1_000_000 + (d.end - d.start) });
207
+ }
208
+ return this.deduplicate(candidates);
65
209
  }
66
- deduplicate(detections) {
67
- if (detections.length === 0)
68
- return detections;
69
- const sorted = [...detections].sort((a, b) => a.start - b.start || (b.end - b.start) - (a.end - a.start));
210
+ /**
211
+ * Remove overlapping detections with priority-aware resolution.
212
+ * Priority: validated (3) > custom (2) > regex-only (1).
213
+ * Within the same tier, longer span wins.
214
+ */
215
+ deduplicate(candidates) {
216
+ if (candidates.length === 0)
217
+ return [];
218
+ const sorted = [...candidates].sort((a, b) => b.score - a.score);
70
219
  const result = [];
71
- for (const det of sorted) {
220
+ const occupied = new Set();
221
+ for (const { det } of sorted) {
72
222
  let overlaps = false;
73
- for (const kept of result) {
74
- if (det.start < kept.end && det.end > kept.start) {
223
+ for (let p = det.start; p < det.end; p++) {
224
+ if (occupied.has(p)) {
75
225
  overlaps = true;
76
226
  break;
77
227
  }
78
228
  }
79
- if (!overlaps)
229
+ if (!overlaps) {
230
+ for (let p = det.start; p < det.end; p++)
231
+ occupied.add(p);
80
232
  result.push(det);
233
+ }
81
234
  }
82
235
  return result;
83
236
  }