Repairs origin as well as answering the review. 2d2c787e committed the
`absorbFileSecretsIntoKeyring` tests without the implementation they
import, so the branch has been failing `tsc` since; this commit brings the
source across.
Round 5's four findings (two inline, two suppressed):
- **The stale-lock takeover was still racy.** Round 4 made *stealing*
liveness-aware but left the takeover as remove-then-recreate, which is
not an election: two waiters can both judge the same lock stale, the
first removes it and takes a fresh one, and the second then removes
*that* — both inside the section, which is the lost update the takeover
exists to prevent. Claiming is now an atomic `rename` to a
token-unique name, so exactly one waiter can take a given stale lock and
the losers get ENOENT. A failed claim also falls through to the deadline
instead of `continue`ing, so a takeover that can never succeed times out
rather than spinning forever on a swallowed error.
- **Every `stat` failure was read as "no file yet".** An existing file we
cannot inspect (EACCES on the directory, a mode-less filesystem) then
produced no warning at all, and the footer went on stating owner-only
permissions having verified nothing. There is now a third state,
`unknown`, carried end to end: `permissionsUnknown` on the descriptor, a
caveat that says the permissions could not be checked, a `warn` tone, and
its own branch in the footer sentence — which otherwise would have
claimed "Owner-only permissions." by omission, the same bug one layer up.
- **mountinfo decoded only `\040`.** The kernel escapes tab, newline and
backslash too (`\011`, `\012`, `\134`), so a mounted path containing any
of them was compared in its escaped form, matched nothing, and read as
the container's writable layer — `memory` selected and persistence
silently lost on a box that had arranged for it. Decoded as octal in
general now, since the rule is octal-in-general and a future addition
would otherwise reintroduce exactly this.
- **File-backed secrets vanished once a keychain appeared.** Save secrets
without libsecret, install libsecret, and the next run selects the
keychain and stops seeing them: still on disk, read by nothing, and
nothing looks broken. `absorbFileSecretsIntoKeyring` hands them over on
the run that finds a keychain, under the rules the two existing plaintext
migrations already use — keychain wins on conflict, the file is removed
only on complete success, and a file that cannot be read is never
deleted, because that is precisely the case where the values would be
lost.
Tests: a two-waiter race over one stale lock asserting a single holder
throughout; `unknown` from `readSecretFilePermissions` through the
descriptor to the footer sentence and tone; the hand-off's happy path,
keychain-wins, write-failure, undecryptable-file, empty-file,
malformed-key, and explicitly-configured-keyring cases; and six more
rejected descriptor shapes, including each file-only field on a non-file
kind. Full `npm run ci` green — 5,732 tests under the coverage gate.
Signed-off-by: cliffhall <cliff@futurescale.com>
201 lines
8.4 KiB
TypeScript
201 lines
8.4 KiB
TypeScript
/**
|
|
* React hook over the `GET /api/config` endpoint — the single fetch of the
|
|
* static `InitialConfigPayload` the dev/prod web backends serve alongside the
|
|
* SPA (see `core/mcp/remote/node/server.ts`). It exposes the three fields the
|
|
* web app reads off that payload:
|
|
*
|
|
* - `sandboxUrl` — the MCP Apps sandbox proxy URL. The backend mounts
|
|
* `sandbox_proxy.html` on a separate controller port and advertises the URL
|
|
* here; the Apps screen embeds it as the trusted outer iframe. `undefined`
|
|
* until the fetch resolves, and whenever the backend omits it (legacy backend,
|
|
* or a build without the sandbox controller) — callers treat that as "Apps
|
|
* unavailable" rather than a blank iframe.
|
|
* - `writable` — whether the session's server list is a writable catalog or a
|
|
* read-only session (`--config` / ad-hoc `--server-url`). Defaults to `true`
|
|
* until the fetch resolves and whenever the field is absent (a legacy backend
|
|
* predating the flag), so the default catalog keeps full CRUD.
|
|
* - `version` — the Inspector version the backend reads from the root
|
|
* `package.json` (the browser can't read it off disk). `undefined` until the
|
|
* fetch resolves, and whenever the backend omits it (legacy backend) — the UI
|
|
* renders nothing then.
|
|
* - `secretStorage` — which store this session's secrets go to (#1950),
|
|
* resolved on the Node side where the keychain probe happens. `undefined`
|
|
* until the fetch resolves and on a backend that omits it; the settings-modal
|
|
* footers render nothing then, because a guessed answer under a secret field
|
|
* is worse than no answer.
|
|
*
|
|
* This consolidates the three former single-field hooks (`useSandboxUrl`,
|
|
* `useServerListWritable`, `useInspectorVersion`), each of which fetched the
|
|
* same static payload separately, into one request (#1643).
|
|
*
|
|
* Fetches on mount, and re-fetches if `baseUrl` or `authToken` changes (rare —
|
|
* effectively a full reload; the GET is idempotent). A response that resolves
|
|
* after unmount or a re-fetch is dropped rather than overwriting current state.
|
|
*/
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import type { SecretStorageInfo } from "../auth/secret-storage-info.js";
|
|
|
|
export interface UseInitialConfigOptions {
|
|
/** Base URL of the remote server (typically `window.location.origin`). */
|
|
baseUrl: string;
|
|
/** Optional auth token for the `x-mcp-remote-auth` header. */
|
|
authToken?: string;
|
|
/** Fetch function to use (default: globalThis.fetch). Useful in tests. */
|
|
fetchFn?: typeof fetch;
|
|
}
|
|
|
|
export interface UseInitialConfigResult {
|
|
/** The Inspector version, or undefined when unavailable / not yet loaded. */
|
|
version: string | undefined;
|
|
/** The sandbox proxy URL, or undefined when unavailable / not yet loaded. */
|
|
sandboxUrl: string | undefined;
|
|
/** Whether the server list is writable (catalog) or read-only (session). */
|
|
writable: boolean;
|
|
/** Where this session's secrets are stored, or undefined when unknown. */
|
|
secretStorage: SecretStorageInfo | undefined;
|
|
/** True while the initial fetch is in flight. */
|
|
loading: boolean;
|
|
}
|
|
|
|
/** Minimal shape we read from the `/api/config` payload. */
|
|
interface ConfigPayload {
|
|
version?: unknown;
|
|
sandboxUrl?: unknown;
|
|
writable?: unknown;
|
|
secretStorage?: unknown;
|
|
}
|
|
|
|
/**
|
|
* Narrow the payload's `secretStorage` before trusting it.
|
|
*
|
|
* The footer states, in the UI, where a user's secret is about to go, so a
|
|
* malformed or partial descriptor must render as "unknown" rather than as a
|
|
* confident half-answer.
|
|
*
|
|
* Every field the footer reads is checked, not just `kind`. A partial
|
|
* `{ kind: "file" }` would otherwise pass, and because a missing `plaintext`
|
|
* is falsy it would render as the quiet, neutral *encrypted* file — the
|
|
* single most misleading thing this component can say, produced by the
|
|
* absence of information rather than by any claim the backend made. Each
|
|
* kind's own required fields are checked for the same reason: a file store
|
|
* with no `path` cannot answer the question a file store exists to answer.
|
|
*/
|
|
function usableSecretStorage(value: unknown): SecretStorageInfo | undefined {
|
|
if (typeof value !== "object" || value === null) return undefined;
|
|
const v = value as Record<string, unknown>;
|
|
if (v.kind !== "keyring" && v.kind !== "file" && v.kind !== "memory") {
|
|
return undefined;
|
|
}
|
|
if (
|
|
v.reason !== "configured" &&
|
|
v.reason !== "default" &&
|
|
v.reason !== "fallback"
|
|
) {
|
|
return undefined;
|
|
}
|
|
if (typeof v.durable !== "boolean") return undefined;
|
|
if (v.kind === "file") {
|
|
if (typeof v.path !== "string" || v.path === "") return undefined;
|
|
if (typeof v.plaintext !== "boolean") return undefined;
|
|
// Optional, so absent is fine — but a *present* value must be a real
|
|
// boolean. `pendingEncryption: "false"` is a truthy string, and the
|
|
// footer would read it as "already re-encrypting" and print advice that
|
|
// is the opposite of the truth.
|
|
if (
|
|
v.pendingEncryption !== undefined &&
|
|
typeof v.pendingEncryption !== "boolean"
|
|
) {
|
|
return undefined;
|
|
}
|
|
if (v.looseMode !== undefined && typeof v.looseMode !== "number") {
|
|
return undefined;
|
|
}
|
|
if (
|
|
v.permissionsUnknown !== undefined &&
|
|
typeof v.permissionsUnknown !== "string"
|
|
) {
|
|
return undefined;
|
|
}
|
|
} else if (
|
|
// File-only fields on a keychain or memory descriptor mean the payload
|
|
// was not built by a backend we understand. Rejecting is cheap and the
|
|
// alternative is rendering a mixture of two stores' answers.
|
|
v.path !== undefined ||
|
|
v.plaintext !== undefined ||
|
|
v.pendingEncryption !== undefined ||
|
|
v.looseMode !== undefined ||
|
|
v.permissionsUnknown !== undefined
|
|
) {
|
|
return undefined;
|
|
}
|
|
return value as SecretStorageInfo;
|
|
}
|
|
|
|
/** Coerce a payload field to a usable non-empty string, else undefined. */
|
|
function usableString(value: unknown): string | undefined {
|
|
return typeof value === "string" && value ? value : undefined;
|
|
}
|
|
|
|
export function useInitialConfig(
|
|
opts: UseInitialConfigOptions,
|
|
): UseInitialConfigResult {
|
|
const { baseUrl, authToken, fetchFn } = opts;
|
|
const doFetch = fetchFn ?? globalThis.fetch;
|
|
const base = baseUrl.replace(/\/$/, "");
|
|
|
|
const [version, setVersion] = useState<string | undefined>(undefined);
|
|
const [sandboxUrl, setSandboxUrl] = useState<string | undefined>(undefined);
|
|
// Default writable so the common (catalog) case shows CRUD immediately and a
|
|
// legacy backend that omits the field keeps working.
|
|
const [writable, setWritable] = useState<boolean>(true);
|
|
const [secretStorage, setSecretStorage] = useState<
|
|
SecretStorageInfo | undefined
|
|
>(undefined);
|
|
const [loading, setLoading] = useState<boolean>(true);
|
|
|
|
// `isCancelled` lets the effect drop a response that resolves after unmount or
|
|
// after a re-run (baseUrl/authToken change), so a stale payload can't overwrite
|
|
// current state. React 18 no longer warns on setState-after-unmount — it's a
|
|
// silent no-op — so the guard is about correctness, not the warning.
|
|
const load = useCallback(
|
|
async (isCancelled: () => boolean): Promise<void> => {
|
|
const headers: Record<string, string> = {};
|
|
if (authToken) headers["x-mcp-remote-auth"] = `Bearer ${authToken}`;
|
|
try {
|
|
const res = await doFetch(`${base}/api/config`, {
|
|
method: "GET",
|
|
headers,
|
|
});
|
|
if (isCancelled() || !res.ok) return;
|
|
const body = (await res.json()) as ConfigPayload;
|
|
if (isCancelled()) return;
|
|
// Tolerate missing/non-usable fields — a legacy backend that omits any
|
|
// of them leaves that value at its "unavailable" default rather than
|
|
// showing a bogus value.
|
|
setVersion(usableString(body.version));
|
|
setSandboxUrl(usableString(body.sandboxUrl));
|
|
// Only an explicit `false` makes the list read-only; a missing field
|
|
// (legacy backend) stays writable.
|
|
setWritable(body.writable !== false);
|
|
setSecretStorage(usableSecretStorage(body.secretStorage));
|
|
} catch {
|
|
// Network error / aborted fetch: leave every field at its default
|
|
// (version/sandboxUrl undefined, writable true).
|
|
} finally {
|
|
if (!isCancelled()) setLoading(false);
|
|
}
|
|
},
|
|
[base, authToken, doFetch],
|
|
);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
void load(() => cancelled);
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [load]);
|
|
|
|
return { version, sandboxUrl, writable, secretStorage, loading };
|
|
}
|