Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Accounts: fixes from the review, on the server

Adarsh6c8091aa063d
diff --git a/docs/public-signup.md b/docs/public-signup.md
index 1931f50a..ef543c7d 100644
--- a/docs/public-signup.md
+++ b/docs/public-signup.md
@@ -120,8 +120,10 @@ deleted (by itself or by the owner), its runs are stopped and removed, and its u
are deleted with it.
An account changes its password in **Settings → Account**: `PATCH /api/auth/me` with
-`{ "current", "next" }` (at least 8 characters, 5 tries a minute). Every other browser signed in to
-the account is signed out; the browser that changed it gets a new session.
+`{ "current", "next" }` (at least 8 characters, and not the current one). Every other browser signed in to the account is
+signed out; the browser that changed it gets a new session. Changing the password and deleting the
+account share one limit: 5 password checks a minute per account. A browser that was signed out gets
+401 from both, so the page can offer Log in; the owner gets 403.
## Your GPU
diff --git a/src/app/api/auth/me/route.ts b/src/app/api/auth/me/route.ts
index 1ae72966..94053480 100644
--- a/src/app/api/auth/me/route.ts
+++ b/src/app/api/auth/me/route.ts
@@ -31,16 +31,30 @@ export async function GET(request: Request) {
}
/**
- * An account deletes itself (its password again, to be sure). Signs it out. Five password checks a minute per account:
- * each is a slow check on purpose.
+ * Five password checks a minute per account, deleting it and changing its password together: each is a slow check on
+ * purpose, and either one tells whether a guess was right.
*/
+const passwordCheck = (id: string) => hit(`password-check:${id}`, 5, 60_000);
+
+/**
+ * A request from no account: signed out (no session, or one that ended when the password was changed in another browser)
+ * is a 401, so the page can show Log in; the owner, whose password isn't an account's, a 403.
+ */
+function notAnAccount(request: Request, signedOut: string, owner: string): Response {
+ const isOwner = !authEnabled() || validToken(request.headers.get("authorization"));
+ return Response.json({ error: isOwner ? owner : signedOut }, { status: isOwner ? 403 : 401 });
+}
+
+/** An account deletes itself (its password again, to be sure). Signs it out. */
export async function DELETE(request: Request) {
const jar = await cookies();
const session = sessionViewer(sessionCookieOf(request));
- if (session?.kind !== "account") return Response.json({ error: "Sign in to the account you want to delete" }, { status: 403 });
+ const ownerRefusal = "This deletes an account, not the studio";
+ if (!session) return notAnAccount(request, "You're signed out. Log in again to delete your account.", ownerRefusal);
+ if (session.kind !== "account") return Response.json({ error: ownerRefusal }, { status: 403 });
const parsed = z.object({ password: z.string().min(1).max(500) }).safeParse(await request.json().catch(() => null));
if (!parsed.success) return Response.json({ error: "Wrong password" }, { status: 403 });
- const rate = hit(`delete-me:${session.account.id}`, 5, 60_000);
+ const rate = passwordCheck(session.account.id);
if (!rate.ok) return Response.json({ error: "Too many tries. Wait a minute, then try again." }, { status: 429, headers: { "Retry-After": String(rate.retryAfterS) } });
if (!(await verifyAccount(session.account.email, parsed.data.password))) return Response.json({ error: "Wrong password" }, { status: 403 });
// Switched off at once, its running jobs stopped (on its own keys), then its runs, files and record go.
@@ -56,18 +70,22 @@ const NewPassword = z.object({
/**
* An account changes its password (the current one first, to be sure). Its other sign-ins end; this browser gets a new
- * session cookie. Five password checks a minute per account, as for deleting it.
+ * session cookie.
*/
export async function PATCH(request: Request) {
const jar = await cookies();
const session = sessionViewer(sessionCookieOf(request));
- if (session?.kind !== "account") return Response.json({ error: "Sign in to change your password" }, { status: 403 });
+ const ownerRefusal = "This changes an account's password, not the studio's";
+ if (!session) return notAnAccount(request, "You're signed out. Log in again to change your password.", ownerRefusal);
+ if (session.kind !== "account") return Response.json({ error: ownerRefusal }, { status: 403 });
const parsed = NewPassword.safeParse(await request.json().catch(() => null));
if (!parsed.success) {
const next = parsed.error.issues.find((i) => i.path[0] === "next");
return Response.json({ error: next?.message ?? "Enter your current password and a new one" }, { status: 400 });
}
- const rate = hit(`password-me:${session.account.id}`, 5, 60_000);
+ // Not a change: it would sign the other browsers out for nothing. (Two of the request's own words: no password check.)
+ if (parsed.data.next === parsed.data.current) return Response.json({ error: "Choose a new password, not the one you use now" }, { status: 400 });
+ const rate = passwordCheck(session.account.id);
if (!rate.ok) return Response.json({ error: "Too many tries. Wait a minute, then try again." }, { status: 429, headers: { "Retry-After": String(rate.retryAfterS) } });
try {
await setAccountPassword(session.account.id, parsed.data.current, parsed.data.next);
diff --git a/src/server/auth.ts b/src/server/auth.ts
index 78370c7c..239d63a5 100644
--- a/src/server/auth.ts
+++ b/src/server/auth.ts
@@ -219,8 +219,8 @@ const fingerprint = (s: string) => crypto.createHash("sha256").update(s).digest(
/**
* The key a session is signed with: the secret and a password's fingerprint. The owner's password (FUOBE_PASSWORD, or
* the Settings one): a new one (after a restart, or in Settings) ends every owner sign-in at once, and the secret alone
- * (auth.json) can't make an owner cookie. An account's own password hash: its new password ends its other sign-ins,
- * and the owner's new password signs no account out.
+ * (auth.json) can't make an owner cookie. An account's own password hash: its new password ends its other sign-ins.
+ * A new FUOBE_PASSWORD signs no account out; a password set in Settings makes a new secret, which signs out everyone.
*/
function keyFor(d: AuthFile, who: "owner" | Pick<Account, "hash">): string {
return `${d.secret}.${fingerprint(who === "owner" ? envPassword() || d.password || "" : who.hash)}`;
diff --git a/tests/public-signup.test.ts b/tests/public-signup.test.ts
index a8991033..2ec78ebe 100644
--- a/tests/public-signup.test.ts
+++ b/tests/public-signup.test.ts
@@ -421,6 +421,12 @@ describe("an account changes its password (Settings → Account)", () => {
headers: { "content-type": "application/json", cookie: `${auth.SESSION_COOKIE}=${cookie}` },
body: JSON.stringify(body),
});
+ const remove = (cookie: string, body: unknown) =>
+ new Request("http://localhost:3000/api/auth/me", {
+ method: "DELETE",
+ headers: { "content-type": "application/json", cookie: `${auth.SESSION_COOKIE}=${cookie}` },
+ body: JSON.stringify(body),
+ });
const secret = () => (JSON.parse(fs.readFileSync(path.join(dir, "auth.json"), "utf8")) as { secret: string }).secret;
/** An account cookie signed with the secret alone, as before account keys held the password's fingerprint. */
const legacy = (id: string) => {
@@ -432,7 +438,10 @@ describe("an account changes its password (Settings → Account)", () => {
it("needs the current password, ends every other sign-in, and keeps this browser signed in", async () => {
const a = auth.createAccount("[email protected]", "first password");
const here = auth.createSession(a.id).value;
+ // Another browser's sign-in, a moment later: a different cookie, not the same string again.
+ await new Promise((r) => setTimeout(r, 5));
const elsewhere = auth.createSession(a.id).value;
+ expect(elsewhere).not.toBe(here);
const old = legacy(a.id);
const owner = auth.createSession().value;
// A cookie from before this change still works, until the account changes its password.
@@ -444,7 +453,12 @@ describe("an account changes its password (Settings → Account)", () => {
const short = await (await me()).PATCH(patch(here, { current: "first password", next: "short" }));
expect(short.status).toBe(400);
expect((await short.json()).error).toMatch(/8 characters/);
+ // The same password again isn't a change: nobody is signed out for it.
+ const same = await (await me()).PATCH(patch(here, { current: "first password", next: "first password" }));
+ expect(same.status).toBe(400);
+ expect((await same.json()).error).toMatch(/new password/);
expect(auth.sessionViewer(here)?.kind).toBe("account");
+ expect(auth.sessionViewer(elsewhere)?.kind).toBe("account");
expect(jar.set).not.toHaveBeenCalled();
const ok = await (await me()).PATCH(patch(here, { current: "first password", next: "second password" }));
@@ -461,8 +475,10 @@ describe("an account changes its password (Settings → Account)", () => {
expect(auth.checkAccount("[email protected]", "second password")?.id).toBe(a.id);
expect(auth.checkAccount("[email protected]", "first password")).toBeNull();
expect(auth.validSession(owner)).toBe(true);
- // The old cookie can't try again.
- expect((await (await me()).PATCH(patch(here, { current: "second password", next: "third password" }))).status).toBe(403);
+ // The old cookie can't try again: it's signed out (401, so the page shows Log in).
+ const again = await (await me()).PATCH(patch(here, { current: "second password", next: "third password" }));
+ expect(again.status).toBe(401);
+ expect((await again.json()).error).toMatch(/signed out/);
});
it("allows five tries a minute, then answers 429", async () => {
@@ -475,10 +491,34 @@ describe("an account changes its password (Settings → Account)", () => {
expect(auth.checkAccount("[email protected]", "the real one")?.id).toBe(a.id);
});
- it("refuses a guest and the owner: it's an account's own password", async () => {
+ it("counts deleting the account with changing its password: five guesses a minute in all, not five each", async () => {
+ const a = auth.createAccount("[email protected]", "the real one");
+ const cookie = auth.createSession(a.id).value;
+ for (let i = 0; i < 3; i++) expect((await (await me()).PATCH(patch(cookie, { current: `guess ${i}`, next: "a new password" }))).status).toBe(403);
+ for (let i = 3; i < 5; i++) expect((await (await me()).DELETE(remove(cookie, { password: `guess ${i}` }))).status).toBe(403);
+ expect((await (await me()).DELETE(remove(cookie, { password: "the real one" }))).status).toBe(429);
+ expect((await (await me()).PATCH(patch(cookie, { current: "the real one", next: "a new password" }))).status).toBe(429);
+ expect(auth.checkAccount("[email protected]", "the real one")?.id).toBe(a.id);
+ });
+
+ it("refuses a guest (signed out: 401) and the owner (403): it's an account's own password", async () => {
const owner = auth.createSession().value;
expect((await (await me()).PATCH(patch(owner, { current: "install password", next: "a new password" }))).status).toBe(403);
- expect((await (await me()).PATCH(patch("", { current: "x", next: "a new password" }))).status).toBe(403);
+ expect((await (await me()).PATCH(patch("", { current: "x", next: "a new password" }))).status).toBe(401);
expect(auth.checkPassword("install password")).toBe(true);
});
+
+ it("deleting from a browser that was signed out elsewhere is a 401 (Log in), not a wrong password", async () => {
+ const a = auth.createAccount("[email protected]", "first password");
+ const here = auth.createSession(a.id).value;
+ await new Promise((r) => setTimeout(r, 5));
+ const elsewhere = auth.createSession(a.id).value;
+ expect((await (await me()).PATCH(patch(elsewhere, { current: "first password", next: "second password" }))).status).toBe(200);
+ const del = await (await me()).DELETE(remove(here, { password: "second password" }));
+ expect(del.status).toBe(401);
+ expect((await del.json()).error).toMatch(/signed out/);
+ expect(auth.checkAccount("[email protected]", "second password")?.id).toBe(a.id);
+ // The owner still gets a 403: the studio isn't an account.
+ expect((await (await me()).DELETE(remove(auth.createSession().value, { password: "install password" }))).status).toBe(403);
+ });
});