Files
Netcatty/domain/credentials.ts
T
cc5ad3be16 perf: isolate Terminal hot props, cut memory/jank, and repay architecture debt (#2790)
* refactor: migrate architecture-debt hooks into application/state

Move SFTP Wave A stores/hooks and related vault/AI/terminal/systemManager
hooks out of components into application/state, update importers, and keep
thin deprecated re-exports at the old paths.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* refactor(sftp): align local bookmarks store API with global bookmarks

Export subscribe/get/set/rehydrate helpers from localSftpBookmarks to match
the globalSftpBookmarks external-store pattern.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(electron): reduce baseline memory via spellcheck/GPU/prewarm guards

Disable spellcheck on BrowserWindows and the default session, drop spare
renderers, gate aggressive GPU switches behind NETCATTY_COMPAT_GPU, and make
settings prewarm opt-in via NETCATTY_PREWARM_SETTINGS.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* Isolate notes and customAccent from TerminalLayer hot props

Route vault notes through notesStore (shellHistoryStore pattern) and
app accent through appearanceChromeStore so note edits and accent-drag
ticks no longer rebuild TerminalLayer. Notes/AI slots and Terminal leaves
subscribe locally; VaultView and open-note callbacks stay unchanged.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(ui): cut unnecessary AppView/VaultView/settings re-renders

Stabilize TopTabs close handler, memo AppHostTreeLayer, exclude accent from
settingsVersion, ignore connectionLogs off the logs section, share lastConnected
host refs, and keep rename dialog drafts local so chrome domain stays stable.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* chore: package size, dead code, and consolidation cleanup

Remove unused direct deps, exclude Vite-bundled renderer packages from
electron-builder, delete high-confidence dead code/no-op transfer warm
wiring, consolidate cloud provider IDs and KnownHost/SFTP helpers, and
land UI/AGENTS quick wins (rAF text batching, notes draft debounce,
streaming plain text, tempDirBridge, diagnostics adapter, External MCP
setters).

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: stabilize rAF text batching and diagnostics tests

Polyfill requestAnimationFrame for Node, flush pending text before
steer buffering, bypass rAF while steer is in flight, and stub
global localStorage for AI panel diagnostics adapter tests.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: correct import paths after application/state hook migrations

Migrated vault/AI/system hooks still used components/-relative imports;
point them at repo-root domain/components/types/lib instead.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: address Bugbot/Codex regressions from perf cleanup

- Keep blank Enter from closing RenameDraftDialog; only reset rename state after a non-empty submit
- Flush note drafts before vault mutations so pending title/body edits are not overwritten
- Re-apply appearanceChromeStore accent on appearanceTheme in Terminal, and keep layer themes unaccented

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(notes): flush draft before markdown export paths

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep editor-close hotkey ref fresh during render

Assign handleRequestCloseEditorTabRef in render instead of useEffect so
Cmd/Ctrl+W never hits the App stub before the effect runs.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep accent in chrome themes without TerminalLayer thrash

Restore accent in globalAppearance for chrome/injection consumers.
Keep resolveFocusedAppearance identity stable across accent drag via
accent ref, while Terminal leaf still re-applies appearanceChromeStore.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep compose chrome accent live and merge note renames

Subscribe theme-panel focusedAppearance to appearanceChromeStore so
compose-bar chrome updates during color-picker drag even when
resolveFocusedAppearance identity stays stable. Tree renames now merge
title into the post-flush note instead of replacing from a stale row.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: clear terminal accent when switching back to theme mode

Re-resolve appearanceTheme by catalog id before applyCustomAccent so a
stale baked custom cursor/selection cannot stick after accentMode returns
to theme while TerminalLayer memo ignores accent props.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: register unsaved prompt singleton during render

AppView close handlers call promptUnsavedChanges outside the old
render-prop. Assign the singleton during render so Cmd/Ctrl+W and tab
close never hit the pre-effect null window that auto-cancels.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep vault import hooks UI-free and flush notes on teardown

Move ImportOptions into application/state and inject a notifier so
useVaultImportHandlers no longer imports components. Notes drafts also
flush on pagehide/beforeunload when Electron tears down without React
unmount.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: move AI terminal session context builders into domain

buildAITerminalSessionInfo and AIPanelContext now live under domain/ so
application hooks no longer import TerminalLayerSupport and create a
UI → application → UI cycle.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: drop duplicate AIPanelContext type from TerminalLayerSupport

The type is re-exported from domain; the local definition conflicted.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep migrated state hooks free of UI-layer imports

Move aiChatStreamingSupport into infrastructure/ai and poll list helpers
into domain/systemManager so useAIChatStreaming / useSystemManager no
longer import from components. Leave thin re-exports at the old paths.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: move remaining migrated-hook deps out of components

Host-key verification types/helpers now live in domain/, and SFTP column
layout types live in application/state/sftp/columnLayout. Application
hooks no longer import those definitions from the UI layer.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: clear lint blockers and flush notes when panels hide

Move AI panel diagnostics persistence into application/state so
components stop importing localStorageAdapter. Flush note drafts when
retained NotesManager mounts become inactive, and silence related lint
warnings in theme/host-key helpers.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(notes): publish notesStore sync on vault note updates

updateNotes/updateNoteGroups now publishNotesSnapshot immediately so
AI/notes consumers do not briefly read a stale catalog after a hide
flush. Flush inactive drafts in useLayoutEffect before paint.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-08-07 14:40:38 +08:00

433 lines
16 KiB
TypeScript

import type { SyncPayload } from "./sync";
const CREDENTIAL_ENCRYPTION_PREFIX = "enc:v1:";
/**
* Base64 pattern: only allows A-Z, a-z, 0-9, +, / and trailing = padding.
*/
const BASE64_RE = /^[A-Za-z0-9+/]+=*$/;
/**
* Chromium/Electron safeStorage ciphertext carries known platform headers:
* - macOS/Linux: plaintext bytes start with "v10" or "v11"
* - Windows (legacy DPAPI blob): leading bytes are 0x01 0x00 0x00 0x00
*
* Detect headers on *decoded* bytes. A four-byte DPAPI version alone is not
* enough — real blobs continue with provider GUID
* {df9d8cd0-1501-11d1-8c7a-00c04fc297eb} (base64 `AQAAANCMnd8...`).
*
* We require a known header AND a complete-enough decoded blob. v10/v11 CBC
* blobs are at least header(3) + one AES block(16) = 19 bytes. Header-only
* base64 such as `enc:v1:djEw` must not be treated as ciphertext.
*
* Keep in sync with electron/bridges/credentialBridge.cjs.
*
* References:
* - components/os_crypt/sync/os_crypt_mac.mm (kObfuscationPrefixV10 = "v10")
* - components/os_crypt/sync/os_crypt_linux.cc (kObfuscationPrefixV10/V11)
* - components/os_crypt/sync/os_crypt_win.cc (DPAPI legacy path)
*/
const V10_HEADER = [0x76, 0x31, 0x30] as const; // "v10"
const V11_HEADER = [0x76, 0x31, 0x31] as const; // "v11"
// Version (4) + provider GUID {df9d8cd0-1501-11d1-8c7a-00c04fc297eb} (16).
const DPAPI_BLOB_PREFIX = [
0x01, 0x00, 0x00, 0x00,
0xd0, 0x8c, 0x9d, 0xdf, 0x01, 0x15, 0xd1, 0x11,
0x8c, 0x7a, 0x00, 0xc0, 0x4f, 0xc2, 0x97, 0xeb,
] as const;
/** Minimum decoded sizes for complete Chromium OSCrypt blobs. */
const MIN_V10_V11_CIPHERTEXT_BYTES = 19; // CBC: header(3) + one AES block(16)
const MIN_V10_V11_GCM_CIPHERTEXT_BYTES = 31; // header(3) + nonce(12) + tag(16)
const MIN_DPAPI_CIPHERTEXT_BYTES = DPAPI_BLOB_PREFIX.length + 1;
/**
* Renderer-safe base64 decode. Avoids Node `Buffer` which is unavailable
* in Electron windows with `nodeIntegration: false`.
*/
const decodeBase64Bytes = (payload: string): Uint8Array | null => {
try {
if (typeof atob === "function") {
const binary = atob(payload);
const out = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i += 1) {
out[i] = binary.charCodeAt(i);
}
return out;
}
} catch {
// fall through
}
// Node / test environments without atob.
if (typeof Buffer !== "undefined") {
try {
return new Uint8Array(Buffer.from(payload, "base64"));
} catch {
return null;
}
}
return null;
};
const startsWithBytes = (
decoded: Uint8Array,
prefix: readonly number[],
): boolean => {
if (decoded.byteLength < prefix.length) return false;
return prefix.every((byte, index) => decoded[index] === byte);
};
/** CBC is header(3) + 16-byte blocks; GCM is at least header+nonce+tag (31). */
const isValidV10V11CiphertextLength = (byteLength: number): boolean => {
if (byteLength >= MIN_V10_V11_GCM_CIPHERTEXT_BYTES) return true;
return byteLength >= MIN_V10_V11_CIPHERTEXT_BYTES
&& (byteLength - 3) % 16 === 0;
};
export const isEncryptedCredentialPlaceholder = (
value: string | undefined | null,
): value is string => {
if (typeof value !== "string" || !value.startsWith(CREDENTIAL_ENCRYPTION_PREFIX)) {
return false;
}
const payload = value.slice(CREDENTIAL_ENCRYPTION_PREFIX.length);
if (!payload || !BASE64_RE.test(payload)) return false;
const decoded = decodeBase64Bytes(payload);
if (!decoded) return false;
if (startsWithBytes(decoded, V10_HEADER) || startsWithBytes(decoded, V11_HEADER)) {
return isValidV10V11CiphertextLength(decoded.byteLength);
}
if (startsWithBytes(decoded, DPAPI_BLOB_PREFIX)) {
return decoded.byteLength >= MIN_DPAPI_CIPHERTEXT_BYTES;
}
return false;
};
/**
* Strip enc:v1: placeholders from a single credential value.
* Used at the terminal connection boundary to avoid sending encrypted
* placeholders as actual passwords to SSH/Telnet servers.
*/
export const sanitizeCredentialValue = (
value: string | undefined,
): string | undefined => {
if (isEncryptedCredentialPlaceholder(value)) return undefined;
return value;
};
/**
* Scan a sync payload for any fields that still carry device-bound
* enc:v1: ciphertext. Returns the dotted paths of offending fields.
* Used as a pre-upload guard to prevent pushing un-decryptable data.
*/
export const findSyncPayloadEncryptedCredentialPaths = (
payload: SyncPayload,
): string[] => {
const issues: string[] = [];
payload.hosts.forEach((host, index) => {
if (isEncryptedCredentialPlaceholder(host.password)) {
issues.push(`hosts[${index}].password`);
}
if (isEncryptedCredentialPlaceholder(host.telnetPassword)) {
issues.push(`hosts[${index}].telnetPassword`);
}
if (isEncryptedCredentialPlaceholder(host.proxyConfig?.password)) {
issues.push(`hosts[${index}].proxyConfig.password`);
}
});
payload.keys.forEach((key, index) => {
if (isEncryptedCredentialPlaceholder(key.privateKey)) {
issues.push(`keys[${index}].privateKey`);
}
if (isEncryptedCredentialPlaceholder(key.passphrase)) {
issues.push(`keys[${index}].passphrase`);
}
});
payload.identities?.forEach((identity, index) => {
if (isEncryptedCredentialPlaceholder(identity.password)) {
issues.push(`identities[${index}].password`);
}
});
payload.proxyProfiles?.forEach((profile, index) => {
if (isEncryptedCredentialPlaceholder(profile.config.password)) {
issues.push(`proxyProfiles[${index}].config.password`);
}
});
payload.groupConfigs?.forEach((config, index) => {
if (isEncryptedCredentialPlaceholder(config.password)) {
issues.push(`groupConfigs[${index}].password`);
}
if (isEncryptedCredentialPlaceholder(config.telnetPassword)) {
issues.push(`groupConfigs[${index}].telnetPassword`);
}
if (isEncryptedCredentialPlaceholder(config.proxyConfig?.password)) {
issues.push(`groupConfigs[${index}].proxyConfig.password`);
}
});
return issues;
};
/**
* Clear device-bound enc:v1 placeholders from a portable sync payload.
*
* Cloud / backup payloads must carry plaintext secrets (protected by the
* master key envelope). If a previous bug uploaded undecryptable local
* ciphertext, stripping placeholders lets download restore a usable vault
* shell so the user can re-enter credentials instead of looping forever.
*/
export const stripSyncPayloadEncryptedCredentials = (
payload: SyncPayload,
): SyncPayload => {
const hosts = payload.hosts.map((host) => {
const next = { ...host };
if (isEncryptedCredentialPlaceholder(next.password)) delete next.password;
if (isEncryptedCredentialPlaceholder(next.telnetPassword)) delete next.telnetPassword;
if (next.proxyConfig && isEncryptedCredentialPlaceholder(next.proxyConfig.password)) {
const { password: _removed, ...proxyRest } = next.proxyConfig;
next.proxyConfig = proxyRest;
}
return next;
});
const keys = payload.keys.map((key) => {
const next = { ...key };
if (isEncryptedCredentialPlaceholder(next.privateKey)) next.privateKey = "";
if (isEncryptedCredentialPlaceholder(next.passphrase)) delete next.passphrase;
return next;
});
const identities = payload.identities?.map((identity) => {
if (!isEncryptedCredentialPlaceholder(identity.password)) return identity;
const next = { ...identity };
delete next.password;
return next;
});
const proxyProfiles = payload.proxyProfiles?.map((profile) => {
if (!isEncryptedCredentialPlaceholder(profile.config.password)) return profile;
const { password: _removed, ...configRest } = profile.config;
return { ...profile, config: configRest };
});
const groupConfigs = payload.groupConfigs?.map((config) => {
const next = { ...config };
if (isEncryptedCredentialPlaceholder(next.password)) delete next.password;
if (isEncryptedCredentialPlaceholder(next.telnetPassword)) delete next.telnetPassword;
if (next.proxyConfig && isEncryptedCredentialPlaceholder(next.proxyConfig.password)) {
const { password: _removed, ...proxyRest } = next.proxyConfig;
next.proxyConfig = proxyRest;
}
return next;
});
return {
...payload,
hosts,
keys,
identities: identities ?? payload.identities,
proxyProfiles: proxyProfiles ?? payload.proxyProfiles,
groupConfigs: groupConfigs ?? payload.groupConfigs,
};
};
const usableCredential = (value: string | undefined): string | undefined => {
if (typeof value !== "string" || value.length === 0) return undefined;
if (isEncryptedCredentialPlaceholder(value)) return undefined;
return value;
};
/**
* Resolve a poisoned secret against preferred then fallback.
*
* If the preferred *entity* exists, an empty/missing preferred secret is an
* explicit deletion and must win over fallback/base. Only when preferred is
* also poisoned (or the preferred entity is absent) may we revive from base.
*/
const healPoisonedCredential = (
preferredEntity: unknown,
preferredValue: string | undefined,
fallbackValue: string | undefined,
): string | undefined => {
if (preferredEntity) {
if (isEncryptedCredentialPlaceholder(preferredValue)) {
return usableCredential(fallbackValue);
}
return usableCredential(preferredValue);
}
return usableCredential(fallbackValue);
};
/**
* Before three-way merge, replace device-bound enc:v1 secrets on `poisoned`
* with usable values from `preferred` then `fallback`. Used for both remote
* and local sides so a poisoned field cannot win the entity-level merge and
* delete a still-usable secret from the other side / base.
*
* Explicit preferred-side deletions (empty/absent secrets on a present entity)
* are authoritative and are not revived from base.
*/
export const healPoisonedSecretsForMerge = (
poisoned: SyncPayload,
preferred: SyncPayload,
fallback: SyncPayload | null | undefined,
): SyncPayload => {
const preferredHosts = new Map(preferred.hosts.map((host) => [host.id, host]));
const fallbackHosts = new Map((fallback?.hosts ?? []).map((host) => [host.id, host]));
const hosts = poisoned.hosts.map((host) => {
const preferredHost = preferredHosts.get(host.id);
const fallbackHost = fallbackHosts.get(host.id);
const next = { ...host };
if (isEncryptedCredentialPlaceholder(next.password)) {
const healed = healPoisonedCredential(
preferredHost,
preferredHost?.password,
fallbackHost?.password,
);
if (healed !== undefined) next.password = healed;
else delete next.password;
}
if (isEncryptedCredentialPlaceholder(next.telnetPassword)) {
const healed = healPoisonedCredential(
preferredHost,
preferredHost?.telnetPassword,
fallbackHost?.telnetPassword,
);
if (healed !== undefined) next.telnetPassword = healed;
else delete next.telnetPassword;
}
if (next.proxyConfig && isEncryptedCredentialPlaceholder(next.proxyConfig.password)) {
const healed = healPoisonedCredential(
preferredHost,
preferredHost?.proxyConfig?.password,
fallbackHost?.proxyConfig?.password,
);
if (healed !== undefined) {
next.proxyConfig = { ...next.proxyConfig, password: healed };
} else {
const { password: _removed, ...proxyRest } = next.proxyConfig;
next.proxyConfig = proxyRest;
}
}
return next;
});
const preferredKeys = new Map(preferred.keys.map((key) => [key.id, key]));
const fallbackKeys = new Map((fallback?.keys ?? []).map((key) => [key.id, key]));
const keys = poisoned.keys.map((key) => {
const preferredKey = preferredKeys.get(key.id);
const fallbackKey = fallbackKeys.get(key.id);
const next = { ...key };
if (isEncryptedCredentialPlaceholder(next.privateKey)) {
next.privateKey = healPoisonedCredential(
preferredKey,
preferredKey?.privateKey,
fallbackKey?.privateKey,
) ?? "";
}
if (isEncryptedCredentialPlaceholder(next.passphrase)) {
const healed = healPoisonedCredential(
preferredKey,
preferredKey?.passphrase,
fallbackKey?.passphrase,
);
if (healed !== undefined) next.passphrase = healed;
else delete next.passphrase;
}
return next;
});
const preferredIdentities = new Map((preferred.identities ?? []).map((identity) => [identity.id, identity]));
const fallbackIdentities = new Map((fallback?.identities ?? []).map((identity) => [identity.id, identity]));
const identities = poisoned.identities?.map((identity) => {
if (!isEncryptedCredentialPlaceholder(identity.password)) return identity;
const preferredIdentity = preferredIdentities.get(identity.id);
const healed = healPoisonedCredential(
preferredIdentity,
preferredIdentity?.password,
fallbackIdentities.get(identity.id)?.password,
);
if (healed !== undefined) return { ...identity, password: healed };
const next = { ...identity };
delete next.password;
return next;
});
const preferredProfiles = new Map((preferred.proxyProfiles ?? []).map((profile) => [profile.id, profile]));
const fallbackProfiles = new Map((fallback?.proxyProfiles ?? []).map((profile) => [profile.id, profile]));
const proxyProfiles = poisoned.proxyProfiles?.map((profile) => {
if (!isEncryptedCredentialPlaceholder(profile.config.password)) return profile;
const preferredProfile = preferredProfiles.get(profile.id);
const healed = healPoisonedCredential(
preferredProfile,
preferredProfile?.config.password,
fallbackProfiles.get(profile.id)?.config.password,
);
if (healed !== undefined) {
return { ...profile, config: { ...profile.config, password: healed } };
}
const { password: _removed, ...configRest } = profile.config;
return { ...profile, config: configRest };
});
const preferredGroupConfigs = new Map((preferred.groupConfigs ?? []).map((config) => [config.path, config]));
const fallbackGroupConfigs = new Map((fallback?.groupConfigs ?? []).map((config) => [config.path, config]));
const groupConfigs = poisoned.groupConfigs?.map((config) => {
const preferredConfig = preferredGroupConfigs.get(config.path);
const fallbackConfig = fallbackGroupConfigs.get(config.path);
const next = { ...config };
let changed = false;
if (isEncryptedCredentialPlaceholder(next.password)) {
const healed = healPoisonedCredential(
preferredConfig,
preferredConfig?.password,
fallbackConfig?.password,
);
if (healed !== undefined) next.password = healed;
else delete next.password;
changed = true;
}
if (isEncryptedCredentialPlaceholder(next.telnetPassword)) {
const healed = healPoisonedCredential(
preferredConfig,
preferredConfig?.telnetPassword,
fallbackConfig?.telnetPassword,
);
if (healed !== undefined) next.telnetPassword = healed;
else delete next.telnetPassword;
changed = true;
}
if (next.proxyConfig && isEncryptedCredentialPlaceholder(next.proxyConfig.password)) {
const healed = healPoisonedCredential(
preferredConfig,
preferredConfig?.proxyConfig?.password,
fallbackConfig?.proxyConfig?.password,
);
if (healed !== undefined) {
next.proxyConfig = { ...next.proxyConfig, password: healed };
} else {
const { password: _removed, ...proxyRest } = next.proxyConfig;
next.proxyConfig = proxyRest;
}
changed = true;
}
return changed ? next : config;
});
return {
...poisoned,
hosts,
keys,
identities: identities ?? poisoned.identities,
proxyProfiles: proxyProfiles ?? poisoned.proxyProfiles,
groupConfigs: groupConfigs ?? poisoned.groupConfigs,
};
};