Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(app-settings): a bot cannot move a provider address once its key is saved

DeryFerd2c0fccb6b0e1
diff --git a/server/app-settings/host.test.ts b/server/app-settings/host.test.ts
index cf429a5437..bb1e0641a5 100644
--- a/server/app-settings/host.test.ts
+++ b/server/app-settings/host.test.ts
@@ -500,6 +500,34 @@ describe("Simple mode: a bot changes settings in the background and never switch
});
});
+describe("a provider address whose key is saved", () => {
+ const withSavedKey = () => {
+ const fx = fixture();
+ fx.cfg.anthropic = { ...(fx.cfg.anthropic as object), key: "sk-ant-saved" } as AppConfig["anthropic"];
+ return fx;
+ };
+
+ it("is the person's to change, because it decides where their saved key is sent", async () => {
+ const fx = withSavedKey();
+ const before = JSON.stringify(fx.saved);
+
+ const refused = await fx.set("providers.anthropic.url", "https://elsewhere.example/v1");
+
+ expect(refused).toMatchObject({ status: 409 });
+ expect(String(refused.body.error)).toContain("only by the person");
+ expect(JSON.stringify(fx.saved)).toBe(before);
+ });
+
+ it("can still be cleared while the key is saved, since clearing sends the key nowhere new", async () => {
+ const fx = withSavedKey();
+
+ const cleared = await fx.set("providers.anthropic.url", "");
+
+ expect(cleared.status).toBe(200);
+ expect(fx.saved).toEqual([{ anthropic: { key: "sk-ant-saved", url: "" } }]);
+ });
+});
+
describe("every other setting applies at once with Undo, Full access or not", () => {
const written = (fx: ReturnType<typeof fixture>) => JSON.stringify([fx.botPatches, fx.saved, fx.routeCalls, [...fx.windowValues]]);
diff --git a/server/app-settings/registry.ts b/server/app-settings/registry.ts
index 19fea1faeb..548b6e67a6 100644
--- a/server/app-settings/registry.ts
+++ b/server/app-settings/registry.ts
@@ -244,16 +244,21 @@ export function createSettingsRegistry(deps: RegistryDeps): SettingEntry[] {
}),
];
- const provider = (id: string, label: string, synonyms: string[], current: () => string | undefined, patch: (text: string) => Record<string, unknown>): SettingEntry => writable({
+ const provider = (id: string, label: string, synonyms: string[], current: () => string | undefined, patch: (text: string) => Record<string, unknown>, keySaved?: () => boolean): SettingEntry => writable({
id, label, help: `${label}: the address MausBot sends requests to. Empty uses the provider's own.`, where: "keys", synonyms: [...synonyms, "url", "base url", "endpoint", "address", "proxy"],
- value: url, allowed: "an http(s) address, or empty to clear", current: () => current() ?? "", write: config(patch),
+ value: url, allowed: "an http(s) address, or empty to clear", current: () => current() ?? "",
+ write: async (text: string) => {
+ // Security: this address decides where the saved key is sent, so once one is saved only the person moves it.
+ if (text !== "" && text !== (current() ?? "") && keySaved?.() === true) return personOnly(`${label} (it decides where the saved key is sent)`, "keys");
+ return config(patch)(text);
+ },
});
const providers: SettingEntry[] = [
- provider("providers.anthropic.url", "Anthropic address", ["anthropic", "claude"], () => cfg().anthropic?.url, (text) => ({ anthropic: { ...cfg().anthropic, url: text } })),
- provider("providers.xai.url", "xAI address", ["xai", "grok"], () => cfg().xai?.url, (text) => ({ xai: { ...cfg().xai, url: text } })),
- provider("providers.groq.url", "Groq address", ["groq"], () => cfg().groq?.url, (text) => ({ groq: { ...cfg().groq, url: text } })),
- provider("providers.meta.url", "Meta address", ["meta", "llama"], () => cfg().meta?.url, (text) => ({ meta: { ...cfg().meta, url: text } })),
- provider("providers.openaiCompat.url", "OpenAI-compatible address", ["openai compatible", "custom provider", "ollama", "lm studio", "openrouter"], () => cfg().openaiCompat?.url, (text) => ({ openaiCompat: { ...cfg().openaiCompat, url: text } })),
+ provider("providers.anthropic.url", "Anthropic address", ["anthropic", "claude"], () => cfg().anthropic?.url, (text) => ({ anthropic: { ...cfg().anthropic, url: text } }), () => Boolean(cfg().anthropic?.key)),
+ provider("providers.xai.url", "xAI address", ["xai", "grok"], () => cfg().xai?.url, (text) => ({ xai: { ...cfg().xai, url: text } }), () => Boolean(cfg().xai?.key)),
+ provider("providers.groq.url", "Groq address", ["groq"], () => cfg().groq?.url, (text) => ({ groq: { ...cfg().groq, url: text } }), () => Boolean(cfg().groq?.key)),
+ provider("providers.meta.url", "Meta address", ["meta", "llama"], () => cfg().meta?.url, (text) => ({ meta: { ...cfg().meta, url: text } }), () => Boolean(cfg().meta?.key)),
+ provider("providers.openaiCompat.url", "OpenAI-compatible address", ["openai compatible", "custom provider", "ollama", "lm studio", "openrouter"], () => cfg().openaiCompat?.url, (text) => ({ openaiCompat: { ...cfg().openaiCompat, url: text } }), () => Boolean(cfg().openaiCompat?.key)),
writable({
id: "providers.openaiCompat.model", label: "OpenAI-compatible model", help: "The model name sent to the OpenAI-compatible provider.", where: "keys",
synonyms: ["openai compatible", "custom provider", "model name"], value: z.string().trim().max(200), allowed: "a model name, or empty to clear",