-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(guardrails): authorize vertexCredential before use in hallucination validation #5400
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
+179
−0
Merged
Changes from all commits
Commits
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,159 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest, hybridAuthMockFns, workflowAuthzMockFns } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockAuthorizeCredentialUse, mockCheckActorUsageLimits, mockValidateHallucination } = | ||
| vi.hoisted(() => ({ | ||
| mockAuthorizeCredentialUse: vi.fn(), | ||
| mockCheckActorUsageLimits: vi.fn(), | ||
| mockValidateHallucination: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth/credential-access', () => ({ | ||
| authorizeCredentialUse: mockAuthorizeCredentialUse, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/calculations/usage-monitor', () => ({ | ||
| checkActorUsageLimits: mockCheckActorUsageLimits, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/guardrails/validate_hallucination', () => ({ | ||
| validateHallucination: mockValidateHallucination, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/guardrails/validate_json', () => ({ | ||
| validateJson: vi.fn(() => ({ passed: true })), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/guardrails/validate_pii', () => ({ | ||
| validatePII: vi.fn(() => ({ passed: true })), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/guardrails/validate_regex', () => ({ | ||
| validateRegex: vi.fn(() => ({ passed: true })), | ||
| })) | ||
|
|
||
| vi.mock('@/ee/access-control/utils/permission-check', () => ({ | ||
| assertPermissionsAllowed: vi.fn(), | ||
| ModelNotAllowedError: class ModelNotAllowedError extends Error {}, | ||
| ProviderNotAllowedError: class ProviderNotAllowedError extends Error {}, | ||
| })) | ||
|
|
||
| import { POST } from '@/app/api/guardrails/validate/route' | ||
|
|
||
| describe('POST /api/guardrails/validate', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ | ||
| success: true, | ||
| userId: 'user-1', | ||
| authType: 'session', | ||
| }) | ||
| workflowAuthzMockFns.mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValue({ | ||
| allowed: true, | ||
| workflow: { id: 'wf-1', workspaceId: 'ws-1' }, | ||
| }) | ||
| mockCheckActorUsageLimits.mockResolvedValue({ isExceeded: false }) | ||
| mockValidateHallucination.mockResolvedValue({ passed: true, score: 8 }) | ||
| }) | ||
|
|
||
| it('rejects a vertexCredential the caller does not have access to before calling validateHallucination', async () => { | ||
| mockAuthorizeCredentialUse.mockResolvedValue({ | ||
| ok: false, | ||
| error: 'You do not have access to this credential.', | ||
| }) | ||
|
|
||
| const res = await POST( | ||
| createMockRequest('POST', { | ||
| validationType: 'hallucination', | ||
| input: 'test input', | ||
| knowledgeBaseId: 'kb-1', | ||
| model: 'vertex/gemini-2.5-pro', | ||
| workflowId: 'wf-1', | ||
| vertexCredential: 'someone-elses-account-id', | ||
| }) | ||
| ) | ||
|
|
||
| expect(res.status).toBe(401) | ||
| expect(mockAuthorizeCredentialUse).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.objectContaining({ | ||
| credentialId: 'someone-elses-account-id', | ||
| workflowId: 'wf-1', | ||
| requireWorkflowIdForInternal: false, | ||
| }) | ||
| ) | ||
| expect(mockValidateHallucination).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('proceeds with hallucination validation when the caller has access to the vertexCredential', async () => { | ||
| mockAuthorizeCredentialUse.mockResolvedValue({ ok: true }) | ||
|
|
||
| const res = await POST( | ||
| createMockRequest('POST', { | ||
| validationType: 'hallucination', | ||
| input: 'test input', | ||
| knowledgeBaseId: 'kb-1', | ||
| model: 'vertex/gemini-2.5-pro', | ||
| workflowId: 'wf-1', | ||
| vertexCredential: 'my-own-account-id', | ||
| }) | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const json = await res.json() | ||
| expect(json.output.passed).toBe(true) | ||
| expect(mockAuthorizeCredentialUse).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.objectContaining({ credentialId: 'my-own-account-id' }) | ||
| ) | ||
| expect(mockValidateHallucination).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not gate on vertexCredential for non-hallucination validation types', async () => { | ||
| const res = await POST( | ||
| createMockRequest('POST', { | ||
| validationType: 'json', | ||
| input: '{"a":1}', | ||
| }) | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| expect(mockAuthorizeCredentialUse).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not gate hallucination validation when no vertexCredential is supplied', async () => { | ||
| const res = await POST( | ||
| createMockRequest('POST', { | ||
| validationType: 'hallucination', | ||
| input: 'test input', | ||
| knowledgeBaseId: 'kb-1', | ||
| model: 'gpt-4o', | ||
| workflowId: 'wf-1', | ||
| }) | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| expect(mockAuthorizeCredentialUse).not.toHaveBeenCalled() | ||
| expect(mockValidateHallucination).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not gate on a leftover vertexCredential when the resolved model is not vertex', async () => { | ||
| const res = await POST( | ||
| createMockRequest('POST', { | ||
| validationType: 'hallucination', | ||
| input: 'test input', | ||
| knowledgeBaseId: 'kb-1', | ||
| model: 'gpt-4o', | ||
| workflowId: 'wf-1', | ||
| vertexCredential: 'someone-elses-account-id', | ||
| }) | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| expect(mockAuthorizeCredentialUse).not.toHaveBeenCalled() | ||
| expect(mockValidateHallucination).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
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.