Reload web password safely without restarting active sessions

This commit is contained in:
2026-11-08 21:47:03 +00:00
parent 828227f549
commit 8016108558
2 changed files with 51 additions and 5 deletions
+14 -5
View File
@@ -47,7 +47,7 @@ const AUTH_FILE =
process.env.PIPELINES_WEB_AUTH_FILE ?? join(homedir(), ".config", "pipelines", "web-auth.json");
const SESSION_COOKIE = "pipelines_session";
function loadAuth() {
function loadAuth(fatal = true) {
if (AUTH_DISABLED) return null;
try {
const parsed = JSON.parse(readFileSync(AUTH_FILE, "utf8"));
@@ -62,12 +62,13 @@ function loadAuth() {
} catch {
// Fall through to the refusal below.
}
console.error(
`web auth: refusing to start without ${AUTH_FILE} (or PIPELINES_WEB_AUTH=disabled for tests)`,
);
if (!fatal) throw new Error("Web authentication is temporarily unavailable");
console.error(`web auth: refusing to start without ${AUTH_FILE} (or PIPELINES_WEB_AUTH=disabled for tests)`);
process.exit(1);
}
const auth = loadAuth();
let auth = loadAuth();
function refreshAuth() { auth = loadAuth(false); }
function cookieValue(req, name) {
for (const part of String(req.headers.cookie ?? "").split(";")) {
@@ -387,6 +388,10 @@ function proxyArtifact(req, res) {
}
const server = createServer(async (req, res) => {
try { refreshAuth(); } catch {
res.writeHead(503, { "Content-Type": "text/plain", "Cache-Control": "no-store" }).end("Web authentication unavailable");
return;
}
const pathname = (req.url ?? "/").split("?")[0];
// Icons and the web manifest are public: the sign-in page shows the icon,
// and browsers and app installers fetch them without the session cookie.
@@ -543,6 +548,10 @@ function proxyUpgrade(req, clientSocket, clientHead) {
}
server.on("upgrade", (req, clientSocket, clientHead) => {
try { refreshAuth(); } catch {
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n");
return;
}
const pathname = (req.url ?? "").split("?")[0];
if (!sameOriginUpgrade(req.headers.origin, req.headers.host)) {
clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
+37
View File
@@ -0,0 +1,37 @@
import test from "node:test";
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { mkdtemp, writeFile, rename, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { createServer } from "node:net";
import { once } from "node:events";
test("atomic password updates take effect without restart and fail closed on invalid files", async (t) => {
const dir = await mkdtemp(join(tmpdir(), "pipelines-auth-reload-"));
t.after(() => rm(dir, { recursive: true, force: true }));
const auth = join(dir, "auth.json");
const data = { password: "old-password", secret: "s".repeat(40) };
await writeFile(auth, JSON.stringify(data));
const portServer = createServer().listen(0,"127.0.0.1"); await once(portServer,"listening");
const port = portServer.address().port; await new Promise((done) => portServer.close(done));
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port)], {
cwd: fileURLToPath(new URL("..", import.meta.url)),
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, stdio:["ignore","pipe","inherit"] });
t.after(async () => { const stopped = once(child,"exit"); child.kill(); await stopped; });
await once(child.stdout,"data");
const base = `http://127.0.0.1:${port}`;
const login = (password) => fetch(base + "/login", { method:"POST", body:new URLSearchParams({password}), redirect:"manual" });
const first = await login("old-password"); assert.equal(first.status,303);
const cookie = first.headers.get("set-cookie").split(";")[0];
await writeFile(auth + ".next", JSON.stringify({ ...data, password:"new-password" })); await rename(auth + ".next",auth);
assert.equal((await login("old-password")).status,401);
assert.equal((await login("new-password")).status,303);
assert.equal((await fetch(base + "/login", {headers:{Cookie:cookie},redirect:"manual"})).status,302);
await writeFile(auth + ".next", "malformed"); await rename(auth + ".next",auth);
assert.equal((await login("new-password")).status,503);
assert.equal((await fetch(base + "/login", {headers:{Cookie:cookie},redirect:"manual"})).status,503);
await writeFile(auth,JSON.stringify(data));
assert.equal((await login("old-password")).status,303);
});