@@ -18,6 +18,9 @@ import { getErrorMessage } from '@sim/utils/errors'
1818 * - Presidio masks names and an email on both sides of a long unbroken run of
1919 * brackets in place, within seconds; spaCy's own tokenizer needs time quadratic
2020 * in such a run's length.
21+ * - Presidio masks every one of thousands of emails in one text within seconds;
22+ * Presidio before 2.2.365 compared every match with every other one when removing
23+ * duplicates and resolving overlaps.
2124 * - A persisted log holding a 5M-character base64 PDF and a 3M-character text that
2225 * names a person at the end: the PDF becomes the binary marker without reaching
2326 * Presidio, the name is masked by real NER, and no request fails.
@@ -35,6 +38,8 @@ if (!reportPath) throw new Error('PII_E2E_REPORT_PATH must be explicitly provide
3538const SPACY_MAX_LENGTH = 1_000_000
3639/** Where Presidio's first NER window ends (`NER_WINDOW_CHARS` in apps/pii/server.py). */
3740const FIRST_NER_WINDOW_END = 100_000
41+ /** Matches in one text: past the pairwise-comparison cost of Presidio before 2.2.365. */
42+ const MANY_MATCHES = 15_000
3843
3944const FILLER = 'The quarterly report covers revenue, costs, and the hiring plan.\n'
4045const NAME_SENTENCE = 'Please send questions to Ada Lovelace before Friday.'
@@ -142,6 +147,31 @@ async function main(): Promise<void> {
142147 const pdfBase64 = pdfBytes . toString ( 'base64' )
143148 const longText = `${ FILLER . repeat ( Math . ceil ( 3_000_000 / FILLER . length ) ) } ${ NAME_SENTENCE } `
144149
150+ /**
151+ * Masks one text with a `/redact_batch` request sent straight to Presidio. A
152+ * `timeoutMs` makes a request that runs longer fail its check.
153+ */
154+ const redactWithPresidio = async (
155+ request : { text : string } & Record < string , unknown > ,
156+ timeoutMs ?: number
157+ ) : Promise < string > => {
158+ const { text, ...options } = request
159+ const response = await realFetch ( new URL ( '/redact_batch' , presidioUrl ) , {
160+ method : 'POST' ,
161+ headers : { 'content-type' : 'application/json' } ,
162+ body : JSON . stringify ( { texts : [ text ] , language : 'en' , ...options } ) ,
163+ signal : timeoutMs === undefined ? undefined : AbortSignal . timeout ( timeoutMs ) ,
164+ } )
165+ const body = await response . text ( )
166+ assert . equal (
167+ response . status ,
168+ 200 ,
169+ `Presidio answered ${ response . status } : ${ body . slice ( 0 , 200 ) } `
170+ )
171+ const [ masked ] = ( JSON . parse ( body ) as { texts : string [ ] } ) . texts
172+ return masked
173+ }
174+
145175 await check ( 'Presidio masks a text longer than one NER pass allows' , async ( ) => {
146176 const prose = ( length : number ) =>
147177 FILLER . repeat ( Math . ceil ( length / FILLER . length ) ) . slice ( 0 , length )
@@ -153,23 +183,11 @@ async function main(): Promise<void> {
153183 'badge EMP 123456 issued\n' +
154184 prose ( SPACY_MAX_LENGTH ) +
155185 NAME_SENTENCE
156- const response = await realFetch ( new URL ( '/redact_batch' , presidioUrl ) , {
157- method : 'POST' ,
158- headers : { 'content-type' : 'application/json' } ,
159- body : JSON . stringify ( {
160- texts : [ text ] ,
161- language : 'en' ,
162- entities : [ 'PERSON' ] ,
163- patterns : [ { regex : 'EMP\\s+\\d{6}' , replacement : 'EMPLOYEE_ID' , name : 'Employee ID' } ] ,
164- } ) ,
186+ const masked = await redactWithPresidio ( {
187+ text,
188+ entities : [ 'PERSON' ] ,
189+ patterns : [ { regex : 'EMP\\s+\\d{6}' , replacement : 'EMPLOYEE_ID' , name : 'Employee ID' } ] ,
165190 } )
166- const body = await response . text ( )
167- assert . equal (
168- response . status ,
169- 200 ,
170- `Presidio answered ${ response . status } : ${ body . slice ( 0 , 200 ) } `
171- )
172- const [ masked ] = ( JSON . parse ( body ) as { texts : string [ ] } ) . texts
173191 assert (
174192 masked . includes ( `${ lead } <PERSON> before Friday.` ) ,
175193 'The name across the first NER window edge was not masked as one <PERSON> span'
@@ -184,23 +202,7 @@ async function main(): Promise<void> {
184202 const run = `${ '[' . repeat ( 20_000 ) } ${ ']' . repeat ( 20_000 ) } `
185203 const after = ' Write to ada@example.com today.\n'
186204 const text = `${ NAME_SENTENCE } ${ run } ${ after } ${ NAME_SENTENCE } `
187- const response = await realFetch ( new URL ( '/redact_batch' , presidioUrl ) , {
188- method : 'POST' ,
189- headers : { 'content-type' : 'application/json' } ,
190- body : JSON . stringify ( {
191- texts : [ text ] ,
192- language : 'en' ,
193- entities : [ 'PERSON' , 'EMAIL_ADDRESS' ] ,
194- } ) ,
195- signal : AbortSignal . timeout ( 30_000 ) ,
196- } )
197- const body = await response . text ( )
198- assert . equal (
199- response . status ,
200- 200 ,
201- `Presidio answered ${ response . status } : ${ body . slice ( 0 , 200 ) } `
202- )
203- const [ masked ] = ( JSON . parse ( body ) as { texts : string [ ] } ) . texts
205+ const masked = await redactWithPresidio ( { text, entities : [ 'PERSON' , 'EMAIL_ADDRESS' ] } , 30_000 )
204206 const maskedSentence = 'Please send questions to <PERSON> before Friday.'
205207 assert . equal (
206208 masked ,
@@ -210,6 +212,18 @@ async function main(): Promise<void> {
210212 return { textChars : text . length }
211213 } )
212214
215+ await check ( 'Presidio masks a text with thousands of matches within seconds' , async ( ) => {
216+ const rows = Array . from ( { length : MANY_MATCHES } , ( _ , i ) => `row ${ i } : ada${ i } @example.com\n` )
217+ const text = rows . join ( '' )
218+ const masked = await redactWithPresidio ( { text, entities : [ 'EMAIL_ADDRESS' ] } , 30_000 )
219+ assert . equal (
220+ masked ,
221+ rows . map ( ( _ , i ) => `row ${ i } : <EMAIL_ADDRESS>\n` ) . join ( '' ) ,
222+ 'An email was not masked in place'
223+ )
224+ return { textChars : text . length , matches : MANY_MATCHES }
225+ } )
226+
213227 await check ( 'A persisted log replaces the PDF and masks the long text' , async ( ) => {
214228 const since = maskRequests . length
215229 const result = await redactPIIFromExecution (
0 commit comments