Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(setup): don't trust a loopback location as a reachable address
emxvcbdb6a6adb0a
diff --git a/apps/client/src/setup.spec.tsx b/apps/client/src/setup.spec.tsxindex 46fbbcd0f7..ddb36bc3cd 100644--- a/apps/client/src/setup.spec.tsx+++ b/apps/client/src/setup.spec.tsx@@ -31,7 +31,7 @@ const serverMock = vi.hoisted(() => ({}));vi.mock("./services/server", () => ({ default: serverMock }));-import { afterLanguage, initialState, openedAtRestore, renderState, SyncFailed, SyncFromServer, SyncInProgress } from "./setup";+import { afterLanguage, getNetworkAddresses, initialState, openedAtRestore, renderState, SyncFailed, SyncFromServer, SyncInProgress } from "./setup";type Stats = { outstandingPullCount: number; totalPullCount: number | null; initialized: boolean; lastSyncError?: string | null };@@ -479,6 +479,40 @@ describe("SyncFromServer", () => {});});+describe("getNetworkAddresses, deciding whose word to trust for this device's address", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("trusts the page's own address when a plain browser reached it over the network", async () => {+ vi.stubGlobal("location", { protocol: "http:", host: "192.168.1.20:8080", hostname: "192.168.1.20" });++ expect(await getNetworkAddresses()).toEqual({ addresses: [ "http://192.168.1.20:8080" ], reachableOnNetwork: true });+ expect(serverMock.get).not.toHaveBeenCalledWith("network-addresses");+ });++ it("distrusts its own loopback address and asks the server instead, as Pocket Trilium's webview would hit", async () => {+ // Pocket Trilium runs a full Trilium server locally on the phone and shows it through a plain+ // webview, no Electron API, but `location.hostname` is still 127.0.0.1 because the page never+ // left the device. Trusting `location.host` here would hand the user back their own loopback+ // address instead of one another device could actually reach.+ vi.stubGlobal("location", { protocol: "http:", host: "127.0.0.1:8080", hostname: "127.0.0.1" });+ mockRoutes({ "network-addresses": { addresses: [ "http://192.168.1.20:8080" ], reachableOnNetwork: true } });++ expect(await getNetworkAddresses()).toEqual({ addresses: [ "http://192.168.1.20:8080" ], reachableOnNetwork: true });+ expect(serverMock.get).toHaveBeenCalledWith("network-addresses");+ });++ it("still asks the server in Electron, whose location points at the internal protocol rather than a real listener", async () => {+ vi.stubGlobal("electronApi", {});+ vi.stubGlobal("location", { protocol: "trilium-app:", host: "", hostname: "" });+ mockRoutes({ "network-addresses": { addresses: [], reachableOnNetwork: false } });++ expect(await getNetworkAddresses()).toEqual({ addresses: [], reachableOnNetwork: false });+ expect(serverMock.get).toHaveBeenCalledWith("network-addresses");+ });+});+describe("where the wizard opens", () => {it("starts at the language step on a first run", () => {expect(initialState({})).toBe("selectLanguage");diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsxindex b47bd68e8d..13e6e19cb3 100644--- a/apps/client/src/setup.tsx+++ b/apps/client/src/setup.tsx@@ -868,22 +868,31 @@ function SetupOptionCard({ title, description, icon, onClick, disabled }: { titl);}-async function getNetworkAddresses(): Promise<NetworkAddressesResponse> {- if (!isElectron()) {+export async function getNetworkAddresses(): Promise<NetworkAddressesResponse> {+ if (!isElectron() && !isLoopbackHostname(location.hostname)) {// The browser already reached this server over the network, so the// address it's using is reachable by definition.return { addresses: [`${location.protocol}//${location.host}`], reachableOnNetwork: true };}- // Node's `os` module isn't available in the renderer (node integration is- // disabled), and the desktop renderer's `location` points at the internal- // `trilium-app://` protocol rather than the real HTTP listener. So the- // server enumerates its interfaces and builds the reachable URLs (correct- // protocol and port included), and reports whether it's actually bound to a- // network-reachable interface.+ // Either we're in Electron (Node's `os` module isn't available in the+ // renderer, and the desktop renderer's `location` points at the internal+ // `trilium-app://` protocol rather than the real HTTP listener), or the+ // page itself was loaded over loopback, e.g. a mobile app's embedded+ // webview talking to a server running locally on the same device, as with+ // Pocket Trilium. Either way `location.host` isn't an address another+ // device could use, so ask the server to enumerate its real network+ // interfaces and build the reachable URLs (correct protocol and port+ // included) instead.return await server.get<NetworkAddressesResponse>("network-addresses");}+/** Mirrors the loopback check the server applies to its own listen host in `isHostReachableOnNetwork`. */+function isLoopbackHostname(hostname: string): boolean {+ const normalized = hostname.toLowerCase();+ return normalized === "localhost" || normalized === "::1" || normalized.startsWith("127.");+}+async function allowLanAccessAndRestart() {// Shows a native confirmation dialog (LAN exposure is a security tradeoff)// and persists the choice to security.json. Only restart once the user has