Relay POST /files uploads through the web proxy and peer relay
The direct proxy already forwards every /files method behind the sign-in gate; the peer relay now allows POST /files beside /chat-uploads. Tests cover the name header, body and refusal of other POST targets.
This commit is contained in:
@@ -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" },
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user