Public: anyone can read this; the crew can change it.
Every username field's pattern is one a browser can read #13800
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13798-every-username-field-s-pattern-is-one-a → main 2 commits fork
10 files
changed +62
−13
src/web/pages/apps.ts +2 −1
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | * 404 for everyone. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 15 | 16 | import { runnerSettings } from "../../crew/runner_env.ts"; |
| 16 | 17 | import { clearCache } from "../../actions/build_cache.ts"; |
| 17 | 18 | import { returnTo, withReturn } from "../return_to.ts"; |
| @@ -386,7 +387,7 @@ function newAppForm( | ||
| 386 | 387 | label: "Name", |
| 387 | 388 | name: "name", |
| 388 | 389 | value: values.name, |
| 389 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 390 | pattern: USERNAME_PATTERN, | |
| 390 | 391 | hint: |
| 391 | 392 | domain === undefined |
| 392 | 393 | ? "Left blank, it comes from your sentence. Use letters, digits, dots, dashes and underscores. The address comes from it." |
src/web/pages/claim.ts +2 −1
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | * so before anyone submits, and where to remove it. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 25 | 26 | import { PASSWORD_TOO_LONG, withinPasswordCap } from "../../core/hash-gate.ts"; |
| 26 | 27 | import { givenEmail, MIN_SIGNUP_PASSWORD, noEmail } from "../../actions/users.ts"; |
| 27 | 28 | import type { Credential } from "../../auth/webauthn.ts"; |
| @@ -282,7 +283,7 @@ function accountForm(ctx: Ctx, action: "/claim" | "/welcome", values: Values, er | ||
| 282 | 283 | required: true, |
| 283 | 284 | autofocus: true, |
| 284 | 285 | hint: "Letters, digits, dots and dashes.", |
| 285 | pattern: "[A-Za-z0-9][A-Za-z0-9._\\-]*", | |
| 286 | pattern: USERNAME_PATTERN, | |
| 286 | 287 | ...(errors.username ? { error: errors.username } : {}), |
| 287 | 288 | })} |
| 288 | 289 | ${mailConfigured(ctx.app) |
src/web/pages/crew.ts +17 −5
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | * the crew is actually there, in words, before anyone assigns anything. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 9 | 10 | import { budgetsCard } from "../studio/crew_budgets.ts"; |
| 10 | 11 | import { crewKeyField } from "../studio/crew_key.ts"; |
| 11 | 12 | import { fundingOf, fundingWords } from "../../spawn/funding.ts"; |
| @@ -388,13 +389,19 @@ function scorecardCard( | ||
| 388 | 389 | ); |
| 389 | 390 | } |
| 390 | 391 | |
| 391 | /** A Returned count above zero links to its fold below; zero stays a bare 0. Only the count sits in the numeric cell. */ | |
| 392 | /** | |
| 393 | * A Returned count above zero links to its fold below; zero stays a bare 0. Only the count sits in | |
| 394 | * the numeric cell. | |
| 395 | */ | |
| 392 | 396 | function returnedCount(l: WeekLine, pulls: readonly ReturnedPull[]): Html | number { |
| 393 | 397 | if (l.returned === 0 || pulls.length === 0) return l.returned; |
| 394 | 398 | return html`<a href="#returned-${l.username}">${l.returned}</a>`; |
| 395 | 399 | } |
| 396 | 400 | |
| 397 | /** The pulls that came back and why, folded in a full-width row under the member's: one link per pull. */ | |
| 401 | /** | |
| 402 | * The pulls that came back and why, folded in a full-width row under the member's: one link per | |
| 403 | * pull. | |
| 404 | */ | |
| 398 | 405 | function returnedRow(l: WeekLine, pulls: readonly ReturnedPull[]): Html | null { |
| 399 | 406 | if (l.returned === 0 || pulls.length === 0) return null; |
| 400 | 407 | const more = l.returned - pulls.length; |
| @@ -530,7 +537,10 @@ function loadSpending(ctx: Ctx) { | ||
| 530 | 537 | })); |
| 531 | 538 | } |
| 532 | 539 | |
| 533 | /** The weekly model review (#7815): each member's models side by side, the verdict, the button, the trail. */ | |
| 540 | /** | |
| 541 | * The weekly model review (#7815): each member's models side by side, the verdict, the button, the | |
| 542 | * trail. | |
| 543 | */ | |
| 534 | 544 | function modelReviewCard(ctx: Ctx): Html { |
| 535 | 545 | const r = readReview(ctx.app); |
| 536 | 546 | const names = new Map(listAllAgentsByName(ctx)); |
| @@ -788,7 +798,7 @@ function crewView( | ||
| 788 | 798 | name: "username", |
| 789 | 799 | value: values.username, |
| 790 | 800 | required: true, |
| 791 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 801 | pattern: USERNAME_PATTERN, | |
| 792 | 802 | ...(usernameError ? { error: usernameError } : {}), |
| 793 | 803 | })} |
| 794 | 804 | ${uiField({ |
| @@ -917,7 +927,9 @@ function modelDaysCard(rows: readonly ModelDay[]): Html { | ||
| 917 | 927 | ); |
| 918 | 928 | } |
| 919 | 929 | |
| 920 | /** Runs that ended on work not theirs to do, per day: the pull closed, nothing pushed, already done. */ | |
| 930 | /** | |
| 931 | * Runs that ended on work not theirs to do, per day: the pull closed, nothing pushed, already done. | |
| 932 | */ | |
| 921 | 933 | function wastedDaysCard(rows: readonly WastedDay[]): Html { |
| 922 | 934 | if (rows.length === 0) return html``; |
| 923 | 935 | return panel( |
src/web/pages/everyday.ts +2 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * land on the home. Admin only, same-origin. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 10 | 11 | import { |
| 11 | 12 | adminOf, |
| 12 | 13 | everydayOf, |
| @@ -113,7 +114,7 @@ function everydayFields(ctx: Ctx, user: User, offerKeep: boolean): Html { | ||
| 113 | 114 | value: suggestedName(ctx, user), |
| 114 | 115 | autocomplete: "username", |
| 115 | 116 | required: true, |
| 116 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 117 | pattern: USERNAME_PATTERN, | |
| 117 | 118 | })} |
| 118 | 119 | ${mailConfigured(ctx.app) |
| 119 | 120 | ? formField({ |
src/web/pages/migrate.ts +2 −1
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | * Every mutation is `migrateRepo`; the page only puts its answer into words. |
| 21 | 21 | */ |
| 22 | 22 | |
| 23 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 23 | 24 | import { repositoryLimitMessage } from "../../actions/repos.ts"; |
| 24 | 25 | import { listAllAgents } from "../../actions/crew.ts"; |
| 25 | 26 | import { |
| @@ -491,7 +492,7 @@ function migrateForm( | ||
| 491 | 492 | label: "Name", |
| 492 | 493 | name: "name", |
| 493 | 494 | value: values.name, |
| 494 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 495 | pattern: USERNAME_PATTERN, | |
| 495 | 496 | hint: "Left blank, it keeps the source's name.", |
| 496 | 497 | ...(error ? { error } : {}), |
| 497 | 498 | })} |
src/web/pages/org.ts +2 −1
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * unknown username now says so. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 12 | 13 | import { |
| 13 | 14 | addTeamMember, |
| 14 | 15 | createOrg, |
| @@ -520,7 +521,7 @@ function orgForm(ctx: Ctx, value: string, error?: string): Response { | ||
| 520 | 521 | value, |
| 521 | 522 | required: true, |
| 522 | 523 | autofocus: true, |
| 523 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 524 | pattern: USERNAME_PATTERN, | |
| 524 | 525 | hint: "Letters, digits, dots, dashes. This becomes the URL.", |
| 525 | 526 | ...(error ? { error } : {}), |
| 526 | 527 | })} |
src/web/pages/site.ts +3 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * typed still in it. A dead end that loses input is a bug, not a message. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 11 | 12 | import { expireCookie, setCookie } from "../../auth/cookies.ts"; |
| 12 | 13 | import { safeNext, withReturn } from "../return_to.ts"; |
| 13 | 14 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| @@ -337,7 +338,7 @@ function authForm( | ||
| 337 | 338 | ...(kind === "signup" |
| 338 | 339 | ? { |
| 339 | 340 | hint: "Letters, digits, dots and dashes.", |
| 340 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 341 | pattern: USERNAME_PATTERN, | |
| 341 | 342 | } |
| 342 | 343 | : {}), |
| 343 | 344 | ...(errors.username ? { error: errors.username } : {}), |
| @@ -852,7 +853,7 @@ function newRepoForm(ctx: Ctx, values: RepoValues, owners: string[], error?: str | ||
| 852 | 853 | value: values.name, |
| 853 | 854 | required: true, |
| 854 | 855 | autofocus: true, |
| 855 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", | |
| 856 | pattern: USERNAME_PATTERN, | |
| 856 | 857 | hint: "Letters, digits, dots, dashes and underscores.", |
| 857 | 858 | })} |
| 858 | 859 | ${formField({ |
src/web/pages/start_claim.ts +2 −1
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | * account is offered, and works, only where `/signup` would let a stranger in |
| 7 | 7 | * (`signupClosed`, and not a passkey-only forge); otherwise the person signs in. |
| 8 | 8 | */ |
| 9 | import { USERNAME_PATTERN } from "../username_pattern.ts"; | |
| 9 | 10 | import { claimStart, CLAIM_WORDS, viewClaim } from "../../actions/claim.ts"; |
| 10 | 11 | import { noEmail, plausibleEmail, signupClosed } from "../../actions/users.ts"; |
| 11 | 12 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| @@ -53,7 +54,7 @@ export function startClaimPage(ctx: Ctx, errors: { username?: string; password?: | ||
| 53 | 54 | </form>` |
| 54 | 55 | : html`<p><a href="/signin?next=${encodeURIComponent(here)}">Sign in</a> to take it${mayMakeAccount(ctx) ? ", or make an account:" : "."}</p> |
| 55 | 56 | ${!mayMakeAccount(ctx) ? null : html`<form method="post" action="${here}/account" class="stack"> |
| 56 | ${formField({ label: "Username", name: "username", autocomplete: "username", required: true, pattern: "[A-Za-z0-9][A-Za-z0-9._\\-]*", ...(errors.username ? { error: errors.username } : {}) })} | |
| 57 | ${formField({ label: "Username", name: "username", autocomplete: "username", required: true, pattern: USERNAME_PATTERN, ...(errors.username ? { error: errors.username } : {}) })} | |
| 57 | 58 | ${formField({ label: "Email", name: "email", type: "email", autocomplete: "email", hint: "Optional.", ...(errors.email ? { error: errors.email } : {}) })} |
| 58 | 59 | ${formField({ label: "Password", name: "password", type: "password", autocomplete: "new-password", required: true, minlength: 12, hint: "At least 12 characters.", ...(errors.password ? { error: errors.password } : {}) })} |
| 59 | 60 | ${btn("Make an account and take it", { variant: "primary", label: "Make an account and take it" })} |
src/web/username_pattern.ts added +3
| @@ -0,0 +1,3 @@ | ||
| 1 | // The `pattern` attribute every username field carries. Browsers compile it with the `v` flag, | |
| 2 | // where a bare `-` inside a class is a syntax error, so the dash is escaped. | |
| 3 | export const USERNAME_PATTERN = "[A-Za-z0-9][A-Za-z0-9._\\-]*"; |
test/username-pattern.test.ts added +27
| @@ -0,0 +1,27 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { Glob } from "bun"; | |
| 3 | import { USERNAME_PATTERN } from "../src/web/username_pattern.ts"; | |
| 4 | ||
| 5 | // Browsers compile a `pattern` attribute as /^(?:value)$/v. | |
| 6 | const compiles = (p: string) => new RegExp(`^(?:${p})$`, "v"); | |
| 7 | ||
| 8 | test("the username pattern compiles with the v flag and matches what the server accepts", () => { | |
| 9 | const re = compiles(USERNAME_PATTERN); | |
| 10 | expect(re.test("ada.lovelace-1_x")).toBe(true); | |
| 11 | expect(re.test("-ada")).toBe(false); | |
| 12 | expect(re.test("a b")).toBe(false); | |
| 13 | }); | |
| 14 | ||
| 15 | test("every pattern written in a page compiles with the v flag", async () => { | |
| 16 | const found: string[] = []; | |
| 17 | for await (const path of new Glob("src/web/**/*.ts").scan({ cwd: import.meta.dir + "/.." })) { | |
| 18 | if (path.endsWith(".test.ts")) continue; | |
| 19 | const text = await Bun.file(`${import.meta.dir}/../${path}`).text(); | |
| 20 | for (const m of text.matchAll(/\bpattern(?:=|:\s*)"([^"$]+)"/g)) found.push(`${path}: ${m[1]}`); | |
| 21 | } | |
| 22 | expect(found.length).toBeGreaterThan(0); | |
| 23 | for (const entry of found) { | |
| 24 | const value = entry.slice(entry.indexOf(": ") + 2); | |
| 25 | expect(() => compiles(value), entry).not.toThrow(); | |
| 26 | } | |
| 27 | }); |
new above
offline