Files
Netcatty/components/terminalLayer/TerminalLayerWorkspaceSection.tsx
T
陈大猫 18b2f435bc feat(terminal): configurable su/sudo password prompt assist (#2156) (#2176)
* feat(terminal): configurable su/sudo password prompt assist (#2156)

Extend the existing confirm-to-fill sudo assist to arm on su as well, and
add a Terminal setting for off / quick Enter fill / WindTerm-like credential
picker. Picker candidates come from the host password plus keychain password
identities; secrets are never shown in the list or sent without confirmation.

* fix(terminal): tighten password prompt assist after codex review

- Hint mode only uses the session host password, never an arbitrary
  keychain identity as a silent Enter target.
- Build autofill candidates from the effective session host so group-
  inherited passwords participate.
- Clear a stale credential picker when the session disconnects.

* fix(terminal): more password-assist polish from codex review

- Do not swallow arrow keys while the quick-fill hint is pending; only the
  multi-credential picker consumes them.
- Clear the picker overlay on hibernate and wake so it cannot outlive the
  autofill controller.
- Show the resolved auth username on the host candidate row (identityId /
  group identity), not a stale host.username after password dedupe.

* fix(terminal): keep password picker select callback stable

Avoid forcing TerminalView memo invalidation on every Terminal render by
using a stable useCallback for credential selection.

* fix(terminal): do not offer full keychain on unarmed sudo prompts

Picker mode only lists all password identities after a real su/sudo
command was armed. Unarmed explicit [sudo] lines still get host-password
quick-fill only, blocking remote prompt phishing of other credentials.

* fix(terminal): fix picker mode switch and armed picker test

Arm before inspecting the picker payload, and when switching settings
mid-prompt recompute whether the command arm is still valid so picker
mode can open the full list.

* fix(terminal): reopen password picker after a wrong credential

Keep the su/sudo arm active after confirm-to-fill so a rejected password
re-prompt can offer the full credential list again instead of falling
back to host-only unarmed assist.

* fix(terminal): only re-assist real auth retries after fill

Keep a short post-fill arm for wrong-password re-prompts, but require an
explicit [sudo] line or authentication-failure text before reopening
assist. Prevents sending the host password into child prompts after a
successful sudo (e.g. sudo mysql -p).

* fix(terminal): scope sudo assist away from child password prompts

Track whether the armed command was sudo or su. su still accepts bare
Password: lines; sudo only assists on [sudo] tags or user-scoped prompts
(password for … / 输入密码), not generic Enter password when sudo is cached.

* fix(terminal): tighten when the full credential picker may open

Exclude child-program prompts (Enter password, Password for user …).
Full keychain picker only for explicit [sudo] lines or armed su Password:
prompts; Kylin-style sudo bare prompts still get host-password hint only.

* fix(terminal): limit full credential picker to armed su only

Multi-identity picker is the WindTerm-style su workflow (#2156). Sudo
keeps host-password quick-fill only, so a cached sudo child cannot open
the full keychain list via a forged or nested password prompt.

* fix(terminal): derive password assist candidates in Terminal + mode gate

- Standalone Terminal popups without TerminalLayer now resolve candidates
  from host/keys/identities so su picker mode still works.
- When switching to picker mid-prompt, keep the su-only full-list gate
  (do not open multi-identity list for an armed sudo prompt).

* fix(terminal): reject SSH-style prompts for su assist and drop dupe keys

- su bare matching only accepts short Password:/密码 lines, not user@host
  SSH password prompts after passwordless su -c.
- Remove duplicate sudoAutofillCandidates keys that failed eslint.

* fix(terminal): cursor-anchor password picker and soft-dismiss Esc

Position the su credential picker with the same cursor-relative layout
helpers as autocomplete instead of fixed bottom-center. Esc only closes
the UI while keeping the arm so Esc/arrows can reopen the list on the
same Password: prompt.
2026-07-14 15:19:16 +08:00

289 lines
11 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { memo } from 'react';
import { terminalLayerWorkspaceCtxEqual } from './terminalLayerViewMemo';
type WorkspaceContext = Record<string, any>;
function TerminalLayerWorkspaceSectionInner({ ctx }: { ctx: WorkspaceContext }) {
const {
workspaceInnerRef,
workspaceOverlayRef,
draggingSessionId,
isFocusMode,
dropHint,
setDropHint,
computeSplitHint,
handleWorkspaceDrop,
TerminalPanesHost,
sessions,
sessionHostsMap,
sessionChainHostsMap,
sessionSudoAutofillPasswordsMap,
sessionSudoAutofillCandidatesMap,
resolvedSessionHostIds,
workspaceById,
workspaceRectsById,
isTerminalLayerVisible,
workspaceFocusHandlersRef,
workspaceBroadcastHandlersRef,
splitHorizontalHandlersRef,
splitVerticalHandlersRef,
resolveSessionAppearance,
hostMap,
keys,
identities,
snippets,
knownHosts,
terminalFontFamilyId,
fontSize,
terminalTheme,
followAppTerminalTheme,
accentMode,
customAccent,
terminalSettings,
hotkeyScheme,
disableTerminalFontZoom,
restoreTerminalCwd,
keyBindings,
resizing,
isComposeBarOpen,
sessionLogConfig,
sshDebugLogsEnabled,
onHotkeyAction,
handleTerminalFontSizeChange,
handleOpenSftp,
handleTerminalCwdChange,
handleTerminalTitleChange,
handleTerminalBell,
handleTerminalOutput,
handleTerminalContextReaderChange,
handleOpenScripts,
handleOpenHistory,
handleOpenSystem,
handleOpenTheme,
handleCloseSession,
handleStatusChange,
handleSessionExit,
handleTerminalDataCapture,
handleOsDetected,
handleUpdateHost,
handleAddKnownHost,
handleCommandExecuted,
handleCommandSubmitted,
onSetWorkspaceFocusedSession,
onSplitSession,
isBroadcastEnabled,
handleBroadcastInput,
handleBroadcastInterruptPriorityChange,
handleToggleWorkspaceComposeBar,
handleSnippetExecutorChange,
handleProgrammaticCommandLogRewriteChange,
handleAddSelectionToAI,
activeResizers,
activeWorkspace,
composeBarThemeColors,
findSplitNode,
focusedSessionId,
handleComposeSend,
handleSnippetFromPanel,
refocusTerminalSession,
setIsComposeBarOpen,
setResizing,
TerminalComposeBar,
Array,
cn,
onStartSessionRename,
onRemoveSessionFromWorkspace,
onReorderTabs,
onStartSessionDrag,
onEndSessionDrag,
} = ctx;
return (
<div className="flex-1 min-h-0 flex flex-col">
<div ref={workspaceInnerRef} className="flex-1 min-h-0 overflow-hidden relative">
{draggingSessionId && !isFocusMode && (
<div
ref={workspaceOverlayRef}
className="absolute inset-0 z-30"
onDragOver={(e) => {
if (isFocusMode) return;
if (!e.dataTransfer.types.includes('session-id')) return;
e.preventDefault();
e.stopPropagation();
const hint = computeSplitHint(e);
setDropHint(hint);
}}
onDragLeave={(e) => {
if (!e.dataTransfer.types.includes('session-id')) return;
setDropHint(null);
}}
onDrop={(e) => {
e.preventDefault();
e.stopPropagation();
handleWorkspaceDrop(e);
}}
>
{dropHint && (
<div className="absolute inset-0 pointer-events-none">
<div
className="absolute bg-emerald-600/35 border border-emerald-400/70 backdrop-blur-sm transition-all duration-150"
style={{
width: dropHint.rect ? `${dropHint.rect.w}px` : dropHint.direction === 'vertical' ? '50%' : '100%',
height: dropHint.rect ? `${dropHint.rect.h}px` : dropHint.direction === 'vertical' ? '100%' : '50%',
left: dropHint.rect ? `${dropHint.rect.x}px` : dropHint.direction === 'vertical' ? (dropHint.position === 'left' ? 0 : '50%') : 0,
top: dropHint.rect ? `${dropHint.rect.y}px` : dropHint.direction === 'vertical' ? 0 : (dropHint.position === 'top' ? 0 : '50%'),
}}
/>
</div>
)}
</div>
)}
<TerminalPanesHost
sessions={sessions}
sessionHostsMap={sessionHostsMap}
sessionChainHostsMap={sessionChainHostsMap}
sessionSudoAutofillPasswordsMap={sessionSudoAutofillPasswordsMap}
sessionSudoAutofillCandidatesMap={sessionSudoAutofillCandidatesMap}
resolvedSessionHostIds={resolvedSessionHostIds}
workspaceById={workspaceById}
workspaceRectsById={workspaceRectsById}
isTerminalLayerVisible={isTerminalLayerVisible}
workspaceFocusHandlersRef={workspaceFocusHandlersRef}
workspaceBroadcastHandlersRef={workspaceBroadcastHandlersRef}
splitHorizontalHandlersRef={splitHorizontalHandlersRef}
splitVerticalHandlersRef={splitVerticalHandlersRef}
resolveSessionAppearance={resolveSessionAppearance}
hostMap={hostMap}
keys={keys}
identities={identities}
snippets={snippets}
knownHosts={knownHosts}
terminalFontFamilyId={terminalFontFamilyId}
fontSize={fontSize}
terminalTheme={terminalTheme}
followAppTerminalTheme={followAppTerminalTheme}
accentMode={accentMode}
customAccent={customAccent}
terminalSettings={terminalSettings}
hotkeyScheme={hotkeyScheme}
disableTerminalFontZoom={disableTerminalFontZoom}
restoreTerminalCwd={restoreTerminalCwd}
keyBindings={keyBindings}
isResizing={!!resizing}
isComposeBarOpen={isComposeBarOpen}
sessionLog={sessionLogConfig}
sshDebugLogEnabled={sshDebugLogsEnabled}
onHotkeyAction={onHotkeyAction}
onTerminalFontSizeChange={handleTerminalFontSizeChange}
onOpenSftp={handleOpenSftp}
onTerminalCwdChange={handleTerminalCwdChange}
onTerminalTitleChange={handleTerminalTitleChange}
onTerminalBell={handleTerminalBell}
onTerminalOutput={handleTerminalOutput}
onTerminalContextReaderChange={handleTerminalContextReaderChange}
onOpenScripts={handleOpenScripts}
onOpenHistory={handleOpenHistory}
onOpenSystem={handleOpenSystem}
onOpenTheme={handleOpenTheme}
onCloseSession={handleCloseSession}
onStatusChange={handleStatusChange}
onSessionExit={handleSessionExit}
onTerminalDataCapture={handleTerminalDataCapture}
onOsDetected={handleOsDetected}
onUpdateHost={handleUpdateHost}
onAddKnownHost={handleAddKnownHost}
onCommandExecuted={handleCommandExecuted}
onCommandSubmitted={handleCommandSubmitted}
onSetWorkspaceFocusedSession={onSetWorkspaceFocusedSession}
onSplitSession={onSplitSession}
isBroadcastEnabled={isBroadcastEnabled}
onBroadcastInput={handleBroadcastInput}
onBroadcastInterruptPriorityChange={handleBroadcastInterruptPriorityChange}
onToggleWorkspaceComposeBar={handleToggleWorkspaceComposeBar}
onSnippetExecutorChange={handleSnippetExecutorChange}
onProgrammaticCommandLogRewriteChange={handleProgrammaticCommandLogRewriteChange}
onAddSelectionToAI={handleAddSelectionToAI}
onStartSessionRename={onStartSessionRename}
onRemoveSessionFromWorkspace={onRemoveSessionFromWorkspace}
onReorderTabs={onReorderTabs}
onStartSessionDrag={onStartSessionDrag}
onEndSessionDrag={onEndSessionDrag}
/>
{!isFocusMode && activeResizers.map((handle: any) => {
const isVertical = handle.direction === 'vertical';
const left = isVertical ? handle.rect.x - 3 : handle.rect.x;
const top = isVertical ? handle.rect.y : handle.rect.y - 3;
const width = isVertical ? handle.rect.w + 6 : handle.rect.w;
const height = isVertical ? handle.rect.h : handle.rect.h + 6;
return (
<div
key={handle.id}
className={cn('absolute group', isVertical ? 'cursor-ew-resize' : 'cursor-ns-resize')}
data-section="terminal-split-resizer"
data-split-direction={handle.direction}
style={{
left: `${left}px`,
top: `${top}px`,
width: `${width}px`,
height: `${height}px`,
zIndex: 25,
}}
onMouseDown={(e) => {
e.preventDefault();
e.stopPropagation();
const ws = activeWorkspace;
if (!ws) return;
const split = findSplitNode(ws.root, handle.splitId);
const childCount = split && split.type === 'split' ? split.children.length : 0;
const sizes = split && split.type === 'split' && split.sizes && split.sizes.length === childCount
? split.sizes
: Array(childCount).fill(1);
setResizing({
workspaceId: ws.id,
splitId: handle.splitId,
index: handle.index,
direction: handle.direction,
startSizes: sizes.length ? sizes : [1, 1],
startArea: handle.splitArea,
startClient: { x: e.clientX, y: e.clientY },
});
}}
>
<div
data-section="terminal-split-resizer-bar"
className={cn(
'absolute bg-border/70 group-hover:bg-primary/60 transition-colors',
isVertical ? 'w-px h-full left-1/2 -translate-x-1/2' : 'h-px w-full top-1/2 -translate-y-1/2',
)}
/>
</div>
);
})}
</div>
{activeWorkspace && isComposeBarOpen && (
<TerminalComposeBar
onSend={handleComposeSend}
onSnippetClick={(snippet) => void handleSnippetFromPanel(snippet)}
snippets={snippets}
onClose={() => {
setIsComposeBarOpen(false);
refocusTerminalSession(focusedSessionId);
}}
isBroadcastEnabled={isBroadcastEnabled?.(activeWorkspace.id)}
themeColors={composeBarThemeColors}
/>
)}
</div>
);
}
export const TerminalLayerWorkspaceSection = memo(
TerminalLayerWorkspaceSectionInner,
(prev, next) => terminalLayerWorkspaceCtxEqual(prev.ctx, next.ctx),
);
TerminalLayerWorkspaceSection.displayName = 'TerminalLayerWorkspaceSection';