Skip to content

Commit f023832

Browse files
committed
fix(pii): read the PII concurrency settings through envNumber
The env helper skips validation, so PII_MASK_CHUNK_CONCURRENCY, PII_REF_CONCURRENCY and PII_SERVICE_CHUNK_CONCURRENCY arrive as strings when set, and mapWithConcurrency refuses a non-number limit: PII masking failed and redaction fell back to the failure marker. Read all three through envNumber, as other numeric settings are.
1 parent ffda23f commit f023832

6 files changed

Lines changed: 93 additions & 9 deletions

File tree

‎apps/sim/lib/guardrails/mask-client.test.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { resetUrlsMock, urlsMockFns } from '@sim/testing'
22
import { createRouteContext } from '@sim/testing/helpers/http'
33
import { authInternalMock, authInternalMockFns } from '@sim/testing/mocks/auth-internal.mock'
4+
import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock'
45
import { hybridAuthMockFns } from '@sim/testing/mocks/hybrid-auth.mock'
56
import { utilsHelpersMock, utilsHelpersMockFns } from '@sim/testing/mocks/utils-helpers.mock'
67
import { NextRequest } from 'next/server'
7-
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
8+
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
89

910
afterAll(resetUrlsMock)
1011

@@ -170,3 +171,34 @@ describe('maskPIIBatchViaHttp', () => {
170171
})
171172
})
172173
})
174+
175+
describe('maskPIIBatchViaHttp with PII_MASK_CHUNK_CONCURRENCY set', () => {
176+
afterEach(() => {
177+
resetEnvMock()
178+
vi.resetModules()
179+
})
180+
181+
it('masks when the deployment sets the chunk concurrency (env values arrive as strings)', async () => {
182+
mockToken.mockResolvedValue('tok')
183+
mockBaseUrl.mockReturnValue('http://app.internal:3000')
184+
vi.stubGlobal(
185+
'fetch',
186+
vi.fn(async (_url: string, init: { body: string }) => {
187+
const { texts } = JSON.parse(init.body) as { texts: string[] }
188+
return new Response(JSON.stringify({ masked: texts.map((t) => `M(${t})`) }), {
189+
status: 200,
190+
headers: { 'content-type': 'application/json' },
191+
})
192+
})
193+
)
194+
setEnv({ PII_MASK_CHUNK_CONCURRENCY: '8' })
195+
vi.resetModules()
196+
const { maskPIIBatchViaHttp: maskWithConfiguredConcurrency } = await import(
197+
'@/lib/guardrails/mask-client'
198+
)
199+
200+
const { masked } = await maskWithConfiguredConcurrency(['a', 'b'], [])
201+
202+
expect(masked).toEqual(['M(a)', 'M(b)'])
203+
})
204+
})

‎apps/sim/lib/guardrails/mask-client.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { backoffWithJitter, parseRetryAfter } from '@sim/utils/retry'
66
import { sanitizeForJsonb } from '@sim/utils/string'
77
import type { GuardrailsMaskBatchResult } from '@/lib/api/contracts'
88
import { generateInternalToken } from '@/lib/auth/internal'
9-
import { env } from '@/lib/core/config/env'
9+
import { env, envNumber } from '@/lib/core/config/env'
1010
import { getInternalApiBaseUrl } from '@/lib/core/utils/urls'
1111
import { chunkIndicesByBudget } from '@/lib/guardrails/pii-batching'
1212
import type { CustomPiiPattern } from '@/lib/guardrails/pii-entities'
@@ -23,7 +23,7 @@ const logger = createLogger('PiiMaskClient')
2323
* (scaled) Presidio service is expected to eventually respond; an unreachable
2424
* service still rejects fast (connection refused) so the caller scrubs.
2525
*/
26-
const CHUNK_CONCURRENCY = env.PII_MASK_CHUNK_CONCURRENCY ?? 64
26+
const CHUNK_CONCURRENCY = envNumber(env.PII_MASK_CHUNK_CONCURRENCY, 64, { min: 1, integer: true })
2727

