Merge branch 'worktree-agent-ac54c8919e940163a'
# Conflicts: # scripts/setup-native.py
This commit is contained in:
+26
-3
@@ -2,6 +2,7 @@
|
||||
// web app, and a proxy to the Pipelines server, with no dependencies.
|
||||
// node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080]
|
||||
// [--root DIR] [--updates DIR]
|
||||
// [--allow loopback,tailnet[,lan]] [--lan-plain-login]
|
||||
//
|
||||
// Serves the app (--root, default ../app/build/web) and proxies /connect,
|
||||
// /artifacts/, /files and /chat-uploads to the Pipelines server, so the page, the
|
||||
@@ -29,6 +30,7 @@ import net from "node:net";
|
||||
import { sameOriginUpgrade } from "./src/proxySecurity.js";
|
||||
import { withServerToken } from "./src/serverToken.js";
|
||||
import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js";
|
||||
import { accessPolicy, PLAIN_LAN_REFUSAL } from "./src/listenPolicy.js";
|
||||
|
||||
// The proxy's own directory holds the sign-in page's icons.
|
||||
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
@@ -36,6 +38,8 @@ const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
// the checkout's app/build/web. `--updates DIR` serves the Android app's
|
||||
// version.json and APK at /updates/, behind the same sign-in.
|
||||
const ROOT = resolve(flag(process.argv, "--root", "") || join(HERE, "..", "app", "build", "web"));
|
||||
// Which networks may connect (src/listenPolicy.js).
|
||||
const access = accessPolicy(process.argv);
|
||||
const UPDATES = flag(process.argv, "--updates", "") ? resolve(flag(process.argv, "--updates", "")) : null;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -420,6 +424,10 @@ const server = createServer(async (req, res) => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (access.plainLanBlocked(req)) {
|
||||
res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store" }).end(PLAIN_LAN_REFUSAL);
|
||||
return;
|
||||
}
|
||||
if (auth) {
|
||||
if (pathname === "/login" && req.method === "GET") {
|
||||
if (isAuthed(req)) {
|
||||
@@ -580,7 +588,7 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
return;
|
||||
}
|
||||
const pathname = (req.url ?? "").split("?")[0];
|
||||
if (!sameOriginUpgrade(req.headers.origin, req.headers.host)) {
|
||||
if (access.plainLanBlocked(req) || !sameOriginUpgrade(req.headers.origin, req.headers.host)) {
|
||||
clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
@@ -598,12 +606,27 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
proxyUpgrade(req, clientSocket, clientHead);
|
||||
});
|
||||
|
||||
// Connections from a network the policy does not admit are dropped before
|
||||
// any HTTP is read.
|
||||
server.on("connection", (socket) => {
|
||||
if (!access.admits(socket)) socket.destroy();
|
||||
});
|
||||
|
||||
const port = parsePort(process.argv);
|
||||
const host = flag(process.argv, "--host", "127.0.0.1");
|
||||
let host = flag(process.argv, "--host", "127.0.0.1");
|
||||
// "::" also reaches a tailnet IPv6 address; a host without IPv6 falls back.
|
||||
function noIPv6(error) {
|
||||
if (host !== "::" || !["EAFNOSUPPORT", "EADDRNOTAVAIL"].includes(error.code)) throw error;
|
||||
host = "0.0.0.0";
|
||||
server.listen(port, host);
|
||||
}
|
||||
server.once("error", noIPv6);
|
||||
server.listen(port, host, () => {
|
||||
const shown = host === "0.0.0.0" ? "127.0.0.1" : host;
|
||||
server.off("error", noIPv6);
|
||||
const shown = host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
|
||||
console.log(`web spike: http://${shown}:${port}/ (bound ${host}:${port})`);
|
||||
console.log(`proxying /connect (ws), /artifacts/ and /files (http) -> ${targetHost}:${targetPort}`);
|
||||
console.log(auth ? "auth: sign-in required" : "auth: DISABLED (tests)");
|
||||
console.log(access.describe());
|
||||
console.log(`serving ${ROOT}${UPDATES ? `; app updates from ${UPDATES}` : ""}`);
|
||||
});
|
||||
|
||||
@@ -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";
|
||||
@@ -0,0 +1,134 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
|
||||
import { networkInterfaces, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { accessPolicy, addressRoute, socketRoute } from "../src/listenPolicy.js";
|
||||
|
||||
const here = fileURLToPath(new URL("..", import.meta.url));
|
||||
|
||||
test("classifies loopback, tailnet and LAN addresses", () => {
|
||||
for (const address of ["127.0.0.1", "127.8.0.2", "::1", "::ffff:127.0.0.1"]) assert.equal(addressRoute(address), "loopback", address);
|
||||
for (const address of ["100.64.0.1", "100.101.7.9", "100.127.255.254", "::ffff:100.100.1.2", "fd7a:115c:a1e0::1", "fd7a:115c:a1e0:ab12::9"]) {
|
||||
assert.equal(addressRoute(address), "tailnet", address);
|
||||
}
|
||||
for (const address of ["192.168.1.20", "10.0.0.5", "100.63.255.255", "100.128.0.1", "fd7a:115c:a1e1::1", "fe80::1%en0", "", undefined, "nonsense"]) {
|
||||
assert.equal(addressRoute(address), "lan", String(address));
|
||||
}
|
||||
});
|
||||
|
||||
test("a connection is tailnet or loopback only when both ends are", () => {
|
||||
assert.equal(socketRoute({ localAddress: "100.101.1.1", remoteAddress: "100.90.2.2" }), "tailnet");
|
||||
assert.equal(socketRoute({ localAddress: "127.0.0.1", remoteAddress: "127.0.0.1" }), "loopback");
|
||||
// A LAN peer aiming at the tailnet address (macOS weak host model) is LAN.
|
||||
assert.equal(socketRoute({ localAddress: "100.101.1.1", remoteAddress: "192.168.1.9" }), "lan");
|
||||
assert.equal(socketRoute({ localAddress: "127.0.0.1", remoteAddress: "100.90.2.2" }), "lan");
|
||||
assert.equal(socketRoute({ localAddress: "192.168.1.20", remoteAddress: "192.168.1.9" }), "lan");
|
||||
});
|
||||
|
||||
test("the policy admits only allowed routes and gates LAN sign-in", () => {
|
||||
const lan = { localAddress: "192.168.1.20", remoteAddress: "192.168.1.9" };
|
||||
const ts = { localAddress: "100.101.1.1", remoteAddress: "100.90.2.2" };
|
||||
const local = { localAddress: "127.0.0.1", remoteAddress: "127.0.0.1" };
|
||||
const strict = accessPolicy(["--allow", "tailnet"]);
|
||||
assert.deepEqual([strict.admits(local), strict.admits(ts), strict.admits(lan)], [true, true, false]);
|
||||
const withLan = accessPolicy(["--allow", "loopback,tailnet,lan"]);
|
||||
assert.equal(withLan.admits(lan), true);
|
||||
assert.equal(withLan.plainLanBlocked({ socket: lan }), true);
|
||||
assert.equal(withLan.plainLanBlocked({ socket: ts }), false);
|
||||
assert.equal(withLan.plainLanBlocked({ socket: local }), false);
|
||||
assert.equal(accessPolicy(["--allow", "loopback,tailnet,lan", "--lan-plain-login"]).plainLanBlocked({ socket: lan }), false);
|
||||
const legacy = accessPolicy([]);
|
||||
assert.equal(legacy.admits(lan) && !legacy.plainLanBlocked({ socket: lan }), true);
|
||||
assert.throws(() => accessPolicy(["--allow", "everything"]), /unknown route/);
|
||||
});
|
||||
|
||||
function hostAddress(match) {
|
||||
for (const entries of Object.values(networkInterfaces())) {
|
||||
for (const entry of entries ?? []) {
|
||||
if (entry.family === "IPv4" && !entry.internal && match(entry.address)) return entry.address;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const lanAddress = hostAddress((address) => addressRoute(address) === "lan");
|
||||
const tailnetAddress = hostAddress((address) => addressRoute(address) === "tailnet");
|
||||
|
||||
async function freePort() {
|
||||
const server = net.createServer().listen(0, "127.0.0.1");
|
||||
await new Promise((done) => server.once("listening", done));
|
||||
const { port } = server.address();
|
||||
await new Promise((done) => server.close(done));
|
||||
return port;
|
||||
}
|
||||
|
||||
async function start(args) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "listen-"));
|
||||
await mkdir(join(dir, "app"));
|
||||
await writeFile(join(dir, "app", "index.html"), "<html><head></head><body>app</body></html>");
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", join(dir, "app"), ...args], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
let output = "";
|
||||
await new Promise((done) => child.stdout.on("data", (chunk) => {
|
||||
output += chunk;
|
||||
if (output.includes("access:")) done();
|
||||
}));
|
||||
return { port, output, stop: () => child.kill() };
|
||||
}
|
||||
|
||||
/** Raw HTTP so a dropped connection is observed as such, not retried. */
|
||||
function rawRequest(address, port, text) {
|
||||
return new Promise((done) => {
|
||||
const socket = net.connect(port, address, () => socket.write(text));
|
||||
let reply = "";
|
||||
socket.on("data", (chunk) => { reply += chunk; });
|
||||
socket.on("error", () => done({ dropped: true, reply }));
|
||||
socket.on("close", () => done({ dropped: reply === "", reply }));
|
||||
socket.setTimeout(3000, () => socket.destroy());
|
||||
});
|
||||
}
|
||||
const get = (path) => `GET ${path} HTTP/1.1\r\nHost: x\r\nAccept: text/html\r\nConnection: close\r\n\r\n`;
|
||||
const login = "POST /login HTTP/1.1\r\nHost: x\r\nContent-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: 17\r\nConnection: close\r\n\r\npassword=whatever";
|
||||
|
||||
test("loopback + tailnet drops a LAN connection and serves loopback and tailnet", { skip: !lanAddress && "no LAN address" }, async (t) => {
|
||||
const proxy = await start(["--host", "::", "--allow", "loopback,tailnet"]);
|
||||
t.after(proxy.stop);
|
||||
const lan = await rawRequest(lanAddress, proxy.port, get("/"));
|
||||
assert.equal(lan.dropped, true, `LAN ${lanAddress} got ${lan.reply}`);
|
||||
const local = await rawRequest("127.0.0.1", proxy.port, get("/"));
|
||||
assert.match(local.reply, /^HTTP\/1.1 200/);
|
||||
if (tailnetAddress) {
|
||||
const ts = await rawRequest(tailnetAddress, proxy.port, get("/"));
|
||||
assert.match(ts.reply, /^HTTP\/1.1 200/, `tailnet ${tailnetAddress}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("an opted-in LAN refuses sign-in and signed-in pages over plain HTTP", { skip: !lanAddress && "no LAN address" }, async (t) => {
|
||||
const proxy = await start(["--host", "0.0.0.0", "--allow", "loopback,tailnet,lan"]);
|
||||
t.after(proxy.stop);
|
||||
for (const request of [login, get("/login"), get("/"), get("/updates/version.json")]) {
|
||||
const reply = await rawRequest(lanAddress, proxy.port, request);
|
||||
assert.match(reply.reply, /^HTTP\/1.1 403/, request.split("\r\n")[0]);
|
||||
assert.match(reply.reply, /plain HTTP on this network is disabled/);
|
||||
}
|
||||
assert.match((await rawRequest(lanAddress, proxy.port, get("/favicon.svg"))).reply, /^HTTP\/1.1 200/);
|
||||
const upgrade = await rawRequest(lanAddress, proxy.port, "GET /connect HTTP/1.1\r\nHost: x\r\nConnection: Upgrade\r\n" +
|
||||
"Upgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n");
|
||||
assert.match(upgrade.reply, /^HTTP\/1.1 403/);
|
||||
assert.match((await rawRequest("127.0.0.1", proxy.port, get("/"))).reply, /^HTTP\/1.1 200/);
|
||||
});
|
||||
|
||||
test("web.lan_plain_login restores plain-HTTP LAN sign-in explicitly", { skip: !lanAddress && "no LAN address" }, async (t) => {
|
||||
const proxy = await start(["--host", "0.0.0.0", "--allow", "loopback,tailnet,lan", "--lan-plain-login"]);
|
||||
t.after(proxy.stop);
|
||||
assert.match(proxy.output, /plain-HTTP LAN sign-in ALLOWED/);
|
||||
assert.match((await rawRequest(lanAddress, proxy.port, get("/"))).reply, /^HTTP\/1.1 200/);
|
||||
});
|
||||
Reference in New Issue
Block a user