|
| 1 | +import { inputValidationMock, inputValidationMockFns } from '@sim/testing' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) |
| 5 | + |
| 6 | +/** Only this service's configs are needed; the full registry is ~6,000 modules. */ |
| 7 | +vi.mock('@/tools/registry', async () => { |
| 8 | + const { partialToolRegistry } = await import('@sim/testing/mocks/tool-registry.mock') |
| 9 | + return { tools: partialToolRegistry(await import('@/tools/databricks')) } |
| 10 | +}) |
| 11 | + |
| 12 | +import { executeTool } from '@/tools/index' |
| 13 | +import { tools } from '@/tools/registry' |
| 14 | + |
| 15 | +type ToolParams = Record<string, unknown> |
| 16 | + |
| 17 | +function urlBuilder(toolId: string): (params: ToolParams) => string { |
| 18 | + const url = tools[toolId].request.url |
| 19 | + if (typeof url !== 'function') throw new Error(`${toolId} has a static url`) |
| 20 | + return url as (params: ToolParams) => string |
| 21 | +} |
| 22 | + |
| 23 | +/** Every identifier any Databricks tool reads while building its URL. */ |
| 24 | +const REQUEST_PARAMS = { |
| 25 | + apiKey: 'dapi-test-token', |
| 26 | + spaceId: 'space1', |
| 27 | + conversationId: 'conv1', |
| 28 | + messageId: 'msg1', |
| 29 | + attachmentId: 'att1', |
| 30 | + statementId: 'stmt1', |
| 31 | + clusterId: 'cluster1', |
| 32 | + jobId: 1, |
| 33 | + runId: 2, |
| 34 | + warehouseId: 'wh1', |
| 35 | + content: 'question', |
| 36 | + sql: 'SELECT 1', |
| 37 | + rating: 'POSITIVE', |
| 38 | +} |
| 39 | + |
| 40 | +const DATABRICKS_TOOL_IDS = Object.keys(tools).filter((id) => id.startsWith('databricks_')) |
| 41 | + |
| 42 | +describe('databricks workspace host allowlist', () => { |
| 43 | + it.each([ |
| 44 | + 'attacker.example.com', |
| 45 | + 'https://attacker.example.com/', |
| 46 | + 'dbc-1.cloud.databricks.com.attacker.example.com', |
| 47 | + 'attacker.example.com/dbc-1.cloud.databricks.com', |
| 48 | + 'dbc-1.cloud.databricks.com@attacker.example.com', |
| 49 | + 'cloud.databricks.com', |
| 50 | + ])('refuses %s in every tool before a URL is built', (host) => { |
| 51 | + expect(DATABRICKS_TOOL_IDS.length).toBe(26) |
| 52 | + const accepted = DATABRICKS_TOOL_IDS.filter((id) => { |
| 53 | + try { |
| 54 | + urlBuilder(id)({ ...REQUEST_PARAMS, host }) |
| 55 | + return true |
| 56 | + } catch { |
| 57 | + return false |
| 58 | + } |
| 59 | + }) |
| 60 | + expect(accepted).toEqual([]) |
| 61 | + }) |
| 62 | + |
| 63 | + it('fails the tool call without sending the token to a foreign host', async () => { |
| 64 | + const result = await executeTool('databricks_list_clusters', { |
| 65 | + host: 'attacker.example.com', |
| 66 | + apiKey: 'dapi-test-token', |
| 67 | + }) |
| 68 | + |
| 69 | + expect(result.success).toBe(false) |
| 70 | + expect(result.error).toContain('host must be a Databricks-hosted domain') |
| 71 | + expect(inputValidationMockFns.mockValidateUrlWithDNS).not.toHaveBeenCalled() |
| 72 | + expect(inputValidationMockFns.mockSecureFetchWithPinnedIP).not.toHaveBeenCalled() |
| 73 | + }) |
| 74 | + |
| 75 | + it.each([ |
| 76 | + ['dbc-a1b2.cloud.databricks.com', 'https://dbc-a1b2.cloud.databricks.com'], |
| 77 | + [' https://dbc-a1b2.cloud.databricks.com/ ', 'https://dbc-a1b2.cloud.databricks.com'], |
| 78 | + ['http://dbc-a1b2.cloud.databricks.com', 'https://dbc-a1b2.cloud.databricks.com'], |
| 79 | + ['adb-123.4.azuredatabricks.net', 'https://adb-123.4.azuredatabricks.net'], |
| 80 | + ['https://123.4.gcp.databricks.com/', 'https://123.4.gcp.databricks.com'], |
| 81 | + ['dbc-a1b2.cloud.databricks.us', 'https://dbc-a1b2.cloud.databricks.us'], |
| 82 | + ['adb-123.4.databricks.azure.us', 'https://adb-123.4.databricks.azure.us'], |
| 83 | + ['adb-123.4.databricks.azure.cn', 'https://adb-123.4.databricks.azure.cn'], |
| 84 | + ])('builds the same request URLs for workspace host %s', (host, origin) => { |
| 85 | + const params = { ...REQUEST_PARAMS, host } |
| 86 | + expect(urlBuilder('databricks_list_clusters')(params)).toBe(`${origin}/api/2.0/clusters/list`) |
| 87 | + expect(urlBuilder('databricks_execute_sql')(params)).toBe(`${origin}/api/2.0/sql/statements/`) |
| 88 | + expect(urlBuilder('databricks_get_job')(params)).toBe(`${origin}/api/2.1/jobs/get?job_id=1`) |
| 89 | + expect(urlBuilder('databricks_get_run_output')(params)).toBe( |
| 90 | + `${origin}/api/2.1/jobs/runs/get-output?run_id=2` |
| 91 | + ) |
| 92 | + expect(urlBuilder('databricks_genie_get_message')(params)).toBe( |
| 93 | + `${origin}/api/2.0/genie/spaces/space1/conversations/conv1/messages/msg1` |
| 94 | + ) |
| 95 | + }) |
| 96 | +}) |
0 commit comments