|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockSecureFetch, mockValidateUrlWithDNS } = vi.hoisted(() => ({ |
| 7 | + mockSecureFetch: vi.fn(), |
| 8 | + mockValidateUrlWithDNS: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/core/security/input-validation.server', () => ({ |
| 12 | + secureFetchWithPinnedIP: mockSecureFetch, |
| 13 | + validateAndPinProxyUrl: vi.fn(), |
| 14 | + validateUrlWithDNS: mockValidateUrlWithDNS, |
| 15 | +})) |
| 16 | + |
| 17 | +import { getDocumentTool } from '@/tools/elasticsearch/get_document' |
| 18 | +import { executeTool } from '@/tools/index' |
| 19 | + |
| 20 | +const PARAMS = { |
| 21 | + deploymentType: 'self_hosted', |
| 22 | + host: 'https://es.example.com:9200', |
| 23 | + authMethod: 'api_key', |
| 24 | + apiKey: 'test-key', |
| 25 | + index: 'products', |
| 26 | + documentId: 'nope', |
| 27 | +} |
| 28 | + |
| 29 | +function jsonResponse(status: number, statusText: string, body: unknown): Response { |
| 30 | + return new Response(JSON.stringify(body), { |
| 31 | + status, |
| 32 | + statusText, |
| 33 | + headers: { 'content-type': 'application/json' }, |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +describe('a 404 never reaches transformResponse', () => { |
| 38 | + let transformSpy: ReturnType<typeof vi.spyOn> |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + vi.clearAllMocks() |
| 42 | + mockValidateUrlWithDNS.mockResolvedValue({ isValid: true, resolvedIP: '203.0.113.10' }) |
| 43 | + transformSpy = vi.spyOn(getDocumentTool, 'transformResponse') |
| 44 | + }) |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + transformSpy.mockRestore() |
| 48 | + }) |
| 49 | + |
| 50 | + /** |
| 51 | + * Replaces an assertion that read `transformResponse.toString()` and checked it |
| 52 | + * did not contain the literal `'404'`. That passed for *any* rewrite, including |
| 53 | + * one that reintroduced a `found: false` branch under a different spelling, and |
| 54 | + * it was the only guard on the behavior the elasticsearch error extractor |
| 55 | + * depends on. The guarantee is a property of the executor, not of the source |
| 56 | + * text: `executeTool` reads the body and throws on `!response.ok` before |
| 57 | + * `transformResponse` is ever invoked. |
| 58 | + */ |
| 59 | + it('fails the call and leaves transformResponse uncalled on a missing document', async () => { |
| 60 | + mockSecureFetch.mockResolvedValue( |
| 61 | + jsonResponse(404, 'Not Found', { _index: 'products', _id: 'nope', found: false }) |
| 62 | + ) |
| 63 | + |
| 64 | + const result = await executeTool('elasticsearch_get_document', PARAMS, { |
| 65 | + skipPostProcess: true, |
| 66 | + }) |
| 67 | + |
| 68 | + expect(result.success).toBe(false) |
| 69 | + expect(transformSpy).not.toHaveBeenCalled() |
| 70 | + expect(result.output?.found).toBeUndefined() |
| 71 | + }) |
| 72 | + |
| 73 | + it('surfaces the named missing-document message rather than a bare "Not Found"', async () => { |
| 74 | + mockSecureFetch.mockResolvedValue( |
| 75 | + jsonResponse(404, 'Not Found', { _index: 'products', _id: 'nope', found: false }) |
| 76 | + ) |
| 77 | + |
| 78 | + const result = await executeTool('elasticsearch_get_document', PARAMS, { |
| 79 | + skipPostProcess: true, |
| 80 | + }) |
| 81 | + |
| 82 | + expect(result.error).toBe('Document "nope" was not found in index "products"') |
| 83 | + }) |
| 84 | + |
| 85 | + it('surfaces the reason and never the WWW-Authenticate challenge on a 401', async () => { |
| 86 | + mockSecureFetch.mockResolvedValue( |
| 87 | + jsonResponse(401, 'Unauthorized', { |
| 88 | + error: { |
| 89 | + root_cause: [ |
| 90 | + { |
| 91 | + type: 'security_exception', |
| 92 | + reason: 'missing authentication credentials for REST request [/products/_doc/nope]', |
| 93 | + header: { 'WWW-Authenticate': ['Basic realm="security"', 'ApiKey'] }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + type: 'security_exception', |
| 97 | + reason: 'missing authentication credentials for REST request [/products/_doc/nope]', |
| 98 | + header: { 'WWW-Authenticate': ['Basic realm="security"', 'ApiKey'] }, |
| 99 | + }, |
| 100 | + status: 401, |
| 101 | + }) |
| 102 | + ) |
| 103 | + |
| 104 | + const result = await executeTool('elasticsearch_get_document', PARAMS, { |
| 105 | + skipPostProcess: true, |
| 106 | + }) |
| 107 | + |
| 108 | + expect(result.success).toBe(false) |
| 109 | + expect(transformSpy).not.toHaveBeenCalled() |
| 110 | + expect(result.error).toBe( |
| 111 | + 'security_exception: missing authentication credentials for REST request [/products/_doc/nope]' |
| 112 | + ) |
| 113 | + expect(result.error).not.toContain('WWW-Authenticate') |
| 114 | + }) |
| 115 | + |
| 116 | + it('still runs transformResponse on a 200, so the guard is not vacuous', async () => { |
| 117 | + mockSecureFetch.mockResolvedValue( |
| 118 | + jsonResponse(200, 'OK', { |
| 119 | + _index: 'products', |
| 120 | + _id: 'abc', |
| 121 | + _version: 3, |
| 122 | + found: true, |
| 123 | + _source: { name: 'Widget' }, |
| 124 | + }) |
| 125 | + ) |
| 126 | + |
| 127 | + const result = await executeTool('elasticsearch_get_document', PARAMS, { |
| 128 | + skipPostProcess: true, |
| 129 | + }) |
| 130 | + |
| 131 | + expect(transformSpy).toHaveBeenCalledTimes(1) |
| 132 | + expect(result.success).toBe(true) |
| 133 | + expect(result.output?.found).toBe(true) |
| 134 | + }) |
| 135 | +}) |
0 commit comments