|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ authorize: vi.fn(), create: vi.fn(), prepare: vi.fn(), request: vi.fn() })) |
| 7 | +vi.mock('@/lib/auth/credential-access', () => ({ authorizeCredentialUseForAuth: mocks.authorize })) |
| 8 | +vi.mock('@/lib/internal/oci/client.server', () => ({ createOciClient: mocks.create })) |
| 9 | + |
| 10 | +import { AuthType } from '@/lib/auth/hybrid' |
| 11 | +import { executeOciStreamingTool } from '@/lib/internal/oci-streaming/execute-tool' |
| 12 | +import type { InternalToolOperationCall } from '@/lib/internal/tool-operations/types' |
| 13 | + |
| 14 | +function call(overrides: Partial<InternalToolOperationCall> = {}): InternalToolOperationCall { |
| 15 | + return { |
| 16 | + toolId: 'oci_streaming_list_streams', |
| 17 | + input: { operation: 'list_streams', ociCredential: 'supplied-reference', compartmentId: 'compartment-1', ociRegion: 'us-ashburn-1' }, |
| 18 | + context: { userId: 'actor-1', workspaceId: 'workspace-1', workflowId: 'workflow-1' }, |
| 19 | + headers: new Headers(), requestId: 'request-1', ...overrides, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('OCI Streaming trusted credential boundary', () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks() |
| 26 | + mocks.authorize.mockResolvedValue({ ok: true, credentialType: 'service_account', resolvedCredentialId: 'resolved-credential', workspaceId: 'workspace-1' }) |
| 27 | + mocks.create.mockResolvedValue({ prepareStaticEndpoint: mocks.prepare, request: mocks.request }) |
| 28 | + mocks.prepare.mockResolvedValue({ origin: 'https://streaming.us-ashburn-1.oci.oraclecloud.com' }) |
| 29 | + mocks.request.mockResolvedValue({ status: 200, body: new TextEncoder().encode('[]'), headers: {} }) |
| 30 | + }) |
| 31 | + |
| 32 | + it('passes only the resolved credential ID and trusted workspace to the foundation', async () => { |
| 33 | + const response = await executeOciStreamingTool(call()) |
| 34 | + expect(response.status).toBe(200) |
| 35 | + expect(mocks.authorize).toHaveBeenCalledWith( |
| 36 | + { success: true, userId: 'actor-1', authType: AuthType.INTERNAL_JWT }, |
| 37 | + { credentialId: 'supplied-reference', workspaceId: 'workspace-1', workflowId: 'workflow-1', callerUserId: 'actor-1' } |
| 38 | + ) |
| 39 | + expect(mocks.create).toHaveBeenCalledWith({ credentialId: 'resolved-credential', workspaceId: 'workspace-1', serviceId: 'oci-streaming', region: 'us-ashburn-1' }) |
| 40 | + }) |
| 41 | + |
| 42 | + it.each([ |
| 43 | + { ok: false }, |
| 44 | + { ok: true, credentialType: 'oauth', resolvedCredentialId: 'resolved', workspaceId: 'workspace-1' }, |
| 45 | + { ok: true, credentialType: 'service_account', workspaceId: 'workspace-1' }, |
| 46 | + { ok: true, credentialType: 'service_account', resolvedCredentialId: 'resolved', workspaceId: 'other-workspace' }, |
| 47 | + ])('starts no provider work for unauthorized credential resolution', async (access) => { |
| 48 | + mocks.authorize.mockResolvedValue(access) |
| 49 | + expect((await executeOciStreamingTool(call())).status).toBe(403) |
| 50 | + expect(mocks.create).not.toHaveBeenCalled() |
| 51 | + expect(mocks.request).not.toHaveBeenCalled() |
| 52 | + }) |
| 53 | + |
| 54 | + it.each([ |
| 55 | + { freeformTags: Object.fromEntries(Array.from({ length: 11 }, (_, i) => [String(i), 'value'])) }, |
| 56 | + { definedTags: { namespace: Object.fromEntries(Array.from({ length: 65 }, (_, i) => [String(i), 'value'])) } }, |
| 57 | + { freeformTags: { key: 'x'.repeat(257) } }, |
| 58 | + { definedTags: { namespace: Object.fromEntries(Array.from({ length: 20 }, (_, i) => [String(i), 'é'.repeat(256)])) } }, |
| 59 | + ])('rejects oversized administrative tags before authorization or provider work', async (tags) => { |
| 60 | + const response = await executeOciStreamingTool(call({ |
| 61 | + toolId: 'oci_streaming_update_stream', |
| 62 | + input: { operation: 'update_stream', ociCredential: 'credential', streamId: 'stream', ...tags }, |
| 63 | + })) |
| 64 | + expect(response.status).toBe(400) |
| 65 | + expect(mocks.authorize).not.toHaveBeenCalled() |
| 66 | + expect(mocks.create).not.toHaveBeenCalled() |
| 67 | + expect(mocks.request).not.toHaveBeenCalled() |
| 68 | + }) |
| 69 | + |
| 70 | + it('rejects forged context in operation input before authorization', async () => { |
| 71 | + const request = call() |
| 72 | + request.input = { operation: 'list_streams', ociCredential: 'credential', compartmentId: 'compartment', workspaceId: 'forged', _context: { userId: 'forged' } } |
| 73 | + expect((await executeOciStreamingTool(request)).status).toBe(400) |
| 74 | + expect(mocks.authorize).not.toHaveBeenCalled() |
| 75 | + expect(mocks.create).not.toHaveBeenCalled() |
| 76 | + }) |
| 77 | + |
| 78 | + it('rejects missing trusted identity and mismatched operation IDs', async () => { |
| 79 | + expect((await executeOciStreamingTool(call({ context: { workflowId: '' } }))).status).toBe(401) |
| 80 | + expect((await executeOciStreamingTool(call({ toolId: 'oci_streaming_delete_stream' }))).status).toBe(400) |
| 81 | + expect(mocks.authorize).not.toHaveBeenCalled() |
| 82 | + }) |
| 83 | + |
| 84 | + it('does not load credentials after cancellation while authorization is pending', async () => { |
| 85 | + const controller = new AbortController() |
| 86 | + let finish: ((value: unknown) => void) | undefined |
| 87 | + mocks.authorize.mockImplementation(() => new Promise((resolve) => { finish = resolve })) |
| 88 | + const pending = executeOciStreamingTool(call({ signal: controller.signal })) |
| 89 | + controller.abort(new DOMException('Canceled', 'AbortError')) |
| 90 | + await pending |
| 91 | + finish?.({ ok: true, credentialType: 'service_account', resolvedCredentialId: 'resolved', workspaceId: 'workspace-1' }) |
| 92 | + await Promise.resolve() |
| 93 | + expect(mocks.create).not.toHaveBeenCalled() |
| 94 | + }) |
| 95 | +}) |
0 commit comments