From cd71262c56ad5431be63a5a337746fde1ba856a3 Mon Sep 17 00:00:00 2001 From: Tanger <49036591+GTanger@users.noreply.github.com> Date: Mon, 26 Oct 2026 20:54:55 +0800 Subject: [PATCH 1/2] feat(chat): mask sensitive extension inputs and show auto-retry countdown --- components/ChatInput.test.mjs | 17 +++++ components/ChatInput.tsx | 97 +++++++++++++++----------- components/ExtensionDialog.test.mjs | 58 ++++++++++++++++ components/ExtensionDialog.tsx | 102 +++++++++++++++++++++------- hooks/useAgentSession.ts | 11 ++- lib/i18n/locales/en.json | 3 + lib/i18n/locales/ja.json | 3 + lib/i18n/locales/zh-CN.json | 3 + lib/types.ts | 2 + 9 files changed, 233 insertions(+), 63 deletions(-) diff --git a/components/ChatInput.test.mjs b/components/ChatInput.test.mjs index 26c8b1367..9ef5e2c1d 100644 --- a/components/ChatInput.test.mjs +++ b/components/ChatInput.test.mjs @@ -424,3 +424,20 @@ test("renders both queued prompts and attached status bar together", () => { assert.match(html, /Waiting for model\.\.\./); assert.match(html, /live-status-dot/); }); + +test("renders retry banner with countdown when delayMs and startedAt are provided", () => { + const now = Date.now(); + const html = renderToStaticMarkup( + React.createElement(ChatInput, { + async onSend() { return { status: "accepted" }; }, + retryInfo: { + attempt: 2, + maxAttempts: 5, + delayMs: 10000, + startedAt: now, + }, + }), + ); + + assert.match(html, /Retrying \(2\/5\) in 10s…/); +}); diff --git a/components/ChatInput.tsx b/components/ChatInput.tsx index dacc49fc8..986e615d7 100644 --- a/components/ChatInput.tsx +++ b/components/ChatInput.tsx @@ -194,7 +194,7 @@ interface Props { onToolPresetChange?: (preset: ToolPreset) => void; /** Display name for the current model when the catalog does not know it. */ modelNameOverride?: string | null; - retryInfo?: { attempt: number; maxAttempts: number; errorMessage?: string } | null; + retryInfo?: { attempt: number; maxAttempts: number; delayMs?: number; startedAt?: number; errorMessage?: string } | null; onAbortRetry?: () => void; queuedMessages?: QueuedMessages | null; inputHistory?: string[]; @@ -442,6 +442,15 @@ export const ChatInput = memo(forwardRef(function ChatIn const [fileIndex, setFileIndex] = useState<{ cwd: string; entries: FileIndexEntry[]; truncated: boolean } | null>(null); const [fileIndexLoading, setFileIndexLoading] = useState(false); const [atServerResult, setAtServerResult] = useState<{ cwd: string; query: string; matches: FileIndexEntry[] } | null>(null); + const [retryNow, setRetryNow] = useState(() => Date.now()); + useEffect(() => { + if (!retryInfo?.delayMs || !retryInfo?.startedAt) return; + setRetryNow(Date.now()); + const interval = setInterval(() => { + setRetryNow(Date.now()); + }, 1000); + return () => clearInterval(interval); + }, [retryInfo?.delayMs, retryInfo?.startedAt]); const textareaRef = useRef(null); const dropdownRef = useRef(null); @@ -2064,43 +2073,55 @@ export const ChatInput = memo(forwardRef(function ChatIn {/* Retry banner */} - {retryInfo && ( -
- - - - - {t("chatInput.retrying", { attempt: retryInfo.attempt, maxAttempts: retryInfo.maxAttempts })}{retryInfo.errorMessage && — {retryInfo.errorMessage}} - {onAbortRetry && ( - - )} -
- )} + {retryInfo && (() => { + const remainingSec = (retryInfo.delayMs !== undefined && retryInfo.startedAt !== undefined) + ? (() => { + const effectiveNow = Math.max(retryNow, retryInfo.startedAt); + const maxSec = Math.ceil(retryInfo.delayMs / 1000); + return Math.max(0, Math.min(maxSec, Math.ceil((retryInfo.startedAt + retryInfo.delayMs - effectiveNow) / 1000))); + })() + : undefined; + return ( +
+ + + + + {remainingSec !== undefined + ? t("chatInput.retryingWithCountdown", { attempt: retryInfo.attempt, maxAttempts: retryInfo.maxAttempts, seconds: remainingSec }) + : t("chatInput.retrying", { attempt: retryInfo.attempt, maxAttempts: retryInfo.maxAttempts })} + {retryInfo.errorMessage && — {retryInfo.errorMessage}} + {onAbortRetry && ( + + )} +
+ ); + })()} {compactResultText && (
{ + const request = { + type: "extension_ui_request", + id: "secret-1", + method: "input", + title: "Enter API Key", + placeholder: "sk-...", + }; + const responses = []; + const onRespond = (current, response) => responses.push({ id: current.id, ...response }); + const dialog = (current) => React.createElement(ExtensionDialog, { request: current, onRespond, attached: true }); + const view = render(dialog(request)); + try { + const input = view.getByPlaceholderText("sk-..."); + assert.equal(input.type, "password"); + assert.equal(input.getAttribute("autocomplete"), "off"); + assert.equal(input.getAttribute("data-1p-ignore"), "true"); + + const toggle = screen.getByRole("button", { name: "Show password" }); + fireEvent.click(toggle); + assert.equal(input.type, "text"); + + fireEvent.click(screen.getByRole("button", { name: "Hide password" })); + assert.equal(input.type, "password"); + + // explicit secret: false does not mask even if title contains sensitive words + view.rerender(dialog({ ...request, id: "secret-2", secret: false })); + const plainInput = view.getByPlaceholderText("sk-..."); + assert.equal(plainInput.type, "text"); + assert.equal(screen.queryByRole("button", { name: "Show password" }), null); + + // explicit secret: true masks regardless of title + view.rerender(dialog({ + type: "extension_ui_request", + id: "secret-3", + method: "input", + title: "Random prompt", + secret: true, + })); + const maskedInput = view.container.querySelector("input"); + assert.ok(maskedInput); + assert.equal(maskedInput.type, "password"); + + // plural prompt test: "Enter your credentials" + view.rerender(dialog({ + type: "extension_ui_request", + id: "secret-4", + method: "input", + title: "Enter your credentials", + })); + const pluralInput = view.container.querySelector("input"); + assert.ok(pluralInput); + assert.equal(pluralInput.type, "password"); + } finally { + view.unmount(); + } +}); diff --git a/components/ExtensionDialog.tsx b/components/ExtensionDialog.tsx index f15082b2c..4799028b4 100644 --- a/components/ExtensionDialog.tsx +++ b/components/ExtensionDialog.tsx @@ -1,6 +1,7 @@ "use client"; import { useEffect, useRef, useState, useSyncExternalStore, type RefObject } from "react"; +import { Eye, EyeOff } from "lucide-react"; import type { ExtensionUiRequest } from "@/lib/types"; import type { RpcAskDialogAnswer } from "@/lib/pi-types"; import { useI18n } from "@/lib/i18n"; @@ -195,6 +196,20 @@ function touchPointer(): boolean { return !window.matchMedia("(pointer: fine)").matches; } +const SENSITIVE_PATTERN = /\b(passwords?|passphrases?|secrets?|tokens?|api[_\s-]?keys?|access[_\s-]?tokens?|private[_\s-]?keys?|auth[_\s-]?tokens?|credentials?)\b/i; +const NON_SECRET_METADATA_PATTERN = /\b(token\s+(?:name|count|limit|budget|usage)|max\s+tokens)\b/i; + +export function isSensitivePrompt(title?: string, placeholder?: string): boolean { + const titleText = (title ?? "").trim(); + const placeholderText = (placeholder ?? "").trim(); + const fullText = `${titleText} ${placeholderText}`.trim(); + if (!fullText) return false; + const strippedTitle = titleText.replace(NON_SECRET_METADATA_PATTERN, ""); + if (SENSITIVE_PATTERN.test(strippedTitle)) return true; + const strippedFull = fullText.replace(NON_SECRET_METADATA_PATTERN, ""); + return SENSITIVE_PATTERN.test(strippedFull); +} + /** Single-select questions start on their recommended option. */ function initialAskDrafts(request: ExtensionDialogRequest): AskDraft[] { if (request.method !== "ask") return []; @@ -226,6 +241,7 @@ export function ExtensionDialog({ }) { const { t } = useI18n(); const [value, setValue] = useState(request.method === "editor" ? request.prefill ?? "" : ""); + const [showPassword, setShowPassword] = useState(false); const [selectedOption, setSelectedOption] = useState(null); const requestIdRef = useRef(request.id); // `pointerType` of the latest press: the pointer media query only describes @@ -240,6 +256,7 @@ export function ExtensionDialog({ if (requestIdRef.current === request.id) return; requestIdRef.current = request.id; setValue(request.method === "editor" ? request.prefill ?? "" : ""); + setShowPassword(false); setSelectedOption(null); setAskDrafts(initialAskDrafts(request)); }, [request]); @@ -427,29 +444,68 @@ export function ExtensionDialog({ })}
)} - {request.method === "input" && ( - setValue(e.target.value)} - onKeyDown={(e) => { - if (e.key === "Enter") submitValue(); - }} - style={{ - width: "100%", - padding: "7px 10px", - borderRadius: 6, - border: "1px solid var(--border)", - background: "var(--bg-panel)", - color: "var(--text)", - outline: "none", - fontSize: 12, - fontFamily: "inherit", - }} - /> - )} + {request.method === "input" && (() => { + const isSensitive = Boolean(request.secret || request.password) || + (request.secret !== false && request.password !== false && isSensitivePrompt(request.title, request.placeholder)); + return ( +
+ setValue(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Enter") submitValue(); + }} + style={{ + width: "100%", + padding: isSensitive ? "7px 34px 7px 10px" : "7px 10px", + borderRadius: 6, + border: "1px solid var(--border)", + background: "var(--bg-panel)", + color: "var(--text)", + outline: "none", + fontSize: 12, + fontFamily: "inherit", + }} + /> + {isSensitive && ( + + )} +
+ ); + })()} {request.method === "editor" && (