Skip to content

Build your personal knowledge base with Trilium Notes

fix(setup): recognize bracketed IPv6 loopback in address check

emxv25013d8b7abb
diff --git a/apps/client/src/setup.spec.tsx b/apps/client/src/setup.spec.tsx
index ddb36bc3cd..c3691de975 100644
--- a/apps/client/src/setup.spec.tsx
+++ b/apps/client/src/setup.spec.tsx
@@ -491,11 +491,7 @@ describe("getNetworkAddresses, deciding whose word to trust for this device's ad
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.
+ it("distrusts its own loopback address and asks the server instead, as a webview on the same device would hit", async () => {
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 } });
@@ -503,6 +499,14 @@ describe("getNetworkAddresses, deciding whose word to trust for this device's ad
expect(serverMock.get).toHaveBeenCalledWith("network-addresses");
});
+ it("distrusts a bracketed IPv6 loopback address the same way", async () => {
+ vi.stubGlobal("location", { protocol: "http:", host: "[::1]:8080", hostname: "[::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: "" });
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index 13e6e19cb3..e9b5b27625 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -875,22 +875,23 @@ export async function getNetworkAddresses(): Promise<NetworkAddressesResponse> {
return { addresses: [`${location.protocol}//${location.host}`], reachableOnNetwork: true };
}
- // 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.
+ // Either we're in Electron (`location` points at the internal `trilium-app://`
+ // protocol, not the real HTTP listener), or the page was loaded over loopback,
+ // e.g. a mobile app's embedded webview hitting a server running on the same
+ // device. Either way `location.host` isn't an address another device could
+ // use, so ask the server to enumerate its real network interfaces instead.
return await server.get<NetworkAddressesResponse>("network-addresses");
}
-/** Mirrors the loopback check the server applies to its own listen host in `isHostReachableOnNetwork`. */
+/**
+ * Mirrors the loopback check the server applies to its own listen host in
+ * `isHostReachableOnNetwork`, but on `location.hostname`, where a browser
+ * serializes an IPv6 address with its brackets intact (`[::1]`), unlike the
+ * server's raw, unbracketed config value.
+ */
function isLoopbackHostname(hostname: string): boolean {
const normalized = hostname.toLowerCase();
- return normalized === "localhost" || normalized === "::1" || normalized.startsWith("127.");
+ return normalized === "localhost" || normalized === "::1" || normalized === "[::1]" || normalized.startsWith("127.");
}
async function allowLanAccessAndRestart() {