Skip to content

Commit c0f9e36

Browse files
fix(mobile): pairing deep links can no longer target a saved environment (#26318)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent feb2e32 commit c0f9e36

3 files changed

Lines changed: 62 additions & 13 deletions

File tree

‎apps/mobile/src/App.tsx‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ import { OverlayPortalHost } from "./components/ui/OverlayPortal";
5252
import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
5353
import { hasCloudPublicConfig } from "./features/cloud/publicConfig";
5454
import { takeScheduledTaskNewLink } from "./features/settings/scheduled-task-editor-state";
55-
import { pairingFormLinkAction, startDevAutoConnect } from "./features/connection/devAutoConnect";
55+
import {
56+
pairingFormLinkAction,
57+
startDevAutoConnect,
58+
withoutPairingTarget,
59+
} from "./features/connection/devAutoConnect";
5660
import {
5761
resolvePendingNewTaskPromptLink,
5862
takeNewTaskPromptLink,
@@ -106,11 +110,13 @@ const linkingConfig = {
106110
function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) {
107111
return takeScheduledTaskNewLink(
108112
startDevAutoConnect(
109-
takeNewTaskPromptLink(
110-
takeThreadPromptLink(
111-
restoreSettingsLinkState(
112-
restoreDraftLinkState(getStateFromPath(path, options), path),
113-
settingsLinkPage(),
113+
withoutPairingTarget(
114+
takeNewTaskPromptLink(
115+
takeThreadPromptLink(
116+
restoreSettingsLinkState(
117+
restoreDraftLinkState(getStateFromPath(path, options), path),
118+
settingsLinkPage(),
119+
),
114120
),
115121
),
116122
),

‎apps/mobile/src/features/connection/devAutoConnect.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ vi.mock("../../lib/rootNavigation", () => ({
6363
},
6464
}));
6565

66+
import { findLinkedRoute } from "../../lib/appLinking";
6667
import { appAtomRegistry } from "../../state/atom-registry";
6768
import {
6869
autoConnectReport,
6970
devAutoConnectAttemptAtom,
7071
pairingFormLinkAction,
7172
startDevAutoConnect,
73+
withoutPairingTarget,
7274
} from "./devAutoConnect";
7375

7476
const PAIRING_URL = "http://192.168.1.10:3773/pair#token=abc";
@@ -89,7 +91,7 @@ it("pairs from a nested settings link once, reports failure, and closes the form
8991

9092
const state = settingsLink({ pairingUrl: ` ${PAIRING_URL} `, autoConnect: "1" });
9193
expect(startDevAutoConnect(state)).toBe(state);
92-
expect(harness.pair).toHaveBeenCalledExactlyOnceWith(PAIRING_URL, undefined);
94+
expect(harness.pair).toHaveBeenCalledExactlyOnceWith(PAIRING_URL);
9395
expect(appAtomRegistry.get(devAutoConnectAttemptAtom)).toEqual({
9496
pairingUrl: PAIRING_URL,
9597
status: "connecting",
@@ -254,3 +256,32 @@ it("a pairing link fills the pairing form already on screen instead of stacking
254256
params: { pairingUrl: PAIRING_URL },
255257
});
256258
});
259+
260+
it("a link cannot aim a pairing at a saved environment", () => {
261+
vi.stubGlobal("__DEV__", true);
262+
harness.pair.mockClear();
263+
harness.pair.mockResolvedValue({ ok: false, error: null });
264+
const linking = {
265+
initialRouteName: "Home",
266+
screens: {
267+
Home: "",
268+
Settings: { path: "settings", screens: { SettingsEnvironmentNew: "environment-new" } },
269+
ConnectionsNew: "connections/new",
270+
},
271+
};
272+
for (const path of ["connections/new", "settings/environment-new"]) {
273+
const link = withoutPairingTarget(
274+
getStateFromPath(
275+
`${path}?routeFor=victim&pairingUrl=${encodeURIComponent(PAIRING_URL)}&autoConnect=1`,
276+
linking,
277+
),
278+
);
279+
expect(findLinkedRoute(link, ["ConnectionsNew", "SettingsEnvironmentNew"])?.params).toEqual({
280+
pairingUrl: PAIRING_URL,
281+
autoConnect: "1",
282+
});
283+
284+
startDevAutoConnect(link);
285+
expect(harness.pair).toHaveBeenLastCalledWith(PAIRING_URL);
286+
}
287+
});

‎apps/mobile/src/features/connection/devAutoConnect.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { NavigationState, PartialState } from "@react-navigation/native";
22
import { CommonActions, StackActions } from "@react-navigation/native";
3-
import type { EnvironmentId } from "@t3tools/contracts";
43
import { Atom } from "effect/reactivity";
54

6-
import { findLinkedRoute } from "../../lib/appLinking";
5+
import { findLinkedRoute, updateLinkedRoute } from "../../lib/appLinking";
76
import { rootNavigationRef, whenRootNavigationReady } from "../../lib/rootNavigation";
87
import { appAtomRegistry } from "../../state/atom-registry";
98
import { leaveSettingsPage } from "../settings/settings-navigation";
@@ -44,10 +43,10 @@ function isAutoConnectFlag(value: unknown): boolean {
4443
return value === "1" || value === "true";
4544
}
4645

47-
async function connect(pairingUrl: string, expectedEnvironmentId: EnvironmentId | undefined) {
46+
async function connect(pairingUrl: string) {
4847
const attempt: DevAutoConnectAttempt = { pairingUrl, status: "connecting" };
4948
appAtomRegistry.set(devAutoConnectAttemptAtom, attempt);
50-
const outcome = await pairEnvironment(pairingUrl, expectedEnvironmentId);
49+
const outcome = await pairEnvironment(pairingUrl);
5150
// A later link replaced or cleared this attempt; its result belongs to no open form.
5251
if (appAtomRegistry.get(devAutoConnectAttemptAtom) !== attempt) return;
5352
if (!outcome.ok) {
@@ -71,6 +70,20 @@ async function connect(pairingUrl: string, expectedEnvironmentId: EnvironmentId
7170
});
7271
}
7372

73+
/**
74+
* `routeFor` makes a pairing an "Add route" to a saved environment, which skips the guard against
75+
* a pairing taking over that environment's id. Only in-app navigation may set it, never a link.
76+
*/
77+
export function withoutPairingTarget(
78+
state: PartialState<NavigationState> | undefined,
79+
): PartialState<NavigationState> | undefined {
80+
return updateLinkedRoute(state, PAIRING_FORM_ROUTES, (route) => {
81+
if (route.params === undefined || !("routeFor" in route.params)) return route;
82+
const { routeFor: _routeFor, ...params } = route.params as { readonly routeFor?: unknown };
83+
return { ...route, params };
84+
});
85+
}
86+
7487
/**
7588
* `connections/new?pairingUrl=…&autoConnect=1` (or `settings/environment-new?…`) pairs from the
7689
* link itself, so a remount of the form never pairs twice. Production builds ignore the flag so
@@ -85,11 +98,10 @@ export function startDevAutoConnect(
8598
const params = (route.params ?? {}) as {
8699
readonly autoConnect?: unknown;
87100
readonly pairingUrl?: unknown;
88-
readonly routeFor?: EnvironmentId;
89101
};
90102
const pairingUrl = typeof params.pairingUrl === "string" ? params.pairingUrl.trim() : "";
91103
if (isAutoConnectFlag(params.autoConnect) && pairingUrl.length > 0) {
92-
void connect(pairingUrl, params.routeFor);
104+
void connect(pairingUrl);
93105
} else {
94106
// Only links put a pairing URL on the form, so a form opened by any other link starts
95107
// clean instead of showing an earlier auto-connect's failure for the same URL.

0 commit comments

Comments
 (0)