From e074bc3f054d96990cc9e50c9ff1a99d0e7c709b Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sat, 18 Jul 2026 23:40:01 -0700 Subject: [PATCH] Gate certificate trust proceed action on runtime capability (#9070) Only offer 'Proceed Anyway (Unsafe)' if the connected remote runtime advertises browser.certificate-trust.v1 support. Older runtimes cannot honor the request and would fail silently, creating a false affordance. Centralize certificate error normalization to prevent divergence between main and renderer certificate matching, and unify URL redaction for Kagi session token stripping across load-error paths. --- .../browser/browser-certificate-identity.ts | 11 +++--- .../browser-certificate-trust-controller.ts | 6 +++- src/main/browser/browser-manager.ts | 27 ++++++++------ .../components/browser-pane/BrowserPane.tsx | 36 ++++++++++++++++++- .../browser-load-failure-overlay.tsx | 8 +---- src/shared/browser-certificate-errors.ts | 10 ++++++ 6 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/main/browser/browser-certificate-identity.ts b/src/main/browser/browser-certificate-identity.ts index 9170f8edd..74d16de04 100644 --- a/src/main/browser/browser-certificate-identity.ts +++ b/src/main/browser/browser-certificate-identity.ts @@ -1,15 +1,12 @@ import { createHash } from 'node:crypto' +import { normalizeCertificateError } from '../../shared/browser-certificate-errors' + +export { normalizeCertificateError } + export const SUPPORTED_CERTIFICATE_ERROR = 'ERR_CERT_AUTHORITY_INVALID' export const SUPPORTED_CERTIFICATE_ERROR_CODE = -202 -export function normalizeCertificateError(error: string): string { - return error - .trim() - .replace(/^net::/i, '') - .toUpperCase() -} - export function getSupportedCertificateErrorCode(error: string): number | null { return normalizeCertificateError(error) === SUPPORTED_CERTIFICATE_ERROR ? SUPPORTED_CERTIFICATE_ERROR_CODE diff --git a/src/main/browser/browser-certificate-trust-controller.ts b/src/main/browser/browser-certificate-trust-controller.ts index e69d7ad2c..53d4810e1 100644 --- a/src/main/browser/browser-certificate-trust-controller.ts +++ b/src/main/browser/browser-certificate-trust-controller.ts @@ -107,7 +107,11 @@ export class BrowserCertificateTrustController { }) } answer(false) - } catch { + } catch (error) { + // Why: fail closed, but log first — a throw in challenge recording would + // otherwise present as "the cert prompt never appears" with no trace, + // matching the logging catch in browser-manager's guest-state notifier. + console.error('[browser-certificate-trust-controller] handleCertificateError failed', error) answer(false) } } diff --git a/src/main/browser/browser-manager.ts b/src/main/browser/browser-manager.ts index 60352732e..33104ad9f 100644 --- a/src/main/browser/browser-manager.ts +++ b/src/main/browser/browser-manager.ts @@ -891,11 +891,11 @@ export class BrowserManager { return } this.clearedLoadErrorsByGuestId.delete(guest.id) - const loadError = { - code: errorCode, - description: errorDescription || 'This site could not be reached.', - validatedUrl: redactKagiSessionToken(validatedURL || guest.getURL() || 'about:blank') - } + const loadError = this.buildLoadError( + errorCode, + errorDescription || 'This site could not be reached.', + validatedURL || guest.getURL() || 'about:blank' + ) this.loadErrorsByGuestId.set(guest.id, loadError) this.forwardOrQueueGuestLoadFailure(guest.id, loadError) this.notifyBrowserGuestStateChanged(guest.id) @@ -1299,17 +1299,24 @@ export class BrowserManager { } } + // Why: both the did-fail-load path and the synthesized certificate failure + // build a load error from an untrusted URL; centralize the redaction so a + // future third site cannot forget to strip Kagi session tokens. + private buildLoadError(code: number, description: string, rawUrl: string): BrowserLoadError { + return { + code, + description, + validatedUrl: redactKagiSessionToken(rawUrl) + } + } + notifyCertificateFailureChanged( webContentsId: number, failure: BrowserCertificateFailure | null, navigationUrl?: string ): void { if (failure && navigationUrl) { - const loadError = { - code: failure.errorCode ?? -1, - description: failure.error, - validatedUrl: redactKagiSessionToken(navigationUrl) - } + const loadError = this.buildLoadError(failure.errorCode ?? -1, failure.error, navigationUrl) this.loadErrorsByGuestId.set(webContentsId, loadError) this.forwardOrQueueGuestLoadFailure(webContentsId, loadError) } diff --git a/src/renderer/src/components/browser-pane/BrowserPane.tsx b/src/renderer/src/components/browser-pane/BrowserPane.tsx index 50b4dc458..2d555f5c7 100644 --- a/src/renderer/src/components/browser-pane/BrowserPane.tsx +++ b/src/renderer/src/components/browser-pane/BrowserPane.tsx @@ -61,6 +61,7 @@ import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover' import { useAppStore } from '@/store' import { getRuntimeEnvironmentIdForWorktree } from '@/lib/worktree-runtime-owner' import { ORCA_BROWSER_BLANK_URL, ORCA_BROWSER_PARTITION } from '../../../../shared/constants' +import { BROWSER_CERTIFICATE_TRUST_RUNTIME_CAPABILITY } from '../../../../shared/protocol-version' import { getOrcaProfileBrowserDefaultPartition } from '../../../../shared/orca-profiles' import type { BrowserCertificateProceedResult, @@ -151,6 +152,7 @@ import { } from '@/runtime/runtime-file-client' import { callRuntimeRpc, + runtimeEnvironmentSupportsCapability, RuntimeRpcCallError, type RuntimeClientTarget } from '@/runtime/runtime-rpc-client' @@ -947,6 +949,38 @@ function RemoteBrowserPagePane({ const closeBrowserTab = useAppStore((s) => s.closeBrowserTab) const keybindings = useAppStore((state) => state.keybindings) + // Why: a remote runtime that predates browser.certificate-trust.v1 cannot + // honor a proceed request, so the "Proceed Anyway (Unsafe)" affordance stays + // hidden until the connected runtime positively advertises support (design + // doc: older runtimes never show an enabled proceed action). + const [remoteCertificateTrustSupported, setRemoteCertificateTrustSupported] = useState(false) + const remoteCertificateEnvironmentId = remotePageHandle?.environmentId ?? null + const certificateChallengeId = certificateFailure?.challengeId ?? null + useEffect(() => { + if (!remoteCertificateEnvironmentId || !certificateChallengeId) { + setRemoteCertificateTrustSupported(false) + return + } + let cancelled = false + void runtimeEnvironmentSupportsCapability( + remoteCertificateEnvironmentId, + BROWSER_CERTIFICATE_TRUST_RUNTIME_CAPABILITY + ) + .then((supported) => { + if (!cancelled) { + setRemoteCertificateTrustSupported(supported) + } + }) + .catch(() => { + if (!cancelled) { + setRemoteCertificateTrustSupported(false) + } + }) + return () => { + cancelled = true + } + }, [remoteCertificateEnvironmentId, certificateChallengeId]) + currentBrowserTabIdRef.current = browserTab.id currentBrowserTabUrlRef.current = browserTab.url activeRuntimeEnvironmentIdRef.current = activeRuntimeEnvironmentId @@ -2696,7 +2730,7 @@ function RemoteBrowserPagePane({ onTryHttps={(url) => void runRemoteNavigation('browser.goto', url)} onCopy={(url) => void window.api.ui.writeClipboardText(url)} onOpenExternal={(url) => void window.api.shell.openUrl(url)} - certificateFailure={certificateFailure} + certificateFailure={remoteCertificateTrustSupported ? certificateFailure : null} expectedBrowserPageId={ remotePageHandle?.environmentId === activeRuntimeEnvironmentId ? remotePageHandle.remotePageId diff --git a/src/renderer/src/components/browser-pane/browser-load-failure-overlay.tsx b/src/renderer/src/components/browser-pane/browser-load-failure-overlay.tsx index 06fa72491..db2d139c8 100644 --- a/src/renderer/src/components/browser-pane/browser-load-failure-overlay.tsx +++ b/src/renderer/src/components/browser-pane/browser-load-failure-overlay.tsx @@ -10,6 +10,7 @@ import type { BrowserLoadError } from '../../../../shared/types' import { isEligibleLocalCertificateHost } from '../../../../shared/browser-url' +import { normalizeCertificateError } from '../../../../shared/browser-certificate-errors' import { formatLoadFailureDescription, formatLoadFailureRecoveryHint, @@ -51,13 +52,6 @@ function getLoadErrorMetadata(loadError: BrowserLoadError): LoadFailureMeta { } } -function normalizeCertificateError(error: string): string { - return error - .trim() - .replace(/^net::/i, '') - .toUpperCase() -} - function getMatchingCertificateFailure(args: { loadError: BrowserLoadError certificateFailure?: BrowserCertificateFailure | null diff --git a/src/shared/browser-certificate-errors.ts b/src/shared/browser-certificate-errors.ts index 9f1eba87d..8cc8beb89 100644 --- a/src/shared/browser-certificate-errors.ts +++ b/src/shared/browser-certificate-errors.ts @@ -7,3 +7,13 @@ const CHROMIUM_CERTIFICATE_ERROR_CODES = new Set([ export function isChromiumCertificateErrorCode(code: number): boolean { return CHROMIUM_CERTIFICATE_ERROR_CODES.has(code) } + +// Why: main (certificate-error handler) and the renderer overlay both compare +// Chromium error strings; keep one normalizer so their challenge-matching can +// never silently diverge. +export function normalizeCertificateError(error: string): string { + return error + .trim() + .replace(/^net::/i, '') + .toUpperCase() +}