Merge branch 'worktree-agent-ac54c8919e940163a'
# Conflicts: # scripts/setup-native.py
This commit is contained in:
@@ -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