feat(desktop): connect the external backend ACP socket to the redirect-resolved backend URL (#11829)

This commit is contained in:
Alex Hancock
2026-09-15 13:52:36 -04:00
committed by GitHub
parent 53672c3f14
commit bfe2d996d2
9 changed files with 602 additions and 309 deletions
+1
View File
@@ -91,6 +91,7 @@ module.exports = [
ReadableStreamDefaultController: 'readonly',
WritableStream: 'readonly',
AbortController: 'readonly',
AbortSignal: 'readonly',
RequestCredentials: 'readonly',
HeadersInit: 'readonly',
KeyboardEvent: 'readonly',
-21
View File
@@ -1,7 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
acpHttpUrlFromHttpBase,
acpWebSocketUrlFromHttpBase,
httpBaseFromAcpWebSocketUrl,
isLoopbackAcpWebSocketUrl,
normalizeAcpHttpBaseUrl,
@@ -110,24 +109,4 @@ describe('HTTP endpoint URLs from ACP HTTP base URLs', () => {
'https://example.com/goose/acp'
);
});
it('adds ACP query tokens when provided', () => {
expect(acpHttpUrlFromHttpBase('https://example.com/goose', 'test secret')).toBe(
'https://example.com/goose/acp?token=test+secret'
);
});
});
describe('acpWebSocketUrlFromHttpBase', () => {
it('derives WSS ACP URLs from HTTPS base URLs', () => {
expect(acpWebSocketUrlFromHttpBase('https://example.com/goose', 'secret')).toBe(
'wss://example.com/goose/acp?token=secret'
);
});
it('derives WS ACP URLs from HTTP base URLs', () => {
expect(acpWebSocketUrlFromHttpBase('http://127.0.0.1:1234', 'secret')).toBe(
'ws://127.0.0.1:1234/acp?token=secret'
);
});
});
+2 -15
View File
@@ -69,19 +69,6 @@ export function statusHttpUrlFromHttpBase(rawBaseUrl: string): string {
return httpEndpointUrlFromHttpBase(rawBaseUrl, 'status');
}
export function acpHttpUrlFromHttpBase(rawBaseUrl: string, token?: string): string {
const url = new URL(httpEndpointUrlFromHttpBase(rawBaseUrl, 'acp'));
if (token) {
url.searchParams.set('token', token);
}
return url.toString();
}
export function acpWebSocketUrlFromHttpBase(rawBaseUrl: string, token: string): string {
const baseUrl = normalizeAcpHttpBaseUrl(rawBaseUrl);
const url = new URL(baseUrl);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.pathname = `${url.pathname.replace(/\/+$/, '')}/acp`;
url.searchParams.set('token', token);
return url.toString();
export function acpHttpUrlFromHttpBase(rawBaseUrl: string): string {
return httpEndpointUrlFromHttpBase(rawBaseUrl, 'acp');
}
-97
View File
@@ -1,97 +0,0 @@
import { describe, expect, it, vi } from 'vitest';
import { checkBackendStatus } from './backendStatus';
type FetchInput = Parameters<typeof globalThis.fetch>[0];
const fetchInputUrl = (input: FetchInput): string => {
if (typeof input === 'string') {
return input;
}
if (input instanceof URL) {
return input.toString();
}
return input.url;
};
describe('checkBackendStatus', () => {
it('checks /status and validates the secret against /acp', async () => {
const fetch = vi.fn(async (input: FetchInput) => {
const url = fetchInputUrl(input);
if (url === 'https://example.com/goose/status') {
return new Response(null, { status: 200 });
}
if (url === 'https://example.com/goose/acp?token=test-secret') {
return new Response(null, { status: 406 });
}
throw new Error(`Unexpected URL: ${url}`);
});
await expect(
checkBackendStatus({
baseUrl: 'https://example.com/goose',
serverSecret: 'test-secret',
fetch,
})
).resolves.toMatchObject({ ok: true, failure: null });
expect(fetch.mock.calls.map(([input]) => fetchInputUrl(input))).toEqual([
'https://example.com/goose/status',
'https://example.com/goose/acp?token=test-secret',
]);
});
it('reports the rejected secret without retrying', async () => {
const fetch = vi.fn(async (input: FetchInput) => {
const url = fetchInputUrl(input);
if (url === 'https://example.com/status') {
return new Response(null, { status: 200 });
}
if (url === 'https://example.com/acp?token=wrong-secret') {
return new Response(null, { status: 401 });
}
throw new Error(`Unexpected URL: ${url}`);
});
const result = await checkBackendStatus({
baseUrl: 'https://example.com',
serverSecret: 'wrong-secret',
fetch,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('Secret key: The backend rejected the secret key (HTTP 401)');
expect(fetch).toHaveBeenCalledTimes(2);
});
it('reports an unusable URL without any request', async () => {
const fetch = vi.fn();
const result = await checkBackendStatus({
baseUrl: 'https://example.com/acp',
serverSecret: 'test-secret',
fetch,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('URL:');
expect(fetch).not.toHaveBeenCalled();
});
it('stops retrying a fatal network failure', async () => {
const fetch = vi.fn(async () => {
throw new Error('net::ERR_NAME_NOT_RESOLVED');
});
const result = await checkBackendStatus({
baseUrl: 'https://nope.example.com',
serverSecret: 'test-secret',
fetch,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('ERR_NAME_NOT_RESOLVED');
expect(fetch).toHaveBeenCalledTimes(1);
});
});
-161
View File
@@ -1,161 +0,0 @@
import {
acpHttpUrlFromHttpBase,
normalizeAcpHttpBaseUrl,
statusHttpUrlFromHttpBase,
} from './acp/url';
const RETRY_BUDGET_MS = 15000;
const RETRY_INTERVAL_MS = 250;
const PROBE_TIMEOUT_MS = 5000;
const FATAL_ERROR_PATTERN = /panicked at|RUST_BACKTRACE|fatal error/i;
const FATAL_NETWORK_PATTERN = /NAME_NOT_RESOLVED|CERT|SSL|CLIENT_AUTH|INVALID_URL|UNSAFE_PORT/;
export interface BackendCheckStep {
name: string;
ok: boolean;
detail: string;
}
export interface BackendCheckResult {
ok: boolean;
steps: BackendCheckStep[];
failure: string | null;
}
export interface BackendCheckParams {
baseUrl: string;
serverSecret: string;
fetch: typeof globalThis.fetch;
errorLog?: string[];
}
interface Probe {
ok: boolean;
detail: string;
retryable: boolean;
}
const delay = (timeoutMs: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, timeoutMs));
const errorText = (error: unknown): string =>
error instanceof Error ? error.message : String(error);
const proxyNote = (response: Response): string => {
const doormanError = response.headers.get('x-sq-cf-doorman-error');
return doormanError && doormanError !== 'none'
? ` A proxy in front of the backend reported "${doormanError}".`
: '';
};
const request = async (
fetch: typeof globalThis.fetch,
url: string,
init: Parameters<typeof globalThis.fetch>[1],
expect: (response: Response) => Probe
): Promise<Probe> => {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS);
try {
return expect(await fetch(url, { ...init, signal: controller.signal }));
} catch (error) {
const detail = errorText(error);
return { ok: false, detail, retryable: !FATAL_NETWORK_PATTERN.test(detail) };
} finally {
clearTimeout(timeout);
}
};
const probeStatus = (
fetch: typeof globalThis.fetch,
baseUrl: string,
secret: string
): Promise<Probe> =>
request(
fetch,
statusHttpUrlFromHttpBase(baseUrl),
{ headers: { 'X-Secret-Key': secret } },
(response) =>
response.ok
? { ok: true, detail: `GET /status returned ${response.status}.`, retryable: false }
: {
ok: false,
detail: `GET /status returned ${response.status} ${response.statusText}.${proxyNote(response)}`,
retryable: response.status >= 500,
}
);
const probeSecret = (
fetch: typeof globalThis.fetch,
baseUrl: string,
secret: string
): Promise<Probe> =>
request(fetch, acpHttpUrlFromHttpBase(baseUrl, secret), {}, (response) => {
if (response.status === 406) {
return { ok: true, detail: 'The backend accepted the secret key.', retryable: false };
}
if (response.status === 401 || response.status === 403) {
return {
ok: false,
detail: `The backend rejected the secret key (HTTP ${response.status}). It must match GOOSE_SERVER__SECRET_KEY on the backend.${proxyNote(response)}`,
retryable: false,
};
}
return {
ok: false,
detail: `GET /acp returned ${response.status} ${response.statusText}, expected 406.${proxyNote(response)}`,
retryable: response.status >= 500,
};
});
export const isFatalError = (line: string): boolean => FATAL_ERROR_PATTERN.test(line);
export const checkBackendStatus = async ({
baseUrl,
serverSecret,
fetch,
errorLog = [],
}: BackendCheckParams): Promise<BackendCheckResult> => {
const steps: BackendCheckStep[] = [];
const run = async (name: string, probe: () => Promise<Probe>): Promise<boolean> => {
const deadline = Date.now() + RETRY_BUDGET_MS;
let result = await probe();
while (
!result.ok &&
result.retryable &&
Date.now() < deadline &&
!errorLog.some(isFatalError)
) {
await delay(RETRY_INTERVAL_MS);
result = await probe();
}
steps.push({ name, ok: result.ok, detail: result.detail });
return result.ok;
};
let normalizedBaseUrl = '';
try {
normalizedBaseUrl = normalizeAcpHttpBaseUrl(baseUrl);
steps.push({ name: 'URL', ok: true, detail: normalizedBaseUrl });
} catch (error) {
steps.push({ name: 'URL', ok: false, detail: errorText(error) });
}
if (normalizedBaseUrl) {
const reachable = await run('Reachable', () =>
probeStatus(fetch, normalizedBaseUrl, serverSecret)
);
if (reachable) {
await run('Secret key', () => probeSecret(fetch, normalizedBaseUrl, serverSecret));
}
}
const failed = steps.find((step) => !step.ok);
return {
ok: !failed,
steps,
failure: failed ? `${failed.name}: ${failed.detail}`.trim() : null,
};
};
+16 -11
View File
@@ -26,13 +26,13 @@ import path from 'node:path';
import os from 'node:os';
import { execFileSync, spawn, execFile } from 'child_process';
import 'dotenv/config';
import { checkBackendStatus } from './backendStatus';
import { connectRemoteBackend } from './remoteBackends';
import { installBackendCertificateVerifiers } from './backendCertificateVerifier';
import { configureProxy } from './proxy';
import { startGooseServe } from './gooseServe';
import { getLoginShellPath } from './loginShellPath';
import { GooseServeLeaseRegistry, type GooseServeLease } from './gooseServeLeaseRegistry';
import { acpWebSocketUrlFromHttpBase, normalizeAcpHttpBaseUrl } from './acp/url';
import { normalizeAcpHttpBaseUrl } from './acp/url';
import { expandTilde, sanitizeGoosePathRoot } from './utils/pathUtils';
import log from './utils/logger';
import { ensureWinShims } from './utils/winShims';
@@ -59,7 +59,7 @@ import type { GooseApp } from './types/apps';
import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';
import { WEB_PROTOCOLS } from './utils/urlSecurity';
import { openExternalUrl } from './utils/openExternalUrl';
import { buildCSP } from './utils/csp';
import { buildCSP, leaseBackendOrigin } from './utils/csp';
import { resolveWorkingDir } from './utils/workingDir';
import {
DesktopFileAccess,
@@ -1116,10 +1116,10 @@ const createChat = async (
);
}
const externalBackendCheck = await checkBackendStatus({
const externalBackendCheck = await connectRemoteBackend({
baseUrl: externalBaseUrl,
serverSecret,
fetch: net.fetch as unknown as typeof globalThis.fetch,
pinnedHostname: externalBackend.certFingerprint ? externalBase.hostname : null,
});
if (!externalBackendCheck.ok) {
externalCertificateTrust?.release();
@@ -1150,13 +1150,18 @@ const createChat = async (
return;
}
const resolvedAcpUrl = externalBackendCheck.acpUrl;
if (!resolvedAcpUrl) {
throw new Error('External backend check did not resolve an ACP endpoint');
}
const originLease = leaseBackendOrigin(resolvedAcpUrl);
const leaseCertificateTrust = externalCertificateTrust;
externalCertificateTrust = null;
gooseServeLease = gooseServeLeases.createExternal(
acpWebSocketUrlFromHttpBase(externalBaseUrl, serverSecret),
serverSecret,
leaseCertificateTrust ? async () => leaseCertificateTrust.release() : undefined
);
gooseServeLease = gooseServeLeases.createExternal(resolvedAcpUrl, serverSecret, async () => {
originLease.release();
leaseCertificateTrust?.release();
});
} catch (error) {
externalCertificateTrust?.release();
log.error('External ACP backend is misconfigured', error);
@@ -2478,7 +2483,7 @@ async function appMain() {
// Add CSP headers to all sessions, recomputed on every response so external
// backend settings take effect without restarting the app.
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
rendererSession.webRequest.onHeadersReceived((details, callback) => {
const currentSettings = getSettings();
callback({
responseHeaders: {
+174
View File
@@ -0,0 +1,174 @@
import { describe, expect, it, vi } from 'vitest';
import { connectRemoteBackend, type RemoteBackendParams } from './remoteBackends';
type HopRequest = NonNullable<RemoteBackendParams['request']>;
const respond = (status: number, location?: string) => ({
status,
statusText: '',
header: () => null,
location: location ?? null,
});
describe('connectRemoteBackend', () => {
it('checks /status and validates the secret against /acp', async () => {
const request = vi.fn(async (url: string) => {
if (url === 'https://example.com/goose/status') {
return respond(200);
}
if (url === 'https://example.com/goose/acp') {
return respond(406);
}
throw new Error(`Unexpected URL: ${url}`);
});
const result = await connectRemoteBackend({
baseUrl: 'https://example.com/goose',
serverSecret: 'test-secret',
request,
});
expect(result).toMatchObject({
ok: true,
failure: null,
acpUrl: 'wss://example.com/goose/acp?token=test-secret',
});
});
it('follows redirects and keeps the resolved ACP endpoint', async () => {
const request = vi.fn(async (url: string) => {
if (url === 'https://example.com/status') {
return respond(302, 'https://backend.example.com/goose/status');
}
if (url === 'https://backend.example.com/goose/status') {
return respond(200);
}
if (url === 'https://backend.example.com/goose/acp') {
return respond(302, 'https://backend.example.com/socket?tenant=x');
}
if (url === 'https://backend.example.com/socket?tenant=x') {
return respond(406);
}
throw new Error(`Unexpected URL: ${url}`);
});
const result = await connectRemoteBackend({
baseUrl: 'https://example.com',
serverSecret: 'test-secret',
request,
});
expect(result.ok).toBe(true);
expect(result.acpUrl).toBe('wss://backend.example.com/socket?tenant=x&token=test-secret');
});
it('sends the secret only to the resolved ACP endpoint', async () => {
const request = vi.fn<HopRequest>(async (url) => {
if (url === 'https://example.com/status') {
return respond(200);
}
if (url === 'https://example.com/acp') {
return respond(302, 'https://backend.example.com/acp');
}
if (url === 'https://backend.example.com/acp') {
return respond(406);
}
throw new Error(`Unexpected URL: ${url}`);
});
await connectRemoteBackend({
baseUrl: 'https://example.com',
serverSecret: 'test-secret',
request,
});
const secretRecipients = request.mock.calls
.filter(([, init]) => init.headers?.['X-Secret-Key'])
.map(([url]) => url);
expect(secretRecipients).toEqual(['https://backend.example.com/acp']);
});
it('rejects an HTTPS to HTTP redirect', async () => {
const request = vi.fn(async () => respond(302, 'http://backend.example.com/status'));
const result = await connectRemoteBackend({
baseUrl: 'https://example.com',
serverSecret: 'test-secret',
request,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('Redirect from HTTPS to HTTP is not allowed');
expect(request).toHaveBeenCalledTimes(1);
});
it('rejects a cross-host redirect when a certificate fingerprint is pinned', async () => {
const request = vi.fn(async () => respond(302, 'https://backend.example.com/status'));
const result = await connectRemoteBackend({
baseUrl: 'https://example.com',
serverSecret: 'test-secret',
request,
pinnedHostname: 'example.com',
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('certificate fingerprint is configured');
});
it('reports the rejected secret without retrying', async () => {
const request = vi.fn(async (url: string) => {
if (url === 'https://example.com/status') {
return respond(200);
}
if (url === 'https://example.com/acp') {
return respond(401);
}
throw new Error(`Unexpected URL: ${url}`);
});
const result = await connectRemoteBackend({
baseUrl: 'https://example.com',
serverSecret: 'wrong-secret',
request,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('Secret key: The backend rejected the secret key (HTTP 401)');
expect(request).toHaveBeenCalledTimes(3);
});
it('reports an unusable URL without any request', async () => {
const request = vi.fn();
const result = await connectRemoteBackend({
baseUrl: 'https://example.com/acp',
serverSecret: 'test-secret',
request,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('URL:');
expect(request).not.toHaveBeenCalled();
});
it('stops retrying a fatal network failure', async () => {
const request = vi.fn(async () => {
throw new Error('net::ERR_NAME_NOT_RESOLVED');
});
const result = await connectRemoteBackend({
baseUrl: 'https://nope.example.com',
serverSecret: 'test-secret',
request,
});
expect(result.ok).toBe(false);
expect(result.failure).toContain('ERR_NAME_NOT_RESOLVED');
expect(request).toHaveBeenCalledTimes(1);
});
});
+377
View File
@@ -0,0 +1,377 @@
import { net } from 'electron';
import {
acpHttpUrlFromHttpBase,
normalizeAcpHttpBaseUrl,
statusHttpUrlFromHttpBase,
} from './acp/url';
const RETRY_BUDGET_MS = 15000;
const RETRY_INTERVAL_MS = 250;
const PROBE_TIMEOUT_MS = 5000;
const MAX_REDIRECT_HOPS = 20;
const FATAL_ERROR_PATTERN = /panicked at|RUST_BACKTRACE|fatal error/i;
const FATAL_NETWORK_PATTERN = /NAME_NOT_RESOLVED|CERT|SSL|CLIENT_AUTH|INVALID_URL|UNSAFE_PORT/;
export interface RemoteBackendStep {
name: string;
ok: boolean;
detail: string;
}
export interface RemoteBackendConnection {
ok: boolean;
steps: RemoteBackendStep[];
failure: string | null;
acpUrl: string | null;
}
export interface RemoteBackendParams {
baseUrl: string;
serverSecret: string;
pinnedHostname?: string | null;
errorLog?: string[];
request?: HopRequest;
}
interface HopInit {
headers?: Record<string, string>;
signal?: AbortSignal;
}
interface Hop {
status: number;
statusText: string;
header(name: string): string | null;
location: string | null;
}
type HopRequest = (url: string, init: HopInit) => Promise<Hop>;
const headerValue = (headers: Record<string, string | string[]>, name: string): string | null => {
const value = headers[name.toLowerCase()];
if (value === undefined) {
return null;
}
return Array.isArray(value) ? (value[0] ?? null) : value;
};
// net.fetch reports an incorrect Response.url, so each hop is issued separately
// with net.request and this module decides whether to follow it.
const netHopRequest: HopRequest = (url, init) =>
new Promise<Hop>((resolve, reject) => {
const request = net.request({ method: 'GET', url, redirect: 'manual', credentials: 'omit' });
let settled = false;
const abort = () => request.abort();
init.signal?.addEventListener('abort', abort, { once: true });
const settle = (finish: () => void) => {
if (settled) {
return;
}
settled = true;
init.signal?.removeEventListener('abort', abort);
finish();
};
// A manual redirect is not followed, so the request is cancelled after this
// event and only the 'abort' event follows.
request.on('redirect', (statusCode, _method, redirectUrl, responseHeaders) => {
settle(() =>
resolve({
status: statusCode,
statusText: '',
header: (name) => headerValue(responseHeaders, name),
location: redirectUrl,
})
);
request.abort();
});
request.on('response', (response) => {
response.on('data', () => undefined);
response.on('end', () =>
settle(() =>
resolve({
status: response.statusCode,
statusText: response.statusMessage,
header: (name) => headerValue(response.headers, name),
location: null,
})
)
);
response.on('error', (error: Error) => settle(() => reject(error)));
});
request.on('error', (error) => settle(() => reject(error)));
request.on('abort', () => settle(() => reject(new Error('Request aborted'))));
for (const [name, value] of Object.entries(init.headers ?? {})) {
request.setHeader(name, value);
}
request.end();
});
class RedirectError extends Error {}
const isRedirect = (status: number): boolean =>
status === 301 || status === 302 || status === 303 || status === 307 || status === 308;
const checkHop = (from: URL, to: URL, pinnedHostname: string | null): void => {
if (to.protocol !== 'http:' && to.protocol !== 'https:') {
throw new RedirectError(`Redirect to ${to.protocol} is not allowed, only http: and https:.`);
}
if (from.protocol === 'https:' && to.protocol === 'http:') {
throw new RedirectError(
`Redirect from HTTPS to HTTP is not allowed (${from.origin} to ${to.origin}).`
);
}
if (pinnedHostname && to.hostname.toLowerCase() !== pinnedHostname) {
throw new RedirectError(
`Redirect to ${to.hostname} is not allowed because a certificate fingerprint is configured for ${pinnedHostname}.`
);
}
};
const resolveRedirects = async (
request: HopRequest,
startUrl: string,
pinnedHostname: string | null,
init: HopInit
): Promise<{ url: string; hop: Hop }> => {
let url = new URL(startUrl);
for (let hopCount = 0; ; hopCount += 1) {
const hop = await request(url.toString(), init);
if (!isRedirect(hop.status)) {
return { url: url.toString(), hop };
}
if (hopCount >= MAX_REDIRECT_HOPS) {
throw new RedirectError(`Exceeded ${MAX_REDIRECT_HOPS} redirects starting at ${startUrl}.`);
}
if (!hop.location) {
throw new RedirectError(`Redirect from ${url.toString()} has no Location header.`);
}
const next = new URL(hop.location, url);
checkHop(url, next, pinnedHostname);
url = next;
}
};
interface Probe {
ok: boolean;
detail: string;
retryable: boolean;
resolvedUrl?: string;
}
const delay = (timeoutMs: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, timeoutMs));
const errorText = (error: unknown): string =>
error instanceof Error ? error.message : String(error);
const proxyNote = (hop: Hop): string => {
const doormanError = hop.header('x-sq-cf-doorman-error');
return doormanError && doormanError !== 'none'
? ` A proxy in front of the backend reported "${doormanError}".`
: '';
};
// Redirects are resolved anonymously; only the resolved URL is re-requested
// with the secret, so no intermediate origin ever sees it.
const probe = async (
request: HopRequest,
url: string,
pinnedHostname: string | null,
credentials: Record<string, string> | null,
expect: (hop: Hop, resolvedUrl: string) => Probe
): Promise<Probe> => {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS);
try {
const { url: resolvedUrl, hop } = await resolveRedirects(request, url, pinnedHostname, {
signal: controller.signal,
});
const finalHop = credentials
? await request(resolvedUrl, { headers: credentials, signal: controller.signal })
: hop;
return expect(finalHop, resolvedUrl);
} catch (error) {
const detail = errorText(error);
return {
ok: false,
detail,
retryable: !(error instanceof RedirectError) && !FATAL_NETWORK_PATTERN.test(detail),
};
} finally {
clearTimeout(timeout);
}
};
const probeStatus = (
request: HopRequest,
baseUrl: string,
pinnedHostname: string | null
): Promise<Probe> =>
probe(request, statusHttpUrlFromHttpBase(baseUrl), pinnedHostname, null, (hop, resolvedUrl) =>
hop.status >= 200 && hop.status < 300
? {
ok: true,
detail: `GET /status returned ${hop.status}.`,
retryable: false,
resolvedUrl,
}
: {
ok: false,
detail: `GET /status returned ${hop.status} ${hop.statusText}.${proxyNote(hop)}`,
retryable: hop.status >= 500,
}
);
const probeAcp = (
request: HopRequest,
baseUrl: string,
secret: string,
pinnedHostname: string | null
): Promise<Probe> =>
probe(
request,
acpHttpUrlFromHttpBase(baseUrl),
pinnedHostname,
{ 'X-Secret-Key': secret },
(hop, resolvedUrl) => {
if (hop.status === 406) {
return {
ok: true,
detail: 'The backend accepted the secret key.',
retryable: false,
resolvedUrl,
};
}
if (hop.status === 401 || hop.status === 403) {
return {
ok: false,
detail: `The backend rejected the secret key (HTTP ${hop.status}). It must match GOOSE_SERVER__SECRET_KEY on the backend.${proxyNote(hop)}`,
retryable: false,
};
}
return {
ok: false,
detail: `GET /acp returned ${hop.status} ${hop.statusText}, expected 406.${proxyNote(hop)}`,
retryable: hop.status >= 500,
};
}
);
const baseUrlFromStatusUrl = (statusUrl: string): string | null => {
const url = new URL(statusUrl);
const pathname = url.pathname.replace(/\/+$/, '');
return pathname.endsWith('/status')
? `${url.origin}${pathname.slice(0, -'/status'.length)}`
: null;
};
// The resolved endpoint is kept whole, including any query parameters a proxy
// added, so the socket connects exactly where the probe succeeded.
const acpWebSocketUrl = (acpUrl: string, secret: string): string => {
const url = new URL(acpUrl);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.hash = '';
url.searchParams.set('token', secret);
return url.toString();
};
const isFatalError = (line: string): boolean => FATAL_ERROR_PATTERN.test(line);
export const connectRemoteBackend = async ({
baseUrl,
serverSecret,
pinnedHostname = null,
errorLog = [],
request = netHopRequest,
}: RemoteBackendParams): Promise<RemoteBackendConnection> => {
const steps: RemoteBackendStep[] = [];
const pin = pinnedHostname?.toLowerCase() ?? null;
const run = async (name: string, attempt: () => Promise<Probe>): Promise<Probe> => {
const deadline = Date.now() + RETRY_BUDGET_MS;
let result = await attempt();
while (
!result.ok &&
result.retryable &&
Date.now() < deadline &&
!errorLog.some(isFatalError)
) {
await delay(RETRY_INTERVAL_MS);
result = await attempt();
}
steps.push({ name, ok: result.ok, detail: result.detail });
return result;
};
let normalizedBaseUrl = '';
try {
normalizedBaseUrl = normalizeAcpHttpBaseUrl(baseUrl);
steps.push({ name: 'URL', ok: true, detail: normalizedBaseUrl });
} catch (error) {
steps.push({ name: 'URL', ok: false, detail: errorText(error) });
}
const resolve = async (): Promise<string | null> => {
if (!normalizedBaseUrl) {
return null;
}
const reachable = await run('Reachable', () => probeStatus(request, normalizedBaseUrl, pin));
if (!reachable.ok || !reachable.resolvedUrl) {
return null;
}
const resolvedBaseUrl = baseUrlFromStatusUrl(reachable.resolvedUrl);
if (!resolvedBaseUrl) {
steps.push({
name: 'Redirect',
ok: false,
detail: `/status resolved to ${reachable.resolvedUrl}, so the ACP path cannot be derived from it.`,
});
return null;
}
if (resolvedBaseUrl !== normalizedBaseUrl) {
steps.push({ name: 'Redirect', ok: true, detail: `Followed to ${resolvedBaseUrl}.` });
}
const accepted = await run('Secret key', () =>
probeAcp(request, resolvedBaseUrl, serverSecret, pin)
);
if (!accepted.ok || !accepted.resolvedUrl) {
return null;
}
const statusOrigin = new URL(resolvedBaseUrl).origin;
const acpOrigin = new URL(accepted.resolvedUrl).origin;
if (statusOrigin !== acpOrigin) {
steps.push({
name: 'Redirect',
ok: false,
detail: `/status and /acp resolved to different origins (${statusOrigin} and ${acpOrigin}).`,
});
return null;
}
return acpWebSocketUrl(accepted.resolvedUrl, serverSecret);
};
const acpUrl = await resolve();
const failed = steps.find((step) => !step.ok);
return {
ok: !failed,
steps,
failure: failed ? `${failed.name}: ${failed.detail}`.trim() : null,
acpUrl: failed ? null : acpUrl,
};
};
+32 -4
View File
@@ -15,8 +15,31 @@ const DEFAULT_CONNECT_SOURCES = [
'https://objects.githubusercontent.com',
];
export interface BackendOriginLease {
release: () => void;
}
interface LeasedBackendOrigin {
origin: string;
insecure: boolean;
}
const leasedBackendOrigins = new Set<LeasedBackendOrigin>();
// A redirected backend serves ACP from an origin the settings do not name, so
// the renderer needs it in connect-src for as long as a window uses it.
export function leaseBackendOrigin(acpUrl: string): BackendOriginLease {
const url = new URL(acpUrl);
const leased: LeasedBackendOrigin = { origin: url.origin, insecure: url.protocol === 'ws:' };
leasedBackendOrigins.add(leased);
return { release: () => leasedBackendOrigins.delete(leased) };
}
export function buildConnectSrc(externalBackend?: ExternalBackendConfig): string {
const sources = [...DEFAULT_CONNECT_SOURCES];
const sources = [
...DEFAULT_CONNECT_SOURCES,
...[...leasedBackendOrigins].map((leased) => leased.origin),
];
if (externalBackend?.enabled && externalBackend.url) {
try {
@@ -36,14 +59,19 @@ export function buildConnectSrc(externalBackend?: ExternalBackendConfig): string
* Returns true when upgrade-insecure-requests should be included in the CSP.
*
* The directive is omitted when the user has configured an external backend
* that uses plain HTTP, because Chromium would silently rewrite those
* requests to HTTPS. The remote server typically does not speak TLS, so the
* upgraded requests fail with "Failed to fetch".
* that uses plain HTTP, or when a leased backend was resolved to plain HTTP,
* because Chromium would silently rewrite those requests to HTTPS. The remote
* server typically does not speak TLS, so the upgraded requests fail with
* "Failed to fetch".
*
* Loopback addresses (127.0.0.1 / localhost) are exempt from the upgrade
* per the CSP spec, which is why the built-in local backend is unaffected.
*/
export function shouldUpgradeInsecureRequests(externalBackend?: ExternalBackendConfig): boolean {
if ([...leasedBackendOrigins].some((leased) => leased.insecure)) {
return false;
}
if (!externalBackend?.enabled || !externalBackend.url) {
return true;
}