Fix Connect approval toggle stuck on load failure, mint replay loss, and concurrent action race

- Revert decodeSecurityMode to fall back to 'account' for invalid persisted
  values instead of failing with ConnectSecurityModeLoadError. This prevents
  the UI toggle from being permanently disabled when the secret store
  contains corrupt data, and also prevents mint replay guard consumption
  from being wasted when getSecurityMode fails after guards are consumed.

- Add synchronous useRef guard in handleConnectClientAction to prevent
  concurrent approve/reject/revoke calls that can slip through the
  asynchronous React state check before re-render disables buttons.
This commit is contained in:
Cursor Agent committed 2026-06-27 14:58:12 +00:00
1 parent 136150d32f
commit 0362fde9e2
2 files changed
+11 -17

No files matched your search

+5 -14
View File
@@ -222,18 +222,9 @@ function fromPresentationMetadata(
};
}
function decodeSecurityMode(
bytes: Uint8Array,
): Effect.Effect<AuthConnectSecurityMode, ConnectSecurityModeLoadError> {
function decodeSecurityMode(bytes: Uint8Array): AuthConnectSecurityMode {
const value = textDecoder.decode(bytes).trim();
if (value === "account" || value === "client-approval") {
return Effect.succeed(value);
}
return Effect.fail(
new ConnectSecurityModeLoadError({
cause: new Error(`Invalid Connect security mode: ${value}`),
}),
);
return value === "account" || value === "client-approval" ? value : "account";
}
function encodeSecurityMode(mode: AuthConnectSecurityMode): Uint8Array {
@@ -265,10 +256,10 @@ export const make = Effect.gen(function* () {
const getSecurityMode: ConnectClientStore["Service"]["getSecurityMode"] = () =>
secrets.get(CONNECT_SECURITY_MODE_SECRET).pipe(
Effect.mapError((cause) => new ConnectSecurityModeLoadError({ cause })),
Effect.flatMap((mode) =>
Option.isSome(mode) ? decodeSecurityMode(mode.value) : Effect.succeed("account" as const),
Effect.map((mode) =>
Option.isSome(mode) ? decodeSecurityMode(mode.value) : ("account" as const),
),
Effect.mapError((cause) => new ConnectSecurityModeLoadError({ cause })),
Effect.withSpan("ConnectClientStore.getSecurityMode"),
);
@@ -8,7 +8,7 @@ import {
TriangleAlertIcon,
} from "lucide-react";
import { useAuth } from "@clerk/react";
import { type ReactNode, memo, useCallback, useEffect, useMemo, useState } from "react";
import { type ReactNode, memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
AuthAccessReadScope,
AuthAccessWriteScope,
@@ -2224,6 +2224,7 @@ export function ConnectionsSettings() {
const [pendingConnectClientActionKey, setPendingConnectClientActionKey] = useState<string | null>(
null,
);
const pendingConnectClientActionRef = useRef(false);
const [addBackendDialogOpen, setAddBackendDialogOpen] = useState(false);
const [savedBackendMode, setSavedBackendMode] = useState<"remote" | "ssh">("remote");
const [savedBackendHost, setSavedBackendHost] = useState("");
@@ -2549,7 +2550,8 @@ export function ConnectionsSettings() {
const handleConnectClientAction = useCallback(
async (action: "approve" | "reject" | "revoke", clientProofKeyThumbprint: string) => {
if (pendingConnectClientActionKey !== null) return;
if (pendingConnectClientActionRef.current) return;
pendingConnectClientActionRef.current = true;
const actionKey = `${action}:${clientProofKeyThumbprint}`;
setPendingConnectClientActionKey(actionKey);
setDesktopAccessManagementMutationError(null);
@@ -2573,10 +2575,11 @@ export function ConnectionsSettings() {
}),
);
} finally {
pendingConnectClientActionRef.current = false;
setPendingConnectClientActionKey(null);
}
},
[pendingConnectClientActionKey],
[],
);
const handleAddSavedBackend = useCallback(async () => {