547d4d9822
The proxy adds the shared token to every upstream request and drops any client copy. The peer relay accepts only tailnet, LAN, ts.net and discovered hosts, resolves each name once and pins the checked address, and flags ws:// endpoints with insecure_transport.
176 lines
9.1 KiB
JavaScript
176 lines
9.1 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createServer } from "node:http";
|
|
import { once } from "node:events";
|
|
import { spawn } from "node:child_process";
|
|
import { mkdtemp, writeFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { classify, PeerAddressPolicy, PeerAddressError } from "../src/peerAddress.js";
|
|
import { remoteRequest } from "../src/peerTransport.js";
|
|
|
|
const fakeDns = (answers) => {
|
|
const calls = [];
|
|
const lookup = async (name) => {
|
|
calls.push(name);
|
|
const value = answers[name];
|
|
return Array.isArray(value) ? value : value === undefined ? [] : [value];
|
|
};
|
|
return { lookup, calls };
|
|
};
|
|
const v4 = (address) => ({ address, family: 4 });
|
|
const v6 = (address) => ({ address, family: 6 });
|
|
|
|
test("addresses are classified by range", () => {
|
|
const cases = {
|
|
"127.0.0.1": "loopback", "127.8.9.10": "loopback", "::1": "loopback", "::ffff:127.0.0.1": "loopback",
|
|
"0.0.0.0": "unspecified", "::": "unspecified",
|
|
"169.254.169.254": "metadata", "100.100.100.100": "metadata", "fd7a:115c:a1e0::53": "metadata", "fd00:ec2::254": "metadata",
|
|
"169.254.10.1": "link-local", "fe80::1": "link-local",
|
|
"100.64.0.1": "tailnet", "100.127.255.254": "tailnet", "fd7a:115c:a1e0:ab12::1": "tailnet",
|
|
"10.1.2.3": "private", "172.16.0.1": "private", "172.31.255.255": "private", "192.168.1.2": "private", "fd12:3456::1": "private",
|
|
"172.32.0.1": "public", "100.128.0.1": "public", "8.8.8.8": "public", "2606:4700::1111": "public", "::ffff:8.8.8.8": "public",
|
|
"224.0.0.251": "multicast", "ff02::fb": "multicast", "255.255.255.255": "reserved",
|
|
};
|
|
for (const [address, category] of Object.entries(cases)) assert.equal(classify(address), category, address);
|
|
});
|
|
|
|
test("loopback, metadata, link-local and public peers are refused by default", async () => {
|
|
const dns = fakeDns({ "evil.example": v4("127.0.0.1"), "public.example": v4("93.184.216.34") });
|
|
const policy = new PeerAddressPolicy({ env: {}, lookup: dns.lookup, discovered: new Set() });
|
|
for (const url of [
|
|
"ws://127.0.0.1:8080/connect", "ws://localhost.:8080/connect", "ws://[::1]:8083/connect",
|
|
"ws://169.254.169.254/connect", "ws://[fe80::1]:8083/connect", "ws://0.0.0.0:8083/connect",
|
|
"ws://100.100.100.100/connect", "ws://evil.example:8083/connect", "ws://public.example:8083/connect",
|
|
"ws://8.8.8.8:8083/connect", "ws://unresolved.example/connect",
|
|
]) {
|
|
await assert.rejects(policy.pin(new URL(url)), PeerAddressError, url);
|
|
}
|
|
});
|
|
|
|
test("tailnet, LAN, ts.net and discovered peers are accepted", async () => {
|
|
const dns = fakeDns({
|
|
"box.tail1234.ts.net": v4("100.101.102.103"),
|
|
"funnel.tail1234.ts.net": v4("203.0.113.9"),
|
|
"desktop.local": [v4("192.168.1.20"), v6("fd12::20")],
|
|
"lab.example": v4("198.51.100.7"),
|
|
});
|
|
const policy = new PeerAddressPolicy({ env: {}, lookup: dns.lookup, discovered: new Set(["lab.example"]) });
|
|
for (const [url, address] of [
|
|
["ws://100.64.1.2:8083/connect", "100.64.1.2"],
|
|
["wss://box.tail1234.ts.net/connect", "100.101.102.103"],
|
|
["wss://funnel.tail1234.ts.net/connect", "203.0.113.9"],
|
|
["ws://desktop.local:8083/connect", "192.168.1.20"],
|
|
["ws://10.0.0.75:8083/connect", "10.0.0.75"],
|
|
["ws://[fd7a:115c:a1e0::1]:8083/connect", "fd7a:115c:a1e0::1"],
|
|
["ws://lab.example:8083/connect", "198.51.100.7"],
|
|
])
|
|
assert.equal((await policy.pin(new URL(url))).address, address, url);
|
|
// A name that also resolves to a refused address is refused outright.
|
|
const mixed = new PeerAddressPolicy({ env: {}, lookup: fakeDns({ "mixed.local": [v4("192.168.1.2"), v4("127.0.0.1")] }).lookup });
|
|
await assert.rejects(mixed.pin(new URL("ws://mixed.local/connect")), PeerAddressError);
|
|
// Discovery never unlocks loopback or metadata.
|
|
const discovered = new PeerAddressPolicy({ env: {}, lookup: fakeDns({ "self.local": v4("127.0.0.1") }).lookup, discovered: new Set(["self.local"]) });
|
|
await assert.rejects(discovered.pin(new URL("ws://self.local/connect")), PeerAddressError);
|
|
});
|
|
|
|
test("PIPELINES_PEER_ALLOW widens the policy explicitly", async () => {
|
|
const loopback = new PeerAddressPolicy({ env: { PIPELINES_PEER_ALLOW: "loopback" } });
|
|
assert.equal((await loopback.pin(new URL("ws://127.0.0.1:1/connect"))).address, "127.0.0.1");
|
|
await assert.rejects(loopback.pin(new URL("ws://169.254.169.254/connect")), PeerAddressError);
|
|
const any = new PeerAddressPolicy({ env: { PIPELINES_PEER_ALLOW: "any" } });
|
|
assert.equal((await any.pin(new URL("ws://169.254.169.254/connect"))).address, "169.254.169.254");
|
|
await assert.rejects(any.pin(new URL("ws://0.0.0.0/connect")), PeerAddressError);
|
|
});
|
|
|
|
test("the checked DNS answer is pinned for the request (no rebinding)", async (t) => {
|
|
const answers = { "peer.example": v4("192.168.1.9") };
|
|
const dns = fakeDns(answers);
|
|
const policy = new PeerAddressPolicy({ env: {}, lookup: dns.lookup });
|
|
const pin = await policy.pin(new URL("ws://peer.example:8083/connect"));
|
|
// The name now "rebinds" to loopback; the pinned lookup still answers the checked address.
|
|
answers["peer.example"] = v4("127.0.0.1");
|
|
const one = await new Promise((done) => pin.lookup("peer.example", {}, (e, a, f) => done([a, f])));
|
|
const all = await new Promise((done) => pin.lookup("peer.example", { all: true }, (e, list) => done(list)));
|
|
assert.deepEqual(one, ["192.168.1.9", 4]);
|
|
assert.deepEqual(all, [{ address: "192.168.1.9", family: 4 }]);
|
|
assert.deepEqual(dns.calls, ["peer.example"]);
|
|
// A real request connects to the pinned address while naming the original host.
|
|
const server = createServer((req, res) => res.end(req.headers.host));
|
|
server.listen(0, "127.0.0.1");
|
|
await once(server, "listening");
|
|
t.after(() => server.close());
|
|
const port = server.address().port;
|
|
const pinnedLoopback = await new PeerAddressPolicy({ env: { PIPELINES_PEER_ALLOW: "loopback" }, lookup: fakeDns({ "never-resolves.invalid": v4("127.0.0.1") }).lookup })
|
|
.pin(new URL(`ws://never-resolves.invalid:${port}/connect`));
|
|
const host = await new Promise((done, fail) => {
|
|
const request = remoteRequest(new URL(`http://never-resolves.invalid:${port}/`), { lookup: pinnedLoopback.lookup });
|
|
request.on("response", async (response) => {
|
|
let body = "";
|
|
for await (const chunk of response) body += chunk;
|
|
done(body);
|
|
});
|
|
request.on("error", fail);
|
|
request.end();
|
|
});
|
|
assert.equal(host, `never-resolves.invalid:${port}`);
|
|
});
|
|
|
|
test("the relay refuses a loopback peer without contacting it and flags plain http", async (t) => {
|
|
const seen = [];
|
|
const remote = createServer((req, res) => {
|
|
seen.push(req.url);
|
|
res.writeHead(302, { Location: "/" }).end();
|
|
});
|
|
remote.listen(0, "127.0.0.1");
|
|
await once(remote, "listening");
|
|
t.after(() => remote.close());
|
|
const dir = await mkdtemp(join(tmpdir(), "peer-address-"));
|
|
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
const auth = join(dir, "auth.json");
|
|
await writeFile(auth, JSON.stringify({ password: "local-password", secret: "s".repeat(40) }));
|
|
const reserve = createServer().listen(0, "127.0.0.1");
|
|
await once(reserve, "listening");
|
|
const port = reserve.address().port;
|
|
await new Promise((done) => reserve.close(done));
|
|
const start = async (allow) => {
|
|
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--pipelines", `127.0.0.1:${remote.address().port}`], {
|
|
cwd: fileURLToPath(new URL("..", import.meta.url)),
|
|
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth, PIPELINES_PEER_ALLOW: allow, PIPELINES_SERVER_TOKEN_FILE: join(dir, "absent") },
|
|
stdio: ["ignore", "pipe", "inherit"],
|
|
});
|
|
await once(child.stdout, "data");
|
|
const base = `http://127.0.0.1:${port}`;
|
|
const login = await fetch(base + "/login", { method: "POST", body: new URLSearchParams({ password: "local-password" }), redirect: "manual" });
|
|
const cookie = login.headers.get("set-cookie").split(";")[0];
|
|
const register = (endpoints) =>
|
|
fetch(base + "/api/peers/p", {
|
|
method: "POST",
|
|
headers: { Cookie: cookie, Origin: base, "Content-Type": "application/json" },
|
|
body: JSON.stringify({ endpoints, password: "remote-password" }),
|
|
});
|
|
return { child, register };
|
|
};
|
|
const strict = await start("");
|
|
for (const endpoint of [`ws://127.0.0.1:${remote.address().port}/connect`, "ws://169.254.169.254/connect", `ws://[::1]:${remote.address().port}/connect`]) {
|
|
const reply = await strict.register([endpoint]);
|
|
assert.equal(reply.status, 400, endpoint);
|
|
const body = await reply.json();
|
|
assert.match(body.error, /not allowed/);
|
|
assert.equal(body.refused_endpoints[0].endpoint, endpoint);
|
|
}
|
|
assert.deepEqual(seen, []);
|
|
const stopped = once(strict.child, "exit");
|
|
strict.child.kill();
|
|
await stopped;
|
|
const allowed = await start("loopback");
|
|
t.after(() => allowed.child.kill());
|
|
const reply = await allowed.register([`ws://127.0.0.1:${remote.address().port}/connect`]);
|
|
const body = await reply.json();
|
|
assert.equal(reply.status, 200);
|
|
assert.equal(body.connected, true);
|
|
assert.equal(body.insecure_transport, true);
|
|
assert.ok(seen.includes("/login"));
|
|
});
|