@link-foundation/sensitive-data-sanitizer 0.16.0 → 0.16.1
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/CHANGELOG.md +6 -0
- package/docs/COVERAGE.md +68 -0
- package/package.json +1 -1
- package/src/context.js +17 -0
- package/src/detection.js +3 -1
- package/src/document-entities.js +2 -0
- package/src/document-patterns.js +224 -0
- package/src/engines.js +16 -0
- package/src/sanitizer.js +8 -4
- package/src/structured.js +84 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.16.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4dfe2b7: Scan standalone and embedded JSON string values with their own line boundaries. Extend the documents profile's contact, identity, name, attribute, employer, school and residence rules, and preserve ordinary relative URLs, operational timestamps and context-free SHA-1-shaped values consistently.
|
|
8
|
+
|
|
3
9
|
## 0.16.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/docs/COVERAGE.md
CHANGED
|
@@ -202,3 +202,71 @@ Structured JSONL streaming supports 8 MiB records and ordered record workers;
|
|
|
202
202
|
and reproducible throughput measurement. Realistic fake mode preserves supported
|
|
203
203
|
scripts/formats/checksums and uses reserved contact ranges where available.
|
|
204
204
|
Generated identities are pseudonyms; valid arithmetic does not prove non-issuance.
|
|
205
|
+
|
|
206
|
+
## Document forms reviewed in issues 77–83
|
|
207
|
+
|
|
208
|
+
Structured JSON/JSONL scans each decoded string at its own line start as well as
|
|
209
|
+
with its field label. This includes array elements, multiline strings and up to
|
|
210
|
+
two embedded JSON container layers. Both the synchronous API and asynchronous
|
|
211
|
+
engine union use these views. Source offsets include original JSON escapes;
|
|
212
|
+
unchanged values and whitespace remain byte-identical. Numeric primitives still
|
|
213
|
+
require a caller schema policy. `debug` can observe `structured-value` events
|
|
214
|
+
with finding counts, without logging the string contents.
|
|
215
|
+
|
|
216
|
+
The `documents` profile additionally recognizes:
|
|
217
|
+
|
|
218
|
+
- Russian mobile numbers with dashed, dotted, spaced or parenthesized forms,
|
|
219
|
+
including ten-digit `9xx` forms and domestic `8` prefixes; `тел.`, `моб.` and
|
|
220
|
+
WhatsApp labels; `wa.me` contact links.
|
|
221
|
+
- Obfuscated email separators `(at)`, `[at]`, `собака`, and English `dot`/`at`.
|
|
222
|
+
This is a finite spelling grammar, not arbitrary email deobfuscation.
|
|
223
|
+
- Personal paths on LinkedIn (`/in/`), GitHub, VK, Instagram and Telegram,
|
|
224
|
+
including scheme-less links; Telegram/телеграм/tg/skype/ник labels without `@`.
|
|
225
|
+
A provider root such as `https://github.com/` remains unchanged. These rules
|
|
226
|
+
cannot distinguish a person from an organization account on the same service.
|
|
227
|
+
- Russian passport series/number phrases, ОГРНИП, subdivision codes, dated
|
|
228
|
+
ОВД/УВД/МВД issuer phrases, Russian/English driving licence labels, ОМС/полис,
|
|
229
|
+
Russian account numbers and numbered diplomas. These contextual patterns do
|
|
230
|
+
not validate issuance; digits are protected even with invalid checksums.
|
|
231
|
+
- Numbered schools, gymnasiums, lyceums and colleges; school acronyms after
|
|
232
|
+
Образование/ВУЗ/Университет/окончил/окончила, with optional `им. Баумана`-style
|
|
233
|
+
suffixes. Acronym detection is contextual rather than a complete school list.
|
|
234
|
+
- Surname/initial shapes in Cyrillic and selected Latin Slavic forms, known
|
|
235
|
+
given-name plus initial, Russian surname/given-name pairs using finite case
|
|
236
|
+
forms and lower case, and surnames after г-жа/господин/госпожа.
|
|
237
|
+
- Birth years with д.р./г.р./год рождения/родился/родилась/Born in context;
|
|
238
|
+
Russian age expressions and Пол labels, English Sex, and gender beside age.
|
|
239
|
+
Age-in-years expressions can also describe nonpersonal durations.
|
|
240
|
+
- Labelled citizenship, marital status, children, nationality, religion,
|
|
241
|
+
disability and criminal record, including the reported English labels.
|
|
242
|
+
- Company/employer/workplace labels, capitalized company/holding/bank names,
|
|
243
|
+
employment prose and year-range employer lines; residence/metro/district
|
|
244
|
+
labels and contextual six-digit postal codes.
|
|
245
|
+
- Russian street-plus-house/flat structures at line start or after
|
|
246
|
+
`проживает по адресу`. Public-place prose such as
|
|
247
|
+
`Музей находится на ул. Ленина, д. 5, кв. 12` stays unchanged. Public addresses
|
|
248
|
+
presented alone have the same shape as personal addresses and may be flagged;
|
|
249
|
+
inspect and review standalone public addresses before publication.
|
|
250
|
+
|
|
251
|
+
Explicit limits accepted for the ambiguous examples in #82/#83:
|
|
252
|
+
`Спасибо, Анна!`, `Анна, добрый день!`, `Менеджер Анна` and `Лютик, менеджер`
|
|
253
|
+
remain unchanged without known values or additional context. Bare given names
|
|
254
|
+
and unmarked employer/title fragments cannot reliably establish identity.
|
|
255
|
+
`зарплата 150 000 руб.` and `Salary: 150000` also remain unchanged: salary is
|
|
256
|
+
outside the native document rules. Supply `knownPersonal` or an additional
|
|
257
|
+
recognizer when these fields must be removed. Initial/name rules do not cover
|
|
258
|
+
all surnames, languages, cases or arbitrary lower-case names.
|
|
259
|
+
|
|
260
|
+
Operational value policy (#79): a single-slash relative URL with no query,
|
|
261
|
+
fragment, userinfo or explicit user/profile/member/home path stays unchanged
|
|
262
|
+
under `url`; other detectors and known values still apply. An ISO-style
|
|
263
|
+
`YYYY-MM-DDTHH:mm:ss[.fraction](Z|±HH:mm)` timestamp under `time` stays unchanged.
|
|
264
|
+
This exemption is syntactic, not date validation. Userinfo, token-bearing URLs
|
|
265
|
+
and personal paths still redact. Context-free 40-character hexadecimal values
|
|
266
|
+
are treated as SHA-1-shaped identifiers in both text and structured modes,
|
|
267
|
+
including quoted values. This does not prove they are harmless: actual bare
|
|
268
|
+
40-hex secrets require `knownSecrets` or credential context. Credential labels,
|
|
269
|
+
provider-specific rules and required external scanners remain authoritative.
|
|
270
|
+
|
|
271
|
+
The [complete requirement inventory and alternatives](case-studies/issue-84/README.md)
|
|
272
|
+
links every reported text row to its regression case.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@link-foundation/sensitive-data-sanitizer",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "Offline sensitive-text sanitization with credential, multilingual personal-data and scanner union detection",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
package/src/context.js
CHANGED
|
@@ -228,6 +228,9 @@ function quotedRange(text, start, opening, maxLength) {
|
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
function plausibleLabel(value, type) {
|
|
231
|
+
if (operationalValue(value, type)) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
231
234
|
if (type === 'PERSON') {
|
|
232
235
|
return plausiblePerson(value.replace(/[,;\d].*$/s, '').trim());
|
|
233
236
|
}
|
|
@@ -240,6 +243,20 @@ function plausibleLabel(value, type) {
|
|
|
240
243
|
return true;
|
|
241
244
|
}
|
|
242
245
|
|
|
246
|
+
function operationalValue(value, type) {
|
|
247
|
+
return (
|
|
248
|
+
(type === 'URL' &&
|
|
249
|
+
/^\/(?!\/)[^\s?@#]*$/.test(value) &&
|
|
250
|
+
!/(?:^|\/)(?:users?|profiles?|people|members?|home)\/[^/]+/i.test(
|
|
251
|
+
value
|
|
252
|
+
)) ||
|
|
253
|
+
(type === 'TIME' &&
|
|
254
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.test(
|
|
255
|
+
value
|
|
256
|
+
))
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
243
260
|
// Shared by native labels and the vendor catalogs. Code expressions and
|
|
244
261
|
// line escapes are not personal field values, even when a key is familiar.
|
|
245
262
|
export function personalValueAllowed(text, range, type, labelStart) {
|
package/src/detection.js
CHANGED
|
@@ -185,7 +185,9 @@ export function nativeDetect(text, options, emit) {
|
|
|
185
185
|
emit,
|
|
186
186
|
{ ...credential, rule: 'quoted-entropy' },
|
|
187
187
|
1,
|
|
188
|
-
(value) =>
|
|
188
|
+
(value) =>
|
|
189
|
+
!/^[a-f0-9]{40}$/i.test(value) &&
|
|
190
|
+
entropy(value) >= (/^[a-f0-9]+$/i.test(value) ? 3 : 4.5)
|
|
189
191
|
);
|
|
190
192
|
collectMatches(
|
|
191
193
|
text,
|
package/src/document-entities.js
CHANGED
|
@@ -3,6 +3,7 @@ import { firstNames } from './names.js';
|
|
|
3
3
|
import { organizationCore, personalRegex } from './personal-variants.js';
|
|
4
4
|
import { documentVocabulary } from './document-vocabulary.js';
|
|
5
5
|
import { months } from './personal-context.js';
|
|
6
|
+
import { detectDocumentPatterns } from './document-patterns.js';
|
|
6
7
|
|
|
7
8
|
const words = (column) =>
|
|
8
9
|
[...new Set(documentVocabulary.flatMap((row) => row[column].split('|')))]
|
|
@@ -87,6 +88,7 @@ export function detectDocumentEntities(text, emit, options) {
|
|
|
87
88
|
if (!options.documentEntities) {
|
|
88
89
|
return;
|
|
89
90
|
}
|
|
91
|
+
detectDocumentPatterns(text, emit);
|
|
90
92
|
const found = [];
|
|
91
93
|
const capture = (finding) => {
|
|
92
94
|
found.push(finding);
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { firstNames } from './names.js';
|
|
2
|
+
import { isNameForm } from './russian-names.js';
|
|
3
|
+
|
|
4
|
+
const boundary = '[\\p{L}\\p{N}_]';
|
|
5
|
+
const word = '[А-ЯЁ][а-яё-]{1,47}';
|
|
6
|
+
const surname =
|
|
7
|
+
'[А-ЯЁа-яё][А-ЯЁа-яё-]{1,28}(?:ов|ев|ин|ын|ск|цк)(?:а|ой|ою|у|ым|ом|е|ы|и|их|ий|ая|ого|ому|ую|ие)?';
|
|
8
|
+
const initials = '(?:[А-ЯЁA-Z]\\.[ \\t]*){1,2}';
|
|
9
|
+
const latinSurname = '[A-Z][a-z]{1,28}(?:ova|eva|ov|ev|in|ina|sky|ski|skaya)';
|
|
10
|
+
const given = [
|
|
11
|
+
...new Set(Object.values(firstNames).flatMap((s) => s.split(' '))),
|
|
12
|
+
].join('|');
|
|
13
|
+
const nameInitials = new RegExp(
|
|
14
|
+
`(?<!${boundary})(?:${surname}[ \\t]+${initials}|${initials}(?:${surname}|${latinSurname})|${latinSurname}[ \\t]+${initials}|(?:${given})[ \\t]+[А-ЯЁA-Z]\\.)(?!${boundary})`,
|
|
15
|
+
'gu'
|
|
16
|
+
);
|
|
17
|
+
const namePairs = new RegExp(
|
|
18
|
+
`(?<!${boundary})(${surname})[ \\t]+([А-ЯЁа-яё]{2,32})|(?<!${boundary})([А-ЯЁа-яё]{2,32})[ \\t]+(${surname})(?!${boundary})`,
|
|
19
|
+
'gu'
|
|
20
|
+
);
|
|
21
|
+
// Every rule is finite and requires either an identifying structure or a label.
|
|
22
|
+
// Public locations/ambiguous bare words are not inferred from arbitrary prose.
|
|
23
|
+
const rules = [
|
|
24
|
+
[
|
|
25
|
+
'PHONE',
|
|
26
|
+
'document-phone',
|
|
27
|
+
/(?<![\p{L}\p{N}_])(?:[78][ .-]?)?9\d{2}(?:[ .-]\d{3}[ .-]\d{2}[ .-]\d{2})(?!\d)|(?<![\p{L}\p{N}_])8[ .-]?\(9\d{2}\)[ .-]?\d{3}[ .-]?\d{2}[ .-]?\d{2}(?!\d)/gu,
|
|
28
|
+
0,
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
'PHONE',
|
|
32
|
+
'document-phone-label',
|
|
33
|
+
/(?<![\p{L}\p{N}_])(?:тел\.?|моб\.?|WhatsApp)(?:[ \t]*:[ \t]*|[ \t]+)((?:[78][ () .-]*)?9\d{2}[ () .-]*\d{3}[ .-]*\d{2}[ .-]*\d{2})(?!\d)/giu,
|
|
34
|
+
1,
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
'EMAIL',
|
|
38
|
+
'obfuscated-email',
|
|
39
|
+
/(?<![\p{L}\p{N}_.+-])[\p{L}\p{N}_+-]{1,64}(?:(?:\.|[ \t]{1,8}dot[ \t]{1,8})[\p{L}\p{N}_+-]{1,64}){0,4}(?:[ \t]{0,8}\(at\)[ \t]{0,8}|[ \t]{0,8}\[at\][ \t]{0,8}|[ \t]{1,8}(?:at|собака)[ \t]{1,8})[\p{L}\p{N}-]{1,63}(?:(?:\.|[ \t]{1,8}dot[ \t]{1,8})[\p{L}\p{N}-]{1,63}){1,8}(?![\p{L}\p{N}_])/giu,
|
|
40
|
+
0,
|
|
41
|
+
],
|
|
42
|
+
[
|
|
43
|
+
'USERNAME',
|
|
44
|
+
'personal-profile-url',
|
|
45
|
+
/(?<![\p{L}\p{N}_./-])(?:https?:\/\/)?(?:www\.)?(?:linkedin\.com\/in|github\.com|vk\.com|instagram\.com|t\.me|wa\.me)\/[\p{L}\p{N}_][\p{L}\p{N}_.-]{1,100}(?:\/[\p{L}\p{N}_.-]{1,100}){0,4}(?![\p{L}\p{N}_])/giu,
|
|
46
|
+
0,
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
'USERNAME',
|
|
50
|
+
'messenger-label',
|
|
51
|
+
/(?<![\p{L}\p{N}_])(?:Telegram|телеграм|tg|skype|ник)(?:[ \t]*:[ \t]*|[ \t]+)(@?[a-z][a-z\d_.-]{2,63})(?![\p{L}\p{N}_])/giu,
|
|
52
|
+
1,
|
|
53
|
+
],
|
|
54
|
+
[
|
|
55
|
+
'PASSPORT_NUMBER',
|
|
56
|
+
'passport-series',
|
|
57
|
+
/(?<![\p{L}\p{N}_])(?:серия|series)[ \t]+(\d{2}[ \t]?\d{2}[ \t]+(?:№|No|номер|number|#)[ \t]*\d{6})(?!\d)/giu,
|
|
58
|
+
1,
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
'ID',
|
|
62
|
+
'identity-number',
|
|
63
|
+
/(?<![\p{L}\p{N}_])(?:ОГРНИП[ \t]+(\d{15})|код подразделения[ \t]+(\d{3}-\d{3})|(?:водительское удостоверение|в\/у|Driver's licen[cs]e)[ \t]+(\d{2}[ \t]?\d{2}[ \t]?\d{6})|(?:ОМС|полис)[ \t]+(\d{16})|сч[её]т[ \t]+(\d{20})|диплом[ \t]+(?:№|No|#)[ \t]*(\d{6}))(?!\d)/giu,
|
|
64
|
+
-1,
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
'ID',
|
|
68
|
+
'passport-issuer',
|
|
69
|
+
/(?<![\p{L}\p{N}_])выдан[ \t]+((?:ОВД|УВД|МВД)[ \t]+[^\r\n";|]{1,120}?\d{2}\.\d{2}\.\d{4})(?!\d)/giu,
|
|
70
|
+
1,
|
|
71
|
+
],
|
|
72
|
+
[
|
|
73
|
+
'EDUCATION',
|
|
74
|
+
'numbered-school',
|
|
75
|
+
/(?<![\p{L}\p{N}_])(?:школа|гимназия|лицей|колледж)[ \t]*(?:№|No|#|:)[ \t]*\d{1,4}(?!\d)/giu,
|
|
76
|
+
0,
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
'EDUCATION',
|
|
80
|
+
'school-acronym-context',
|
|
81
|
+
/(?<![\p{L}\p{N}_])(?:образование|ВУЗ|университет|окончил[аи]?)(?:[ \t]*:[ \t]*|[ \t]+)([А-ЯЁ]{2,12}(?:[ \t]+им\.[ \t]+[А-ЯЁ][а-яё-]{2,40})?)(?![\p{L}\p{N}_])/giu,
|
|
82
|
+
1,
|
|
83
|
+
],
|
|
84
|
+
['PERSON', 'name-initials', nameInitials, 0],
|
|
85
|
+
[
|
|
86
|
+
'PERSON',
|
|
87
|
+
'name-title',
|
|
88
|
+
new RegExp(
|
|
89
|
+
`(?<!${boundary})(?:г-жа|господин|госпожа)[ \\t]+(${word})(?!${boundary})`,
|
|
90
|
+
'gu'
|
|
91
|
+
),
|
|
92
|
+
1,
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
'DATE_OF_BIRTH',
|
|
96
|
+
'birth-year',
|
|
97
|
+
/(?<![\p{L}\p{N}_])(?:д\.р\.|год рождения|родил(?:ся|ась)(?:[ \t]+в)?|Born(?:[ \t]+in)?)[ \t]*:?[ \t]*((?:19|20)\d{2})(?!\d)|(?<!\d)((?:19|20)\d{2})(?=[ \t]+г\.р\.)/giu,
|
|
98
|
+
-1,
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
'AGE',
|
|
102
|
+
'age-context',
|
|
103
|
+
/(?<![\p{L}\p{N}_])(?:возраст[ \t]*:|мне|исполнилось)[ \t]*(\d{1,3})(?!\d)|(?<![\p{L}\p{N}_])(\d{1,3})(?=[ \t]+(?:года|год|лет)(?![\p{L}\p{N}_]))/giu,
|
|
104
|
+
-1,
|
|
105
|
+
],
|
|
106
|
+
[
|
|
107
|
+
'GENDER',
|
|
108
|
+
'gender-context',
|
|
109
|
+
/(?<![\p{L}\p{N}_])(?:Пол|Sex)[ \t]*:[ \t]*(женский|мужской|male|female)(?![\p{L}\p{N}_])|(?<![\p{L}\p{N}_])(Женщина|Мужчина)(?=,[ \t]*\d{1,3}[ \t]+(?:года|год|лет))/giu,
|
|
110
|
+
-1,
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
'PERSONAL_ATTRIBUTE',
|
|
114
|
+
'profile-attribute',
|
|
115
|
+
/(?<![\p{L}\p{N}_])(?:Гражданство|Citizenship|Семейное положение|Marital status|Дети|национальность|Вероисповедание|инвалидность|судимость)[ \t]*:[ \t]*([^\r\n";|]{1,160})/giu,
|
|
116
|
+
1,
|
|
117
|
+
],
|
|
118
|
+
[
|
|
119
|
+
'ORGANIZATION',
|
|
120
|
+
'employer-label',
|
|
121
|
+
new RegExp(
|
|
122
|
+
`(?<!${boundary})(?:[Кк]омпания|[Рр]аботодатель|[Мм]есто работы)[ \\t]*:[ \\t]*(${word})(?!${boundary})`,
|
|
123
|
+
'gu'
|
|
124
|
+
),
|
|
125
|
+
1,
|
|
126
|
+
],
|
|
127
|
+
[
|
|
128
|
+
'ORGANIZATION',
|
|
129
|
+
'employer-prose',
|
|
130
|
+
new RegExp(
|
|
131
|
+
`(?<!${boundary})(?:компания|работал[аи]?[ \\t]+в|холдинг|[Бб]анк)[ \\t]+(«[^«»\\r\\n]{1,120}»|${word})(?!${boundary})`,
|
|
132
|
+
'gu'
|
|
133
|
+
),
|
|
134
|
+
1,
|
|
135
|
+
],
|
|
136
|
+
[
|
|
137
|
+
'ORGANIZATION',
|
|
138
|
+
'employer-years',
|
|
139
|
+
new RegExp(
|
|
140
|
+
`(?:^|\\n)[ \\t]*(?:19|20)\\d{2}[–—-](?:19|20)\\d{2}[ \\t]+(${word})(?!${boundary})`,
|
|
141
|
+
'gu'
|
|
142
|
+
),
|
|
143
|
+
1,
|
|
144
|
+
],
|
|
145
|
+
[
|
|
146
|
+
'LOCATION',
|
|
147
|
+
'residence-prose',
|
|
148
|
+
new RegExp(
|
|
149
|
+
`(?<!${boundary})(?:живу[ \\t]+в|из|м\\.|метро|район)[ \\t]+(${word})(?!${boundary})`,
|
|
150
|
+
'gu'
|
|
151
|
+
),
|
|
152
|
+
1,
|
|
153
|
+
],
|
|
154
|
+
[
|
|
155
|
+
'LOCATION',
|
|
156
|
+
'postal-code',
|
|
157
|
+
/(?<![\p{L}\p{N}_])индекс[ \t]+(\d{6})(?!\d)|(?<!\d)(\d{6})(?=,[ \t]*[А-ЯЁ][а-яё-]{2,47})/gu,
|
|
158
|
+
-1,
|
|
159
|
+
],
|
|
160
|
+
];
|
|
161
|
+
|
|
162
|
+
export function detectDocumentPatterns(text, emit) {
|
|
163
|
+
for (const [type, rule, pattern, group] of rules) {
|
|
164
|
+
for (const match of text.matchAll(pattern)) {
|
|
165
|
+
const value =
|
|
166
|
+
group === -1
|
|
167
|
+
? match.slice(1).find((part) => part !== undefined)
|
|
168
|
+
: match[group].trim();
|
|
169
|
+
if (!documentValueAllowed(rule, value)) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const start =
|
|
173
|
+
match.index + (group === 0 ? 0 : match[0].lastIndexOf(value));
|
|
174
|
+
emit({
|
|
175
|
+
start,
|
|
176
|
+
end: start + value.length,
|
|
177
|
+
type,
|
|
178
|
+
rule,
|
|
179
|
+
category: 'personal',
|
|
180
|
+
confidence: group === -1 ? 0.85 : 0.75,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const match of text.matchAll(namePairs)) {
|
|
185
|
+
if (isNameForm(match[2] ?? match[3])) {
|
|
186
|
+
emit({
|
|
187
|
+
start: match.index,
|
|
188
|
+
end: match.index + match[0].length,
|
|
189
|
+
type: 'PERSON',
|
|
190
|
+
rule: 'name-case',
|
|
191
|
+
category: 'personal',
|
|
192
|
+
confidence: 0.8,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// An address at line start or in explicit residence context is personal.
|
|
197
|
+
// Public-place prose with a street address does not supply that context.
|
|
198
|
+
const street =
|
|
199
|
+
/(?:^|\n)[ \t]*((?:ул\.|улица)[ \t]+[А-ЯЁ][а-яё-]{1,40},[ \t]*(?:(?:д\.|дом)[ \t]*)?\d{1,5}(?:[ \t]*,[ \t]*кв\.[ \t]*\d{1,5})?)|проживает по адресу[ \t]+((?:ул\.|улица)[ \t]+[А-ЯЁ][а-яё-]{1,40},[ \t]*(?:(?:д\.|дом)[ \t]*)?\d{1,5}(?:[ \t]*,[ \t]*кв\.[ \t]*\d{1,5})?)/gu;
|
|
200
|
+
for (const match of text.matchAll(street)) {
|
|
201
|
+
const value = match[1] ?? match[2];
|
|
202
|
+
const start = match.index + match[0].lastIndexOf(value);
|
|
203
|
+
emit({
|
|
204
|
+
start,
|
|
205
|
+
end: start + value.length,
|
|
206
|
+
type: 'ADDRESS',
|
|
207
|
+
rule: 'document-street',
|
|
208
|
+
category: 'personal',
|
|
209
|
+
confidence: 0.85,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function documentValueAllowed(rule, value) {
|
|
215
|
+
return (
|
|
216
|
+
Boolean(value) &&
|
|
217
|
+
value !== '[REDACTED]' &&
|
|
218
|
+
!(
|
|
219
|
+
rule === 'school-acronym-context' &&
|
|
220
|
+
!/^[А-ЯЁ]{2,12}(?![А-ЯЁа-яё])/.test(value)
|
|
221
|
+
) &&
|
|
222
|
+
!(rule === 'messenger-label' && /^(?:недоступен|unavailable)$/i.test(value))
|
|
223
|
+
);
|
|
224
|
+
}
|
package/src/engines.js
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
} from './placeholders.js';
|
|
25
25
|
import {
|
|
26
26
|
structuredBatches,
|
|
27
|
+
structuredValueViews,
|
|
27
28
|
mapStructuredFindings,
|
|
28
29
|
verifyStructured,
|
|
29
30
|
} from './structured.js';
|
|
@@ -143,6 +144,21 @@ async function analyzeStructured(
|
|
|
143
144
|
)) {
|
|
144
145
|
findings.push(finding);
|
|
145
146
|
}
|
|
147
|
+
for (const view of structuredValueViews(batch, options)) {
|
|
148
|
+
const detected = await analyze(
|
|
149
|
+
view.text,
|
|
150
|
+
[],
|
|
151
|
+
true,
|
|
152
|
+
{ ...options, structured: undefined },
|
|
153
|
+
nativeFindings === undefined ? undefined : []
|
|
154
|
+
);
|
|
155
|
+
for (const finding of detected) {
|
|
156
|
+
const mapped = view.map(finding);
|
|
157
|
+
if (mapped) {
|
|
158
|
+
findings.push(mapped);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
146
162
|
}
|
|
147
163
|
// Native structured inspection validates, locates and reapplies the
|
|
148
164
|
// same fake-value/public policy to source offsets from all engines.
|
package/src/sanitizer.js
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
} from './placeholders.js';
|
|
32
32
|
import {
|
|
33
33
|
structuredBatches,
|
|
34
|
+
inspectStructuredValues,
|
|
34
35
|
mapStructuredFindings,
|
|
35
36
|
preserveIdentifierKeys,
|
|
36
37
|
structuredValueRanges,
|
|
@@ -392,8 +393,8 @@ function inspectText(text, options = {}) {
|
|
|
392
393
|
validateOptions(options);
|
|
393
394
|
validateInput(text, options);
|
|
394
395
|
if (options.structured) {
|
|
395
|
-
const findings = structuredBatches(text, options).flatMap((batch) =>
|
|
396
|
-
mapStructuredFindings(
|
|
396
|
+
const findings = structuredBatches(text, options).flatMap((batch) => [
|
|
397
|
+
...mapStructuredFindings(
|
|
397
398
|
batch,
|
|
398
399
|
inspect(batch.text, {
|
|
399
400
|
...options,
|
|
@@ -401,8 +402,11 @@ function inspectText(text, options = {}) {
|
|
|
401
402
|
findings: [],
|
|
402
403
|
}),
|
|
403
404
|
options
|
|
404
|
-
)
|
|
405
|
-
|
|
405
|
+
),
|
|
406
|
+
...inspectStructuredValues(batch, options, (value) =>
|
|
407
|
+
inspect(value, { ...options, structured: undefined, findings: [] })
|
|
408
|
+
),
|
|
409
|
+
]);
|
|
406
410
|
return inspectFindingsOnly(text, findings, options);
|
|
407
411
|
}
|
|
408
412
|
const findings = [];
|
package/src/structured.js
CHANGED
|
@@ -280,3 +280,87 @@ export function verifyStructured(text, options) {
|
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
}
|
|
283
|
+
|
|
284
|
+
// Scan decoded strings at their own line start, retaining a map through every
|
|
285
|
+
// JSON escape. Both native and asynchronous engines consume these same views.
|
|
286
|
+
export function* structuredValueViews(
|
|
287
|
+
batch,
|
|
288
|
+
options,
|
|
289
|
+
depth = 0,
|
|
290
|
+
outerMap = (f) => f
|
|
291
|
+
) {
|
|
292
|
+
for (const entry of batch.entries) {
|
|
293
|
+
const value = JSON.parse(entry.raw);
|
|
294
|
+
const offsets = stringOffsets(entry.raw);
|
|
295
|
+
const map = (finding) => {
|
|
296
|
+
const raw = {
|
|
297
|
+
...finding,
|
|
298
|
+
start: offsets ? offsets.starts[finding.start] : finding.start + 1,
|
|
299
|
+
end: offsets ? offsets.ends[finding.end - 1] : finding.end + 1,
|
|
300
|
+
};
|
|
301
|
+
if (
|
|
302
|
+
preserveStructuredEntry(
|
|
303
|
+
entry,
|
|
304
|
+
{
|
|
305
|
+
...raw,
|
|
306
|
+
start: entry.batchStart + raw.start,
|
|
307
|
+
end: entry.batchStart + raw.end,
|
|
308
|
+
},
|
|
309
|
+
batch.text,
|
|
310
|
+
options
|
|
311
|
+
)
|
|
312
|
+
) {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
return outerMap({
|
|
316
|
+
...raw,
|
|
317
|
+
start: entry.start + raw.start,
|
|
318
|
+
end: entry.start + raw.end,
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
yield { text: value, map };
|
|
322
|
+
// Match the encoded detector's finite depth; arbitrary nesting is a limit.
|
|
323
|
+
if (depth < 2 && /^[ \t\r\n]*[[{]/.test(value)) {
|
|
324
|
+
let batches;
|
|
325
|
+
try {
|
|
326
|
+
batches = structuredBatches(value, { ...options, structured: 'json' });
|
|
327
|
+
} catch (error) {
|
|
328
|
+
if (error.code !== 'ERR_JSON') {
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
batches = [];
|
|
332
|
+
}
|
|
333
|
+
for (const nested of batches) {
|
|
334
|
+
yield* structuredValueViews(nested, options, depth + 1, map);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
export function inspectStructuredValues(batch, options, inspectValue) {
|
|
340
|
+
const result = [];
|
|
341
|
+
for (const view of structuredValueViews(batch, options)) {
|
|
342
|
+
const findings = inspectValue(view.text);
|
|
343
|
+
options.debug?.({ event: 'structured-value', count: findings.length });
|
|
344
|
+
for (const finding of findings) {
|
|
345
|
+
const mapped = view.map(finding);
|
|
346
|
+
if (mapped) {
|
|
347
|
+
result.push(mapped);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
353
|
+
function stringOffsets(raw) {
|
|
354
|
+
if (!raw.includes('\\')) {
|
|
355
|
+
return undefined;
|
|
356
|
+
}
|
|
357
|
+
const starts = [],
|
|
358
|
+
ends = [];
|
|
359
|
+
for (let i = 1; i < raw.length - 1; ) {
|
|
360
|
+
const start = i;
|
|
361
|
+
i += raw[i] === '\\' ? (raw[i + 1] === 'u' ? 6 : 2) : 1;
|
|
362
|
+
starts.push(start);
|
|
363
|
+
ends.push(i);
|
|
364
|
+
}
|
|
365
|
+
return { starts, ends };
|
|
366
|
+
}
|