Public: anyone can read this; the crew can change it.
A form that fails validation comes back with the person's input kept and the one field named #13960
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13948-a-form-that-fails-validation-comes-back → main 3 commits fork 2 threads · 2 unresolved
3 files
changed +136
−5
docs/features/form-keeps-input.md +4 −2
| @@ -11,7 +11,8 @@ with focus on the first field that has a message. Nothing above the form moves. | ||
| 11 | 11 | |
| 12 | 12 | Where it lives: `refused` and `kept` in `src/web/form.ts`; a page builds one `Refused` from the form |
| 13 | 13 | and its messages and passes each field through `kept`. First users: the profile form (display |
| 14 | name, email, commit email) and the password change. | |
| 14 | name, email, commit email) and the password change. Also kept, each by its own page: sign-in (the username, | |
| 15 | never the password, the failing password field marked), a new repository (a refused or taken name is marked on the Name field) and a new issue (the title marked). Other forms still use their own error handling: #14003, #14004. | |
| 15 | 16 | |
| 16 | 17 | Try it: open `/settings/password`, type different new and confirm passwords, submit; the message |
| 17 | 18 | sits beside New password with focus and no password is echoed. Or give `/settings` an email of |
| @@ -21,4 +22,5 @@ The terminal (both doors): it has no HTML forms and no profile or password scree | ||
| 21 | 22 | there shows the reason as a danger toast (`ctx.toast(errorText(...), "danger")` in `src/tui`) and |
| 22 | 23 | leaves the screen as it was; nothing changes in the terminal. |
| 23 | 24 | |
| 24 | Pinned by `test/form-keeps-input.test.ts`. | |
| 25 | Pinned by `test/form-keeps-input.test.ts` and `test/form-keep.test.ts` (a bad value on three forms; the kept | |
| 26 | text escaped). |
src/web/pages/site.ts +18 −3
| @@ -831,13 +831,24 @@ function ownersFor(ctx: Ctx, user: User): string[] { | ||
| 831 | 831 | return [user.username, ...writeOrgs.map((o) => o.username)]; |
| 832 | 832 | } |
| 833 | 833 | |
| 834 | function newRepoForm(ctx: Ctx, values: RepoValues, owners: string[], error?: string): Html { | |
| 834 | /** | |
| 835 | * A refusal is `{ text, field }`: with a field the message sits beside it, otherwise above the | |
| 836 | * form. | |
| 837 | */ | |
| 838 | function newRepoForm( | |
| 839 | ctx: Ctx, | |
| 840 | values: RepoValues, | |
| 841 | owners: string[], | |
| 842 | refusal?: { text: string; field?: "name" }, | |
| 843 | ): Html { | |
| 844 | const error = refusal?.text; | |
| 845 | const errorField = refusal?.field; | |
| 835 | 846 | return html`<div class="narrow-wide" data-new-repo> |
| 836 | 847 | ${pageHead("New repository", { |
| 837 | 848 | sub: "A repository is a bare git repo plus its issues, pulls and settings.", |
| 838 | 849 | })} |
| 839 | 850 | <form method="post" action="/new"> |
| 840 | ${error ? html`<p class="error" role="alert">${error}</p>` : null} | |
| 851 | ${error && errorField === undefined ? html`<p class="error" role="alert">${error}</p>` : null} | |
| 841 | 852 | ${selectField({ |
| 842 | 853 | label: "Owner", |
| 843 | 854 | name: "owner", |
| @@ -855,6 +866,7 @@ function newRepoForm(ctx: Ctx, values: RepoValues, owners: string[], error?: str | ||
| 855 | 866 | autofocus: true, |
| 856 | 867 | pattern: USERNAME_PATTERN, |
| 857 | 868 | hint: "Letters, digits, dots, dashes and underscores.", |
| 869 | ...(error && errorField === "name" ? { error } : {}), | |
| 858 | 870 | })} |
| 859 | 871 | ${formField({ |
| 860 | 872 | label: "Description", |
| @@ -945,7 +957,10 @@ export async function newRepoSubmit(ctx: Ctx): Promise<Response> { | ||
| 945 | 957 | ctx, |
| 946 | 958 | values, |
| 947 | 959 | ownersFor(ctx, ctx.auth.user), |
| 948 | messages[created.error] ?? "That name won't work.", | |
| 960 | { | |
| 961 | text: messages[created.error] ?? "That name won't work.", | |
| 962 | ...(created.error === "name_taken" || created.error === "invalid_name" ? { field: "name" as const } : {}), | |
| 963 | }, | |
| 949 | 964 | ), |
| 950 | 965 | }, |
| 951 | 966 | 422, |
test/form-keep.test.ts added +114
| @@ -0,0 +1,114 @@ | ||
| 1 | /** | |
| 2 | * A POST that fails validation comes back with what was typed kept, escaped, | |
| 3 | * and the failing field named (#13948). Three forms, three kinds of field: | |
| 4 | * sign-in (a username kept, the password never), a new repository (a name, | |
| 5 | * a description and a choice) and a new issue (a long body kept after an | |
| 6 | * empty title). | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 9 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { createRepo } from "../src/actions/repos.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { proveAll } from "./helpers/verified.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | let root = ""; | |
| 22 | let app: App; | |
| 23 | let servers: Servers; | |
| 24 | let cookie = ""; | |
| 25 | const XSS = `<script>alert("kept")</script>`; | |
| 26 | const ESCAPED = "<script>alert("; | |
| 27 | // Breaks out of a value="…" attribute if the quote is not escaped. | |
| 28 | const QUOTE = `"><img onerror=x>`; | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | root = mkdtempSync(join(tmpdir(), "forge-form-keep-")); | |
| 32 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 33 | const u = await createUser(app.db, { | |
| 34 | origin: "seed", | |
| 35 | trusted: true, | |
| 36 | username: "alice", | |
| 37 | email: "[email protected]", | |
| 38 | password: "a-good-passphrase", | |
| 39 | adminIfFirst: true, | |
| 40 | }); | |
| 41 | if (!u.ok) throw new Error(u.error); | |
| 42 | proveAll(app); | |
| 43 | servers = await serve(app); | |
| 44 | const signin = await fetch(`${servers.url}/signin`, { | |
| 45 | method: "POST", | |
| 46 | headers: { origin: servers.url }, | |
| 47 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 48 | redirect: "manual", | |
| 49 | }); | |
| 50 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 51 | const made = await createRepo(app, u.value, { name: "kept", visibility: "public", description: "" }); | |
| 52 | if (!made.ok) throw new Error(made.error); | |
| 53 | }); | |
| 54 | afterAll(() => { | |
| 55 | servers.stop(); | |
| 56 | app.close(); | |
| 57 | rmSync(root, { recursive: true, force: true }); | |
| 58 | }); | |
| 59 | ||
| 60 | const post = (path: string, fields: Record<string, string>, signedIn = true): Promise<Response> => | |
| 61 | fetch(`${servers.url}${path}`, { | |
| 62 | method: "POST", | |
| 63 | redirect: "manual", | |
| 64 | headers: { origin: servers.url, ...(signedIn ? { cookie } : {}) }, | |
| 65 | body: new URLSearchParams(fields), | |
| 66 | }); | |
| 67 | ||
| 68 | test("a refused sign-in keeps the username escaped and never the password", async () => { | |
| 69 | const res = await post("/signin", { username: XSS, password: "never-echo-this-1" }, false); | |
| 70 | expect(res.status).toBe(422); | |
| 71 | const html = await res.text(); | |
| 72 | expect(html).not.toContain(XSS); | |
| 73 | expect(html).toContain(ESCAPED); | |
| 74 | expect(html).not.toContain("never-echo-this-1"); | |
| 75 | expect(html).toMatch(/class="error"/); | |
| 76 | expect(html).toMatch(/id="f-password-err"/); | |
| 77 | expect(html).toMatch(/aria-invalid="true"/); | |
| 78 | }); | |
| 79 | ||
| 80 | test("a quote in a kept value cannot leave its attribute", async () => { | |
| 81 | const signin = await (await post("/signin", { username: QUOTE, password: "x" }, false)).text(); | |
| 82 | expect(signin).not.toContain(QUOTE); | |
| 83 | expect(signin).not.toContain("<img onerror=x>"); | |
| 84 | const repo = await (await post("/new", { owner: "alice", name: `bad name ${QUOTE}`, description: QUOTE, visibility: "public" })).text(); | |
| 85 | expect(repo).not.toContain(QUOTE); | |
| 86 | expect(repo).not.toContain("<img onerror=x>"); | |
| 87 | expect(repo).toContain(""><img onerror=x>"); | |
| 88 | const issue = await (await post("/alice/kept/issues/new", { title: "", body: QUOTE })).text(); | |
| 89 | expect(issue).not.toContain("<img onerror=x>"); | |
| 90 | expect(issue).toContain(""><img onerror=x>"); | |
| 91 | }); | |
| 92 | ||
| 93 | test("a refused new repository keeps every field escaped and names the name", async () => { | |
| 94 | const res = await post("/new", { owner: "alice", name: `bad name ${XSS}`, description: XSS, visibility: "public" }); | |
| 95 | expect(res.status).toBe(422); | |
| 96 | const html = await res.text(); | |
| 97 | expect(html).not.toContain(XSS); | |
| 98 | expect(html.split(ESCAPED).length - 1).toBeGreaterThanOrEqual(2); | |
| 99 | expect(html).toMatch(/class="error" id="f-name-err"/); | |
| 100 | expect(html).toMatch(/value="public"\s+selected/); | |
| 101 | }); | |
| 102 | ||
| 103 | test("a refused new issue keeps the long body escaped and names the title", async () => { | |
| 104 | const body = `${XSS}\n${"a paragraph nobody retypes. ".repeat(20)}`; | |
| 105 | const res = await post("/alice/kept/issues/new", { title: "", body }); | |
| 106 | expect(res.status).toBe(422); | |
| 107 | const html = await res.text(); | |
| 108 | expect(html).not.toContain(XSS); | |
| 109 | expect(html).toContain(ESCAPED); | |
| 110 | expect(html).toContain("a paragraph nobody retypes."); | |
| 111 | expect(html).toContain("An issue needs a title."); | |
| 112 | expect(html).toMatch(/id="f-title-err"/); | |
| 113 | expect(html).toMatch(/aria-invalid="true"/); | |
| 114 | }); |
new above
offline
Signature is now over 100 columns and gains a fifth positional parameter. Wrap it or take an options object.