diff --git a/conformance/function/relay-selection.json b/conformance/function/relay-selection.json index dafb068e..1a1c3e1d 100644 --- a/conformance/function/relay-selection.json +++ b/conformance/function/relay-selection.json @@ -326,6 +326,76 @@ "function": "relay-selection.selectRelay", "id": "breaks-a-tie-by-lowest-device-id", "kind": "function" + }, + { + "args": [ + { + "localNetworks": [ + "10.34.16.48/23", + "100.88.132.111/32", + "fe80::1/64" + ], + "offers": [ + { + "addresses": [ + "ws://192.168.1.39:36937/", + "ws://172.18.0.1:36937/", + "ws://100.123.240.67:36937/" + ], + "deviceHex": "1111111111111111111111111111111111111111111111111111111111111111", + "machineLocal": false + } + ], + "own": "0000000000000000000000000000000000000000000000000000000000000000", + "publicHub": "wss://mesh.example.com/", + "trusted": [ + "1111111111111111111111111111111111111111111111111111111111111111" + ] + } + ], + "expect": { + "deviceHex": "1111111111111111111111111111111111111111111111111111111111111111", + "tier": "trusted", + "url": "ws://100.123.240.67:36937/" + }, + "from": "relay-selection.unit.test.ts: selectRelay > skips private addresses on networks this host is not on", + "function": "relay-selection.selectRelay", + "id": "skips-private-addresses-off-local-networks", + "kind": "function" + }, + { + "args": [ + { + "localNetworks": [ + "192.168.1.20/24" + ], + "offers": [ + { + "addresses": [ + "ws://192.168.1.39:36937/", + "ws://172.18.0.1:36937/", + "ws://100.123.240.67:36937/" + ], + "deviceHex": "1111111111111111111111111111111111111111111111111111111111111111", + "machineLocal": false + } + ], + "own": "0000000000000000000000000000000000000000000000000000000000000000", + "publicHub": "wss://mesh.example.com/", + "trusted": [ + "1111111111111111111111111111111111111111111111111111111111111111" + ] + } + ], + "expect": { + "deviceHex": "1111111111111111111111111111111111111111111111111111111111111111", + "tier": "lan", + "url": "ws://192.168.1.39:36937/" + }, + "from": "relay-selection.unit.test.ts: selectRelay > still prefers a private address on a network this host is on", + "function": "relay-selection.selectRelay", + "id": "prefers-private-address-on-local-network", + "kind": "function" } ] } diff --git a/conformance/manifest.json b/conformance/manifest.json index 5a97442c..2577177e 100644 --- a/conformance/manifest.json +++ b/conformance/manifest.json @@ -65,7 +65,7 @@ "skip": [] }, "function/relay-selection.json": { - "cases": 10, + "cases": 12, "skip": [] }, "function/request-timeouts.json": { diff --git a/src/core/relay-role.ts b/src/core/relay-role.ts index b4ab1add..a5b93360 100644 --- a/src/core/relay-role.ts +++ b/src/core/relay-role.ts @@ -12,7 +12,11 @@ import type { ElectionSessions } from "./election-sessions.js"; import type { GatewayTrustReader } from "./gateway-trust.js"; import type { TransportEvents } from "./transport.js"; import { RelayServer } from "./relay-server.js"; -import { selectRelay, type RelayOffer } from "./relay-selection.js"; +import { + localNetworkCidrs, + selectRelay, + type RelayOffer, +} from "./relay-selection.js"; export interface RelayRoleDeps { /** The interface a served relay listens on (RelayServerOptions.host). */ @@ -106,6 +110,7 @@ export class RelayRole { isTrusted: (deviceHex) => this.deps.gatewayTrust.isReachable(deviceHex), ownDeviceHex: this.deps.ownDeviceHex, publicHubUrl, + localNetworks: localNetworkCidrs(), }).url; } diff --git a/src/core/relay-selection.ts b/src/core/relay-selection.ts index 4aaf1e77..6dbc64a5 100644 --- a/src/core/relay-selection.ts +++ b/src/core/relay-selection.ts @@ -7,6 +7,7 @@ */ import { BlockList, isIPv4, isIPv6 } from "node:net"; +import * as os from "node:os"; import { compareCodeUnits } from "./ordering.js"; /** A relay offer this store has heard, with what it knows about the device making it. */ @@ -36,6 +37,53 @@ export interface RelaySelectionInput { ownDeviceHex: string; /** The configured public hub, the fallback when no offer qualifies. */ publicHubUrl: string; + /** The CIDRs of this host's own network interfaces (localNetworkCidrs). When given, a private or link-local address outside every one of them is skipped in every tier: it belongs to a network this host is not on, so dialling it can only time out (a device offering its home-LAN address to a peer on another network over a tailnet). When absent, every private address counts as local, as before. */ + localNetworks?: readonly string[] | undefined; +} + +/** The CIDRs of this host's non-internal interfaces, as os.networkInterfaces reports them: the input selectRelay's localNetworks expects. */ +export function localNetworkCidrs( + interfaces: NodeJS.Dict = os.networkInterfaces(), +): string[] { + const result: string[] = []; + for (const addresses of Object.values(interfaces)) { + for (const address of addresses ?? []) { + if (!address.internal && address.cidr !== null) result.push(address.cidr); + } + } + return result; +} + +/** A BlockList of the given CIDRs, or undefined when none were given (selection then treats every private address as local). Malformed entries are ignored. */ +function localNetworkList( + cidrs: readonly string[] | undefined, +): BlockList | undefined { + if (cidrs === undefined) return undefined; + const list = new BlockList(); + for (const cidr of cidrs) { + const [address, prefix] = cidr.split("/"); + if (address === undefined || prefix === undefined) continue; + const family = isIPv4(address) + ? "ipv4" + : isIPv6(address) + ? "ipv6" + : undefined; + if (family === undefined) continue; + try { + list.addSubnet(address, Number(prefix), family); + } catch { + // An out-of-range prefix: not a network this host can be judged to be on. + } + } + return list; +} + +/** Whether a private host sits on a network this host is on, judged against localNetworks; true when localNetworks was not given. */ +function onLocalNetwork(host: string, local: BlockList | undefined): boolean { + if (local === undefined) return true; + if (isIPv4(host)) return local.check(host, "ipv4"); + if (isIPv6(host)) return local.check(host, "ipv6"); + return true; } /** The private (RFC 1918), IPv4 link-local (RFC 3927), IPv6 unique-local (RFC 4193) and IPv6 link-local ranges: addresses reachable on the local network and nowhere else. */ @@ -82,10 +130,12 @@ function relayHost(address: string): string | undefined { function addressFor( offer: Readonly, tier: RelayTier, + local: BlockList | undefined, ): string | undefined { for (const address of offer.addresses) { const host = relayHost(address); if (host === undefined) continue; + if (isPrivateHost(host) && !onLocalNetwork(host, local)) continue; if (tier === "machine" && isLoopbackHost(host)) return address; if (tier === "lan" && isPrivateHost(host)) return address; if (tier === "trusted" && !isLoopbackHost(host)) return address; @@ -106,6 +156,7 @@ export function selectRelay( const eligible = input.offers .filter((offer) => offer.deviceHex !== input.ownDeviceHex) .toSorted((a, b) => compareCodeUnits(a.deviceHex, b.deviceHex)); + const local = localNetworkList(input.localNetworks); for (const tier of OFFER_TIERS) { for (const offer of eligible) { const admitted = @@ -113,7 +164,7 @@ export function selectRelay( ? offer.machineLocal : input.isTrusted(offer.deviceHex); if (!admitted) continue; - const url = addressFor(offer, tier); + const url = addressFor(offer, tier, local); if (url !== undefined) return { url, tier, deviceHex: offer.deviceHex }; } } diff --git a/src/test/conformance-functions.helper.ts b/src/test/conformance-functions.helper.ts index 09e73a04..b3ec82ad 100644 --- a/src/test/conformance-functions.helper.ts +++ b/src/test/conformance-functions.helper.ts @@ -271,6 +271,10 @@ function relaySelection(value: unknown): unknown { isTrusted: (hex) => trusted.includes(hex), ownDeviceHex: str(input.own), publicHubUrl: str(input.publicHub), + localNetworks: + input.localNetworks === undefined + ? undefined + : strings(input.localNetworks), }); }