d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
127 lines
4.8 KiB
TypeScript
127 lines
4.8 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { envFlagsMock } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const { mockAgent, mockUndiciFetch, capturedAgentOptions, agentCloses } = vi.hoisted(() => {
|
|
const capturedAgentOptions: unknown[] = []
|
|
const agentCloses: unknown[] = []
|
|
class MockAgent {
|
|
constructor(options: unknown) {
|
|
capturedAgentOptions.push(options)
|
|
}
|
|
close() {
|
|
agentCloses.push(this)
|
|
return Promise.resolve()
|
|
}
|
|
}
|
|
return {
|
|
mockAgent: MockAgent,
|
|
mockUndiciFetch: vi.fn(),
|
|
capturedAgentOptions,
|
|
agentCloses,
|
|
}
|
|
})
|
|
|
|
vi.mock('undici', () => ({ Agent: mockAgent, fetch: mockUndiciFetch }))
|
|
vi.mock('@/lib/core/config/env-flags', () => envFlagsMock)
|
|
|
|
import { createPinnedFetch } from '@/lib/core/security/input-validation.server'
|
|
|
|
type LookupCallback = (err: Error | null, address: string, family: number) => void
|
|
type PinnedLookup = (hostname: string, options: { all?: boolean }, callback: LookupCallback) => void
|
|
|
|
describe('createPinnedFetch', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
capturedAgentOptions.length = 0
|
|
agentCloses.length = 0
|
|
mockUndiciFetch.mockResolvedValue(new Response('ok'))
|
|
})
|
|
|
|
it('builds an undici Agent whose pinned lookup always resolves to the validated IP', async () => {
|
|
createPinnedFetch('203.0.113.10')
|
|
|
|
expect(capturedAgentOptions).toHaveLength(1)
|
|
const { connect } = capturedAgentOptions[0] as { connect: { lookup: PinnedLookup } }
|
|
expect(typeof connect.lookup).toBe('function')
|
|
|
|
const resolved = await new Promise<{ address: string; family: number }>((resolve) => {
|
|
connect.lookup('rebind.attacker.tld', {}, (_err, address, family) =>
|
|
resolve({ address, family })
|
|
)
|
|
})
|
|
expect(resolved).toEqual({ address: '203.0.113.10', family: 4 })
|
|
})
|
|
|
|
it('uses IPv6 family when the validated IP is IPv6', async () => {
|
|
createPinnedFetch('2606:4700:4700::1111')
|
|
const { connect } = capturedAgentOptions[0] as { connect: { lookup: PinnedLookup } }
|
|
const resolved = await new Promise<{ address: string; family: number }>((resolve) => {
|
|
connect.lookup('example.com', {}, (_err, address, family) => resolve({ address, family }))
|
|
})
|
|
expect(resolved).toEqual({ address: '2606:4700:4700::1111', family: 6 })
|
|
})
|
|
|
|
it('forwards the pinned dispatcher on every call while preserving init options', async () => {
|
|
const pinned = createPinnedFetch('203.0.113.10')
|
|
const controller = new AbortController()
|
|
|
|
await pinned('https://myresource.openai.azure.com/openai/v1/responses', {
|
|
method: 'POST',
|
|
headers: { 'api-key': 'secret' },
|
|
body: '{}',
|
|
signal: controller.signal,
|
|
})
|
|
|
|
expect(mockUndiciFetch).toHaveBeenCalledTimes(1)
|
|
const [url, init] = mockUndiciFetch.mock.calls[0]
|
|
expect(url).toBe('https://myresource.openai.azure.com/openai/v1/responses')
|
|
const typedInit = init as RequestInit & { dispatcher?: unknown }
|
|
expect(typedInit.dispatcher).toBeInstanceOf(mockAgent)
|
|
expect(typedInit.method).toBe('POST')
|
|
expect(typedInit.headers).toEqual({ 'api-key': 'secret' })
|
|
expect(typedInit.body).toBe('{}')
|
|
expect(typedInit.signal).toBe(controller.signal)
|
|
})
|
|
|
|
it('handles an undefined init by still attaching the dispatcher', async () => {
|
|
const pinned = createPinnedFetch('203.0.113.10')
|
|
await pinned('https://example.com')
|
|
const init = mockUndiciFetch.mock.calls[0][1] as { dispatcher?: unknown }
|
|
expect(init.dispatcher).toBeInstanceOf(mockAgent)
|
|
})
|
|
|
|
it('reuses one captured dispatcher across all calls of a single instance', async () => {
|
|
const pinned = createPinnedFetch('203.0.113.10')
|
|
await pinned('https://example.com/a')
|
|
await pinned('https://example.com/b')
|
|
|
|
expect(capturedAgentOptions).toHaveLength(1)
|
|
const d1 = (mockUndiciFetch.mock.calls[0][1] as { dispatcher: unknown }).dispatcher
|
|
const d2 = (mockUndiciFetch.mock.calls[1][1] as { dispatcher: unknown }).dispatcher
|
|
expect(d1).toBe(d2)
|
|
})
|
|
|
|
it('creates an independent dispatcher per instance', async () => {
|
|
const a = createPinnedFetch('203.0.113.10')
|
|
const b = createPinnedFetch('203.0.113.10')
|
|
await a('https://example.com/a')
|
|
await b('https://example.com/b')
|
|
|
|
expect(capturedAgentOptions).toHaveLength(2)
|
|
const d1 = (mockUndiciFetch.mock.calls[0][1] as { dispatcher: unknown }).dispatcher
|
|
const d2 = (mockUndiciFetch.mock.calls[1][1] as { dispatcher: unknown }).dispatcher
|
|
expect(d1).not.toBe(d2)
|
|
})
|
|
|
|
it('returns the response produced by undici fetch', async () => {
|
|
mockUndiciFetch.mockResolvedValueOnce(new Response('pong', { status: 201 }))
|
|
const pinned = createPinnedFetch('203.0.113.10')
|
|
const response = await pinned('https://example.com')
|
|
expect(response.status).toBe(201)
|
|
expect(await response.text()).toBe('pong')
|
|
})
|
|
})
|