The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.
TypeScript ai-receptionistanswering-serviceappointment-bookinglivekitlocal-firstmcpopen-sourceself-hostedsiptelephonytypescriptvoice-agentvoice-ai
Recording pass-through: guarded fetch, timeout, audio only (PR 2027)
Tim Gutberlet0d8913ceb904
diff --git a/apps/api/src/recording-route.ts b/apps/api/src/recording-route.tsindex 3f060f75..6aa670aa 100644--- a/apps/api/src/recording-route.ts+++ b/apps/api/src/recording-route.ts@@ -16,6 +16,7 @@ import { db } from './db.ts'import { noteProviderFailure, noteProviderOk } from './provider-failures.ts'import { s3Problem } from './provider-test.ts'import { RecordingConfigError, recordingStoreNamed, S3RequestError } from './recording-store.ts'+import { s3Fetch } from './s3-endpoint.ts'import { SecretsKeyError, signWithSecretsKey } from './secrets.ts'const SIGNED_URL_TTL_SEC = 300@@ -76,20 +77,45 @@ export async function serveRecording(return serveCallRecording(and(eq(calls.id, callId), eq(calls.workspace_id, actor.workspaceId)), rangeHeader, through)}-/** The bucket's answer for a signed link, passed on as the API's own (status, body and the headers a player needs). */+/** How long the bucket may take to answer a pass-through before the API gives up (the body then streams freely). */+const PASS_THROUGH_TIMEOUT_MS = 15_000++/**+ * The bucket's answer for a signed link, passed on as the API's own: its status, body and the headers a player needs.+ * Sent with the S3 store's fetch (never to a link-local or metadata address, also after a DNS change), no redirects;+ * only audio is served as such, with `nosniff`, so an object in the bucket never renders as a page of the app.+ */export async function passThrough(url: string,rangeHeader: string | undefined,- doFetch: typeof fetch = fetch,+ doFetch: typeof fetch = s3Fetch(),): Promise<Response> {- const answer = await doFetch(url, { headers: rangeHeader ? { range: rangeHeader } : {}, redirect: 'error' })+ const abort = new AbortController()+ const timer = setTimeout(() => abort.abort(), PASS_THROUGH_TIMEOUT_MS)+ let answer: Response+ try {+ answer = await doFetch(url, {+ headers: rangeHeader ? { range: rangeHeader } : {},+ redirect: 'error',+ signal: abort.signal,+ })+ } catch {+ return new Response(null, { status: 502 })+ } finally {+ clearTimeout(timer)+ }if (!answer.ok) {void answer.body?.cancel()// Only the status: the page draws no waveform then, and the bucket's words stay out of it.return new Response(null, { status: answer.status === 416 ? 416 : 502 })}- const headers = new Headers({ 'cache-control': 'private, no-store' })- for (const name of ['content-type', 'content-length', 'content-range', 'accept-ranges']) {+ const type = answer.headers.get('content-type') ?? ''+ const headers = new Headers({+ 'cache-control': 'private, no-store',+ 'x-content-type-options': 'nosniff',+ 'content-type': /^audio\/[\w.+-]+$/i.test(type.split(';')[0]?.trim() ?? '') ? type : 'application/octet-stream',+ })+ for (const name of ['content-length', 'content-range', 'accept-ranges']) {const value = answer.headers.get(name)if (value) headers.set(name, value)}diff --git a/apps/api/src/recording-through.test.ts b/apps/api/src/recording-through.test.tsindex 0378539c..7fe6b75b 100644--- a/apps/api/src/recording-through.test.ts+++ b/apps/api/src/recording-through.test.ts@@ -15,15 +15,16 @@ test('passes the bucket answer on with the headers a player needs, and the range'x-amz-id': 'secret-ish',})const answer = await passThrough('https://bucket.example/rec.ogg?sig=1', 'bytes=0-3', doFetch)- expect(doFetch).toHaveBeenCalledWith('https://bucket.example/rec.ogg?sig=1', {- headers: { range: 'bytes=0-3' },- redirect: 'error',- })+ expect(doFetch).toHaveBeenCalledWith(+ 'https://bucket.example/rec.ogg?sig=1',+ expect.objectContaining({ headers: { range: 'bytes=0-3' }, redirect: 'error' }),+ )expect(answer.status).toBe(206)expect(answer.headers.get('content-type')).toBe('audio/ogg')expect(answer.headers.get('content-range')).toBe('bytes 0-3/10')expect(answer.headers.get('x-amz-id')).toBeNull()expect(answer.headers.get('cache-control')).toBe('private, no-store')+ expect(answer.headers.get('x-content-type-options')).toBe('nosniff')expect(await answer.text()).toBe('OggS')})@@ -36,3 +37,22 @@ test('a refusal of the bucket is a 502, never its body', async () => {expect(answer.status).toBe(502)expect(await answer.text()).not.toContain('AccessDenied')})++test('an object that is not audio is served as plain bytes, never as a page of the app', async () => {+ const answer = await passThrough(+ 'https://bucket.example/rec.ogg',+ undefined,+ bucket(200, { 'content-type': 'text/html; charset=utf-8' }, '<script>'),+ )+ expect(answer.headers.get('content-type')).toBe('application/octet-stream')+ expect(answer.headers.get('x-content-type-options')).toBe('nosniff')+})++test('a link-local or metadata address is never fetched, and a failed fetch is a 502', async () => {+ const answer = await passThrough('http://169.254.169.254/latest/meta-data/', undefined)+ expect(answer.status).toBe(502)+ const failing = vi.fn(async () => {+ throw new TypeError('fetch failed')+ }) as unknown as typeof fetch+ expect((await passThrough('https://bucket.example/rec.ogg', undefined, failing)).status).toBe(502)+})