Closes #1950 Before this, a box with no reachable OS keychain — the published container (no D-Bus session, #1848), Android/Termux (no platform binary, #1905), a minimal Linux install without libsecret — could not persist an OAuth client secret or a stdio `env:` value at all. `KeyringSecretStore` degraded into a store that silently holds nothing: `get` returned null, `delete` no-opped, and `set` hard-failed with a 503. That is the correct failure behavior, but it left the documented, supported way to run the Inspector with no secret persistence whatsoever. Adds the two missing implementations and, more importantly, the policy that picks one and the surfaces that say which was picked. - `FileSecretStore` — one `0600` JSON document at `~/.mcp-inspector/secrets.json`, encrypted with AES-256-GCM whenever `MCP_INSPECTOR_SECRET_KEY` is set (scrypt, per-file random salt). The whole map is encrypted as a unit rather than value-by-value so the account names — `${serverId}:${field}` — are hidden too; a per-value scheme would leave a readable index of which servers you hold a client secret for. A file that can no longer be decrypted reads as empty (matching the interface's read tolerance) but *refuses to be written*, because replacing a file of still-valid secrets to satisfy an additive request destroys data. - `secret-store-selection.ts` — the policy. `MCP_INSPECTOR_SECRET_STORE=keyring|file|memory` wins outright; otherwise probe the keychain and, if unreachable, fall back loudly. The fallback is `memory` in a container whose secrets directory is not on a mount, and `file` otherwise — so mounting the volume the README already recommends for the catalog flips the same run to durable storage with no configuration, and an unmounted container gets the honest answer rather than a file `--rm` will discard. Cached per process so the banner, `/api/config`, and the store doing the writing cannot disagree. - Automatic, but never silent. The downgrade prints at startup, rides `GET /api/config`, and is named in a permanent 32px footer at the bottom of both dialogs that accept a secret. A startup banner is seen once by whoever started the process; a toast is seen once; a dismissible banner is the thing a user dismisses before doing the work it describes. The plaintext and memory cases render their caveat inline in a warning tone — a warning you must hover to discover is not one. - `SecretStoreUnavailableError` is now the base every store's `set` throws, so the routes translate one condition into a 503 instead of matching `KeychainUnavailableError` by name. Docs: the container section of the root README gains a table of which store you get and how to make secrets survive, plus the `MCP_INSPECTOR_SECRET_KEY` upgrade/mismatch rules. Tests: the existing interface contract block is extracted to `secretStoreContract.ts` and run against all three implementations, so none can drift on the availability contract. `FileSecretStore` is tested against a real temp filesystem — mode, cross-instance round-trip, that the ciphertext does not contain the plaintext, the read-modify-write race, and every refusal path. Full `npm run ci` passes, per-file gate included. Signed-off-by: cliffhall <cliff@futurescale.com>
118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
/**
|
|
* Node-only client.json persistence with OS keychain for IdP clientSecret.
|
|
*/
|
|
|
|
import {
|
|
SecretStoreUnavailableError,
|
|
type SecretStore,
|
|
} from "../auth/node/secret-store.js";
|
|
import { SECRET_FIELD_IDP_CLIENT_SECRET } from "../auth/secret-fields.js";
|
|
import {
|
|
deleteStoreFile,
|
|
parseStore,
|
|
readStoreFile,
|
|
serializeStore,
|
|
writeStoreFile,
|
|
} from "../storage/store-io.js";
|
|
import { parseClientConfig } from "./config-parse.js";
|
|
import type { ClientConfig } from "./types.js";
|
|
import {
|
|
CLIENT_KEYCHAIN_ID,
|
|
extractSecretsFromClientConfig,
|
|
hasClientPlaintextSecret,
|
|
mergeSecretsIntoClientConfig,
|
|
} from "./secrets.js";
|
|
|
|
async function readIdpSecretFromKeychain(
|
|
secretStore: SecretStore,
|
|
): Promise<Record<string, string>> {
|
|
const secret = await secretStore.get(
|
|
CLIENT_KEYCHAIN_ID,
|
|
SECRET_FIELD_IDP_CLIENT_SECRET,
|
|
);
|
|
if (!secret) return {};
|
|
return { [SECRET_FIELD_IDP_CLIENT_SECRET]: secret };
|
|
}
|
|
|
|
async function migrateClientPlaintextSecret(
|
|
filePath: string,
|
|
config: ClientConfig,
|
|
secretStore: SecretStore,
|
|
): Promise<ClientConfig> {
|
|
const { stripped, secrets } = extractSecretsFromClientConfig(config);
|
|
const value = secrets[SECRET_FIELD_IDP_CLIENT_SECRET];
|
|
if (!value) return config;
|
|
|
|
try {
|
|
const existing = await secretStore.get(
|
|
CLIENT_KEYCHAIN_ID,
|
|
SECRET_FIELD_IDP_CLIENT_SECRET,
|
|
);
|
|
if (existing === null) {
|
|
await secretStore.set(
|
|
CLIENT_KEYCHAIN_ID,
|
|
SECRET_FIELD_IDP_CLIENT_SECRET,
|
|
value,
|
|
);
|
|
}
|
|
await writeStoreFile(filePath, serializeStore(stripped));
|
|
return stripped;
|
|
} catch (err) {
|
|
if (err instanceof SecretStoreUnavailableError) {
|
|
return config;
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/** Read client.json from disk and rehydrate IdP clientSecret from the keychain. */
|
|
export async function readClientConfigStore(
|
|
filePath: string,
|
|
secretStore: SecretStore,
|
|
): Promise<ClientConfig> {
|
|
const raw = await readStoreFile(filePath);
|
|
if (raw === null) {
|
|
return {};
|
|
}
|
|
|
|
let config = parseClientConfig(parseStore(raw));
|
|
if (hasClientPlaintextSecret(config)) {
|
|
config = await migrateClientPlaintextSecret(filePath, config, secretStore);
|
|
}
|
|
|
|
const secrets = await readIdpSecretFromKeychain(secretStore);
|
|
return mergeSecretsIntoClientConfig(config, secrets);
|
|
}
|
|
|
|
/** Validate, strip IdP clientSecret to keychain, and write client.json. */
|
|
export async function writeClientConfigStore(
|
|
filePath: string,
|
|
body: unknown,
|
|
secretStore: SecretStore,
|
|
): Promise<void> {
|
|
const validated = parseClientConfig(body);
|
|
const { stripped, secrets } = extractSecretsFromClientConfig(validated);
|
|
const idpSecret = secrets[SECRET_FIELD_IDP_CLIENT_SECRET];
|
|
if (idpSecret) {
|
|
await secretStore.set(
|
|
CLIENT_KEYCHAIN_ID,
|
|
SECRET_FIELD_IDP_CLIENT_SECRET,
|
|
idpSecret,
|
|
);
|
|
} else {
|
|
await secretStore.delete(
|
|
CLIENT_KEYCHAIN_ID,
|
|
SECRET_FIELD_IDP_CLIENT_SECRET,
|
|
);
|
|
}
|
|
await writeStoreFile(filePath, serializeStore(stripped));
|
|
}
|
|
|
|
/** Remove client.json and the install-level IdP secret from the keychain. */
|
|
export async function deleteClientConfigStore(
|
|
filePath: string,
|
|
secretStore: SecretStore,
|
|
): Promise<void> {
|
|
await deleteStoreFile(filePath);
|
|
await secretStore.delete(CLIENT_KEYCHAIN_ID, SECRET_FIELD_IDP_CLIENT_SECRET);
|
|
}
|