2828
/**
2929
* Attempts per chunk for transient failures (network errors, timeouts, and the

‎apps/sim/lib/guardrails/validate-pii.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest'
1+
import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock'
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
23
import {
34
MAX_PII_VALIDATION_DETECTED_ENTITIES,
45
MAX_PII_VALIDATION_RESPONSE_BYTES,
@@ -237,3 +238,30 @@ describe('validate-pii (Presidio service)', () => {
237238
})
238239
})
239240
})
241+
242+
describe('maskPIIBatch with PII_SERVICE_CHUNK_CONCURRENCY set', () => {
243+
afterEach(() => {
244+
resetEnvMock()
245+
vi.resetModules()
246+
})
247+
248+
it('masks when the deployment sets the service chunk concurrency (env values arrive as strings)', async () => {
249+
vi.stubGlobal(
250+
'fetch',
251+
vi.fn(async (_url: string, init: { body: string }) => {
252+
const body = JSON.parse(init.body) as { texts: string[]; entities?: string[] }
253+
const texts = body.texts.map((t) => applyReplace(t, emailSpans(t, body.entities)))
254+
return new Response(JSON.stringify({ texts }), { status: 200 })
255+
})
256+
)
257+
setEnv({ PII_SERVICE_CHUNK_CONCURRENCY: '4' })
258+
vi.resetModules()
259+
const { maskPIIBatch: maskWithConfiguredConcurrency } = await import(
260+
'@/lib/guardrails/validate-pii'
261+
)
262+
263+
const masked = await maskWithConfiguredConcurrency(['mail a@b.com'], ['EMAIL_ADDRESS'])
264+
265+
expect(masked).toEqual(['mail <EMAIL_ADDRESS>'])
266+
})
267+
})

‎apps/sim/lib/guardrails/validate-pii.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
22
import { mapWithConcurrency } from '@sim/utils/concurrency'
33
import { getErrorMessage } from '@sim/utils/errors'
44
import { toWellFormed, truncate } from '@sim/utils/string'
5-
import { env } from '@/lib/core/config/env'
5+
import { env, envNumber } from '@/lib/core/config/env'
66
import {
77
DEFAULT_MAX_ERROR_BODY_BYTES,
88
readResponseJsonWithLimit,
@@ -52,7 +52,7 @@ function displayEntityType(type: string, patterns?: CustomPiiPattern[]): string
5252
* raise via `PII_SERVICE_CHUNK_CONCURRENCY` for a scaled Presidio fleet (this is
5353
* the route → Presidio fan-out, inner to the app → route `PII_MASK_CHUNK_CONCURRENCY`).
5454
*/
55-
const CHUNK_CONCURRENCY = env.PII_SERVICE_CHUNK_CONCURRENCY ?? 4
55+
const CHUNK_CONCURRENCY = envNumber(env.PII_SERVICE_CHUNK_CONCURRENCY, 4, { min: 1, integer: true })
5656

5757
/** Presidio service serving /analyze, /anonymize, and combined /redact (VIN is native there). */
5858
const PII_URL = env.PII_URL || 'http://localhost:5001'

‎apps/sim/lib/logs/execution/pii-large-values.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock'
12
import {
23
executionPayloadStoreMock,
34
executionPayloadStoreMockFns,
45
} from '@sim/testing/mocks/execution-payload-store.mock'
56
import { maskClientMock, maskClientMockFns } from '@sim/testing/mocks/mask-client.mock'
6-
import { beforeEach, describe, expect, it, vi } from 'vitest'
7+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
78

89
const { mockCompact } = vi.hoisted(() => ({
910
mockCompact: vi.fn(),
@@ -346,3 +347,26 @@ describe('redactLargeValueRefsInValue (arbitrary blockStates)', () => {
346347
).rejects.toBeInstanceOf(PiiRedactionError)
347348
})
348349
})
350+
351+
describe('redactLargeValueRefs with PII_REF_CONCURRENCY set', () => {
352+
afterEach(() => {
353+
resetEnvMock()
354+
vi.resetModules()
355+
})
356+
357+
it('masks refs when the deployment sets the ref concurrency (env values arrive as strings)', async () => {
358+
setEnv({ PII_REF_CONCURRENCY: '2' })
359+
vi.resetModules()
360+
const { redactLargeValueRefs: redactWithConfiguredConcurrency } = await import(
361+
'@/lib/logs/execution/pii-large-values'
362+
)
363+
chunkData.set(REF.id, { note: 'contact bob', id: 42 })
364+
365+
const result = await redactWithConfiguredConcurrency(
366+
{ finalOutput: REF },
367+
{ entityTypes: ['PERSON'], language: 'en', store: STORE }
368+
)
369+
370+
expect(result.finalOutput).toEqual({ note: 'MASKED(contact bob)', id: 42 })
371+
})
372+
})

‎apps/sim/lib/logs/execution/pii-large-values.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { mapWithConcurrency } from '@sim/utils/concurrency'
33
import { getErrorMessage } from '@sim/utils/errors'
4-
import { env } from '@/lib/core/config/env'
4+
import { env, envNumber } from '@/lib/core/config/env'
55
import type { LargeArrayManifest } from '@/lib/execution/payloads/large-array-manifest'
66
import {
77
appendLargeArrayManifest,
@@ -156,7 +156,7 @@ async function redactValueRefs(
156156
* Multiplies with the mask-batch chunk concurrency for total in-flight Presidio
157157
* load, which the load-balanced fleet behind the internal ALB absorbs.
158158
*/
159-
const REF_CONCURRENCY = env.PII_REF_CONCURRENCY ?? 4
159+
const REF_CONCURRENCY = envNumber(env.PII_REF_CONCURRENCY, 4, { min: 1, integer: true })
160160

161161
/**
162162
* A single ref past the default inline ceiling hydrates its whole blob at once

0 commit comments

Comments
 (0)