From f905ff0b55650dddbb01bffd719047f4c6b5b3b6 Mon Sep 17 00:00:00 2001 From: vaakx-dev Date: Wed, 25 Nov 2026 00:21:31 +1100 Subject: [PATCH] fix(pcs): pictures load in the Android app The app's WebView upgrades plain-http pictures on its https page and the upgrade fails, so every signed media link to the PC's http route showed Image unavailable. Links to the home PC now use the page's own origin, which the app proxies to the PC across its routes. Dev servers read the connected route instead of wire.url to pick the address this device reaches. --- package.json | 2 +- plugins/agent/dev-servers/src/web/index.ts | 2 +- plugins/agent/dev-servers/src/web/open.ts | 33 ++++++--- plugins/devices/pcs/src/page/app.ts | 2 + plugins/devices/pcs/src/wire/url.ts | 18 ++--- plugins/ui/chat/chat/test/web/app.ts | 83 ++++++++++++++++++++++ 6 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 plugins/ui/chat/chat/test/web/app.ts diff --git a/package.json b/package.json index 81a6436a5..78d3ab16b 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "bench:gate": "bun apps/bench/src/gate/main.ts", "test": "bun test --parallel=4 --timeout 60000 ./apps ./packages ./plugins", "test:upgrade": "bun test --timeout 1200000 ./plugins/host/daemon/test/upgrade/upgrade.ts", - "test:phone": "bun test --parallel --timeout 30000 ./plugins/ui/look/kit/test/gallery/gallery.ts ./plugins/ui/shell/shell/test/phone/window.ts ./plugins/ui/shell/shell/test/phone/drawers.ts ./plugins/ui/shell/shell/test/phone/keyboard.ts ./plugins/core/features/test/web/matrix.ts ./plugins/ui/chat/chat/test/web/follow.ts ./plugins/ui/chat/chat/test/web/catch-up.ts ./plugins/ui/chat/chat/test/web/draft.ts ./plugins/ui/chat/chat/test/web/early.ts ./plugins/ui/chat/chat/test/web/reply.ts ./plugins/ui/chat/chat/test/web/upload.ts ./plugins/bots/views/test/web/history.ts ./plugins/bots/views/test/web/tabs.ts ./plugins/core/server/test/web/hello.ts ./plugins/ui/shell/shell/test/web/parts.ts ./plugins/ui/chat/html/test/web/zoom.ts" + "test:phone": "bun test --parallel --timeout 30000 ./plugins/ui/look/kit/test/gallery/gallery.ts ./plugins/ui/shell/shell/test/phone/window.ts ./plugins/ui/shell/shell/test/phone/drawers.ts ./plugins/ui/shell/shell/test/phone/keyboard.ts ./plugins/core/features/test/web/matrix.ts ./plugins/ui/chat/chat/test/web/follow.ts ./plugins/ui/chat/chat/test/web/catch-up.ts ./plugins/ui/chat/chat/test/web/draft.ts ./plugins/ui/chat/chat/test/web/early.ts ./plugins/ui/chat/chat/test/web/reply.ts ./plugins/ui/chat/chat/test/web/upload.ts ./plugins/ui/chat/chat/test/web/app.ts ./plugins/bots/views/test/web/history.ts ./plugins/bots/views/test/web/tabs.ts ./plugins/core/server/test/web/hello.ts ./plugins/ui/shell/shell/test/web/parts.ts ./plugins/ui/chat/html/test/web/zoom.ts" }, "devDependencies": { "@sand/boot": "workspace:*", diff --git a/plugins/agent/dev-servers/src/web/index.ts b/plugins/agent/dev-servers/src/web/index.ts index 37241bb28..4438ebda2 100644 --- a/plugins/agent/dev-servers/src/web/index.ts +++ b/plugins/agent/dev-servers/src/web/index.ts @@ -31,7 +31,7 @@ export default definePlugin({ const notices = toastsOf(ctx) const card = serversCard(ctx.kit, { servers: shownIn, - open: server => void notices.attempt(`Couldn't open localhost:${server.port}`, () => openServer(ctx.wire, server)), + open: server => void notices.attempt(`Couldn't open localhost:${server.port}`, () => openServer(ctx, server)), stop: server => void notices.attempt(`Couldn't stop localhost:${server.port}`, () => ctx.wire.call(devServerStop, { port: server.port, session: server.session }, server.device)), }) ctx.effect(() => ctx.inspector.row({ id: 'dev-servers', group: 'helpers', order: 70, shows: subject => !subject.draft && shownIn(subject).length > 0, render: card })) diff --git a/plugins/agent/dev-servers/src/web/open.ts b/plugins/agent/dev-servers/src/web/open.ts index 5550efe3f..242a4f0ab 100644 --- a/plugins/agent/dev-servers/src/web/open.ts +++ b/plugins/agent/dev-servers/src/web/open.ts @@ -1,8 +1,12 @@ -import type { Wire } from '@sand/devices-contract' -import { devicePaths } from '@sand/devices-contract' +import type { Pcs, Wire } from '@sand/devices-contract' import { devServerOpen, devServerRelay } from '../contract' import type { Shown } from './card' +interface Reach { + wire: Wire + pcs: Pcs +} + const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']) const hostnameOf = (link: string) => new URL(link).hostname @@ -23,18 +27,31 @@ const show = (tab: Window | null, link: string) => { else window.open(link, '_blank', 'noopener') } -const relayed = (wire: Wire, pc: string) => new URL(wire.url('/', pc)).pathname.startsWith(`${devicePaths.via}/`) +const routeTo = (pcs: Pcs, pc: string) => { + const status = pcs.get(pc)?.status + if (status?.phase !== 'connected') throw new Error(`${pcs.name(pc)} isn't connected`) + return status.route +} + +const addressOf = (pcs: Pcs, pc: string) => { + const { url } = routeTo(pcs, pc) + if (!url) throw new Error(`${pcs.name(pc)} isn't connected to this page directly`) + return url +} -const reachableLinks = async (wire: Wire, server: Shown) => { +const reachableLinks = async ({ wire, pcs }: Reach, server: Shown) => { const links = await wire.call(devServerOpen, { port: server.port, session: server.session }, server.device) - return relayed(wire, server.device) ? { links: await wire.call(devServerRelay, { pc: server.device, links }), pc: undefined } : { links, pc: server.device } + const { url, via } = routeTo(pcs, server.device) + if (url) return { links, address: url } + if (!via) throw new Error(`${pcs.name(server.device)} has no route this page can link to`) + return { links: await wire.call(devServerRelay, { pc: server.device, links }, via), address: addressOf(pcs, via) } } -export const openServer = async (wire: Wire, server: Shown) => { +export const openServer = async (reach: Reach, server: Shown) => { const tab = blankTab() try { - const { links, pc } = await reachableLinks(wire, server) - const link = linkFor(links, hostnameOf(wire.url('/', pc))) + const { links, address } = await reachableLinks(reach, server) + const link = linkFor(links, hostnameOf(address)) if (!link) throw new Error("sand on that PC doesn't listen on an address this device reaches") show(tab, link) } catch (error) { diff --git a/plugins/devices/pcs/src/page/app.ts b/plugins/devices/pcs/src/page/app.ts index d82c4aadc..9d513dbaa 100644 --- a/plugins/devices/pcs/src/page/app.ts +++ b/plugins/devices/pcs/src/page/app.ts @@ -37,6 +37,8 @@ export const thisApp = lazy((): AndroidApp | undefined => { return window.sandApp && start ? appOver(window.sandApp, start) : undefined }) +export const appProxyOrigin = () => (thisApp() ? location.origin : undefined) + const reachableUrls = (connection: PcConnection) => { const status = connection.status() const connected = status.phase === 'connected' ? [status.route.url] : [] diff --git a/plugins/devices/pcs/src/wire/url.ts b/plugins/devices/pcs/src/wire/url.ts index 0ddfff4d9..0dc731aae 100644 --- a/plugins/devices/pcs/src/wire/url.ts +++ b/plugins/devices/pcs/src/wire/url.ts @@ -1,5 +1,6 @@ import { PcUnavailable, viaPath } from '@sand/devices-contract' import type { Fleet } from '../fleet/fleet' +import { appProxyOrigin } from '../page/app' const routeTo = (fleet: Fleet, pc: string) => { const status = fleet.connection(pc).status() @@ -7,15 +8,14 @@ const routeTo = (fleet: Fleet, pc: string) => { return status.route } -const directBase = (fleet: Fleet, pc: string) => { - const { url } = routeTo(fleet, pc) - if (!url) throw new Error(`${fleet.nameOf(pc)} isn't connected to this page directly`) - return url -} +const directBase = (fleet: Fleet, pc: string) => (pc === fleet.home ? appProxyOrigin() : undefined) ?? routeTo(fleet, pc).url export const pcUrl = (fleet: Fleet, path: string, pc: string) => { - const route = routeTo(fleet, pc) - if (route.url) return new URL(path, route.url).href - if (!route.via) throw new Error(`${fleet.nameOf(pc)} has no route this page can link to`) - return new URL(viaPath(pc, path), directBase(fleet, route.via)).href + const direct = directBase(fleet, pc) + if (direct) return new URL(path, direct).href + const { via } = routeTo(fleet, pc) + if (!via) throw new Error(`${fleet.nameOf(pc)} has no route this page can link to`) + const relay = directBase(fleet, via) + if (!relay) throw new Error(`${fleet.nameOf(via)} isn't connected to this page directly`) + return new URL(viaPath(pc, path), relay).href } diff --git a/plugins/ui/chat/chat/test/web/app.ts b/plugins/ui/chat/chat/test/web/app.ts new file mode 100644 index 000000000..7ba2b654b --- /dev/null +++ b/plugins/ui/chat/chat/test/web/app.ts @@ -0,0 +1,83 @@ +import { afterAll, beforeAll, expect, test } from 'bun:test' +import { listen } from '@vaakx-dev/vrui' +import { chromium, type Browser, type BrowserContext, type Page, type Route, type WebSocketRoute } from 'playwright' +import { startSandbox } from './sandbox' + +const APP_ORIGIN = 'https://sand.localhost' +const LAN_ORIGIN = 'https://pc.test' + +let sand: Awaited> +let pc: URL +let browser: Browser + +beforeAll(async () => { + sand = await startSandbox([]) + pc = new URL(sand.url) + browser = await chromium.launch() +}, 60_000) + +afterAll(async () => { + await browser?.close() + await sand?.stop() +}) + +const onPc = (url: string) => { + const { pathname, search } = new URL(url) + return `${pc.origin}${pathname}${search}` +} + +const proxyToPc = async (route: Route) => { + await route.fulfill({ response: await route.fetch({ url: onPc(route.request().url()) }) }) +} + +const refuseImages = async (route: Route) => (route.request().resourceType() === 'image' ? route.abort() : proxyToPc(route)) + +const pipeToPc = (page: WebSocketRoute) => { + const socket = new WebSocket(onPc(page.url()).replace(/^http/, 'ws')) + socket.binaryType = 'arraybuffer' + const opened = new Promise(resolve => listen(socket, 'open', resolve)) + const stops = [ + listen(socket, 'message', ({ data }) => page.send(typeof data === 'string' ? data : Buffer.from(data))), + listen(socket, 'close', ({ code, reason }) => page.close({ code, reason })), + ] + page.onMessage(message => void opened.then(() => socket.send(typeof message === 'string' ? message : new Uint8Array(message)))) + page.onClose(() => { + stops.forEach(stop => stop()) + socket.close() + }) +} + +const androidApp = async (): Promise => { + const context = await browser.newContext({ serviceWorkers: 'block', viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }) + await context.addInitScript(routes => { + Object.assign(window, { sandApp: Object.assign(new EventTarget(), { postMessage: () => {} }), sandAppStart: { routes, push: false, versionCode: 1 } }) + }, [LAN_ORIGIN]) + await context.route(`${APP_ORIGIN}/**`, proxyToPc) + await context.route(`${LAN_ORIGIN}/**`, refuseImages) + await context.routeWebSocket(`${LAN_ORIGIN.replace(/^https/, 'wss')}/**`, pipeToPc) + return context +} + +const pictureOutcome = async (page: Page, name: string) => { + const outcome = await page.waitForFunction(alt => { + const shown = [...document.images].some(image => image.alt === alt && image.complete && image.naturalWidth > 0) + if (shown) return 'shown' + return document.body.innerText.includes('Image unavailable') && 'unavailable' + }, name) + return outcome.jsonValue() +} + +test('in the Android app, a chat picture loads through the page the app serves, since the WebView loads no pictures straight from the PC', async () => { + const context = await androidApp() + const page = await context.newPage() + await page.goto(`${APP_ORIGIN}/${pc.hash}`) + const box = page.getByRole('textbox').first() + await box.waitFor() + await page.locator('input[type=file]').first().setInputFiles({ name: 'tide.png', mimeType: 'image/png', buffer: await page.screenshot() }) + await box.fill('look at this') + await page.getByRole('button', { name: 'Send' }).click() + await page.waitForURL(/view=thread/) + await page.reload() + expect(await pictureOutcome(page, 'tide.png')).toBe('shown') + await context.close() +}, 60_000)