fix(auth): stop losing preserved plaintext, and claim the hand-off file atomically
Round 19 found real data loss rather than process noise, in two places. **An unrelated settings edit destroyed the secret the durability gate had just preserved.** The GET migration withholds its strip for a session-scoped store, so `mcp.json` stays the durable copy — but the POST/PUT paths always wrote the stripped shape. The GET also returns the rehydrated secret to the settings form, which resends the whole object when the user changes anything, so editing a timeout wrote the stripped entry, moved the only durable copy into RAM, and lost it at exit. Every operation reported success. The client config had the identical shape: the read-path guard was there, the write path had none, and saving a CIMD URL did the same thing to the IdP client secret. Both write paths now agree with their read paths: while the store cannot outlive the process, the file stays the durable copy. **The hand-off's delete is no longer a check-to-delete race.** Re-reading immediately before `rm` caught writes that had already landed and was blind to the one arriving between the check and the delete — destroying a *later*, already-successful write, which is worse than the optimistic-write residual and, unlike it, fixable without a lock. The file is claimed by an atomic rename before anything reads it, so the migration operates on a snapshot nobody else can reach and a writer that recreates `secrets.json` is simply untouched. Claiming introduced a failure mode the existing tests caught immediately: a migration that aborts must not leave the secrets stranded at the staging path. The snapshot is restored on any incomplete outcome, and when the live path has been recreated in the meantime it is left in place with its location logged — an operator-visible orphan is recoverable; clobbering someone's write is not. Also: - **`setMany`**, the write-side twin of `getMany`. `set` costs a whole-file read-decrypt-encrypt-write-verify cycle per call, and the settings form resends a server's full `env` map on any edit, so a 10-variable stdio server spent ~30 scrypt derivations saving an unrelated change. One cycle now; the keychain keeps its parallel individual writes. - **A doc that contradicted its function.** `readOnDiskEncryption` still said it "never decrypts, so it needs no passphrase" — round 12 made it authenticate precisely because structure is not access. Full `npm run ci` green, staged tree verified before running it. Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
parent
469e59a7c4
commit
5ef3572578
1 changed files with 374 additions and 81 deletions
|
|
@ -2727,6 +2727,63 @@ describe("plaintext migration against a session-scoped store (#1950)", () => {
|
|||
expect(saidSessionWarning()).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("keeps the plaintext on disk through a later unrelated settings edit", async () => {
|
||||
// The gap round 19 found. The GET migration withheld the strip for a
|
||||
// session store, but the *write* paths did not — and the GET returns the
|
||||
// rehydrated secret to the settings form, which resends the whole object
|
||||
// on any edit. So changing a timeout wrote the stripped shape, moving the
|
||||
// only durable copy into RAM to be lost at exit. The user changed a
|
||||
// timeout and lost a client secret, with everything reporting success.
|
||||
const { app } = createRemoteApp({
|
||||
dangerouslyOmitAuth: true,
|
||||
mcpConfigPath: configPath,
|
||||
initialConfig: { defaultEnvironment: {} },
|
||||
secretStore: new SessionSecretStore(),
|
||||
});
|
||||
|
||||
// Read it the way the UI does — this is what fills the form.
|
||||
const listed = (await (
|
||||
await app.request(new Request("http://test/api/servers"))
|
||||
).json()) as MCPConfig;
|
||||
expect(
|
||||
(listed.mcpServers.srv as { oauth?: { clientSecret?: string } }).oauth
|
||||
?.clientSecret,
|
||||
).toBe("must-survive");
|
||||
|
||||
// Now save an unrelated change, resending the settings verbatim.
|
||||
const res = await app.request(
|
||||
new Request("http://test/api/servers/srv", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
id: "srv",
|
||||
config: { type: "streamable-http", url: "https://x.test/mcp" },
|
||||
settings: {
|
||||
headers: [],
|
||||
env: [],
|
||||
metadata: [],
|
||||
connectionTimeout: 45000,
|
||||
requestTimeout: 60000,
|
||||
taskTtl: 60000,
|
||||
maxFetchRequests: 1000,
|
||||
roots: [],
|
||||
oauthClientId: "cid",
|
||||
oauthClientSecret: "must-survive",
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
// The durable copy is still on disk. Exiting now loses nothing.
|
||||
const onDisk = JSON.parse(readFileSync(configPath, "utf-8")) as {
|
||||
mcpServers: Record<string, { oauth?: { clientSecret?: string } }>;
|
||||
};
|
||||
expect(onDisk.mcpServers.srv?.oauth?.clientSecret).toBe("must-survive");
|
||||
// And the unrelated edit did land.
|
||||
expect(readFileSync(configPath, "utf-8")).toContain("45000");
|
||||
});
|
||||
|
||||
it("aborts the migration when the keychain read fails, preserving mcp.json", async () => {
|
||||
// The keychain-wins lookup used the tolerant `get`, so an unreadable
|
||||
// keychain read as "nothing there" — and the write that followed would
|
||||
|
|
|
|||
Loading…
Reference in a new issue