Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(meta-muse-code): ignore a cwd .env's host opt-in for the Muse login
Jack Rudenkof65763adbb4c
diff --git a/packages/cli/src/env-snapshot.ts b/packages/cli/src/env-snapshot.tsnew file mode 100644index 00000000..250b22bd--- /dev/null+++ b/packages/cli/src/env-snapshot.ts@@ -0,0 +1,11 @@+/**+ * Side-effect module: snapshot environment variables that must never be+ * satisfiable from a cwd `.env`, BEFORE `index.ts` calls `dotenv.config()`.+ *+ * ES modules evaluate every import before the importing module's body runs,+ * and `index.ts` calls `config()` in its body, so importing this module from+ * `index.ts`'s import list is what makes the snapshot pre-dotenv.+ */+import { captureInheritedMuseEnv } from "./providers/meta-muse/muse-env-trust.js";++captureInheritedMuseEnv();diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex 45279cc1..18aec91a 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -8,6 +8,9 @@ import {assertPaneChildInteractive,paneChildBootNotes,} from "./pane/child-env.js";+// Before `.env` loading: the variables that decide where the Muse Code login may+// be sent are read from the INHERITED environment only (env-snapshot.ts).+import "./env-snapshot.js";// Load .env file before anything else (quiet mode to suppress verbose output)import { config } from "dotenv";diff --git a/packages/cli/src/providers/meta-muse/muse-env-trust.ts b/packages/cli/src/providers/meta-muse/muse-env-trust.tsnew file mode 100644index 00000000..bc6536cd--- /dev/null+++ b/packages/cli/src/providers/meta-muse/muse-env-trust.ts@@ -0,0 +1,44 @@+/**+ * The two Muse Code host variables as the process INHERITED them, before+ * `index.ts` loads a `.env` from the current directory.+ *+ * `MUSE_CODE_BASE_URL` decides where the Muse Code login is sent, and+ * `CLAUDISH_MUSE_CODE_ALLOW_CUSTOM_HOST` permits a host other than+ * api.meta.ai. If either could come from a cwd `.env`, a cloned repository+ * could ship both and redirect the login. So `museTrustedBaseUrl` honours the+ * opt-in only from this snapshot, and refuses a non-Meta base URL that the+ * snapshot does not carry (it came from `.env`).+ *+ * Captured by an explicit call that `index.ts` makes from its import list,+ * which ES modules evaluate before the module body calls `dotenv.config()`.+ * When nothing captured it, the snapshot is unknown and the opt-in is+ * unavailable: the safe answer.+ */++export interface InheritedMuseEnv {+ baseUrl: string | undefined;+ allowCustomHost: string | undefined;+}++let snapshot: InheritedMuseEnv | null = null;++/** Record the inherited values. Only the first call counts. */+export function captureInheritedMuseEnv(+ env: Record<string, string | undefined> = process.env+): void {+ if (snapshot) return;+ snapshot = {+ baseUrl: env.MUSE_CODE_BASE_URL,+ allowCustomHost: env.CLAUDISH_MUSE_CODE_ALLOW_CUSTOM_HOST,+ };+}++/** The inherited values, or null when nothing captured them. */+export function inheritedMuseEnv(): InheritedMuseEnv | null {+ return snapshot;+}++/** Test seam: forget the snapshot. */+export function _resetInheritedMuseEnvForTest(): void {+ snapshot = null;+}diff --git a/packages/cli/src/providers/meta-muse/muse-mint.ts b/packages/cli/src/providers/meta-muse/muse-mint.tsindex f98fd2d9..a0f4678c 100644--- a/packages/cli/src/providers/meta-muse/muse-mint.ts+++ b/packages/cli/src/providers/meta-muse/muse-mint.ts@@ -42,6 +42,7 @@import { markOwnTimeout } from "../../handlers/shared/connection-error.js";import { VERSION } from "../../version.js";+import { inheritedMuseEnv } from "./muse-env-trust.js";/** The default base host. `MUSE_CODE_BASE_URL` overrides it (the provider's `baseUrlEnvVars`). */export const MUSE_CODE_DEFAULT_BASE_URL = "https://api.meta.ai";@@ -119,18 +120,24 @@ export const MUSE_CODE_TRUSTED_HOST = "api.meta.ai";export const MUSE_CODE_ALLOW_CUSTOM_HOST_ENV = "CLAUDISH_MUSE_CODE_ALLOW_CUSTOM_HOST";/**- * The base URL the `dca:` login (the mint) and the minted key (the model list)- * may be sent to.+ * The base URL the `dca:` login (the mint) and the minted key (the model list+ * and `/v1/responses`) may be sent to.** WHY THIS REFUSES. The `dca:` token is the account's durable login, possibly* the one copied from the Muse Code CLI's Keychain item, and it mints keys until- * Meta revokes it. `MUSE_CODE_BASE_URL` can come from a `.env` in whatever- * directory claudish runs in (index.ts loads one), so a cloned repository could- * redirect the login to its own host. So an override is honoured only when it- * is `https://api.meta.ai…`; anything else needs- * `CLAUDISH_MUSE_CODE_ALLOW_CUSTOM_HOST=1`, and even then plain `http://` is- * accepted only for loopback (a local test server). Every refusal is terminal- * and names the variable.+ * Meta revokes it. `index.ts` loads a `.env` from whatever directory claudish+ * runs in, so a cloned repository could set `MUSE_CODE_BASE_URL` (and the+ * opt-in) and redirect the login to its own host. The rules:+ *+ * - `https://api.meta.ai`, with no userinfo, query or fragment, is honoured+ * from anywhere; the result is the URL's origin plus its path, nothing else.+ * - Any other host needs `CLAUDISH_MUSE_CODE_ALLOW_CUSTOM_HOST=1` in the+ * environment the process INHERITED (`muse-env-trust.ts`, snapshotted before+ * dotenv runs), AND the base URL itself must be the inherited value: a `.env`+ * can supply neither. Plain `http://` is then accepted only for loopback.+ *+ * Every refusal is terminal and names the variable. When `env` is not+ * `process.env` (the injected test seam), it is treated as inherited.*/export function museTrustedBaseUrl(env: Record<string, string | undefined> = process.env): string {const raw = env.MUSE_CODE_BASE_URL?.trim();@@ -141,11 +148,26 @@ export function museTrustedBaseUrl(env: Record<string, string | undefined> = pro} catch {throw new MuseMintProtocolError(`MUSE_CODE_BASE_URL is not a URL: ${raw}`);}- const base = raw.replace(/\/+$/, "");+ if (url.username || url.password || url.search || url.hash) {+ throw new MuseMintProtocolError(+ "MUSE_CODE_BASE_URL may not carry credentials, a query or a fragment."+ );+ }+ const base = `${url.origin}${url.pathname.replace(/\/+$/, "")}`;if (url.protocol === "https:" && url.hostname === MUSE_CODE_TRUSTED_HOST) return base;- if (env[MUSE_CODE_ALLOW_CUSTOM_HOST_ENV] !== "1") {++ const inherited =+ env === process.env+ ? inheritedMuseEnv()+ : { baseUrl: env.MUSE_CODE_BASE_URL, allowCustomHost: env[MUSE_CODE_ALLOW_CUSTOM_HOST_ENV] };+ const fromDotenv = inherited?.baseUrl?.trim() !== raw;+ if (inherited?.allowCustomHost !== "1" || fromDotenv) {throw new MuseMintProtocolError(- `MUSE_CODE_BASE_URL points at ${url.protocol}//${url.host}, not https://${MUSE_CODE_TRUSTED_HOST}: claudish will not send your Muse Code login there. Unset it, or set ${MUSE_CODE_ALLOW_CUSTOM_HOST_ENV}=1 if you run that host yourself.`+ `MUSE_CODE_BASE_URL points at ${url.protocol}//${url.host}, not https://${MUSE_CODE_TRUSTED_HOST}: claudish will not send your Muse Code login there. ${+ fromDotenv+ ? "It was not set by your shell (a .env in the current directory may not redirect the login)."+ : `Unset it, or export ${MUSE_CODE_ALLOW_CUSTOM_HOST_ENV}=1 in your shell (a .env does not count) if you run that host yourself.`+ }`);}const loopback = ["localhost", "127.0.0.1", "[::1]", "::1"].includes(url.hostname);diff --git a/packages/cli/src/providers/transport/meta-muse-code.ts b/packages/cli/src/providers/transport/meta-muse-code.tsindex 31d9fe6a..691c4669 100644--- a/packages/cli/src/providers/transport/meta-muse-code.ts+++ b/packages/cli/src/providers/transport/meta-muse-code.ts@@ -23,6 +23,7 @@import { credentials } from "../../auth/credentials/authority.js";import type { RequestAuth } from "../../auth/credentials/types.js";import { forceRefreshMuseApiKey, requestPathMayPrompt } from "../meta-muse/muse-credentials.js";+import { museTrustedBaseUrl } from "../meta-muse/muse-mint.js";import { discoverContextWindow } from "../model-discovery.js";import { OpenAIProviderTransport } from "./openai.js";@@ -41,7 +42,20 @@ export class MuseCodeTransport extends OpenAIProviderTransport {private window = 0;private windowRetryAt = 0;+ /**+ * The base URL `/v1/responses` goes to, under the same host rule as the mint+ * (`museTrustedBaseUrl`): the request carries the minted key and the prompt.+ * Resolved in refreshAuth, so a refused host is a terminal answer at the auth+ * site rather than a throw out of getEndpoint.+ */+ private trustedBase?: string;++ override getEndpoint(): string {+ return this.trustedBase ? `${this.trustedBase}/v1/responses` : super.getEndpoint();+ }+async refreshAuth(): Promise<void> {+ this.trustedBase = museTrustedBaseUrl();this.auth = await credentials.getRequestAuth("meta-muse-code", { model: this.modelName });if (!this.window && Date.now() >= this.windowRetryAt) {this.window = (await discoverContextWindow("meta-muse-code", this.modelName)) ?? 0;