Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
// Why: use Chromium's exact certificate codes so unrelated network failures
|
||||
// never receive certificate-specific recovery copy.
|
||||
const CHROMIUM_CERTIFICATE_ERROR_CODES = new Set([
|
||||
-200, -201, -202, -203, -204, -205, -206, -207, -208, -210, -211, -212, -213, -214, -217, -219
|
||||
])
|
||||
|
||||
export function isChromiumCertificateErrorCode(code: number): boolean {
|
||||
return CHROMIUM_CERTIFICATE_ERROR_CODES.has(code)
|
||||
}
|
||||
@@ -2,10 +2,15 @@ import { describe, expect, it } from 'vitest'
|
||||
import { ORCA_BROWSER_BLANK_URL } from './constants'
|
||||
import {
|
||||
buildSearchUrl,
|
||||
classifySchemeLessLocalDevAddress,
|
||||
isEligibleLocalCertificateHost,
|
||||
normalizeKagiSessionLink,
|
||||
normalizeBrowserNavigationUrl,
|
||||
normalizeExternalBrowserUrl,
|
||||
redactKagiSessionToken
|
||||
redactKagiSessionToken,
|
||||
resolveRemoteFailureExternalUrl,
|
||||
toSecureCertificateEndpoint,
|
||||
toHttpsRecoveryUrl
|
||||
} from './browser-url'
|
||||
|
||||
describe('browser-url helpers', () => {
|
||||
@@ -20,6 +25,98 @@ describe('browser-url helpers', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps the legacy scheme-less local-dev classifier broader than certificate eligibility', () => {
|
||||
for (const input of [
|
||||
'localhost:3000/path',
|
||||
'127.0.0.1:5173',
|
||||
'0.0.0.0:8080',
|
||||
'[::1]:3000',
|
||||
'[2001:db8::1]:3000'
|
||||
]) {
|
||||
expect(classifySchemeLessLocalDevAddress(input), input).not.toBeNull()
|
||||
}
|
||||
expect(classifySchemeLessLocalDevAddress('app.localhost:3000')).toBeNull()
|
||||
expect(isEligibleLocalCertificateHost('0.0.0.0')).toBe(false)
|
||||
expect(isEligibleLocalCertificateHost('[2001:db8::1]')).toBe(false)
|
||||
})
|
||||
|
||||
it('recognizes only canonical loopback certificate hosts', () => {
|
||||
for (const hostname of [
|
||||
'localhost',
|
||||
'LOCALHOST.',
|
||||
'app.localhost',
|
||||
'deep.app.localhost.',
|
||||
'127.0.0.1',
|
||||
'127.255.255.255',
|
||||
'::1',
|
||||
'[::1]'
|
||||
]) {
|
||||
expect(isEligibleLocalCertificateHost(hostname), hostname).toBe(true)
|
||||
}
|
||||
for (const hostname of [
|
||||
'0.0.0.0',
|
||||
'::',
|
||||
'[2001:db8::1]',
|
||||
'192.168.1.1',
|
||||
'localhost.example.com',
|
||||
'notlocalhost',
|
||||
'.localhost',
|
||||
'-bad.localhost',
|
||||
'bad-.localhost',
|
||||
'127.0.0.999',
|
||||
'127.00.0.1'
|
||||
]) {
|
||||
expect(isEligibleLocalCertificateHost(hostname), hostname).toBe(false)
|
||||
}
|
||||
})
|
||||
|
||||
it('constructs HTTPS recovery URLs without changing the rest of the address', () => {
|
||||
expect(toHttpsRecoveryUrl('http://localhost:3000/path?q=1#preview')).toBe(
|
||||
'https://localhost:3000/path?q=1#preview'
|
||||
)
|
||||
expect(toHttpsRecoveryUrl('http://user:pass@127.0.0.2:8080/')).toBe(
|
||||
'https://user:pass@127.0.0.2:8080/'
|
||||
)
|
||||
expect(toHttpsRecoveryUrl('http://localhost:80/path')).toBe('https://localhost/path')
|
||||
expect(toHttpsRecoveryUrl('https://localhost:3000/')).toBeNull()
|
||||
expect(toHttpsRecoveryUrl('http://0.0.0.0:3000/')).toBeNull()
|
||||
expect(toHttpsRecoveryUrl('http://example.com/')).toBeNull()
|
||||
expect(toHttpsRecoveryUrl('not a url')).toBeNull()
|
||||
})
|
||||
|
||||
it('canonicalizes secure certificate endpoints without path or credential data', () => {
|
||||
expect(toSecureCertificateEndpoint('https://User:secret@LOCALHOST.:443/path?q=1')).toBe(
|
||||
'https://localhost:443'
|
||||
)
|
||||
expect(toSecureCertificateEndpoint('wss://localhost:3000/socket')).toBe(
|
||||
'https://localhost:3000'
|
||||
)
|
||||
expect(toSecureCertificateEndpoint('https://[::1]/')).toBe('https://[::1]:443')
|
||||
expect(toSecureCertificateEndpoint('http://localhost:3000/')).toBeNull()
|
||||
expect(toSecureCertificateEndpoint('not a url')).toBeNull()
|
||||
})
|
||||
|
||||
it('offers Open Externally for a remote failure only when the URL is desktop-reachable', () => {
|
||||
// Loopback / wildcard hosts are unreachable from the desktop system browser.
|
||||
expect(resolveRemoteFailureExternalUrl('https://localhost:3000/')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('https://127.0.0.1:3000/')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('https://127.0.0.9:3000/')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('https://[::1]:3000/')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('https://app.localhost:3000/')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('http://0.0.0.0:3000/')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('http://[::]:3000/')).toBeNull()
|
||||
// Public hosts are reachable, so the action is offered.
|
||||
expect(resolveRemoteFailureExternalUrl('https://example.com/app')).toBe(
|
||||
'https://example.com/app'
|
||||
)
|
||||
expect(resolveRemoteFailureExternalUrl('http://example.com:8080/x')).toBe(
|
||||
'http://example.com:8080/x'
|
||||
)
|
||||
// Non-web schemes and garbage never become an external target.
|
||||
expect(resolveRemoteFailureExternalUrl('file:///etc/passwd')).toBeNull()
|
||||
expect(resolveRemoteFailureExternalUrl('not a url')).toBeNull()
|
||||
})
|
||||
|
||||
it('keeps normal web URLs and blank tabs in the allowed set', () => {
|
||||
expect(normalizeBrowserNavigationUrl('https://example.com')).toBe('https://example.com/')
|
||||
expect(normalizeBrowserNavigationUrl('')).toBe(ORCA_BROWSER_BLANK_URL)
|
||||
|
||||
+108
-6
@@ -34,6 +34,111 @@ const SEARCH_ENGINE_URLS: Record<SearchEngine, string> = {
|
||||
|
||||
export const DEFAULT_SEARCH_ENGINE: SearchEngine = 'google'
|
||||
|
||||
export function classifySchemeLessLocalDevAddress(rawInput: string): URL | null {
|
||||
const trimmed = rawInput.trim()
|
||||
if (!LOCAL_ADDRESS_PATTERN.test(trimmed)) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return new URL(`http://${trimmed}`)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeCertificateHostname(hostname: string): string {
|
||||
const lower = hostname.trim().toLowerCase()
|
||||
const unbracketed = lower.startsWith('[') && lower.endsWith(']') ? lower.slice(1, -1) : lower
|
||||
return unbracketed.endsWith('.') ? unbracketed.slice(0, -1) : unbracketed
|
||||
}
|
||||
|
||||
function isValidDnsName(name: string): boolean {
|
||||
if (name.length === 0 || name.length > 253) {
|
||||
return false
|
||||
}
|
||||
return name
|
||||
.split('.')
|
||||
.every(
|
||||
(label) =>
|
||||
label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label)
|
||||
)
|
||||
}
|
||||
|
||||
function isIpv4Loopback(hostname: string): boolean {
|
||||
const octets = hostname.split('.')
|
||||
if (octets.length !== 4 || octets.some((octet) => !/^\d{1,3}$/.test(octet))) {
|
||||
return false
|
||||
}
|
||||
const values = octets.map(Number)
|
||||
return (
|
||||
values[0] === 127 &&
|
||||
values.every((value, index) => value >= 0 && value <= 255 && octets[index] === String(value))
|
||||
)
|
||||
}
|
||||
|
||||
export function isEligibleLocalCertificateHost(hostname: string): boolean {
|
||||
const normalized = normalizeCertificateHostname(hostname)
|
||||
if (normalized === '::1' || isIpv4Loopback(normalized)) {
|
||||
return true
|
||||
}
|
||||
if (!isValidDnsName(normalized)) {
|
||||
return false
|
||||
}
|
||||
return normalized === 'localhost' || normalized.endsWith('.localhost')
|
||||
}
|
||||
|
||||
function isWildcardBindHost(hostname: string): boolean {
|
||||
const normalized = normalizeCertificateHostname(hostname)
|
||||
return normalized === '0.0.0.0' || normalized === '::'
|
||||
}
|
||||
|
||||
export function toHttpsRecoveryUrl(rawUrl: string): string | null {
|
||||
try {
|
||||
const parsed = new URL(rawUrl)
|
||||
if (parsed.protocol !== 'http:' || !isEligibleLocalCertificateHost(parsed.hostname)) {
|
||||
return null
|
||||
}
|
||||
parsed.protocol = 'https:'
|
||||
return parsed.toString()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function toSecureCertificateEndpoint(rawUrl: string): string | null {
|
||||
try {
|
||||
const parsed = new URL(rawUrl)
|
||||
if (parsed.protocol !== 'https:' && parsed.protocol !== 'wss:') {
|
||||
return null
|
||||
}
|
||||
const normalizedHostname = normalizeCertificateHostname(parsed.hostname)
|
||||
if (!normalizedHostname) {
|
||||
return null
|
||||
}
|
||||
const endpointHost = normalizedHostname.includes(':')
|
||||
? `[${normalizedHostname}]`
|
||||
: normalizedHostname
|
||||
return `https://${endpointHost}:${parsed.port || '443'}`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Why: a remote-owned browser page's loopback URL is unreachable from the
|
||||
// desktop system browser, so Open Externally is offered only for publicly
|
||||
// reachable (non-loopback) failure URLs.
|
||||
export function resolveRemoteFailureExternalUrl(rawUrl: string): string | null {
|
||||
try {
|
||||
const parsed = new URL(rawUrl)
|
||||
if (isWildcardBindHost(parsed.hostname) || isEligibleLocalCertificateHost(parsed.hostname)) {
|
||||
return null
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
return normalizeExternalBrowserUrl(rawUrl)
|
||||
}
|
||||
|
||||
export function normalizeKagiSessionLink(rawLink: string): string | null {
|
||||
const trimmed = rawLink.trim()
|
||||
if (!trimmed) {
|
||||
@@ -163,12 +268,9 @@ export function normalizeBrowserNavigationUrl(
|
||||
return ORCA_BROWSER_BLANK_URL
|
||||
}
|
||||
|
||||
if (LOCAL_ADDRESS_PATTERN.test(trimmed)) {
|
||||
try {
|
||||
return new URL(`http://${trimmed}`).toString()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
const localDevAddress = classifySchemeLessLocalDevAddress(trimmed)
|
||||
if (localDevAddress) {
|
||||
return localDevAddress.toString()
|
||||
}
|
||||
|
||||
if (WINDOWS_UNC_PATH_PATTERN.test(trimmed)) {
|
||||
|
||||
@@ -38,6 +38,7 @@ export const AI_VAULT_RUNTIME_CAPABILITY = 'aiVault.v1' as const
|
||||
// offscreen backend). Advertised only when that backend is actually available, so
|
||||
// clients never fall back to a local desktop browser tab for a remote-owned page.
|
||||
export const BROWSER_HEADLESS_RUNTIME_CAPABILITY = 'browser.headless.v1' as const
|
||||
export const BROWSER_CERTIFICATE_TRUST_RUNTIME_CAPABILITY = 'browser.certificate-trust.v1' as const
|
||||
// Why: hosts without this strip terminal.send's inputKind (zod object drops
|
||||
// unknown keys), so a mobile xterm query reply would land as ordinary
|
||||
// floor-taking input. Mobile must not forward replies unless advertised.
|
||||
|
||||
@@ -8,6 +8,8 @@ import type {
|
||||
import type {
|
||||
BaseRefSearchResult,
|
||||
BrowserCookieImportResult,
|
||||
BrowserCertificateFailure,
|
||||
BrowserLoadError,
|
||||
BrowserSessionProfile,
|
||||
BrowserSessionProfileSource,
|
||||
CreateWorktreeResult,
|
||||
@@ -206,6 +208,8 @@ export type RuntimeMobileSessionBrowserTab = {
|
||||
loading: boolean
|
||||
canGoBack: boolean
|
||||
canGoForward: boolean
|
||||
loadError?: BrowserLoadError | null
|
||||
certificateFailure?: BrowserCertificateFailure | null
|
||||
color?: string | null
|
||||
isPinned?: boolean
|
||||
isActive: boolean
|
||||
@@ -818,6 +822,11 @@ export type BrowserTabInfo = {
|
||||
url: string
|
||||
title: string
|
||||
active: boolean
|
||||
// Why: a failed load leaves getURL() at chrome-error://; surface the structured
|
||||
// error so an agent driving the browser can tell a bypassable certificate
|
||||
// failure from an ordinary network error the way the UI can.
|
||||
loadError?: BrowserLoadError | null
|
||||
certificateFailure?: BrowserCertificateFailure | null
|
||||
worktreeId?: string | null
|
||||
profileId?: string | null
|
||||
profileLabel?: string | null
|
||||
|
||||
@@ -889,6 +889,28 @@ export type BrowserLoadError = {
|
||||
validatedUrl: string
|
||||
}
|
||||
|
||||
export type BrowserCertificateFailure = {
|
||||
challengeId: string
|
||||
browserPageId: string
|
||||
errorCode: number | null
|
||||
error: string
|
||||
origin: string
|
||||
displayHost: string
|
||||
canProceed: boolean
|
||||
observedAt: number
|
||||
}
|
||||
|
||||
export type BrowserCertificateProceedFailureReason =
|
||||
| 'expired'
|
||||
| 'changed'
|
||||
| 'ineligible'
|
||||
| 'missing'
|
||||
| 'navigated'
|
||||
|
||||
export type BrowserCertificateProceedResult =
|
||||
| { ok: true }
|
||||
| { ok: false; reason: BrowserCertificateProceedFailureReason }
|
||||
|
||||
// Why: BrowserPage persists the active viewport preset so CDP emulation can be
|
||||
// reapplied on reload/navigation without the user re-picking from the toolbar.
|
||||
export type BrowserViewportPresetId =
|
||||
|
||||
Reference in New Issue
Block a user