Merge branch 'worktree-agent-ac54c8919e940163a'

# Conflicts:
#	scripts/setup-native.py
This commit is contained in:
2026-11-10 01:07:12 +00:00
34 changed files with 1825 additions and 66 deletions
+78
View File
@@ -0,0 +1,78 @@
// Which networks may reach the web proxy, decided per connection.
//
// The installer's default is "loopback + tailnet": the proxy binds the
// wildcard address but drops every connection that did not arrive on, and
// come from, a loopback or Tailscale address. Binding the wildcard and
// filtering is deliberate: Tailscale often comes up after the proxy (login
// autostart) and its address can change, and a filter needs no rebinding or
// retry loop. Both ends are checked: macOS accepts a packet for the tailnet
// address on any interface (weak host model), but a LAN peer cannot complete a
// TCP handshake from a tailnet source address because the reply is routed into
// the tunnel.
//
// A plain-HTTP LAN listener is opt-in (`--allow ...,lan`). Even then the
// password, the session cookie and every signed-in page stay off the LAN's
// cleartext unless `--lan-plain-login` says otherwise.
import net from "node:net";
const tailnet = new net.BlockList();
tailnet.addSubnet("100.64.0.0", 10, "ipv4"); // Tailscale's CGNAT range
tailnet.addSubnet("fd7a:115c:a1e0::", 48, "ipv6"); // Tailscale's ULA prefix
const loopback = new net.BlockList();
loopback.addSubnet("127.0.0.0", 8, "ipv4");
loopback.addAddress("::1", "ipv6");
export const ROUTES = ["loopback", "tailnet", "lan"];
/** "loopback", "tailnet" or "lan" for one address (IPv4-mapped IPv6 unwrapped). */
export function addressRoute(address) {
let value = String(address ?? "").replace(/%.*$/, "");
if (value.toLowerCase().startsWith("::ffff:") && net.isIPv4(value.slice(7))) value = value.slice(7);
const family = net.isIPv4(value) ? "ipv4" : net.isIPv6(value) ? "ipv6" : null;
if (!family) return "lan";
if (loopback.check(value, family)) return "loopback";
if (tailnet.check(value, family)) return "tailnet";
return "lan";
}
/** The route of a connection: loopback or tailnet only when both ends agree. */
export function socketRoute(socket) {
const local = addressRoute(socket?.localAddress);
return local !== "lan" && local === addressRoute(socket?.remoteAddress) ? local : "lan";
}
/**
* The policy from the proxy's flags. Without `--allow` every route is
* admitted with plain-HTTP sign-in, as before (a direct `node serve.mjs`
* binds 127.0.0.1 by default); the installer always passes `--allow`.
*/
export function accessPolicy(argv) {
const i = argv.indexOf("--allow");
const given = i !== -1 && argv[i + 1] ? argv[i + 1] : null;
const allow = new Set(given === null ? ROUTES : given.split(",").map((part) => part.trim()).filter(Boolean));
for (const route of allow) {
if (!ROUTES.includes(route)) throw new Error(`--allow: unknown route ${route} (use ${ROUTES.join(",")})`);
}
allow.add("loopback"); // the health check, tailscale serve and local tools
const lanPlainLogin = given === null || argv.includes("--lan-plain-login");
return {
allow,
lanPlainLogin,
/** Whether a new connection may proceed at all. */
admits(socket) {
return allow.has(socketRoute(socket));
},
/** Whether this request must not carry the password or a signed-in page. */
plainLanBlocked(req) {
return !lanPlainLogin && socketRoute(req.socket) === "lan";
},
describe() {
return `access: ${[...allow].join(" + ")}` +
(allow.has("lan") ? (lanPlainLogin ? " (plain-HTTP LAN sign-in ALLOWED)" : " (no sign-in over plain-HTTP LAN)") : "");
},
};
}
export const PLAIN_LAN_REFUSAL =
"Signing in over plain HTTP on this network is disabled: the password and session cookie would cross it unencrypted.\n" +
"Use this machine's Tailscale address (for example its https://<name>.ts.net address), or set web.lan_plain_login in the installation.\n";