mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 19:07:17 +00:00
fix(terminal): sync vaultInitialized with VaultHost props to avoid stale key boot (#4428)
This commit is contained in:
1 parent
6236f5bb95
commit
17fa55bedc
8 files changed
+250
-1
No files matched your search
@@ -281,7 +281,7 @@ function AppViewInner({ domains }: AppViewProps) {
|
||||
handleEndSessionDrag, handleFollowAppTerminalThemeChange, handleHostConnectWithProtocolCheck, handleHotkeyAction, handleKeyboardInteractiveCancel, handleKeyboardInteractiveSubmit,
|
||||
handleOpenHostFromVaultNote, handleOpenQuickSwitcher, handleOpenSettings, handleOpenVaultHostFromChat, handleOpenVaultNoteFromChat, handleOpenVaultSectionFromChat, handleOpenVaultSnippetFromChat, handleRootContextMenu, handlePassphraseCancel, handlePassphraseSkip, handlePassphraseSubmit, handleFidoPromptSubmit, handleFidoPromptCancel, handleProtocolSelect,
|
||||
handleRequestCloseEditorTabRef, handleSessionStatusChange, handleSyncNowManual, handleTerminalDataCapture, handleUpdateHostFromTerminal,
|
||||
hostById, hosts, terminalHosts, updateTerminalHosts, hotkeyScheme, identities, importOrReuseKey, isBroadcastEnabled, isComposeSendToAll, onToggleComposeSendTarget, isGlobalBroadcastEnabled, isGlobalComposeSendToAll, onToggleGlobalBroadcast, onToggleGlobalComposeSendTarget, canUseGlobalBroadcast, isCreateWorkspaceOpen, isMacClient, isQuickSwitcherOpen,
|
||||
hostById, hosts, terminalHosts, updateTerminalHosts, hotkeyScheme, identities, importOrReuseKey, isVaultInitialized, isBroadcastEnabled, isComposeSendToAll, onToggleComposeSendTarget, isGlobalBroadcastEnabled, isGlobalComposeSendToAll, onToggleGlobalBroadcast, onToggleGlobalComposeSendTarget, canUseGlobalBroadcast, isCreateWorkspaceOpen, isMacClient, isQuickSwitcherOpen,
|
||||
keyBindings, keyboardInteractiveQueue, keys, logViews, managedSources, navigateToSection, openLogView, openNoteRequest, orderedTabsWithEditors, orphanSessions,
|
||||
passphraseQueue, fidoPromptQueue, protocolSelectHost, proxyProfiles, portForwardingRules, removeSessionFromWorkspace, reorderWorkTabs, reorderWorkspaceSessions,
|
||||
resolveEmptyVaultConflict, resolveSessionAppearance, runSnippet, sessionLogsDir, sessionLogsEnabled, sessionLogsFormat, sessionLogsTimestampsEnabled, sessionRenameTarget, sshDebugLogsEnabled,
|
||||
@@ -734,6 +734,7 @@ function AppViewInner({ domains }: AppViewProps) {
|
||||
/>
|
||||
|
||||
<TerminalLayerMount
|
||||
isVaultInitialized={isVaultInitialized}
|
||||
hosts={terminalHosts}
|
||||
portForwardingRules={portForwardingRules}
|
||||
customGroups={customGroups}
|
||||
|
||||
@@ -55,6 +55,7 @@ export function VaultHost() {
|
||||
hosts: vault.hosts,
|
||||
identities: vault.identities,
|
||||
importOrReuseKey: actions?.importOrReuseKey,
|
||||
isVaultInitialized: vault.isVaultInitialized,
|
||||
keys: vault.keys,
|
||||
managedSources: vault.managedSources,
|
||||
navigateToSection: local.navigateToSection,
|
||||
@@ -90,6 +91,7 @@ export function VaultHost() {
|
||||
vault.groupConfigs,
|
||||
vault.hosts,
|
||||
vault.identities,
|
||||
vault.isVaultInitialized,
|
||||
vault.keys,
|
||||
vault.managedSources,
|
||||
vault.proxyProfiles,
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import React, { useEffect, useLayoutEffect } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { JSDOM } from 'jsdom';
|
||||
|
||||
import {
|
||||
EMPTY_VAULT_SNAPSHOT,
|
||||
publishVaultSnapshot,
|
||||
useVaultSnapshot,
|
||||
useVaultSnapshotField,
|
||||
} from '../state/vaultSnapshotStore.ts';
|
||||
import {
|
||||
publishAppShellDomainSlice,
|
||||
useAppShellProps,
|
||||
} from './appShellPropsStore.ts';
|
||||
import { registerAppHandlers } from './appHandlersBridge.ts';
|
||||
import { resolveTerminalVaultInitialized } from '../../components/terminal/restoredSessionGate.ts';
|
||||
import { terminalLayerAreEqual } from '../../components/terminalLayerMemo.ts';
|
||||
import {
|
||||
terminalLayerViewCtxEqual,
|
||||
terminalLayerWorkspaceCtxEqual,
|
||||
} from '../../components/terminalLayer/terminalLayerViewMemo.ts';
|
||||
import type { Host, Identity, SSHKey } from '../../domain/models.ts';
|
||||
|
||||
const storageBacking = new Map<string, string>();
|
||||
Object.defineProperty(globalThis, 'localStorage', {
|
||||
configurable: true,
|
||||
value: {
|
||||
getItem: (key: string) => storageBacking.get(key) ?? null,
|
||||
setItem: (key: string, value: string) => {
|
||||
storageBacking.set(key, value);
|
||||
},
|
||||
removeItem: (key: string) => {
|
||||
storageBacking.delete(key);
|
||||
},
|
||||
clear: () => {
|
||||
storageBacking.clear();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { VaultHost } = await import('./hosts/VaultHost.tsx');
|
||||
|
||||
test('VaultHost keeps isVaultInitialized synchronized with hydrated hosts, keys, and identities for Terminal boot (#4428)', async () => {
|
||||
const dom = new JSDOM('<!doctype html><html><body><div id="root"></div></body></html>');
|
||||
const prevWindow = globalThis.window;
|
||||
const prevDocument = globalThis.document;
|
||||
const prevActEnv = (globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT;
|
||||
|
||||
(globalThis as Record<string, unknown>).window = dom.window;
|
||||
(globalThis as Record<string, unknown>).document = dom.window.document;
|
||||
(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
|
||||
const host: Host = {
|
||||
id: 'host-1',
|
||||
label: 'test-myhost',
|
||||
hostname: '10.243.2.249',
|
||||
port: 22,
|
||||
username: 'user1',
|
||||
authMethod: 'key',
|
||||
identityFileId: 'key-1',
|
||||
tags: [],
|
||||
createdAt: 0,
|
||||
};
|
||||
const key: SSHKey = {
|
||||
id: 'key-1',
|
||||
label: 'Imported Key',
|
||||
type: 'ED25519',
|
||||
privateKey: '-----BEGIN OPENSSH PRIVATE KEY-----\ntest\n-----END OPENSSH PRIVATE KEY-----',
|
||||
publicKey: 'ssh-ed25519 AAAA test',
|
||||
passphrase: 'secret',
|
||||
source: 'imported',
|
||||
category: 'key',
|
||||
created: 0,
|
||||
};
|
||||
const identity: Identity = {
|
||||
id: 'id-1',
|
||||
label: 'User Identity',
|
||||
username: 'user1',
|
||||
authMethod: 'key',
|
||||
keyId: 'key-1',
|
||||
createdAt: 0,
|
||||
};
|
||||
|
||||
publishVaultSnapshot(EMPTY_VAULT_SNAPSHOT);
|
||||
registerAppHandlers({} as never);
|
||||
|
||||
// Mirror TerminalHost's layout-effect slice publication order (sibling after VaultHost).
|
||||
function TerminalHostMirror() {
|
||||
const vault = useVaultSnapshot();
|
||||
useLayoutEffect(() => {
|
||||
publishAppShellDomainSlice('terminal', { terminalHosts: vault.hosts });
|
||||
}, [vault.hosts]);
|
||||
return null;
|
||||
}
|
||||
|
||||
const syncedBoots: Array<{
|
||||
hostCount: number;
|
||||
keyCount: number;
|
||||
identityCount: number;
|
||||
hasPrivateKey: boolean;
|
||||
}> = [];
|
||||
|
||||
function SyncedTerminal({
|
||||
vaultInitializedOverride,
|
||||
hosts,
|
||||
keys,
|
||||
identities,
|
||||
}: {
|
||||
vaultInitializedOverride: boolean;
|
||||
hosts: readonly Host[];
|
||||
keys: readonly SSHKey[];
|
||||
identities: readonly Identity[];
|
||||
}) {
|
||||
const sharedVaultInitialized = useVaultSnapshotField('isVaultInitialized');
|
||||
const vaultInitialized = resolveTerminalVaultInitialized(
|
||||
sharedVaultInitialized,
|
||||
vaultInitializedOverride,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!vaultInitialized) return;
|
||||
syncedBoots.push({
|
||||
hostCount: hosts.length,
|
||||
keyCount: keys.length,
|
||||
identityCount: identities.length,
|
||||
hasPrivateKey: Boolean(keys[0]?.privateKey),
|
||||
});
|
||||
// Intentionally matches useTerminalEffects' boot effect dep shape (gated on vaultInitialized, not keys).
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [vaultInitialized]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function ShellHarness() {
|
||||
const { domains } = useAppShellProps();
|
||||
const vaultDomain = domains?.vault as
|
||||
| { isVaultInitialized?: boolean; keys?: readonly SSHKey[]; identities?: readonly Identity[] }
|
||||
| undefined;
|
||||
const terminalDomain = domains?.terminal as
|
||||
| { terminalHosts?: readonly Host[] }
|
||||
| undefined;
|
||||
|
||||
return React.createElement(
|
||||
React.Fragment,
|
||||
null,
|
||||
React.createElement(VaultHost),
|
||||
React.createElement(TerminalHostMirror),
|
||||
vaultDomain && terminalDomain
|
||||
? React.createElement(SyncedTerminal, {
|
||||
vaultInitializedOverride: vaultDomain.isVaultInitialized ?? false,
|
||||
hosts: terminalDomain.terminalHosts ?? [],
|
||||
keys: vaultDomain.keys ?? [],
|
||||
identities: vaultDomain.identities ?? [],
|
||||
})
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
const root = createRoot(dom.window.document.getElementById('root')!);
|
||||
try {
|
||||
await React.act(async () => {
|
||||
root.render(React.createElement(ShellHarness));
|
||||
});
|
||||
assert.equal(syncedBoots.length, 0, 'restored terminal must not boot before vault hydration');
|
||||
|
||||
// Publish hydrated hosts, keys, identities, and isVaultInitialized together.
|
||||
await React.act(async () => {
|
||||
publishVaultSnapshot({
|
||||
...EMPTY_VAULT_SNAPSHOT,
|
||||
isVaultInitialized: true,
|
||||
hosts: [host],
|
||||
keys: [key],
|
||||
identities: [identity],
|
||||
});
|
||||
});
|
||||
|
||||
assert.deepEqual(syncedBoots, [
|
||||
{
|
||||
hostCount: 1,
|
||||
keyCount: 1,
|
||||
identityCount: 1,
|
||||
hasPrivateKey: true,
|
||||
},
|
||||
]);
|
||||
} finally {
|
||||
await React.act(async () => {
|
||||
root.unmount();
|
||||
});
|
||||
publishVaultSnapshot(EMPTY_VAULT_SNAPSHOT);
|
||||
registerAppHandlers(null);
|
||||
(globalThis as Record<string, unknown>).window = prevWindow;
|
||||
(globalThis as Record<string, unknown>).document = prevDocument;
|
||||
(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = prevActEnv;
|
||||
}
|
||||
});
|
||||
|
||||
test('terminal layer memo comparators propagate isVaultInitialized changes', () => {
|
||||
const baseLayerProps = {
|
||||
isVaultInitialized: false,
|
||||
hosts: [],
|
||||
sessions: [],
|
||||
};
|
||||
assert.equal(
|
||||
terminalLayerAreEqual(baseLayerProps, { ...baseLayerProps, isVaultInitialized: true }),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
terminalLayerWorkspaceCtxEqual(
|
||||
{ isVaultInitialized: false },
|
||||
{ isVaultInitialized: true },
|
||||
),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
terminalLayerViewCtxEqual(
|
||||
{ isVaultInitialized: false },
|
||||
{ isVaultInitialized: true },
|
||||
),
|
||||
false,
|
||||
);
|
||||
|
||||
const appViewSource = readFileSync(new URL('./AppView.tsx', import.meta.url), 'utf8');
|
||||
const supportSource = readFileSync(
|
||||
new URL('../../components/terminalLayer/TerminalLayerSupport.tsx', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
assert.match(appViewSource, /<TerminalLayerMount[\s\S]*?isVaultInitialized=\{isVaultInitialized\}/);
|
||||
assert.match(supportSource, /vaultInitializedOverride=\{isVaultInitialized\}/);
|
||||
});
|
||||
@@ -192,6 +192,7 @@ function buildScriptSessionMeta(
|
||||
}
|
||||
|
||||
const TerminalLayerInner: React.FC<TerminalLayerProps> = ({
|
||||
isVaultInitialized,
|
||||
hosts,
|
||||
portForwardingRules = [],
|
||||
customGroups,
|
||||
@@ -2802,6 +2803,7 @@ const TerminalLayerInner: React.FC<TerminalLayerProps> = ({
|
||||
onToggleGlobalComposeSendTarget,
|
||||
canUseGlobalBroadcast,
|
||||
isComposeBarOpen,
|
||||
isVaultInitialized,
|
||||
keyBindings,
|
||||
keys,
|
||||
knownHosts,
|
||||
|
||||
@@ -683,6 +683,7 @@ export const AIChatPanelsHost = memo(AIChatPanelsHostInner, aiChatPanelsHostAreE
|
||||
AIChatPanelsHost.displayName = 'AIChatPanelsHost';
|
||||
|
||||
export interface TerminalLayerProps {
|
||||
isVaultInitialized?: boolean;
|
||||
hosts: Host[];
|
||||
portForwardingRules?: import('../../domain/models').PortForwardingRule[];
|
||||
customGroups: string[];
|
||||
@@ -822,6 +823,7 @@ interface TerminalPaneProps {
|
||||
splitVerticalHandlersRef: React.MutableRefObject<Map<string, () => void>>;
|
||||
resolveSessionAppearance: (hostScope: TerminalAppearanceHostScope) => ResolvedAppearance;
|
||||
hostMap: Map<string, Host>;
|
||||
isVaultInitialized?: boolean;
|
||||
keys: SSHKey[];
|
||||
identities: Identity[];
|
||||
snippets: Snippet[];
|
||||
@@ -961,6 +963,7 @@ const terminalPanePropsAreEqual = (
|
||||
prev.splitHorizontalHandlersRef === next.splitHorizontalHandlersRef &&
|
||||
prev.splitVerticalHandlersRef === next.splitVerticalHandlersRef &&
|
||||
getPaneAppearanceThemeId(prev) === getPaneAppearanceThemeId(next) &&
|
||||
prev.isVaultInitialized === next.isVaultInitialized &&
|
||||
prev.keys === next.keys &&
|
||||
prev.identities === next.identities &&
|
||||
prev.snippets === next.snippets &&
|
||||
@@ -1253,6 +1256,7 @@ const TerminalPane: React.FC<TerminalPaneProps> = memo(({
|
||||
splitVerticalHandlersRef,
|
||||
resolveSessionAppearance,
|
||||
hostMap,
|
||||
isVaultInitialized,
|
||||
keys,
|
||||
identities,
|
||||
snippets,
|
||||
@@ -1680,6 +1684,7 @@ const TerminalPane: React.FC<TerminalPaneProps> = memo(({
|
||||
sessionId={session.id}
|
||||
workspaceId={session.workspaceId}
|
||||
restoreState={session.restoreState}
|
||||
vaultInitializedOverride={isVaultInitialized}
|
||||
pendingInitialCwd={session.pendingInitialCwd}
|
||||
shellType={session.shellType}
|
||||
lastCwd={session.lastCwd}
|
||||
@@ -1776,6 +1781,7 @@ interface TerminalPanesHostProps {
|
||||
splitVerticalHandlersRef: React.MutableRefObject<Map<string, () => void>>;
|
||||
resolveSessionAppearance: (hostScope: TerminalAppearanceHostScope) => ResolvedAppearance;
|
||||
hostMap: Map<string, Host>;
|
||||
isVaultInitialized?: boolean;
|
||||
keys: SSHKey[];
|
||||
identities: Identity[];
|
||||
snippets: Snippet[];
|
||||
@@ -1871,6 +1877,7 @@ const terminalPanesHostPropsAreEqual = (
|
||||
if (prev.splitVerticalHandlersRef !== next.splitVerticalHandlersRef) return false;
|
||||
if (prev.resolveSessionAppearance !== next.resolveSessionAppearance) return false;
|
||||
if (prev.hostMap !== next.hostMap) return false;
|
||||
if (prev.isVaultInitialized !== next.isVaultInitialized) return false;
|
||||
if (prev.keys !== next.keys) return false;
|
||||
if (prev.identities !== next.identities) return false;
|
||||
if (prev.snippets !== next.snippets) return false;
|
||||
|
||||
@@ -62,6 +62,7 @@ function TerminalLayerWorkspaceSectionInner({ ctx }: { ctx: WorkspaceContext })
|
||||
splitVerticalHandlersRef,
|
||||
resolveSessionAppearance,
|
||||
hostMap,
|
||||
isVaultInitialized,
|
||||
keys,
|
||||
identities,
|
||||
snippets,
|
||||
@@ -230,6 +231,7 @@ function TerminalLayerWorkspaceSectionInner({ ctx }: { ctx: WorkspaceContext })
|
||||
splitVerticalHandlersRef={splitVerticalHandlersRef}
|
||||
resolveSessionAppearance={resolveSessionAppearance}
|
||||
hostMap={hostMap}
|
||||
isVaultInitialized={isVaultInitialized}
|
||||
keys={keys}
|
||||
identities={identities}
|
||||
snippets={snippets}
|
||||
|
||||
@@ -383,6 +383,7 @@ const WORKSPACE_CTX_KEYS = [
|
||||
'splitVerticalHandlersRef',
|
||||
'resolveSessionAppearance',
|
||||
'hostMap',
|
||||
'isVaultInitialized',
|
||||
'keys',
|
||||
'identities',
|
||||
'snippets',
|
||||
|
||||
@@ -4,6 +4,7 @@ export const terminalLayerAreEqual = (
|
||||
prev: Record<string, unknown>,
|
||||
next: Record<string, unknown>,
|
||||
): boolean => (
|
||||
prev.isVaultInitialized === next.isVaultInitialized &&
|
||||
prev.hosts === next.hosts &&
|
||||
prev.customGroups === next.customGroups &&
|
||||
prev.groupConfigs === next.groupConfigs &&
|
||||
|
||||
Reference in new issue
Block a user