@maskera/core 0.7.4 → 0.8.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/dist/index.d.cts CHANGED
@@ -52,7 +52,7 @@ declare function canonicalize(input: string): CanonicalText;
52
52
  * Built-in PII labels. The `string` fallback lets you add custom detectors
53
53
  * with arbitrary labels without fighting the type system.
54
54
  */
55
- type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
55
+ type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "KONTONUMMER" | "JOURNALNUMMER" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
56
56
  /** A raw match found by a detector, before placeholder assignment. */
57
57
  interface Detection {
58
58
  /** Inclusive start index into the source string. */
@@ -113,6 +113,31 @@ declare const personnummer: Detector;
113
113
  declare const samordningsnummer: Detector;
114
114
  /** Organisationsnummer: NNNNNN-NNNN, Luhn-checked, third digit >= 2. */
115
115
  declare const organisationsnummer: Detector;
116
+ /**
117
+ * E-post: `anna@example.com`, Unicode letters on both sides of the @.
118
+ *
119
+ * Not built with regexDetector, because the bounded quantifiers above
120
+ * reintroduced a leak of their own: a local part or domain LONGER than its
121
+ * window still matches, but only the window was reported and the rest of the
122
+ * address stayed in the clear - "anna.svensson." + 70 x's masked as
123
+ * "anna.svensson.xxxxxxxx[EPOST_1]", and `anna@` + "sub."×80 + "example.com"
124
+ * masked as "[EPOST_1].sub.sub…example.com". So after each match the span is
125
+ * widened to the whole contiguous run: LEFT while the characters belong to
126
+ * the local-part class, RIGHT while they belong to the domain class. The two
127
+ * classes differ (`_`, `%`, `+` are local-only), and which one applies is
128
+ * decided by position, not by where the window was cut: the pattern requires
129
+ * at least one local-part character before the literal `@`, so the match
130
+ * always starts inside the local part and always ends inside the domain, and
131
+ * `@` itself is in neither class - which also bounds each walk at the nearest
132
+ * `@`, keeping detection linear on hostile runs. Over-masking the rest of the
133
+ * run is the right side to err on for a redaction tool: a clipped run is
134
+ * never safe to leave visible, and `resolveOverlaps()` in redact already
135
+ * arbitrates any span this grows into. The one exception is the trailing
136
+ * edge: a "." or "-" can never end a domain label, so characters like the
137
+ * sentence period in "mejlar anna@example.com." are trimmed back off the
138
+ * span - masking them would mangle the prose around the token for no
139
+ * redaction gain.
140
+ */
116
141
  declare const email: Detector;
117
142
  /**
118
143
  * Phone numbers. Swedish: +46 / 0 prefix, mobile and landline, including the
@@ -153,6 +178,21 @@ declare const bankgiro: Detector;
153
178
  declare const plusgiro: Detector;
154
179
  /** Swedish IBAN: SE + 22 digits (spaces tolerated). */
155
180
  declare const iban: Detector;
181
+ /**
182
+ * Contextual domestic account number. Sweden has no single checksum-backed
183
+ * syntax for ordinary bank accounts, so the digit shape alone is much too
184
+ * broad: it also describes customer, order and journal identifiers. Requiring
185
+ * an explicit account label keeps this useful without turning every grouped
186
+ * number into PII. This detector is opt-in for rules-only callers and enabled
187
+ * by the hybrid pipeline, whose input is normally free-form customer text.
188
+ */
189
+ declare const kontonummer: Detector;
190
+ /**
191
+ * Context-labeled journal identifier. The identifier format is provider-
192
+ * specific, but the explicit Swedish field label is strong evidence and keeps
193
+ * checksum-shaped values from being misclassified as payment identifiers.
194
+ */
195
+ declare const journalnummer: Detector;
156
196
  declare const creditCard: Detector;
157
197
  declare const ipAddress: Detector;
158
198
  declare const url: Detector;
@@ -179,6 +219,8 @@ declare const regnummer: Detector;
179
219
  * `redact(text, { detectors: [...defaultDetectors, ...heuristicDetectors] })`.
180
220
  */
181
221
  declare const heuristicDetectors: Detector[];
222
+ /** Context-dependent formats that are safe only when their label is present. */
223
+ declare const contextualDetectors: Detector[];
182
224
  /**
183
225
  * Default detector set, ordered so that the most specific / highest-confidence
184
226
  * detectors run first. Overlap resolution keeps the longest, earliest match.
@@ -254,4 +296,4 @@ declare function isSamordningsnummer(value: string): boolean;
254
296
  /** Swedish organisationsnummer: 10 digits, third digit >= 2, Luhn valid. */
255
297
  declare function isOrganisationsnummer(value: string): boolean;
256
298
 
257
- export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
299
+ export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
package/dist/index.d.ts CHANGED
@@ -52,7 +52,7 @@ declare function canonicalize(input: string): CanonicalText;
52
52
  * Built-in PII labels. The `string` fallback lets you add custom detectors
53
53
  * with arbitrary labels without fighting the type system.
54
54
  */
55
- type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
55
+ type PiiLabel = "PERSONNUMMER" | "SAMORDNINGSNUMMER" | "ORGANISATIONSNUMMER" | "EPOST" | "TELEFON" | "POSTNUMMER" | "BANKGIRO" | "PLUSGIRO" | "IBAN" | "IP_ADRESS" | "URL" | "KORTNUMMER" | "KONTONUMMER" | "JOURNALNUMMER" | "ADRESS" | "LAGENHETSNUMMER" | "REGNUMMER" | (string & {});
56
56
  /** A raw match found by a detector, before placeholder assignment. */
57
57
  interface Detection {
58
58
  /** Inclusive start index into the source string. */
@@ -113,6 +113,31 @@ declare const personnummer: Detector;
113
113
  declare const samordningsnummer: Detector;
114
114
  /** Organisationsnummer: NNNNNN-NNNN, Luhn-checked, third digit >= 2. */
115
115
  declare const organisationsnummer: Detector;
116
+ /**
117
+ * E-post: `anna@example.com`, Unicode letters on both sides of the @.
118
+ *
119
+ * Not built with regexDetector, because the bounded quantifiers above
120
+ * reintroduced a leak of their own: a local part or domain LONGER than its
121
+ * window still matches, but only the window was reported and the rest of the
122
+ * address stayed in the clear - "anna.svensson." + 70 x's masked as
123
+ * "anna.svensson.xxxxxxxx[EPOST_1]", and `anna@` + "sub."×80 + "example.com"
124
+ * masked as "[EPOST_1].sub.sub…example.com". So after each match the span is
125
+ * widened to the whole contiguous run: LEFT while the characters belong to
126
+ * the local-part class, RIGHT while they belong to the domain class. The two
127
+ * classes differ (`_`, `%`, `+` are local-only), and which one applies is
128
+ * decided by position, not by where the window was cut: the pattern requires
129
+ * at least one local-part character before the literal `@`, so the match
130
+ * always starts inside the local part and always ends inside the domain, and
131
+ * `@` itself is in neither class - which also bounds each walk at the nearest
132
+ * `@`, keeping detection linear on hostile runs. Over-masking the rest of the
133
+ * run is the right side to err on for a redaction tool: a clipped run is
134
+ * never safe to leave visible, and `resolveOverlaps()` in redact already
135
+ * arbitrates any span this grows into. The one exception is the trailing
136
+ * edge: a "." or "-" can never end a domain label, so characters like the
137
+ * sentence period in "mejlar anna@example.com." are trimmed back off the
138
+ * span - masking them would mangle the prose around the token for no
139
+ * redaction gain.
140
+ */
116
141
  declare const email: Detector;
117
142
  /**
118
143
  * Phone numbers. Swedish: +46 / 0 prefix, mobile and landline, including the
@@ -153,6 +178,21 @@ declare const bankgiro: Detector;
153
178
  declare const plusgiro: Detector;
154
179
  /** Swedish IBAN: SE + 22 digits (spaces tolerated). */
155
180
  declare const iban: Detector;
181
+ /**
182
+ * Contextual domestic account number. Sweden has no single checksum-backed
183
+ * syntax for ordinary bank accounts, so the digit shape alone is much too
184
+ * broad: it also describes customer, order and journal identifiers. Requiring
185
+ * an explicit account label keeps this useful without turning every grouped
186
+ * number into PII. This detector is opt-in for rules-only callers and enabled
187
+ * by the hybrid pipeline, whose input is normally free-form customer text.
188
+ */
189
+ declare const kontonummer: Detector;
190
+ /**
191
+ * Context-labeled journal identifier. The identifier format is provider-
192
+ * specific, but the explicit Swedish field label is strong evidence and keeps
193
+ * checksum-shaped values from being misclassified as payment identifiers.
194
+ */
195
+ declare const journalnummer: Detector;
156
196
  declare const creditCard: Detector;
157
197
  declare const ipAddress: Detector;
158
198
  declare const url: Detector;
@@ -179,6 +219,8 @@ declare const regnummer: Detector;
179
219
  * `redact(text, { detectors: [...defaultDetectors, ...heuristicDetectors] })`.
180
220
  */
181
221
  declare const heuristicDetectors: Detector[];
222
+ /** Context-dependent formats that are safe only when their label is present. */
223
+ declare const contextualDetectors: Detector[];
182
224
  /**
183
225
  * Default detector set, ordered so that the most specific / highest-confidence
184
226
  * detectors run first. Overlap resolution keeps the longest, earliest match.
@@ -254,4 +296,4 @@ declare function isSamordningsnummer(value: string): boolean;
254
296
  /** Swedish organisationsnummer: 10 digits, third digit >= 2, Luhn valid. */
255
297
  declare function isOrganisationsnummer(value: string): boolean;
256
298
 
257
- export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
299
+ export { type CanonicalText, type Detection, type Detector, type PiiLabel, type RedactOptions, type RedactResult, type Redaction, adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
package/dist/index.js CHANGED
@@ -178,7 +178,18 @@ function regexDetector(label, regex, validate) {
178
178
  const value = m[group];
179
179
  if (value.length === 0) continue;
180
180
  const span = hasIndices ? m.indices?.[group] : void 0;
181
- const start = span ? span[0] : m.index + m[0].indexOf(value);
181
+ let start;
182
+ if (span) {
183
+ start = span[0];
184
+ } else {
185
+ const first = m[0].indexOf(value);
186
+ if (first !== m[0].lastIndexOf(value)) {
187
+ throw new Error(
188
+ `maskera: detector "${label}" matched a value that occurs more than once inside its own match, and this engine lacks regex match indices (the "d" flag, Chrome 90 / Safari 15 / Node 16) to tell which occurrence is the value. Upgrade the runtime, or rewrite the pattern so the captured value appears only once per match.`
189
+ );
190
+ }
191
+ start = m.index + first;
192
+ }
182
193
  if (validate && !validate(value)) continue;
183
194
  out.push({ start, end: start + value.length, value });
184
195
  }
@@ -259,10 +270,27 @@ var organisationsnummer = regexDetector(
259
270
  /\b\d{6}[-]?\d{4}\b/g,
260
271
  isOrganisationsnummer
261
272
  );
262
- var email = regexDetector(
263
- "EPOST",
264
- /[\p{L}\p{N}._%+-]{1,64}@[\p{L}\p{N}.-]{1,255}\.\p{L}{2,63}\b/giu
265
- );
273
+ var EMAIL_PATTERN = /[\p{L}\p{N}._%+-]{1,64}@[\p{L}\p{N}.-]{1,255}\.\p{L}{2,63}\b/giu;
274
+ var EMAIL_LOCAL_CHAR = /[\p{L}\p{N}._%+-]/u;
275
+ var EMAIL_DOMAIN_CHAR = /[\p{L}\p{N}.-]/u;
276
+ var EMAIL_ALNUM = /[\p{L}\p{N}]/u;
277
+ var email = {
278
+ label: "EPOST",
279
+ detect(input) {
280
+ const out = [];
281
+ EMAIL_PATTERN.lastIndex = 0;
282
+ for (const m of input.matchAll(EMAIL_PATTERN)) {
283
+ if (m.index === void 0) continue;
284
+ let start = m.index;
285
+ let end = m.index + m[0].length;
286
+ while (start > 0 && EMAIL_LOCAL_CHAR.test(input[start - 1])) start--;
287
+ while (end < input.length && EMAIL_DOMAIN_CHAR.test(input[end])) end++;
288
+ while (end > start && !EMAIL_ALNUM.test(input[end - 1])) end--;
289
+ out.push({ start, end, value: input.slice(start, end) });
290
+ }
291
+ return out;
292
+ }
293
+ };
266
294
  var phone = regexDetector(
267
295
  "TELEFON",
268
296
  /(?:^|[^\d])((?:\+46[\s-]?(?:\(0\)[\s-]?)?|0)(?:7[02369]|[1-9]\d?)(?:[\s-]?\d){6,8}|\+\d{1,3}(?:[\s-]?\d){6,12})\b/g
@@ -282,6 +310,18 @@ var plusgiro = regexDetector(
282
310
  (v) => luhnValid(v.replace(/\D/g, ""))
283
311
  );
284
312
  var iban = regexDetector("IBAN", /\bSE\d{2}(?:\s?\d){20}\b/gi);
313
+ var kontonummer = regexDetector(
314
+ "KONTONUMMER",
315
+ /(?:^|[^\p{L}\p{N}])(?:bankkonto|kontonummer|utbetalningskonto|konto)(?:\s+nr\.?)?\s*[:#-]?\s*((?:\d[ -]?){6,19}\d)\b/giu,
316
+ (value) => {
317
+ const digits = value.replace(/\D/g, "");
318
+ return digits.length >= 7 && digits.length <= 20;
319
+ }
320
+ );
321
+ var journalnummer = regexDetector(
322
+ "JOURNALNUMMER",
323
+ /(?:^|[^\p{L}\p{N}])(?:journalnummer|journalnr|journal-id|journalid)\s*[:#-]?\s*((?:[\p{L}\p{N}]{2,12}[-/]){1,3}[\p{L}\p{N}]{2,12}|\d{5,20})\b/giu
324
+ );
285
325
  var creditCard = regexDetector("KORTNUMMER", /\b\d(?:[ -]?\d){12,18}\b/g, (v) => {
286
326
  const digits = v.replace(/\D/g, "");
287
327
  return digits.length >= 13 && digits.length <= 19 && luhnValid(digits);
@@ -293,7 +333,7 @@ var ipAddress = regexDetector(
293
333
  var url = regexDetector("URL", /\b(?:https?:\/\/|www\.)[^\s<>")]*[^\s<>").,;:!?]/gi);
294
334
  var adress = regexDetector(
295
335
  "ADRESS",
296
- /(?:^|[^A-Za-zÅÄÖåäö0-9])((?:(?:[A-ZÅÄÖ][a-zåäö]+\s)?[A-ZÅÄÖa-zåäö][a-zåäö]*(?:gatan|vägen|gränd|gränden|torget|stigen|backen|allén|plan|gata|väg)|[A-ZÅÄÖ]+(?:GATAN|VÄGEN|GRÄND|GRÄNDEN|TORGET|STIGEN|BACKEN|ALLÉN|PLAN|GATA|VÄG))\s?\d{1,3}[A-Za-z]?)\b/g
336
+ /(?:^|[^A-Za-zÅÄÖåäö0-9])((?:(?:[A-ZÅÄÖ][a-zåäö]+\s)?[A-ZÅÄÖa-zåäö][a-zåäö]*(?:gatan|vägen|gränd|gränden|torget|stigen|backen|allén|gången|kajen|stranden|terrassen|leden|stråket|plan|gata|väg)|[A-ZÅÄÖ]+(?:GATAN|VÄGEN|GRÄND|GRÄNDEN|TORGET|STIGEN|BACKEN|ALLÉN|GÅNGEN|KAJEN|STRANDEN|TERRASSEN|LEDEN|STRÅKET|PLAN|GATA|VÄG))\s?\d{1,3}[A-Za-z]?)\b/g
297
337
  );
298
338
  var lagenhetsnummer = regexDetector(
299
339
  "LAGENHETSNUMMER",
@@ -306,6 +346,7 @@ var regnummer = regexDetector(
306
346
  (v) => !CURRENCY_CODES.has(v.slice(0, 3))
307
347
  );
308
348
  var heuristicDetectors = [adress, lagenhetsnummer, regnummer];
349
+ var contextualDetectors = [kontonummer, journalnummer];
309
350
  var defaultDetectors = [
310
351
  email,
311
352
  url,
@@ -326,6 +367,8 @@ function defaultPlaceholder(label, index) {
326
367
  return `[${label}_${index}]`;
327
368
  }
328
369
  var SAME_SPAN_PRIORITY = {
370
+ JOURNALNUMMER: -10,
371
+ KONTONUMMER: -10,
329
372
  TELEFON: 0,
330
373
  PERSONNUMMER: 10,
331
374
  SAMORDNINGSNUMMER: 11
@@ -438,6 +481,7 @@ function redactFromDetections(input, detections, options = {}) {
438
481
  }
439
482
  const resolved = resolveOverlaps(detections, input);
440
483
  const counters = /* @__PURE__ */ new Map();
484
+ const shortestToken = /* @__PURE__ */ new Map();
441
485
  const tokenByKey = /* @__PURE__ */ new Map();
442
486
  const map = {};
443
487
  const redactions = [];
@@ -452,8 +496,10 @@ function redactFromDetections(input, detections, options = {}) {
452
496
  next += 1;
453
497
  token = placeholder(d.label, next);
454
498
  attempts += 1;
499
+ const shortest = Math.min(shortestToken.get(d.label) ?? Infinity, token.length);
500
+ shortestToken.set(d.label, shortest);
455
501
  if (token.length > 0 && !occursInInput(d.label, token) && !(token in map)) break;
456
- if (token.length === 0 || attempts > 8 + input.length / token.length) {
502
+ if (token.length === 0 || attempts > 8 + input.length / shortest) {
457
503
  throw new Error(
458
504
  "maskera: placeholder() must return a token that is unique per (label, index) and does not occur in the input"
459
505
  );
@@ -524,6 +570,6 @@ function restore(text, map) {
524
570
  return out + text.slice(last);
525
571
  }
526
572
 
527
- export { adress, bankgiro, canonicalize, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
573
+ export { adress, bankgiro, canonicalize, contextualDetectors, creditCard, defaultDetectors, email, heuristicDetectors, iban, ipAddress, isOrganisationsnummer, isPersonnummer, isPersonnummerShape, isSamordningsnummer, isSamordningsnummerShape, journalnummer, kontonummer, lagenhetsnummer, luhnValid, organisationsnummer, personnummer, phone, plusgiro, postnummer, redact, redactFromDetections, regexDetector, regnummer, restore, runDetectors, samordningsnummer, url };
528
574
  //# sourceMappingURL=index.js.map
529
575
  //# sourceMappingURL=index.js.map