Public: anyone can read this; the crew can change it.

Every username field's pattern is one a browser can read #13800

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/13798-every-username-field-s-pattern-is-one-a → main 2 commits fork

  • Merged. mason/13798-every-username-field-s-pattern-is-one-a was merged into main as 635dde5.

Review

Sign in to review this change.
10 files changed +62 −13
src/web/pages/apps.ts +2 −1
@@ -12,6 +12,7 @@
1212 * 404 for everyone.
1313 */
1414
15 import { USERNAME_PATTERN } from "../username_pattern.ts";
1516 import { runnerSettings } from "../../crew/runner_env.ts";
1617 import { clearCache } from "../../actions/build_cache.ts";
1718 import { returnTo, withReturn } from "../return_to.ts";
@@ -386,7 +387,7 @@ function newAppForm(
386387 label: "Name",
387388 name: "name",
388389 value: values.name,
389 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
390 pattern: USERNAME_PATTERN,
390391 hint:
391392 domain === undefined
392393 ? "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 @@
2222 * so before anyone submits, and where to remove it.
2323 */
2424
25 import { USERNAME_PATTERN } from "../username_pattern.ts";
2526 import { PASSWORD_TOO_LONG, withinPasswordCap } from "../../core/hash-gate.ts";
2627 import { givenEmail, MIN_SIGNUP_PASSWORD, noEmail } from "../../actions/users.ts";
2728 import type { Credential } from "../../auth/webauthn.ts";
@@ -282,7 +283,7 @@ function accountForm(ctx: Ctx, action: "/claim" | "/welcome", values: Values, er
282283 required: true,
283284 autofocus: true,
284285 hint: "Letters, digits, dots and dashes.",
285 pattern: "[A-Za-z0-9][A-Za-z0-9._\\-]*",
286 pattern: USERNAME_PATTERN,
286287 ...(errors.username ? { error: errors.username } : {}),
287288 })}
288289 ${mailConfigured(ctx.app)
src/web/pages/crew.ts +17 −5
@@ -6,6 +6,7 @@
66 * the crew is actually there, in words, before anyone assigns anything.
77 */
88
9 import { USERNAME_PATTERN } from "../username_pattern.ts";
910 import { budgetsCard } from "../studio/crew_budgets.ts";
1011 import { crewKeyField } from "../studio/crew_key.ts";
1112 import { fundingOf, fundingWords } from "../../spawn/funding.ts";
@@ -388,13 +389,19 @@ function scorecardCard(
388389 );
389390 }
390391
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 */
392396 function returnedCount(l: WeekLine, pulls: readonly ReturnedPull[]): Html | number {
393397 if (l.returned === 0 || pulls.length === 0) return l.returned;
394398 return html`<a href="#returned-${l.username}">${l.returned}</a>`;
395399 }
396400
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 */
398405 function returnedRow(l: WeekLine, pulls: readonly ReturnedPull[]): Html | null {
399406 if (l.returned === 0 || pulls.length === 0) return null;
400407 const more = l.returned - pulls.length;
@@ -530,7 +537,10 @@ function loadSpending(ctx: Ctx) {
530537 }));
531538 }
532539
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 */
534544 function modelReviewCard(ctx: Ctx): Html {
535545 const r = readReview(ctx.app);
536546 const names = new Map(listAllAgentsByName(ctx));
@@ -788,7 +798,7 @@ function crewView(
788798 name: "username",
789799 value: values.username,
790800 required: true,
791 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
801 pattern: USERNAME_PATTERN,
792802 ...(usernameError ? { error: usernameError } : {}),
793803 })}
794804 ${uiField({
@@ -917,7 +927,9 @@ function modelDaysCard(rows: readonly ModelDay[]): Html {
917927 );
918928 }
919929
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 */
921933 function wastedDaysCard(rows: readonly WastedDay[]): Html {
922934 if (rows.length === 0) return html``;
923935 return panel(
src/web/pages/everyday.ts +2 −1
@@ -7,6 +7,7 @@
77 * land on the home. Admin only, same-origin.
88 */
99
10 import { USERNAME_PATTERN } from "../username_pattern.ts";
1011 import {
1112 adminOf,
1213 everydayOf,
@@ -113,7 +114,7 @@ function everydayFields(ctx: Ctx, user: User, offerKeep: boolean): Html {
113114 value: suggestedName(ctx, user),
114115 autocomplete: "username",
115116 required: true,
116 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
117 pattern: USERNAME_PATTERN,
117118 })}
118119 ${mailConfigured(ctx.app)
119120 ? formField({
src/web/pages/migrate.ts +2 −1
@@ -20,6 +20,7 @@
2020 * Every mutation is `migrateRepo`; the page only puts its answer into words.
2121 */
2222
23 import { USERNAME_PATTERN } from "../username_pattern.ts";
2324 import { repositoryLimitMessage } from "../../actions/repos.ts";
2425 import { listAllAgents } from "../../actions/crew.ts";
2526 import {
@@ -491,7 +492,7 @@ function migrateForm(
491492 label: "Name",
492493 name: "name",
493494 value: values.name,
494 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
495 pattern: USERNAME_PATTERN,
495496 hint: "Left blank, it keeps the source's name.",
496497 ...(error ? { error } : {}),
497498 })}
src/web/pages/org.ts +2 −1
@@ -9,6 +9,7 @@
99 * unknown username now says so.
1010 */
1111
12 import { USERNAME_PATTERN } from "../username_pattern.ts";
1213 import {
1314 addTeamMember,
1415 createOrg,
@@ -520,7 +521,7 @@ function orgForm(ctx: Ctx, value: string, error?: string): Response {
520521 value,
521522 required: true,
522523 autofocus: true,
523 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
524 pattern: USERNAME_PATTERN,
524525 hint: "Letters, digits, dots, dashes. This becomes the URL.",
525526 ...(error ? { error } : {}),
526527 })}
src/web/pages/site.ts +3 −2
@@ -8,6 +8,7 @@
88 * typed still in it. A dead end that loses input is a bug, not a message.
99 */
1010
11 import { USERNAME_PATTERN } from "../username_pattern.ts";
1112 import { expireCookie, setCookie } from "../../auth/cookies.ts";
1213 import { safeNext, withReturn } from "../return_to.ts";
1314 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
@@ -337,7 +338,7 @@ function authForm(
337338 ...(kind === "signup"
338339 ? {
339340 hint: "Letters, digits, dots and dashes.",
340 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
341 pattern: USERNAME_PATTERN,
341342 }
342343 : {}),
343344 ...(errors.username ? { error: errors.username } : {}),
@@ -852,7 +853,7 @@ function newRepoForm(ctx: Ctx, values: RepoValues, owners: string[], error?: str
852853 value: values.name,
853854 required: true,
854855 autofocus: true,
855 pattern: "[A-Za-z0-9][A-Za-z0-9._-]*",
856 pattern: USERNAME_PATTERN,
856857 hint: "Letters, digits, dots, dashes and underscores.",
857858 })}
858859 ${formField({
src/web/pages/start_claim.ts +2 −1
@@ -6,6 +6,7 @@
66 * account is offered, and works, only where `/signup` would let a stranger in
77 * (`signupClosed`, and not a passkey-only forge); otherwise the person signs in.
88 */
9 import { USERNAME_PATTERN } from "../username_pattern.ts";
910 import { claimStart, CLAIM_WORDS, viewClaim } from "../../actions/claim.ts";
1011 import { noEmail, plausibleEmail, signupClosed } from "../../actions/users.ts";
1112 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
@@ -53,7 +54,7 @@ export function startClaimPage(ctx: Ctx, errors: { username?: string; password?:
5354 </form>`
5455 : html`<p><a href="/signin?next=${encodeURIComponent(here)}">Sign in</a> to take it${mayMakeAccount(ctx) ? ", or make an account:" : "."}</p>
5556 ${!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 } : {}) })}
5758 ${formField({ label: "Email", name: "email", type: "email", autocomplete: "email", hint: "Optional.", ...(errors.email ? { error: errors.email } : {}) })}
5859 ${formField({ label: "Password", name: "password", type: "password", autocomplete: "new-password", required: true, minlength: 12, hint: "At least 12 characters.", ...(errors.password ? { error: errors.password } : {}) })}
5960 ${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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs