Proxy authenticated file explorer downloads and previews

This commit is contained in:
2026-11-07 01:51:12 +00:00
parent 54ffb5d022
commit b33970182c
3 changed files with 62 additions and 3 deletions
+50
View File
@@ -6,6 +6,8 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import net from "node:net";
import { createServer } from "node:http";
import { once } from "node:events";
const here = fileURLToPath(new URL("..", import.meta.url));
@@ -89,3 +91,51 @@ test("icons and the manifest need no sign-in; the app does", async (t) => {
assert.equal((await fetch(`${base}/main.dart.js`)).status, 401);
assert.equal((await fetch(`${base}/icons/../main.dart.js`)).status, 401);
});
test("files require sign-in and proxy query, ranges and attachment headers", async (t) => {
const requests = [];
const upstream = createServer((req, res) => {
requests.push({ method: req.method, url: req.url, range: req.headers.range });
res.writeHead(req.headers.range ? 206 : 200, {
"Content-Type": "application/octet-stream",
"Content-Disposition": 'attachment; filename="report.bin"',
"Accept-Ranges": "bytes",
...(req.headers.range ? { "Content-Range": "bytes 0-2/6" } : {}),
});
res.end(req.headers.range ? "abc" : "abcdef");
}).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-files-"));
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%2Freport%20one.bin&download=1";
assert.equal((await fetch(base + path)).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 fetch(base + path, { headers: { Cookie: cookie, Range: "bytes=0-2" } });
assert.equal(reply.status, 206);
assert.equal(await reply.text(), "abc");
assert.equal(reply.headers.get("content-disposition"), 'attachment; filename="report.bin"');
assert.equal(reply.headers.get("content-range"), "bytes 0-2/6");
const head = await fetch(base + path, { method: "HEAD", headers: { Cookie: cookie } });
assert.equal(head.status, 200);
assert.equal(await head.text(), "");
assert.deepEqual(requests, [
{ method: "GET", url: path, range: "bytes=0-2" },
{ method: "HEAD", url: path, range: undefined },
]);
});