Suno-style web UI for a self-hosted MiniMax-Music3 inference server
TypeScript ai-musicdiffusersdockerexpressminimaxminimax-musicmusic-generationreactself-hostedsgl-omnisunotailwindcsstext-to-musictypescriptvite
Bind API keys to their saved origin
Adam B.1120f7131137
diff --git a/server/src/app.ts b/server/src/app.tsindex d49a37e..cb250b2 100644--- a/server/src/app.ts+++ b/server/src/app.ts@@ -8,7 +8,7 @@ import { extFor, RenderQueue, UpstreamClient, UpstreamError } from './upstream.jimport { normalizeEnhance, normalizeGenerate, normalizeLyrics, ValidationError } from './validate.js';import { writeLyrics } from './lyrics.js';import { EnhanceError, enhancePrompt, type SkillFiles } from './enhance.js';-import { normalizeMusicApi, SettingsStore } from './settings.js';+import { normalizeMusicApi, sameOrigin, SettingsStore } from './settings.js';import { randomTitle } from './names.js';import { FORMATS, type Template, type Track } from './types.js';import { patchWavSizes, WAV_HEADER_BYTES } from './wav.js';@@ -188,7 +188,13 @@ export function createApp(deps: AppDeps) {let url = e.musicApi;let key = e.apiKey;try {- if (typeof b.musicApi === 'string' && !locked.musicApi) url = normalizeMusicApi(b.musicApi);+ if (typeof b.musicApi === 'string' && !locked.musicApi) {+ url = normalizeMusicApi(b.musicApi);+ if (!sameOrigin(e.musicApi, url)) {+ if (locked.apiKey) throw new ValidationError('MUSIC_API_KEY is set in the environment and cannot be used with a different musicApi origin');+ key = null;+ }+ }if (typeof b.apiKey === 'string' && !locked.apiKey) key = b.apiKey.trim() || null;} catch (err) {return res.status(400).json({ ok: false, error: (err as Error).message });diff --git a/server/src/settings.ts b/server/src/settings.tsindex b0c6230..02ec8eb 100644--- a/server/src/settings.ts+++ b/server/src/settings.ts@@ -59,6 +59,11 @@ export function normalizeLlmApi(v: unknown): string | null {return url.toString().replace(/\/+$/, '');}+export function sameOrigin(a: string | null, b: string | null): boolean {+ if (a === null || b === null) return a === b;+ return new URL(a).origin === new URL(b).origin;+}+const llmSource = (env: string | null | undefined, stored: string | null) => (env ? 'env' : stored ? 'settings' : 'none') as 'env' | 'settings' | 'none';/** Persists user-editable upstream settings; env vars always win over stored values. */@@ -137,7 +142,12 @@ export class SettingsStore {const next = { ...this.stored }; // applied only if every field validatesif (patch.musicApi !== undefined) {if (this.env.musicApi !== null) throw new ValidationError('MUSIC_API is set in the environment and cannot be changed here');- next.musicApi = normalizeMusicApi(patch.musicApi);+ const musicApi = normalizeMusicApi(patch.musicApi);+ if (!sameOrigin(this.effective().musicApi, musicApi)) {+ if (this.env.apiKey !== null) throw new ValidationError('MUSIC_API_KEY is set in the environment and cannot be used with a different musicApi origin');+ if (patch.apiKey === undefined) next.apiKey = null;+ }+ next.musicApi = musicApi;}if (patch.apiKey !== undefined) {if (this.env.apiKey !== null) throw new ValidationError('MUSIC_API_KEY is set in the environment and cannot be changed here');@@ -150,7 +160,12 @@ export class SettingsStore {}if (patch.llmApi !== undefined) {if (this.env.llmApi != null) throw new ValidationError('LLM_API is set in the environment and cannot be changed here');- next.llmApi = normalizeLlmApi(patch.llmApi);+ const llmApi = normalizeLlmApi(patch.llmApi);+ if (!sameOrigin(this.env.llmApi ?? this.stored.llmApi, llmApi)) {+ if (this.env.llmApiKey != null) throw new ValidationError('LLM_API_KEY is set in the environment and cannot be used with a different llmApi origin');+ if (patch.llmApiKey === undefined) next.llmApiKey = null;+ }+ next.llmApi = llmApi;}if (patch.llmApiKey !== undefined) {if (this.env.llmApiKey != null) throw new ValidationError('LLM_API_KEY is set in the environment and cannot be changed here');diff --git a/server/test/app.test.ts b/server/test/app.test.tsindex 93b050d..423b38b 100644--- a/server/test/app.test.ts+++ b/server/test/app.test.ts@@ -239,6 +239,99 @@ describe('app', () => {expect((await request(app).get('/api/settings')).body.musicApi).toBe('http://127.0.0.1:7862');});+ it('settings/test only reuses a saved music key for the same origin', async () => {+ const { app } = await setup({}, null, { musicApi: null, apiKey: null });+ await request(app).put('/api/settings').send({ musicApi: fake!.url, apiKey: 'saved-key' });+ const other = await startFakeUpstream();+ try {+ const withoutKey = await request(app).post('/api/settings/test').send({ musicApi: other.url });+ expect(withoutKey.status).toBe(200);+ expect(withoutKey.body.ok).toBe(true);+ expect(other.requests[0]?.auth).toBeUndefined();++ const withKey = await request(app).post('/api/settings/test').send({ musicApi: other.url, apiKey: 'candidate-key' });+ expect(withKey.status).toBe(200);+ expect(other.requests.at(-1)?.auth).toBe('Bearer candidate-key');++ const sameOrigin = await request(app).post('/api/settings/test').send({ musicApi: `${fake!.url}/` });+ expect(sameOrigin.body.ok).toBe(true);+ expect(fake!.requests.at(-1)?.auth).toBe('Bearer saved-key');+ } finally {+ await other.close();+ }+ });++ it('settings PUT clears old music and LLM keys when origins change unless replaced', async () => {+ const { app, dir } = await setup({}, null, { musicApi: null, apiKey: null });+ const otherMusic = await startFakeUpstream();+ const oldLlm = await startFakeLlm([{ content: 'ok' }]);+ const newLlm = await startFakeLlm([{ content: 'ok' }]);+ await seedSkill(dir);+ try {+ await request(app).put('/api/settings').send({ musicApi: fake!.url, apiKey: 'music-old', llmApi: oldLlm.url, llmApiKey: 'llm-old', llmModel: 'test-model' });++ const sameOrigin = await request(app).put('/api/settings').send({ musicApi: `${fake!.url}/path`, llmApi: `${oldLlm.url}/path` });+ expect(sameOrigin.body).toMatchObject({ apiKeySet: true, llmKeySet: true });+ await request(app).put('/api/settings').send({ musicApi: fake!.url, llmApi: oldLlm.url });++ await request(app).get('/api/health');+ expect(fake!.requests.at(-1)?.auth).toBe('Bearer music-old');+ await request(app).post('/api/enhance').send({ prompt: 'test' });+ expect(oldLlm.requests.at(-1)?.auth).toBe('Bearer llm-old');++ const movedMusic = await request(app).put('/api/settings').send({ musicApi: otherMusic.url });+ expect(movedMusic.body.apiKeySet).toBe(false);+ await request(app).get('/api/health');+ expect(otherMusic.requests.at(-1)?.auth).toBeUndefined();++ const replacedMusic = await request(app).put('/api/settings').send({ musicApi: fake!.url, apiKey: 'music-new' });+ expect(replacedMusic.body.apiKeySet).toBe(true);+ await request(app).get('/api/health');+ expect(fake!.requests.at(-1)?.auth).toBe('Bearer music-new');++ const movedLlm = await request(app).put('/api/settings').send({ llmApi: newLlm.url });+ expect(movedLlm.body.llmKeySet).toBe(false);+ await request(app).post('/api/enhance').send({ prompt: 'test' });+ expect(newLlm.requests.at(-1)?.auth).toBeUndefined();++ const replacedLlm = await request(app).put('/api/settings').send({ llmApi: oldLlm.url, llmApiKey: 'llm-new' });+ expect(replacedLlm.body.llmKeySet).toBe(true);+ await request(app).post('/api/enhance').send({ prompt: 'test' });+ expect(oldLlm.requests.at(-1)?.auth).toBe('Bearer llm-new');+ } finally {+ await otherMusic.close();+ await oldLlm.close();+ await newLlm.close();+ }+ });++ it('rejects changing an API origin when its key is environment-locked', async () => {+ const { app, dir } = await setup({}, null, { musicApi: null, apiKey: 'env-music-key', llmApi: null, llmApiKey: 'env-llm-key', llmModel: null });+ const otherLlm = await startFakeLlm([{ content: 'ok' }]);+ try {+ const test = await request(app).post('/api/settings/test').send({ musicApi: fake!.url });+ expect(test.status).toBe(400);+ expect(test.body.error).toMatch(/MUSIC_API_KEY/);+ expect(fake!.requests).toHaveLength(0);++ const music = await request(app).put('/api/settings').send({ musicApi: fake!.url });+ expect(music.status).toBe(400);+ expect(music.body.error).toMatch(/MUSIC_API_KEY/);++ const llm = await request(app).put('/api/settings').send({ llmApi: otherLlm.url });+ expect(llm.status).toBe(400);+ expect(llm.body.error).toMatch(/LLM_API_KEY/);+ expect(otherLlm.requests).toHaveLength(0);++ expect((await request(app).get('/api/settings')).body).toMatchObject({+ musicApi: 'http://127.0.0.1:7862', apiKeySet: true, llmApi: null, llmKeySet: true,+ });+ await expect(fs.readFile(path.join(dir, 'settings.json'), 'utf8')).rejects.toMatchObject({ code: 'ENOENT' });+ } finally {+ await otherLlm.close();+ }+ });+it('settings: LLM fields persist, gate health.enhance, and never return the key', async () => {const { app, dir } = await setup();expect((await request(app).get('/api/health')).body.enhance).toBe(false);