Skip to content

Commit bd398ed

Browse files
t3dotggclaude
andcommitted
fix(devices): Android fold controls work over T3 Connect
The device hub proxy no longer forwards x-forwarded-proto, so the hub stops rejecting every mutation from an HTTPS client with 403. A fold change that fails with an expired hub ticket renews the ticket and sends once more. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9c16146 commit bd398ed

6 files changed

Lines changed: 71 additions & 7 deletions

File tree

‎apps/server/src/device/DeviceHubProxy.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ const fixture = (
2525
) => {
2626
let finalized = 0;
2727
const requests: string[] = [];
28+
const headers: Array<Readonly<Record<string, string>>> = [];
2829
const client = HttpClient.make((request, _url, signal) =>
2930
Effect.gen(function* () {
3031
requests.push(request.url);
32+
headers.push(request.headers);
3133
signal.addEventListener("abort", () => {
3234
finalized++;
3335
});
@@ -61,7 +63,7 @@ const fixture = (
6163
{ disableLogger: true },
6264
);
6365
disposers.push(dispose);
64-
return { handler, requests, finalized: () => finalized };
66+
return { handler, requests, headers, finalized: () => finalized };
6567
};
6668

6769
describe("device hub proxy", () => {
@@ -131,6 +133,25 @@ describe("device hub proxy", () => {
131133
]);
132134
});
133135

136+
it("presents fold changes from an HTTPS client as same-origin hub requests", async () => {
137+
const { handler, headers } = fixture([AuthOrchestrationOperateScope]);
138+
const response = await handler(
139+
new Request(
140+
"https://relay.test/api/device-hub/vendor/serve-emu/api/fold?device=emulator-5554",
141+
{
142+
method: "POST",
143+
headers: { origin: "https://relay.test", "x-forwarded-proto": "https" },
144+
body: '{"posture":"opened"}',
145+
},
146+
),
147+
);
148+
expect(response.status).toBe(200);
149+
await response.text();
150+
expect(headers).toHaveLength(1);
151+
expect(headers[0]?.origin).toBe("http://hub.test");
152+
expect(headers[0]).not.toHaveProperty("x-forwarded-proto");
153+
});
154+
134155
it("never forwards the vendor shell endpoint", async () => {
135156
const { handler, requests } = fixture([AuthOrchestrationOperateScope]);
136157
expect(

‎apps/server/src/device/DeviceHubProxy.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const DROPPED_REQUEST_HEADERS = new Set([
5757
"dpop",
5858
"content-length",
5959
"accept-encoding",
60+
// Describes the client's HTTPS hop to T3. The hub builds its own origin from it and then
61+
// rejects the loopback Origin set below, so every mutation fails over T3 Connect.
62+
"x-forwarded-proto",
6063
]);
6164

6265
const isWebSocketUpgrade = (request: HttpServerRequest.HttpServerRequest) =>

‎apps/web/src/components/device/DeviceAndroidFoldControls.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from "react";
33
import { Button } from "~/components/ui/button";
44
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
55
import {
6+
AndroidFoldError,
67
readAndroidFold,
78
setAndroidFold,
89
type AndroidFoldPosture,
@@ -18,6 +19,7 @@ export function DeviceAndroidFoldControls(props: {
1819
readonly screenWidth: number | undefined;
1920
readonly screenHeight: number | undefined;
2021
readonly onFoldAngle: (angle: number | null) => void;
22+
readonly renewAccess: () => Promise<DeviceHubAccess | null>;
2123
}) {
2224
const [fold, setFold] = useState<AndroidFoldState | null>(null);
2325
const [pending, setPending] = useState(false);
@@ -47,8 +49,10 @@ export function DeviceAndroidFoldControls(props: {
4749
);
4850
}
4951
})
50-
.catch(() => {
51-
if (!controller.signal.aborted) retry = window.setTimeout(read, 3_000);
52+
.catch((cause: unknown) => {
53+
// Retrying an expired ticket cannot succeed. New access re-runs this effect.
54+
const expired = cause instanceof AndroidFoldError && cause.status === 401;
55+
if (!controller.signal.aborted && !expired) retry = window.setTimeout(read, 3_000);
5256
});
5357
};
5458
read();
@@ -68,7 +72,16 @@ export function DeviceAndroidFoldControls(props: {
6872
props.onFoldAngle(posture === "closed" ? 0 : 180);
6973
const controller = new AbortController();
7074
const timeout = window.setTimeout(() => controller.abort(), 12_000);
71-
void setAndroidFold(props.access, props.deviceId, posture, controller.signal)
75+
const send = (access: DeviceHubAccess) =>
76+
setAndroidFold(access, props.deviceId, posture, controller.signal);
77+
void send(props.access)
78+
.catch(async (cause: unknown) => {
79+
// Hub tickets expire after a few minutes, while the panel stays open much longer.
80+
if (!(cause instanceof AndroidFoldError && cause.status === 401)) throw cause;
81+
const renewed = await props.renewAccess();
82+
if (!renewed || controller.signal.aborted) throw cause;
83+
return send(renewed);
84+
})
7285
.then((next) => {
7386
if (mounted.current) {
7487
setFold(next);

‎apps/web/src/components/device/DeviceStreamView.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect, useRef, useState, type ReactNode } from "react";
33

44
import { cn } from "~/lib/utils";
55
import { Button } from "~/components/ui/button";
6-
import { refreshDeviceHubAccess, useDeviceHubAccess } from "~/state/device";
6+
import { refreshDeviceHubAccess, renewDeviceHubAccess, useDeviceHubAccess } from "~/state/device";
77
import { createCanvasFrameSink } from "@t3tools/client-runtime/device/frame";
88
import { resolveDeviceShape } from "@t3tools/client-runtime/device/shape-profile";
99
import { deviceKeyboard, deviceModel } from "./deviceModels";
@@ -343,6 +343,7 @@ export function DeviceStreamView(props: {
343343
screenWidth={screen?.width}
344344
screenHeight={screen?.height}
345345
onFoldAngle={setFoldAngle}
346+
renewAccess={() => renewDeviceHubAccess(props.environmentId, props.hostId)}
346347
/>
347348
) : showPhone && isDuo && screen?.supportsHingeAngle ? (
348349
<DeviceDuoControls

‎apps/web/src/components/device/deviceFold.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ export interface AndroidFoldState {
99
readonly hingeAngle: number | null;
1010
}
1111

12+
export class AndroidFoldError extends Error {
13+
readonly status: number;
14+
15+
constructor(message: string, status: number) {
16+
super(message);
17+
this.status = status;
18+
}
19+
}
20+
1221
const isRecord = (value: unknown): value is Record<string, unknown> =>
1322
typeof value === "object" && value !== null && !Array.isArray(value);
1423

@@ -54,8 +63,9 @@ const foldRequest = async (
5463
if (!response.ok) {
5564
const payload: unknown = await response.json().catch(() => null);
5665
const error = isRecord(payload) ? payload.error : null;
57-
throw new Error(
66+
throw new AndroidFoldError(
5867
typeof error === "string" ? error : `Fold command failed (${response.status}).`,
68+
response.status,
5969
);
6070
}
6171
return parseFold(await response.json());

‎apps/web/src/state/device.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import type { DeviceServiceState, EnvironmentId } from "@t3tools/contracts";
88
import * as Effect from "effect/Effect";
99
import * as Option from "effect/Option";
10-
import { AsyncResult, Atom } from "effect/reactivity";
10+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
1111

1212
import { connectionAtomRuntime } from "../connection/runtime";
1313
import { appAtomRegistry } from "../rpc/atomRegistry";
@@ -69,3 +69,19 @@ const EMPTY_ACCESS_ATOM = Atom.make(AsyncResult.initial<DeviceHubAccess, never>(
6969
export function refreshDeviceHubAccess(environmentId: EnvironmentId): void {
7070
appAtomRegistry.refresh(deviceHubAccessAtom(environmentId));
7171
}
72+
73+
/** Mints a fresh hub ticket for a one-off request that failed with 401. Resolves null on failure. */
74+
export function renewDeviceHubAccess(
75+
environmentId: EnvironmentId,
76+
hostId = "local",
77+
): Promise<DeviceHubAccess | null> {
78+
const atom = deviceHubAccessAtom(environmentId);
79+
appAtomRegistry.refresh(atom);
80+
return Effect.runPromise(
81+
AtomRegistry.getResult(appAtomRegistry, atom, { suspendOnWaiting: true }).pipe(
82+
Effect.map((access) => ({ ...access, query: { ...access.query, hostId } })),
83+
Effect.timeout("10 seconds"),
84+
Effect.orElseSucceed(() => null),
85+
),
86+
);
87+
}

0 commit comments

Comments
 (0)