Skip to content
TypeScript

fix(mobile): pairing deep links can no longer target a saved environment (#26318)

Julius Marmingec0f9e3618495
diff --git a/apps/mobile/src/App.tsx b/apps/mobile/src/App.tsx
index 5798c1adb06..92369287e8e 100644
--- a/apps/mobile/src/App.tsx
+++ b/apps/mobile/src/App.tsx
@@ -52,7 +52,11 @@ import { OverlayPortalHost } from "./components/ui/OverlayPortal";
import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
import { hasCloudPublicConfig } from "./features/cloud/publicConfig";
import { takeScheduledTaskNewLink } from "./features/settings/scheduled-task-editor-state";
-import { pairingFormLinkAction, startDevAutoConnect } from "./features/connection/devAutoConnect";
+import {
+ pairingFormLinkAction,
+ startDevAutoConnect,
+ withoutPairingTarget,
+} from "./features/connection/devAutoConnect";
import {
resolvePendingNewTaskPromptLink,
takeNewTaskPromptLink,
@@ -106,11 +110,13 @@ const linkingConfig = {
function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) {
return takeScheduledTaskNewLink(
startDevAutoConnect(
- takeNewTaskPromptLink(
- takeThreadPromptLink(
- restoreSettingsLinkState(
- restoreDraftLinkState(getStateFromPath(path, options), path),
- settingsLinkPage(),
+ withoutPairingTarget(
+ takeNewTaskPromptLink(
+ takeThreadPromptLink(
+ restoreSettingsLinkState(
+ restoreDraftLinkState(getStateFromPath(path, options), path),
+ settingsLinkPage(),
+ ),
),
),
),
diff --git a/apps/mobile/src/features/connection/devAutoConnect.test.ts b/apps/mobile/src/features/connection/devAutoConnect.test.ts
index 760e68ac598..12942042f7d 100644
--- a/apps/mobile/src/features/connection/devAutoConnect.test.ts
+++ b/apps/mobile/src/features/connection/devAutoConnect.test.ts
@@ -63,12 +63,14 @@ vi.mock("../../lib/rootNavigation", () => ({
},
}));
+import { findLinkedRoute } from "../../lib/appLinking";
import { appAtomRegistry } from "../../state/atom-registry";
import {
autoConnectReport,
devAutoConnectAttemptAtom,
pairingFormLinkAction,
startDevAutoConnect,
+ withoutPairingTarget,
} from "./devAutoConnect";
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
const state = settingsLink({ pairingUrl: ` ${PAIRING_URL} `, autoConnect: "1" });
expect(startDevAutoConnect(state)).toBe(state);
- expect(harness.pair).toHaveBeenCalledExactlyOnceWith(PAIRING_URL, undefined);
+ expect(harness.pair).toHaveBeenCalledExactlyOnceWith(PAIRING_URL);
expect(appAtomRegistry.get(devAutoConnectAttemptAtom)).toEqual({
pairingUrl: PAIRING_URL,
status: "connecting",
@@ -254,3 +256,32 @@ it("a pairing link fills the pairing form already on screen instead of stacking
params: { pairingUrl: PAIRING_URL },
});
});
+
+it("a link cannot aim a pairing at a saved environment", () => {
+ vi.stubGlobal("__DEV__", true);
+ harness.pair.mockClear();
+ harness.pair.mockResolvedValue({ ok: false, error: null });
+ const linking = {
+ initialRouteName: "Home",
+ screens: {
+ Home: "",
+ Settings: { path: "settings", screens: { SettingsEnvironmentNew: "environment-new" } },
+ ConnectionsNew: "connections/new",
+ },
+ };
+ for (const path of ["connections/new", "settings/environment-new"]) {
+ const link = withoutPairingTarget(
+ getStateFromPath(
+ `${path}?routeFor=victim&pairingUrl=${encodeURIComponent(PAIRING_URL)}&autoConnect=1`,
+ linking,
+ ),
+ );
+ expect(findLinkedRoute(link, ["ConnectionsNew", "SettingsEnvironmentNew"])?.params).toEqual({
+ pairingUrl: PAIRING_URL,
+ autoConnect: "1",
+ });
+
+ startDevAutoConnect(link);
+ expect(harness.pair).toHaveBeenLastCalledWith(PAIRING_URL);
+ }
+});
diff --git a/apps/mobile/src/features/connection/devAutoConnect.ts b/apps/mobile/src/features/connection/devAutoConnect.ts
index 213504d8dfd..67f3ab1eee9 100644
--- a/apps/mobile/src/features/connection/devAutoConnect.ts
+++ b/apps/mobile/src/features/connection/devAutoConnect.ts
@@ -1,9 +1,8 @@
import type { NavigationState, PartialState } from "@react-navigation/native";
import { CommonActions, StackActions } from "@react-navigation/native";
-import type { EnvironmentId } from "@t3tools/contracts";
import { Atom } from "effect/reactivity";
-import { findLinkedRoute } from "../../lib/appLinking";
+import { findLinkedRoute, updateLinkedRoute } from "../../lib/appLinking";
import { rootNavigationRef, whenRootNavigationReady } from "../../lib/rootNavigation";
import { appAtomRegistry } from "../../state/atom-registry";
import { leaveSettingsPage } from "../settings/settings-navigation";
@@ -44,10 +43,10 @@ function isAutoConnectFlag(value: unknown): boolean {
return value === "1" || value === "true";
}
-async function connect(pairingUrl: string, expectedEnvironmentId: EnvironmentId | undefined) {
+async function connect(pairingUrl: string) {
const attempt: DevAutoConnectAttempt = { pairingUrl, status: "connecting" };
appAtomRegistry.set(devAutoConnectAttemptAtom, attempt);
- const outcome = await pairEnvironment(pairingUrl, expectedEnvironmentId);
+ const outcome = await pairEnvironment(pairingUrl);
// A later link replaced or cleared this attempt; its result belongs to no open form.
if (appAtomRegistry.get(devAutoConnectAttemptAtom) !== attempt) return;
if (!outcome.ok) {
@@ -71,6 +70,20 @@ async function connect(pairingUrl: string, expectedEnvironmentId: EnvironmentId
});
}
+/**
+ * `routeFor` makes a pairing an "Add route" to a saved environment, which skips the guard against
+ * a pairing taking over that environment's id. Only in-app navigation may set it, never a link.
+ */
+export function withoutPairingTarget(
+ state: PartialState<NavigationState> | undefined,
+): PartialState<NavigationState> | undefined {
+ return updateLinkedRoute(state, PAIRING_FORM_ROUTES, (route) => {
+ if (route.params === undefined || !("routeFor" in route.params)) return route;
+ const { routeFor: _routeFor, ...params } = route.params as { readonly routeFor?: unknown };
+ return { ...route, params };
+ });
+}
+
/**
* `connections/new?pairingUrl=…&autoConnect=1` (or `settings/environment-new?…`) pairs from the
* link itself, so a remount of the form never pairs twice. Production builds ignore the flag so
@@ -85,11 +98,10 @@ export function startDevAutoConnect(
const params = (route.params ?? {}) as {
readonly autoConnect?: unknown;
readonly pairingUrl?: unknown;
- readonly routeFor?: EnvironmentId;
};
const pairingUrl = typeof params.pairingUrl === "string" ? params.pairingUrl.trim() : "";
if (isAutoConnectFlag(params.autoConnect) && pairingUrl.length > 0) {
- void connect(pairingUrl, params.routeFor);
+ void connect(pairingUrl);
} else {
// Only links put a pairing URL on the form, so a form opened by any other link starts
// clean instead of showing an earlier auto-connect's failure for the same URL.