Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Coach: drop the stored key when the endpoint moves to another host

Duarte Santosfe9f5d14ffc4
diff --git a/api/coach/routes.js b/api/coach/routes.js
index 6b6fdafa5..400d114bc 100644
--- a/api/coach/routes.js
+++ b/api/coach/routes.js
@@ -266,7 +266,16 @@ export function coachRoutes({ json, readBody, readSession, requireAdmin }) {
Math.min(cfgStore.MAX_OUTPUT_TOKENS_CEILING, +body.maxOutputTokens || current.maxOutputTokens)
);
}
- cfgStore.save(patch);
+ const hostOf = cfg => { try { return new URL(baseUrlFor(target, cfg) || '').host; } catch { return ''; } };
+ const before = hostOf(current);
+ const saved = cfgStore.save(patch);
+ // The stored key is never read back, and it should not leave by another door either: sent
+ // to a new host it is handed to whoever runs that host on the next model list or job. A
+ // different host for the same provider means the key is entered again.
+ if (patch.providerOptions && before && hostOf(saved) !== before && saved.auth?.[target]) {
+ cfgStore.saveAuth(target, null);
+ console.log('coach: stored key for', target, 'cleared, endpoint host changed');
+ }
json(res, 200, { ok: true });
},
diff --git a/api/test/routes.test.js b/api/test/routes.test.js
index 73ccb7b94..0f6766a57 100644
--- a/api/test/routes.test.js
+++ b/api/test/routes.test.js
@@ -333,3 +333,17 @@ test('an admin can store extra headers for the compatible endpoint; framing name
assert.equal((await call('GET /api/admin/coach')).body.headers, null);
assert.equal('headers' in cfg.load().providerOptions.compatible, false);
});
+
+// The stored key is never read back. Pointing the provider at another host would have sent it
+// there on the next model list or job, so a new host means entering the key again.
+test('a stored key is dropped when the endpoint moves to another host, kept when only the path does', async () => {
+ fresh({ provider: 'compatible', providerOptions: { compatible: { baseUrl: 'https://llm.home.test/v1' } } });
+ cfg.saveAuth('compatible', { type: 'apikey', data: cfg.encrypt({ token: 'sk-owner' }) });
+ const { call } = harness();
+ let r = await call('POST /api/admin/coach/config', { baseUrl: 'https://llm.home.test/openai/v1' });
+ assert.equal(r.status, 200);
+ assert.ok(cfg.load().auth.compatible, 'same host: the key stays');
+ r = await call('POST /api/admin/coach/config', { baseUrl: 'https://collector.example/v1' });
+ assert.equal(r.status, 200);
+ assert.equal(cfg.load().auth.compatible, undefined, 'another host: the key is gone');
+});