Send the server token from the web proxy and limit peer relay addresses
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.
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
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"));
|
||||
});
|
||||
@@ -175,6 +175,8 @@ async function fixture(t) {
|
||||
...process.env,
|
||||
PIPELINES_WEB_AUTH: "",
|
||||
PIPELINES_WEB_AUTH_FILE: auth,
|
||||
// The fixture machines listen on loopback, which the relay refuses by default.
|
||||
PIPELINES_PEER_ALLOW: "loopback",
|
||||
},
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
},
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer, request } from "node:http";
|
||||
import { createHash } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, writeFile, chmod, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { serverToken, withServerToken } from "../src/serverToken.js";
|
||||
|
||||
const TOKEN = "proxy-token-0123456789abcdef0123456789abcdef";
|
||||
|
||||
async function freePort() {
|
||||
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));
|
||||
return port;
|
||||
}
|
||||
|
||||
test("the token file must be private and is never taken from the client", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "server-token-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const path = join(dir, "server-token");
|
||||
assert.equal(serverToken(path), "");
|
||||
await writeFile(path, TOKEN + "\n", { mode: 0o600 });
|
||||
await chmod(path, 0o600);
|
||||
assert.equal(serverToken(path), TOKEN);
|
||||
await chmod(path, 0o644);
|
||||
assert.throws(() => serverToken(path), /private/);
|
||||
assert.deepEqual(withServerToken({ "X-Pipelines-Token": "browser-chosen", a: "b" }, TOKEN), { a: "b", "x-pipelines-token": TOKEN });
|
||||
assert.deepEqual(withServerToken({ "x-pipelines-token": "browser-chosen" }, ""), {});
|
||||
});
|
||||
|
||||
test("the proxy sends the server token on every upstream request and the relay never forwards it", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "server-token-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const tokenFile = join(dir, "server-token");
|
||||
await writeFile(tokenFile, TOKEN, { mode: 0o600 });
|
||||
await chmod(tokenFile, 0o600);
|
||||
const seen = [];
|
||||
const upstream = createServer((req, res) => {
|
||||
seen.push({ path: req.url.split("?")[0], token: req.headers["x-pipelines-token"], host: req.headers.host, origin: req.headers.origin });
|
||||
if (req.url === "/discovery") {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(JSON.stringify({ discovered_servers: [] }));
|
||||
return;
|
||||
}
|
||||
if (req.url === "/login") {
|
||||
res.writeHead(302, { Location: "/" }).end();
|
||||
return;
|
||||
}
|
||||
res.end("ok");
|
||||
});
|
||||
upstream.on("upgrade", (req, socket) => {
|
||||
seen.push({ path: req.url, token: req.headers["x-pipelines-token"], host: req.headers.host, origin: req.headers.origin });
|
||||
const accept = createHash("sha1").update(req.headers["sec-websocket-key"] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");
|
||||
socket.end(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`);
|
||||
});
|
||||
upstream.listen(0, "127.0.0.1");
|
||||
await once(upstream, "listening");
|
||||
t.after(() => { upstream.closeAllConnections(); upstream.close(); });
|
||||
const upstreamHost = `127.0.0.1:${upstream.address().port}`;
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--pipelines", upstreamHost], {
|
||||
cwd: fileURLToPath(new URL("..", import.meta.url)),
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled", PIPELINES_SERVER_TOKEN_FILE: tokenFile, PIPELINES_PEER_ALLOW: "loopback" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
t.after(() => child.kill());
|
||||
await once(child.stdout, "data");
|
||||
const base = `http://127.0.0.1:${port}`;
|
||||
const forged = { "X-Pipelines-Token": "browser-chosen" };
|
||||
assert.equal((await fetch(`${base}/files?path=a.txt`, { headers: forged })).status, 200);
|
||||
assert.equal((await fetch(`${base}/artifacts/1/1/a.txt`, { headers: forged })).status, 200);
|
||||
assert.equal((await fetch(`${base}/chat-uploads`, { method: "POST", headers: forged, body: "x" })).status, 200);
|
||||
assert.equal((await fetch(`${base}/api/discovery`, { headers: forged })).status, 200);
|
||||
const upgraded = await new Promise((done, fail) => {
|
||||
const req = request(`${base}/connect`, {
|
||||
headers: { Connection: "Upgrade", Upgrade: "websocket", "Sec-WebSocket-Version": "13", "Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==", Origin: base, ...forged },
|
||||
});
|
||||
req.on("upgrade", (response, socket) => { socket.destroy(); done(response.statusCode); });
|
||||
req.on("response", (response) => done(response.statusCode));
|
||||
req.on("error", fail);
|
||||
req.end();
|
||||
});
|
||||
assert.equal(upgraded, 101);
|
||||
const local = seen.filter((entry) => entry.path !== "/login");
|
||||
assert.deepEqual(local.map((entry) => entry.path), ["/files", "/artifacts/1/1/a.txt", "/chat-uploads", "/discovery", "/connect"]);
|
||||
for (const entry of local) {
|
||||
assert.equal(entry.token, TOKEN, entry.path);
|
||||
assert.equal(entry.host, upstreamHost, entry.path);
|
||||
assert.equal(entry.origin, undefined, entry.path);
|
||||
}
|
||||
// A peer machine (here the same fixture, reached through the relay) never
|
||||
// receives this machine's server token.
|
||||
seen.length = 0;
|
||||
const reg = await fetch(`${base}/api/peers/peer`, {
|
||||
method: "POST",
|
||||
headers: { Origin: base, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ endpoints: [`ws://${upstreamHost}/connect`] }),
|
||||
});
|
||||
assert.equal((await reg.json()).connected, true);
|
||||
assert.equal((await fetch(`${base}/peers/peer/files?path=a.txt`, { headers: forged })).status, 200);
|
||||
assert.ok(seen.length >= 2);
|
||||
for (const entry of seen) assert.equal(entry.token, undefined, entry.path);
|
||||
});
|
||||
Reference in New Issue
Block a user