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: an error answer shows the provider's JSON message, never the raw body

Duarte Santos86e0c96ddd7b
diff --git a/api/coach/core/adapters/http.js b/api/coach/core/adapters/http.js
index 982580586..c74152262 100644
--- a/api/coach/core/adapters/http.js
+++ b/api/coach/core/adapters/http.js
@@ -58,6 +58,15 @@ const LEADING_VERSION = new RegExp('^' + VERSION_SEGMENT.source, 'i');
// without the version is remembered so the next request goes there first. A bare host and every
// base that works today still gets exactly the request it got before.
const versionlessBases = new Set();
+// What an error answer is allowed to say back: the provider's own JSON error message, never the
+// raw body. The endpoint of a compatible provider is any URL an admin types, so echoing the body
+// would let one admin read the first lines of any page the server can reach (a router, a service
+// on the LAN). A provider that failed says why in JSON; anything else is reported by its status.
+function shownError(spec, data, max = 200) {
+ const m = spec.errorMessage(data);
+ return typeof m === 'string' && m ? trim(m, max) : '(the endpoint sent no error message)';
+}
+
function candidateUrls(spec, base, path) {
const url = endpointUrl(base, path);
if (!spec.versionlessFallback) return [url];
@@ -144,7 +153,7 @@ export function httpAdapter(spec) {
return { ok: false, error: e.name === 'AbortError' ? 'timed out' : `could not reach ${hostOf(base)}: ${trim(e.message, 120)}`, models: [] };
}
const { data, text } = await readJson(res);
- if (!res.ok) return { ok: false, error: `${res.status} ${trim(spec.errorMessage(data) || text, 200)}`, models: [] };
+ if (!res.ok) return { ok: false, error: `${res.status} ${shownError(spec, data)}`, models: [] };
let models;
try { models = spec.readModels(data); } catch { models = null; }
if (!Array.isArray(models)) return { ok: false, error: 'unexpected model list shape', models: [] };
@@ -194,7 +203,7 @@ export function httpAdapter(spec) {
retriedWithoutJsonMode = true;
continue;
}
- return { code: 1, text: '', stderr: `${res.status} ${trim(msg, 280)}` };
+ return { code: 1, text: '', stderr: `${res.status} ${shownError(spec, data, 280)}` };
}
let out;
try { out = spec.readText(data); } catch (e) { out = { error: `unexpected response shape: ${trim(e.message, 100)}` }; }
diff --git a/api/test/adapters-http.test.js b/api/test/adapters-http.test.js
index 6849c2f37..152727855 100644
--- a/api/test/adapters-http.test.js
+++ b/api/test/adapters-http.test.js
@@ -266,11 +266,17 @@ test('an abort that lands during a retry pause sends nothing more and ends as a
assert.equal(r.timedOut, true);
});
-test('a body that is not JSON on an error still yields a readable stderr', async () => {
+test('a body that is not JSON on an error yields its status, never the body itself', async () => {
const f = fakeFetch([{ status: 502, body: '<html>bad gateway</html>' }]);
const r = await gemini.invoke({ cfg: {}, prompt: 'P', env, fetch: f });
assert.equal(r.code, 1);
- assert.match(r.stderr, /^502 .*bad gateway/);
+ assert.match(r.stderr, /^502 \(the endpoint sent no error message\)$/);
+ // The model list too: an admin pointing a compatible endpoint at a page inside the network
+ // learns the status, not the page.
+ const g = fakeFetch([{ status: 403, body: 'router admin: secret-ssid' }, { status: 403, body: 'router admin: secret-ssid' }]);
+ const m = await compatible.models(cfgCompat, {}, { fetch: g });
+ assert.equal(m.ok, false);
+ assert.doesNotMatch(m.error, /secret/);
});
test('an answer cut off at the output limit fails as "provider", not as a half-parsed proposal', async () => {