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