Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(voice): the browser transcribe POST reaches whisper instead of the 415 wall

purpleb05e04bb08cb
diff --git a/src/api/voice.ts b/src/api/voice.ts
index 30a2e83a..f9ec6ee1 100644
--- a/src/api/voice.ts
+++ b/src/api/voice.ts
@@ -182,7 +182,13 @@ export async function checkWhisperAvailable(): Promise<{
if (isTauri()) {
return await backendCall("whisper_status");
}
- const res = await fetch("/local-api/transcribe-status");
+ // The /local-api middleware refuses any request without the CSRF header
+ // (403). backendCall sends it on every dev-mode call; these two voice
+ // fetches went out bare, so the browser path never reached whisper
+ // (GitHub #115).
+ const res = await fetch("/local-api/transcribe-status", {
+ headers: { "x-locally-uncensored": "true" },
+ });
return res.json();
} catch {
return { available: false, backend: null, error: "Failed to reach transcribe-status endpoint" };
@@ -213,7 +219,11 @@ export async function transcribeAudio(audioBlob: Blob): Promise<string> {
const res = await fetch("/local-api/transcribe", {
method: "POST",
- headers: { "Content-Type": audioBlob.type || "audio/webm" },
+ headers: {
+ "Content-Type": audioBlob.type || "audio/webm",
+ // CSRF header the middleware demands on every /local-api call (#115).
+ "x-locally-uncensored": "true",
+ },
body: audioBlob,
});
const data = await res.json();
diff --git a/src/lib/__tests__/local-api-guard.test.ts b/src/lib/__tests__/local-api-guard.test.ts
new file mode 100644
index 00000000..bfb9075f
--- /dev/null
+++ b/src/lib/__tests__/local-api-guard.test.ts
@@ -0,0 +1,90 @@
+/**
+ * GitHub #115 (graysoncooper): browser voice recording always failed, the
+ * /local-api middleware 415'd the transcribe POST because it enforced
+ * application/json on every body, and the transcribe body IS the recorded
+ * audio. On top, both voice fetches went out without the CSRF header every
+ * backendCall sends, so even a passing body would have met a 403.
+ *
+ * These tests lock the carve-out to exactly one endpoint (negative controls:
+ * JSON on transcribe is still refused, audio anywhere else is still refused)
+ * and pin the wiring in vite.config.ts and voice.ts.
+ *
+ * Run: npx vitest run src/lib/__tests__/local-api-guard.test.ts
+ */
+import { readFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { describe, it, expect } from 'vitest'
+import { isTranscribePath, postContentTypeAllowed, postContentTypeError } from '../local-api-guard'
+
+describe('isTranscribePath', () => {
+ it('matches the transcribe endpoint, with and without a query', () => {
+ expect(isTranscribePath('/transcribe')).toBe(true)
+ expect(isTranscribePath('/transcribe?lang=de')).toBe(true)
+ })
+
+ it('does not match its neighbours or an absent url (negative control)', () => {
+ expect(isTranscribePath('/transcribe-status')).toBe(false)
+ expect(isTranscribePath('/download-model')).toBe(false)
+ expect(isTranscribePath(undefined)).toBe(false)
+ })
+})
+
+describe('postContentTypeAllowed', () => {
+ it('lets every real recorder body through on /transcribe', () => {
+ expect(postContentTypeAllowed('/transcribe', 'audio/webm;codecs=opus')).toBe(true)
+ expect(postContentTypeAllowed('/transcribe', 'audio/mp4')).toBe(true)
+ expect(postContentTypeAllowed('/transcribe', 'audio/wav')).toBe(true)
+ expect(postContentTypeAllowed('/transcribe', 'video/webm')).toBe(true)
+ expect(postContentTypeAllowed('/transcribe', 'application/octet-stream')).toBe(true)
+ expect(postContentTypeAllowed('/transcribe', 'AUDIO/WEBM')).toBe(true)
+ })
+
+ it('NEGATIVE CONTROL: the carve-out is not a free pass, JSON and text on /transcribe stay refused', () => {
+ expect(postContentTypeAllowed('/transcribe', 'application/json')).toBe(false)
+ expect(postContentTypeAllowed('/transcribe', 'text/plain')).toBe(false)
+ expect(postContentTypeAllowed('/transcribe', '')).toBe(false)
+ })
+
+ it('every other endpoint keeps the strict JSON rule', () => {
+ expect(postContentTypeAllowed('/download-model', 'application/json')).toBe(true)
+ expect(postContentTypeAllowed('/download-model', 'application/json; charset=utf-8')).toBe(true)
+ expect(postContentTypeAllowed(undefined, 'application/json')).toBe(true)
+ })
+
+ it('NEGATIVE CONTROL: the audio exception does not leak to other endpoints', () => {
+ expect(postContentTypeAllowed('/download-model', 'audio/webm')).toBe(false)
+ expect(postContentTypeAllowed('/shell-execute', 'application/octet-stream')).toBe(false)
+ expect(postContentTypeAllowed('/transcribe-status', 'audio/webm')).toBe(false)
+ })
+})
+
+describe('postContentTypeError', () => {
+ it('names the audio requirement on /transcribe', () => {
+ expect(postContentTypeError('/transcribe')).toContain('audio')
+ })
+
+ it('keeps the exact old wording everywhere else, nothing changes for JSON endpoints', () => {
+ expect(postContentTypeError('/download-model')).toBe('Unsupported Media Type: Must be application/json')
+ })
+})
+
+describe('wiring (source guards)', () => {
+ const lf = (p: string) => readFileSync(join(__dirname, p), 'utf8').replace(/\r\n/g, '\n')
+ const viteSrc = lf('../../../vite.config.ts')
+ const voiceSrc = lf('../../api/voice.ts')
+
+ it('the middleware asks the shared guard instead of hard-coding JSON', () => {
+ expect(viteSrc).toContain("import { postContentTypeAllowed, postContentTypeError } from './src/lib/local-api-guard'")
+ expect(viteSrc).toContain('if (!postContentTypeAllowed(req.url, contentType))')
+ expect(viteSrc).toContain('res.end(postContentTypeError(req.url))')
+ })
+
+ it('both voice fetches carry the CSRF header the middleware demands', () => {
+ const hits = voiceSrc.split('"x-locally-uncensored": "true"').length - 1
+ expect(hits).toBeGreaterThanOrEqual(2)
+ })
+
+ it('the transcribe POST still declares the real audio type of the clip', () => {
+ expect(voiceSrc).toContain('"Content-Type": audioBlob.type || "audio/webm"')
+ })
+})
diff --git a/src/lib/local-api-guard.ts b/src/lib/local-api-guard.ts
new file mode 100644
index 00000000..b9ff8096
--- /dev/null
+++ b/src/lib/local-api-guard.ts
@@ -0,0 +1,49 @@
+/**
+ * GitHub #115 (graysoncooper): browser voice recording always failed because
+ * the /local-api security middleware enforced application/json on EVERY POST
+ * body, and /local-api/transcribe is the one endpoint whose body IS the raw
+ * recorded audio (audio/webm from Chrome's MediaRecorder, audio/mp4 from
+ * Safari). The 415 fired before the whisper handler ever saw the request.
+ *
+ * This module is the pure decision the middleware applies, importable both
+ * by vite.config.ts (Node side) and by vitest. The transcribe carve-out
+ * swaps the JSON requirement for an audio requirement rather than dropping
+ * the check: a JSON or text body on /transcribe is still refused, and every
+ * other endpoint keeps the strict application/json rule. CSRF protection
+ * (the x-locally-uncensored header and the origin check) stays on for
+ * transcribe, the client sends the header like every backendCall does.
+ *
+ * `url` is mount-relative, the middleware mounts at '/local-api', so the
+ * transcribe endpoint arrives here as '/transcribe'.
+ */
+
+/** The raw-audio POST endpoint. transcribe-status is a GET and stays under
+ * the JSON rule if anything ever POSTs to it. */
+export function isTranscribePath(url: string | undefined): boolean {
+ if (!url) return false
+ return url === '/transcribe' || url.startsWith('/transcribe?')
+}
+
+/** Body types a MediaRecorder clip legitimately arrives as. */
+function isAudioContentType(contentType: string): boolean {
+ const ct = contentType.toLowerCase()
+ return (
+ ct.startsWith('audio/') ||
+ // Chrome labels some Opus recordings video/webm even for audio-only.
+ ct.startsWith('video/webm') ||
+ ct.startsWith('application/octet-stream')
+ )
+}
+
+/** The middleware's POST body verdict for a mount-relative /local-api URL. */
+export function postContentTypeAllowed(url: string | undefined, contentType: string): boolean {
+ if (isTranscribePath(url)) return isAudioContentType(contentType)
+ return contentType.includes('application/json')
+}
+
+/** Matching 415 body, names what the endpoint actually accepts. */
+export function postContentTypeError(url: string | undefined): string {
+ return isTranscribePath(url)
+ ? 'Unsupported Media Type: Must be an audio body (audio/*, video/webm or application/octet-stream)'
+ : 'Unsupported Media Type: Must be application/json'
+}
diff --git a/vite.config.ts b/vite.config.ts
index d46c476c..def781e9 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -12,6 +12,7 @@ import { dirname } from 'path'
import os from 'os'
import dns from 'node:dns'
import net from 'node:net'
+import { postContentTypeAllowed, postContentTypeError } from './src/lib/local-api-guard'
// ── Dev-server SSRF guard ───────────────────────────────────────
// The dev proxies that fetch a *user-supplied* ?url= (proxy-image,
@@ -368,12 +369,16 @@ function comfyLauncher(): Plugin {
return next();
}
- // 1. Strict Content-Type enforcement for POST requests
+ // 1. Strict Content-Type enforcement for POST requests. The rule is
+ // application/json everywhere except /transcribe, whose body IS the
+ // raw recorded audio and was 415'd here before the whisper handler
+ // ever saw it (GitHub #115, graysoncooper). The carve-out swaps the
+ // JSON requirement for an audio one, it does not drop the check.
if (req.method === 'POST') {
- const contentType = req.headers['content-type'] || '';
- if (!contentType.includes('application/json')) {
+ const contentType = String(req.headers['content-type'] || '');
+ if (!postContentTypeAllowed(req.url, contentType)) {
res.writeHead(415, { 'Content-Type': 'text/plain' });
- res.end('Unsupported Media Type: Must be application/json');
+ res.end(postContentTypeError(req.url));
return;
}
}