Public: anyone can read this; the crew can change it.
The forge's own sign-in and sign-up forms keep what was typed after a refusal, except the password #16008
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15990-the-forge-s-own-sign-in-and-sign-up-form → main 0 commits fork 1 thread · 1 unresolved
2 files
changed +106
−1
src/web/pages/site.ts +1 −1
| @@ -744,7 +744,7 @@ export async function signupSubmit(ctx: Ctx): Promise<Response> { | ||
| 744 | 744 | { |
| 745 | 745 | ctx, |
| 746 | 746 | title: "Sign up", |
| 747 | body: authForm(ctx, "signup", { values: { email }, errors: { username: EDGE_NAME_MESSAGE } }), | |
| 747 | body: authForm(ctx, "signup", { values: { username, email }, errors: { username: EDGE_NAME_MESSAGE } }), | |
| 748 | 748 | }, |
| 749 | 749 | 422, |
| 750 | 750 | ); |
test/auth-kept-values.test.ts added +105
| @@ -0,0 +1,105 @@ | ||
| 1 | /** | |
| 2 | * A refused sign-up or sign-in draws the form again with the username (and | |
| 3 | * email) kept, escaped, and the failing field named; the password is never | |
| 4 | * echoed (#15990). | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { setLimit } from "../src/settings/limits.ts"; | |
| 14 | ||
| 15 | describe("refused auth forms keep what was typed", () => { | |
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | ||
| 20 | const send = (path: string, fields: Record<string, string>) => | |
| 21 | fetch(`${servers.url}${path}`, { | |
| 22 | method: "POST", | |
| 23 | headers: { origin: servers.url }, | |
| 24 | body: new URLSearchParams(fields), | |
| 25 | }); | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dataDir = await mkdtemp(join(tmpdir(), "forge-auth-kept-")); | |
| 29 | app = createApp({ dataDir, port: 0 }); | |
| 30 | servers = await serve(app); | |
| 31 | setLimit(app.db, "signup.per_source", 100, "system"); | |
| 32 | }); | |
| 33 | afterAll(async () => { | |
| 34 | await servers.stop(); | |
| 35 | app.close(); | |
| 36 | await rm(dataDir, { recursive: true, force: true }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("a weak password at sign-up keeps username and email, names the password, never echoes it", async () => { | |
| 40 | const res = await send("/signup", { username: "kept-name", email: "[email protected]", password: "Secret1sh" }); | |
| 41 | expect(res.status).toBe(422); | |
| 42 | const text = await res.text(); | |
| 43 | expect(text).toContain('value="kept-name"'); | |
| 44 | expect(text).toContain('value="[email protected]"'); | |
| 45 | expect(text).toContain("Passwords need at least 12 characters."); | |
| 46 | expect(text).not.toContain("Secret1sh"); | |
| 47 | }); | |
| 48 | ||
| 49 | test("a bad username is kept escaped, not as markup", async () => { | |
| 50 | const typed = `a"><script>alert(1)</script>`; | |
| 51 | const res = await send("/signup", { username: typed, email: "[email protected]", password: "long enough password" }); | |
| 52 | expect(res.status).toBe(422); | |
| 53 | const text = await res.text(); | |
| 54 | expect(text).not.toContain(typed); | |
| 55 | expect(text).toContain("<script>"); | |
| 56 | expect(text).toContain("Usernames are letters, digits, dots, dashes."); | |
| 57 | }); | |
| 58 | ||
| 59 | test("a refused sign-in keeps the username, says nothing of whether it exists, never echoes the password", async () => { | |
| 60 | const res = await send("/signin", { username: "nobody-here", password: "hunter2-hunter2" }); | |
| 61 | expect(res.status).toBe(422); | |
| 62 | const text = await res.text(); | |
| 63 | expect(text).toContain('value="nobody-here"'); | |
| 64 | expect(text).toContain("That username and password don't match."); | |
| 65 | expect(text).not.toContain("hunter2-hunter2"); | |
| 66 | }); | |
| 67 | }); | |
| 68 | ||
| 69 | describe("a sign-up refused because the edge vouched for another name", () => { | |
| 70 | const TOKEN = "edge-secret-0123456789"; | |
| 71 | let dataDir: string; | |
| 72 | let app: App; | |
| 73 | let servers: Servers; | |
| 74 | ||
| 75 | beforeAll(async () => { | |
| 76 | dataDir = await mkdtemp(join(tmpdir(), "forge-auth-kept-edge-")); | |
| 77 | app = createApp({ dataDir, port: 0, url: "https://forge.example", edgeToken: TOKEN }); | |
| 78 | servers = await serve(app); | |
| 79 | setLimit(app.db, "signup.per_source", 100, "system"); | |
| 80 | // The edge vouches for an account that exists, so the form has no | |
| 81 | // newcomer name to pin and shows what was typed. | |
| 82 | const made = await createUser(app.db, { origin: "edge", username: "vouched-name", email: "[email protected]" }); | |
| 83 | if (!made.ok) throw new Error(made.error); | |
| 84 | app.db.query("INSERT INTO edge_link (login, user_id) SELECT username, id FROM user WHERE username = ?1").run("vouched-name"); | |
| 85 | }); | |
| 86 | afterAll(async () => { | |
| 87 | await servers.stop(); | |
| 88 | app.close(); | |
| 89 | await rm(dataDir, { recursive: true, force: true }); | |
| 90 | }); | |
| 91 | ||
| 92 | test("keeps the typed username and email, shows the edge message, never echoes the password", async () => { | |
| 93 | const res = await fetch(`${servers.url}/signup`, { | |
| 94 | method: "POST", | |
| 95 | headers: { origin: "https://forge.example", "x-plat-user": "vouched-name", "x-plat-edge-token": TOKEN }, | |
| 96 | body: new URLSearchParams({ username: "typed-name", email: "[email protected]", password: "Edge-secret-pass-1" }), | |
| 97 | }); | |
| 98 | expect(res.status).toBe(422); | |
| 99 | const text = await res.text(); | |
| 100 | expect(text).toContain('value="typed-name"'); | |
| 101 | expect(text).toContain('value="[email protected]"'); | |
| 102 | expect(text).toContain("Sign up under the name your platform sign-in gave you."); | |
| 103 | expect(text).not.toContain("Edge-secret-pass-1"); | |
| 104 | }); | |
| 105 | }); |
new above
offline
None of these three tests reaches the changed line in signupSubmit, so they pass without the fix. Add a case for the edge-name refusal (pinnedTo false) that asserts the username is kept, escaped, and the edge message is shown.