-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(guardrails): route PII validation through app runtime #7227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest, hybridAuthMockFns } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockValidatePII } = vi.hoisted(() => ({ | ||
| mockValidatePII: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/guardrails/validate_pii', () => ({ | ||
| validatePII: mockValidatePII, | ||
| })) | ||
|
|
||
| import { POST } from '@/app/api/guardrails/pii/validate/route' | ||
|
|
||
| describe('POST /api/guardrails/pii/validate', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ success: true }) | ||
| mockValidatePII.mockResolvedValue({ passed: true, detectedEntities: [] }) | ||
| }) | ||
|
|
||
| it('authenticates before validating the request body', async () => { | ||
| hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ | ||
| success: false, | ||
| error: 'Internal authentication required', | ||
| }) | ||
|
|
||
| const response = await POST(createMockRequest('POST', { text: 42 })) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(mockValidatePII).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('runs Presidio validation inside the app boundary', async () => { | ||
| const request = createMockRequest('POST', { | ||
| text: 'email a@b.com', | ||
| entityTypes: ['EMAIL_ADDRESS'], | ||
| mode: 'mask', | ||
| language: 'en', | ||
| }) | ||
|
|
||
| const response = await POST(request) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| await expect(response.json()).resolves.toEqual({ passed: true, detectedEntities: [] }) | ||
| expect(mockValidatePII).toHaveBeenCalledWith({ | ||
| text: 'email a@b.com', | ||
| entityTypes: ['EMAIL_ADDRESS'], | ||
| mode: 'mask', | ||
| language: 'en', | ||
| customPatterns: undefined, | ||
| requestId: 'mock-request-id', | ||
| abortSignal: request.signal, | ||
| }) | ||
| }) | ||
|
|
||
| it('rejects malformed input before calling Presidio', async () => { | ||
| const response = await POST( | ||
| createMockRequest('POST', { text: 'claim', entityTypes: [], mode: 'invalid' }) | ||
| ) | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(mockValidatePII).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { guardrailsPiiValidateContract } from '@/lib/api/contracts' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { checkInternalAuth } from '@/lib/auth/hybrid' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { validatePII } from '@/lib/guardrails/validate_pii' | ||
|
|
||
| /** | ||
| * App-container capability boundary for single-text PII validation. Presidio is | ||
| * intentionally ECS-internal, so remote workflow runtimes authenticate here | ||
| * instead of importing its client and attempting to reach `PII_URL` directly. | ||
| */ | ||
| export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| const auth = await checkInternalAuth(request, { requireWorkflowId: false }) | ||
| if (!auth.success) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(guardrailsPiiValidateContract, request, {}) | ||
| if (!parsed.success) return parsed.response | ||
|
|
||
| const { text, entityTypes, mode, language, customPatterns } = parsed.data.body | ||
| const result = await validatePII({ | ||
| text, | ||
| entityTypes, | ||
| mode, | ||
| language, | ||
| customPatterns, | ||
| requestId: generateRequestId(), | ||
| abortSignal: request.signal, | ||
| }) | ||
|
|
||
| return NextResponse.json(guardrailsPiiValidateContract.response.schema.parse(result)) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** Maximum characters accepted or returned by single-text PII validation. */ | ||
| export const MAX_PII_VALIDATION_TEXT_CHARACTERS = 10_000_000 | ||
|
|
||
| /** Maximum detected spans materialized into one guardrail verdict. */ | ||
| export const MAX_PII_VALIDATION_DETECTED_ENTITIES = 10_000 | ||
|
|
||
| /** Maximum bytes read or serialized for one PII validation result. */ | ||
| export const MAX_PII_VALIDATION_RESPONSE_BYTES = 10 * 1024 * 1024 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.