diff --git a/web/src/peerRelay.js b/web/src/peerRelay.js index 48bf40c2..18525a84 100644 --- a/web/src/peerRelay.js +++ b/web/src/peerRelay.js @@ -17,6 +17,8 @@ const allowed = (path) => path === "/chat-uploads" || path.startsWith("/artifacts/") || path.startsWith("/updates/"); +// Raw-body uploads: chat attachments and files stored into a home folder. +const postable = (path) => path === "/chat-uploads" || path === "/files"; export class PeerRelay extends PeerRegistry { target(req, path) { const parsed = new URL(req.url, "http://localhost"); @@ -45,7 +47,7 @@ export class PeerRelay extends PeerRegistry { !target || !allowed(target.url.pathname) || !["GET", "HEAD", "POST"].includes(req.method) || - (req.method === "POST" && target.url.pathname !== "/chat-uploads") + (req.method === "POST" && !postable(target.url.pathname)) ) { json(res, 404, { error: "Unsupported remote resource" }); return true; diff --git a/web/test/peerRelay.test.mjs b/web/test/peerRelay.test.mjs index 6cee5a8c..6869a5fc 100644 --- a/web/test/peerRelay.test.mjs +++ b/web/test/peerRelay.test.mjs @@ -76,6 +76,22 @@ async function fixture(t) { return; } res.setHeader("Set-Cookie", "do-not-forward=private"); + if (req.method === "POST") { + let body = ""; + for await (const chunk of req) body += chunk; + res.end( + JSON.stringify({ + name, + method: req.method, + url: req.url, + cookie: req.headers.cookie, + upload: req.headers["x-pipelines-upload-name"], + type: req.headers["content-type"], + body, + }), + ); + return; + } if ( req.url.startsWith("/artifacts/") || req.url.includes("untrusted.svg") @@ -289,6 +305,46 @@ test("browser can authenticate both machines, relay files and keep local credent ); }); +test("file uploads relay to the chosen machine's home folder and other POSTs are refused", async (t) => { + const f = await fixture(t); + await f.register("one", { + endpoints: [f.remotes[0]], + password: "password-one", + }); + const upload = (path, headers = {}) => + fetch(f.base + `/peers/one${path}`, { + method: "POST", + headers: { + Cookie: f.cookie, + "Content-Type": "text/plain", + "X-Pipelines-Upload-Name": "notes%20two.txt", + ...headers, + }, + body: "hello upload", + }); + const reply = await upload("/files?path=%2Fhome%2Fproject"); + assert.equal(reply.status, 200); + assert.equal(reply.headers.get("set-cookie"), null); + assert.deepEqual(await reply.json(), { + name: "one", + method: "POST", + url: "/files?path=%2Fhome%2Fproject", + cookie: "pipelines_session=remote-one", + upload: "notes%20two.txt", + type: "text/plain", + body: "hello upload", + }); + // The remote never sees a POST the relay does not allow. + const before = f.observations.length; + assert.equal((await upload("/artifacts/1/2/report.txt")).status, 404); + assert.equal((await upload("/discovery")).status, 404); + assert.equal(f.observations.length, before); + assert.equal( + (await upload("/files?path=%2Fhome", { Cookie: await f.login() })).status, + 401, + ); +}); + test("websocket relay authenticates the chosen machine without forwarding browser Origin", async (t) => { const f = await fixture(t); await f.register("two", { diff --git a/web/test/serve.test.mjs b/web/test/serve.test.mjs index f08e9f3f..b85b4966 100644 --- a/web/test/serve.test.mjs +++ b/web/test/serve.test.mjs @@ -139,3 +139,53 @@ test("files require sign-in and proxy query, ranges and attachment headers", asy { method: "HEAD", url: path, range: undefined }, ]); }); + +test("file uploads require sign-in and reach the server with their name header", async (t) => { + const requests = []; + const upstream = createServer(async (req, res) => { + let body = ""; + for await (const chunk of req) body += chunk; + requests.push({ + method: req.method, + url: req.url, + upload: req.headers["x-pipelines-upload-name"], + type: req.headers["content-type"], + body, + }); + res.writeHead(409, { "Content-Type": "text/plain; charset=utf-8" }); + res.end("a file named notes.txt already exists"); + }).listen(0, "127.0.0.1"); + await once(upstream, "listening"); + t.after(() => new Promise((done) => upstream.close(done))); + const dir = await mkdtemp(join(tmpdir(), "serve-upload-")); + const auth = join(dir, "auth.json"); + await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) })); + const port = await freePort(); + const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), + "--pipelines", `127.0.0.1:${upstream.address().port}`], { + cwd: here, + env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, + stdio: ["ignore", "pipe", "inherit"], + }); + t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; }); + await once(child.stdout, "data"); + const base = `http://127.0.0.1:${port}`; + const path = "/files?path=%2Fhome%2Fproject"; + const post = (headers) => fetch(base + path, { + method: "POST", + headers: { "Content-Type": "text/plain", "X-Pipelines-Upload-Name": "notes.txt", ...headers }, + body: "hello upload", + }); + assert.equal((await post({})).status, 401); + assert.equal(requests.length, 0); + const login = await fetch(base + "/login", { + method: "POST", body: new URLSearchParams({ password: "password-1" }), redirect: "manual", + }); + const cookie = login.headers.get("set-cookie").split(";")[0]; + const reply = await post({ Cookie: cookie }); + assert.equal(reply.status, 409); + assert.equal(await reply.text(), "a file named notes.txt already exists"); + assert.deepEqual(requests, [ + { method: "POST", url: path, upload: "notes.txt", type: "text/plain", body: "hello upload" }, + ]); +});