Files
loki/scripts/test/endpoint-guard.ts
CatoandClaude 338a46d0ed feat(models): the whole landscape, the Chinese labs directly, and your own machine (#1129)
The store listed ten vendors and the models filed under them. People choose
from the whole landscape — Kimi, GLM, Qwen, Llama, MiniMax, Nemotron — and
they choose on open weights, price, region and what a model can see. And
the one way to run a model for free, on your own laptop, had no path.

- /models is now every chat model in OpenRouter's public catalogue (~460
  from ~60 labs): open weights read from the Hugging Face id, Artificial
  Analysis' intelligence index as relayed, price in/out, context, listed
  date, vision/reasoning/tools, and every way to run it through Loki, lit
  when the person holds the key. Filters (open, free to try, sees images,
  reasoning, on your keys, lab, price ceiling) and sorts (best value,
  smartest, cheapest, newest, longest context) live in the URL. "Use"
  switches a model on a held key in one request; "Add key" opens Settings
  with vendor and model chosen. Phone rows render as cards.
- src/config/model-vendors.ts is the one vendor table: ai-kit's ten with
  the human half, plus Moonshot (Kimi), Z.ai (GLM), Alibaba (Qwen), MiniMax
  and Fireworks — each checked live with a fake key — and "custom". Folds
  model-store.ts and own-model-vendors.ts into it. own-model.ts builds the
  link itself (byok: true, dailyTokens: 0, the vendor's or the row's host);
  lib/models/probe.ts probes extras and endpoints in ai-kit's shape.
- Your own endpoint: an OpenAI-compatible server the person runs (Ollama or
  LM Studio behind a tunnel). lib/models/endpoint-guard.ts: https only, no
  credentials, no private/loopback/link-local/metadata literal, no LAN-only
  name, port 443 or >1024; and a guarded undici dialer that resolves the
  name at connect time and refuses any private answer, redirects refused.
  private-address.ts gains the hex v4-mapped form `new URL` produces. A
  keyless endpoint seals "" and the walker sends no Authorization header.
- Migration 0088: user_model_keys.base_url and .label, only for custom.
- Settings → AI: providers grouped (one key/labs/hosts/your own machine),
  the endpoint form with URL + name + optional key, endpoint rows named.
- Tests: model-vendors (6), endpoint-guard (7), model-store rewritten (12),
  own-model (11) and loki-prompt-budget on Loki's own producer. Doc
  sections: the landscape, vendors beyond ai-kit, your own endpoint, the
  Loki+Cat follow-up. Changelog.


Claude-Session: https://claude.ai/code/session_01YKx4TPejLvFR9Uj4xrTf6Y

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-10 03:50:33 -07:00

120 lines
4.4 KiB
TypeScript

/**
* The gate a person's own endpoint passes before Loki's server sends their
* key to it — the whole reason "your own endpoint" can exist at all.
*
* Pins: what the person types is refused for every shape that would let the
* server reach something only the server can reach (plain http, credentials
* in the URL, a private or loopback address, a LAN-only name, a privileged
* port); a public https URL is accepted and normalised; and the connect-time
* judge refuses a name whose answers include ONE private address, which is
* the rebinding case the save-time check cannot see.
*
* Run: npx tsx scripts/test/endpoint-guard.ts
*/
import assert from "node:assert/strict";
import { assertPublicHost, judgeAnswers, parseEndpoint } from "@/lib/models/endpoint-guard";
let passed = 0;
async function check(name: string, fn: () => void | Promise<void>) {
await fn();
passed++;
console.log(` ✓ ${name}`);
}
const refused = (url: string, why: RegExp) => {
const v = parseEndpoint(url);
assert.equal(v.ok, false, `${url} should be refused`);
if (!v.ok) assert.match(v.reason, why, url);
};
console.log("endpoint-guard:");
async function main() {
await check("plain http, credentials, and junk are refused with the reason", () => {
refused("http://my-box.example.com/v1", /https/);
refused("https://user:pw@my-box.example.com/v1", /username and password/);
refused("my-box.example.com/v1", /full URL/);
refused("", /Paste/);
refused("https://" + "a".repeat(300) + ".com", /too long/);
refused("https://my-box.example.com/v1?key=1", /query/);
});
await check("every private, loopback, link-local and metadata literal is refused", () => {
for (const host of [
"127.0.0.1",
"10.0.0.1",
"172.16.5.5",
"192.168.1.20",
"169.254.169.254",
"100.64.0.1",
"0.0.0.0",
"[::1]",
"[fd00::1]",
"[fe80::1]",
"[::ffff:10.0.0.1]",
]) {
refused(`https://${host}:8443/v1`, /private|network/);
}
});
await check("names that only mean something on a LAN are refused", () => {
for (const host of ["localhost", "ollama.local", "box.internal", "nas.lan", "pc.home"]) {
refused(`https://${host}/v1`, /own network/);
}
});
await check("ports: 443 and anything above 1024 pass; a privileged port does not", () => {
assert.equal(parseEndpoint("https://mac.tail1234.ts.net/v1").ok, true);
assert.equal(parseEndpoint("https://mac.tail1234.ts.net:443/v1").ok, true);
assert.equal(parseEndpoint("https://mac.tail1234.ts.net:8443/v1").ok, true);
assert.equal(parseEndpoint("https://mac.tail1234.ts.net:10000/v1").ok, true);
refused("https://mac.tail1234.ts.net:80/v1", /port/);
refused("https://mac.tail1234.ts.net:22/v1", /port/);
});
await check("a public https URL is accepted and normalised to its base", () => {
const v = parseEndpoint(" https://Mac.tail1234.ts.net/v1/chat/completions/ ");
assert.deepEqual(v, { ok: true, baseUrl: "https://mac.tail1234.ts.net/v1" });
assert.deepEqual(parseEndpoint("https://gw.example.com/openai/v1/models"), {
ok: true,
baseUrl: "https://gw.example.com/openai/v1",
});
assert.deepEqual(parseEndpoint("https://gw.example.com"), {
ok: true,
baseUrl: "https://gw.example.com",
});
assert.equal(parseEndpoint("https://8.8.8.8:8443/v1").ok, true, "a public literal is fine");
});
await check("the connect-time judge refuses ONE private answer among public ones", () => {
assert.equal(judgeAnswers("x", [{ address: "8.8.8.8" }]), null);
assert.match(judgeAnswers("x", [{ address: "8.8.8.8" }, { address: "10.0.0.5" }])!, /private/);
assert.match(judgeAnswers("x", [{ address: "::1" }])!, /private/);
assert.match(judgeAnswers("x", [])!, /does not resolve/);
});
await check("assertPublicHost uses the lookup it is given and never guesses", async () => {
const answers = await assertPublicHost("mac.tail1234.ts.net", async () => [
{ address: "93.184.216.34" },
]);
assert.equal(answers.length, 1);
await assert.rejects(
assertPublicHost("rebind.example.com", async () => [
{ address: "93.184.216.34" },
{ address: "127.0.0.1" },
]),
/private/,
);
await assert.rejects(
assertPublicHost("nope.example", async () => {
throw new Error("ENOTFOUND");
}),
/does not resolve/,
);
});
console.log(`\nendpoint-guard: ${passed} passed`);
}
void main();