Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): fail-safe apiAccess flags + validate Kokoro voice

Adam Eivydcc6e0e70308
diff --git a/server/lib/apiRegistry.js b/server/lib/apiRegistry.js
index 113ea946e5..d76c20bd74 100644
--- a/server/lib/apiRegistry.js
+++ b/server/lib/apiRegistry.js
@@ -59,15 +59,21 @@ export const API_REGISTRY = [
];
// Resolve one registry entry's runtime flags against persisted settings,
-// falling back to the entry's defaults when `apiAccess.<settingsKey>` (or a
-// specific flag) is absent. The `?? defaults` fallback is what keeps an install
-// with no `apiAccess` key (fresh / pre-migration) fully gated.
+// falling back to the entry's defaults when `apiAccess.<settingsKey>` is absent
+// OR carries a non-boolean value. Accepting ONLY real booleans is a security
+// requirement, not just hygiene: the auth gate calls this against settings read
+// straight off disk (it bypasses the PUT /api/settings Zod validation), so a
+// hand-edited / corrupted `"exposed": "false"` must NOT be treated as truthy
+// and open the route. A non-boolean falls back to the (closed-by-default)
+// value, so corruption fails safe.
+const asBool = (value, fallback) => (typeof value === 'boolean' ? value : fallback);
+
const resolveEntry = (entry, settings) => {
const persisted = settings?.apiAccess?.[entry.settingsKey] ?? {};
return {
...entry,
- exposed: persisted.exposed ?? entry.defaults.exposed,
- requireAuth: persisted.requireAuth ?? entry.defaults.requireAuth,
+ exposed: asBool(persisted.exposed, entry.defaults.exposed),
+ requireAuth: asBool(persisted.requireAuth, entry.defaults.requireAuth),
};
};
diff --git a/server/lib/apiRegistry.test.js b/server/lib/apiRegistry.test.js
index a384b67960..c90c205267 100644
--- a/server/lib/apiRegistry.test.js
+++ b/server/lib/apiRegistry.test.js
@@ -82,6 +82,18 @@ describe('apiRegistry', () => {
it('returns false for non-string path', () => {
expect(isRegistryPublic(settingsWith({ voice: { exposed: true } }), null)).toBe(false);
});
+
+ it('fails safe when a corrupted settings.json has non-boolean flags', () => {
+ // The auth gate reads settings straight off disk (no Zod), so a
+ // hand-edited string like "false" must NOT be treated as truthy.
+ const s = settingsWith({ voice: { exposed: 'true', requireAuth: 'false' } });
+ expect(isRegistryPublic(s, '/api/voice/public/synthesize')).toBe(false);
+ });
+
+ it('treats a non-boolean exposed as the (closed) default', () => {
+ const s = settingsWith({ voice: { exposed: 1, requireAuth: false } });
+ expect(isRegistryPublic(s, '/api/voice/public/synthesize')).toBe(false);
+ });
});
describe('resolveApiAccess', () => {
@@ -108,5 +120,11 @@ describe('apiRegistry', () => {
expect(voice.exposed).toBe(true);
expect(voice.requireAuth).toBe(false); // default
});
+
+ it('coerces non-boolean persisted flags to the default', () => {
+ const resolved = resolveApiAccess(settingsWith({ voice: { exposed: 'true' } }));
+ const voice = resolved.find((a) => a.id === 'voice');
+ expect(voice.exposed).toBe(false); // string "true" is not a boolean → default
+ });
});
});
diff --git a/server/routes/publicApiIntegration.test.js b/server/routes/publicApiIntegration.test.js
index 5efabe7365..1520479edd 100644
--- a/server/routes/publicApiIntegration.test.js
+++ b/server/routes/publicApiIntegration.test.js
@@ -53,7 +53,12 @@ const buildApp = async () => {
const app = express();
app.use(express.json());
app.use(authGate);
- app.set('io', { emit: () => {} });
+ // Intentionally do NOT set `io`: asyncHandler's error path only fires
+ // emitErrorEvent (→ errorEvents.emit('error', …)) when `io` is present, and
+ // vi.resetModules() re-imports a FRESH errorHandler each buildApp() with no
+ // 'error' listener — a bare EventEmitter throws on an unhandled 'error'
+ // event. Leaving `io` unset skips that path; the 400 response is still sent.
+ // None of the routes exercised here need `io` (we don't hit /api/voice/speak).
app.use('/api/voice/public', voicePublicRoutes);
app.use('/api/voice', voiceRoutes);
return app;
@@ -73,6 +78,26 @@ describe('public voice API — end-to-end through authGate', () => {
expect(res.headers['content-type']).toMatch(/audio\/wav/);
});
+ it('400 UNKNOWN_VOICE for an unknown Kokoro voice override', async () => {
+ // synthesize() validates Kokoro voices against the catalog (symmetric with
+ // Piper) so a bogus id returns the documented 400 instead of erroring in
+ // the model. tts.js + kokoro-voices.js are real here; only the backend is mocked.
+ const app = await buildApp();
+ const res = await request(app)
+ .post('/api/voice/public/synthesize')
+ .send({ text: 'hi', engine: 'kokoro', voice: 'not_a_real_voice' });
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('UNKNOWN_VOICE');
+ });
+
+ it('accepts a valid Kokoro voice override', async () => {
+ const app = await buildApp();
+ const res = await request(app)
+ .post('/api/voice/public/synthesize')
+ .send({ text: 'hi', engine: 'kokoro', voice: 'af_heart' });
+ expect(res.status).toBe(200);
+ });
+
it('auth ON + exposed + passwordless: public synthesize works WITHOUT a token', async () => {
const auth = await import('../services/auth.js');
await auth.setPassword({ newPassword: 'correct-horse' });
diff --git a/server/services/voice/kokoro-voices.js b/server/services/voice/kokoro-voices.js
index a487cfa88a..6589e8a42f 100644
--- a/server/services/voice/kokoro-voices.js
+++ b/server/services/voice/kokoro-voices.js
@@ -34,3 +34,10 @@ export const KOKORO_VOICES = Object.freeze({
bm_george: { gender: 'Male', language: 'en-GB', grade: 'C' },
bm_lewis: { gender: 'Male', language: 'en-GB', grade: 'D+' },
});
+
+// True iff `id` is a known Kokoro voice. Mirrors `findPiperVoice` so the TTS
+// façade can reject an unknown Kokoro voice override (otherwise an invalid
+// `voice` would pass straight to the model and either error or synthesize the
+// wrong voice). `Object.hasOwn` avoids matching inherited props like `toString`.
+export const isKokoroVoice = (id) =>
+ typeof id === 'string' && Object.hasOwn(KOKORO_VOICES, id);
diff --git a/server/services/voice/tts.js b/server/services/voice/tts.js
index b60f6f5518..788784f508 100644
--- a/server/services/voice/tts.js
+++ b/server/services/voice/tts.js
@@ -4,6 +4,7 @@ import { getVoiceConfig, piperVoiceTildePath } from './config.js';
import { synthesizeKokoro, listKokoroVoices } from './tts-kokoro.js';
import { synthesizePiper, listPiperVoices } from './tts-piper.js';
import { findPiperVoice } from './piper-voices.js';
+import { isKokoroVoice } from './kokoro-voices.js';
import { ServerError } from '../../lib/errorHandler.js';
// Single source of truth for the supported TTS engine names. Imported by
@@ -47,6 +48,15 @@ export const synthesize = async (text, opts = {}) => {
// Spread from `ttsCfg` (not `cfg.tts`) so a transient rate override applied
// above is preserved alongside the voice override.
if (engine === 'kokoro') {
+ // Reject unknown Kokoro voice overrides (symmetric with the Piper branch
+ // below) so the public synth API returns the documented 400 UNKNOWN_VOICE
+ // instead of forwarding a bogus id to the model and erroring/wrong-voicing.
+ if (!isKokoroVoice(opts.voice)) {
+ throw new ServerError(`unknown kokoro voice: ${opts.voice}`, {
+ status: 400,
+ code: 'UNKNOWN_VOICE',
+ });
+ }
ttsCfg = { ...ttsCfg, kokoro: { ...ttsCfg.kokoro, voice: opts.voice } };
} else {
// Reject Piper voice overrides that aren't in the curated catalog —