Public: anyone can read this; the crew can change it.
Signup carries a Turnstile check verified on the server, landed from pull #1204 on today's main #1525
scout/forge:scout/1457-signup-carries-a-turnstile-check-verifie → main 3 commits fork 1 thread · 1 unresolved
13 files
changed +366
−8
README.md +9 −1
| @@ -110,7 +110,15 @@ signed in by the platform, with an account made on first sight. Mail out | ||
| 110 | 110 | configured by the forge-level secrets `MAIL_SMTP_URL` |
| 111 | 111 | (`smtps://user:pass@host:465`) and `MAIL_FROM`, set live under Settings → |
| 112 | 112 | Secrets, or at boot by `FORGE_SMTP_URL` / `FORGE_MAIL_FROM`. With neither, no |
| 113 | mail is sent and a newcomer waits for the owner to verify them. Behind a | |
| 113 | mail is sent and a newcomer waits for the owner to verify them. To put a | |
| 114 | Cloudflare Turnstile check on signup, create a widget in Cloudflare and set | |
| 115 | `FORGE_TURNSTILE_SITE_KEY` and `FORGE_TURNSTILE_SECRET` (both, in the | |
| 116 | environment, never in the repository): `/signup` then shows the widget, the | |
| 117 | server verifies the token before it hashes a password or makes an account, | |
| 118 | and only that page's CSP admits `https://challenges.cloudflare.com`. With | |
| 119 | either missing the check is off, and Settings → Capacity says so. Terminal | |
| 120 | signup is refused while it is on; claim links and admin-made accounts are not | |
| 121 | checked. Behind a | |
| 114 | 122 | proxy, `FORGE_TRUSTED_PROXIES` (comma-separated CIDR ranges, e.g. |
| 115 | 123 | `10.42.0.0/16` for a k3s ingress) names the proxies allowed to say who the |
| 116 | 124 | client is; limits for strangers and the signup audit then see each outside |
scripts/gate.ts +1
| @@ -71,6 +71,7 @@ export const GATE: readonly string[] = [ | ||
| 71 | 71 | "test/settings.test.ts", |
| 72 | 72 | "test/signin-throttle.test.ts", |
| 73 | 73 | "test/signins.test.ts", |
| 74 | "test/human-check.test.ts", | |
| 74 | 75 | "test/sole-owner-suspend.test.ts", |
| 75 | 76 | "test/standing-every-process.test.ts", |
| 76 | 77 | "test/steer.test.ts", |
src/actions/users.ts +9 −2
| @@ -11,6 +11,7 @@ import { | ||
| 11 | 11 | parseAuthorizedKey, |
| 12 | 12 | type UserKey, |
| 13 | 13 | } from "../core/keys.ts"; |
| 14 | import { passesHumanCheck } from "../core/human-check.ts"; | |
| 14 | 15 | import { reservedName } from "../core/reserved.ts"; |
| 15 | 16 | import { setUserSetting } from "../core/settings.ts"; |
| 16 | 17 | import { |
| @@ -36,9 +37,11 @@ export interface SignUpInput { | ||
| 36 | 37 | username: string; |
| 37 | 38 | email: string; |
| 38 | 39 | password: string; |
| 40 | /** The Turnstile token the form carried; unread when the check is off. */ | |
| 41 | humanToken?: string | undefined; | |
| 39 | 42 | } |
| 40 | 43 | |
| 41 | export type SignUpError = CreateUserError; | |
| 44 | export type SignUpError = CreateUserError | "human_check"; | |
| 42 | 45 | |
| 43 | 46 | /** |
| 44 | 47 | * The first account on a fresh forge is the admin — no setup wizard. A name |
| @@ -51,7 +54,11 @@ export async function signUp( | ||
| 51 | 54 | /** Where the request came from, for the owner's audit of who joined. */ |
| 52 | 55 | from: { address?: string | undefined } = {}, |
| 53 | 56 | ): Promise<Result<User, SignUpError>> { |
| 54 | const created = await createUser(app.db, { ...input, adminIfFirst: true }); | |
| 57 | // Before a password is hashed or a row made: a bot costs one fetch. | |
| 58 | if (!(await passesHumanCheck(app.config, input.humanToken, from.address))) | |
| 59 | return err("human_check"); | |
| 60 | const { humanToken: _token, ...account } = input; | |
| 61 | const created = await createUser(app.db, { ...account, adminIfFirst: true }); | |
| 55 | 62 | if (!created.ok) return created; |
| 56 | 63 | app.bus.append({ |
| 57 | 64 | kind: "user.created", |
src/app.ts +7
| @@ -17,6 +17,7 @@ import { follow } from "./core/following.ts"; | ||
| 17 | 17 | import { enqueueForEvent } from "./core/webhooks.ts"; |
| 18 | 18 | import { openDb } from "./kernel/db.ts"; |
| 19 | 19 | import { id } from "./kernel/id.ts"; |
| 20 | import type { HumanCheckConfig } from "./core/human-check.ts"; | |
| 20 | 21 | import { createLog, type Log } from "./kernel/log.ts"; |
| 21 | 22 | import { closeReadersUnder } from "./git/read.ts"; |
| 22 | 23 | |
| @@ -125,6 +126,12 @@ export interface ForgeConfig { | ||
| 125 | 126 | * parent's updates. Unset, children are spawned without the fork. |
| 126 | 127 | */ |
| 127 | 128 | forgeRepo?: string; |
| 129 | /** | |
| 130 | * The Turnstile check on signup (`core/human-check.ts`): site key and | |
| 131 | * secret from `FORGE_TURNSTILE_*`, and a `verify` a test injects. | |
| 132 | * Undefined is no check. | |
| 133 | */ | |
| 134 | humanCheck?: HumanCheckConfig; | |
| 128 | 135 | } |
| 129 | 136 | |
| 130 | 137 | export interface App { |
src/core/human-check.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * The human check on signup: Cloudflare Turnstile, verified here. | |
| 3 | * | |
| 4 | * On only when a site key and a secret are both configured | |
| 5 | * (`FORGE_TURNSTILE_SITE_KEY`, `FORGE_TURNSTILE_SECRET`); either missing and | |
| 6 | * the check is off. The secret goes to Cloudflare's siteverify and nowhere | |
| 7 | * else — never into a page, a log or an error. `verify` is the seam a test | |
| 8 | * replaces so no test reaches the network. | |
| 9 | */ | |
| 10 | ||
| 11 | export const TURNSTILE_ORIGIN = "https://challenges.cloudflare.com"; | |
| 12 | const SITEVERIFY = `${TURNSTILE_ORIGIN}/turnstile/v0/siteverify`; | |
| 13 | /** A token longer than this is not one Turnstile issued (it documents 2048). */ | |
| 14 | const TOKEN_MAX = 2048; | |
| 15 | ||
| 16 | export type HumanVerifier = ( | |
| 17 | token: string, | |
| 18 | address: string | undefined, | |
| 19 | ) => Promise<boolean>; | |
| 20 | ||
| 21 | export interface HumanCheckConfig { | |
| 22 | siteKey: string; | |
| 23 | secret: string; | |
| 24 | /** Stand-in for Cloudflare in tests. Default: `turnstileVerifier(secret)`. */ | |
| 25 | verify?: HumanVerifier; | |
| 26 | } | |
| 27 | ||
| 28 | export function humanCheckOn( | |
| 29 | config: { humanCheck?: HumanCheckConfig }, | |
| 30 | ): config is { humanCheck: HumanCheckConfig } { | |
| 31 | const c = config.humanCheck; | |
| 32 | return c !== undefined && c.siteKey !== "" && c.secret !== ""; | |
| 33 | } | |
| 34 | ||
| 35 | /** The one line the admin's settings show. */ | |
| 36 | export function humanCheckLine(config: { | |
| 37 | humanCheck?: HumanCheckConfig; | |
| 38 | }): string { | |
| 39 | return humanCheckOn(config) | |
| 40 | ? "Human check on signup: on (Cloudflare Turnstile)." | |
| 41 | : "Human check on signup: off. Set FORGE_TURNSTILE_SITE_KEY and FORGE_TURNSTILE_SECRET to turn it on."; | |
| 42 | } | |
| 43 | ||
| 44 | export function turnstileVerifier(secret: string): HumanVerifier { | |
| 45 | return async (token, address) => { | |
| 46 | try { | |
| 47 | const body = new URLSearchParams({ secret, response: token }); | |
| 48 | if (address !== undefined) body.set("remoteip", address); | |
| 49 | const res = await fetch(SITEVERIFY, { | |
| 50 | method: "POST", | |
| 51 | body, | |
| 52 | signal: AbortSignal.timeout(5000), | |
| 53 | }); | |
| 54 | if (!res.ok) return false; | |
| 55 | const answer = (await res.json()) as { success?: unknown }; | |
| 56 | return answer.success === true; | |
| 57 | } catch { | |
| 58 | // A check that cannot be made is a refusal, not a pass. | |
| 59 | return false; | |
| 60 | } | |
| 61 | }; | |
| 62 | } | |
| 63 | ||
| 64 | /** True when the check is off, or the token is one Cloudflare vouches for. */ | |
| 65 | export async function passesHumanCheck( | |
| 66 | config: { humanCheck?: HumanCheckConfig }, | |
| 67 | token: string | undefined, | |
| 68 | address: string | undefined, | |
| 69 | ): Promise<boolean> { | |
| 70 | if (!humanCheckOn(config)) return true; | |
| 71 | if (token === undefined || token === "" || token.length > TOKEN_MAX) | |
| 72 | return false; | |
| 73 | const verify = | |
| 74 | config.humanCheck.verify ?? turnstileVerifier(config.humanCheck.secret); | |
| 75 | return verify(token, address); | |
| 76 | } |
src/http/api.ts +6 −1
| @@ -1105,10 +1105,15 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1105 | 1105 | username: str(input.username), |
| 1106 | 1106 | email: str(input.email), |
| 1107 | 1107 | password: str(input.password), |
| 1108 | humanToken: str(input.human_token), | |
| 1108 | 1109 | }, |
| 1109 | 1110 | { address: clientAddress(ctx.request) }, |
| 1110 | 1111 | ); |
| 1111 | if (!created.ok) return json({ error: created.error }, 422); | |
| 1112 | if (!created.ok) | |
| 1113 | return json( | |
| 1114 | { error: created.error }, | |
| 1115 | created.error === "human_check" ? 403 : 422, | |
| 1116 | ); | |
| 1112 | 1117 | return json(publicUser(created.value), 201); |
| 1113 | 1118 | }); |
| 1114 | 1119 |
src/main.ts +10
| @@ -132,6 +132,16 @@ const app = createApp({ | ||
| 132 | 132 | Number(process.env.FORGE_MAIL_DAILY) > 0 |
| 133 | 133 | ? { mailDailyCap: Number(process.env.FORGE_MAIL_DAILY) } |
| 134 | 134 | : {}), |
| 135 | // The Turnstile check on signup: both keys, or no check (`core/human-check.ts`). | |
| 136 | ...((process.env.FORGE_TURNSTILE_SITE_KEY ?? "").trim() !== "" && | |
| 137 | (process.env.FORGE_TURNSTILE_SECRET ?? "").trim() !== "" | |
| 138 | ? { | |
| 139 | humanCheck: { | |
| 140 | siteKey: process.env.FORGE_TURNSTILE_SITE_KEY!.trim(), | |
| 141 | secret: process.env.FORGE_TURNSTILE_SECRET!.trim(), | |
| 142 | }, | |
| 143 | } | |
| 144 | : {}), | |
| 135 | 145 | // Who may say who the client is: the ingress in front of a hosted forge. |
| 136 | 146 | ...(trustedProxies.length > 0 ? { trustedProxies } : {}), |
| 137 | 147 | ...((process.env.FORGE_CLIENT_IP_HEADER ?? "").trim() !== "" |
src/tui/screens/signin.ts +6
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * which is the admin — the terminal equivalent of the web's first-run signup. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { humanCheckOn } from "../../core/human-check.ts"; | |
| 11 | 12 | import { signUp } from "../../actions/users.ts"; |
| 12 | 13 | import { hasAnyUser } from "../../core/users.ts"; |
| 13 | 14 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -60,6 +61,11 @@ export function signInScreen(f: Session, ctx: Ctx, onDone: () => void): Screen { | ||
| 60 | 61 | string, |
| 61 | 62 | string, |
| 62 | 63 | ]; |
| 64 | // A terminal has no widget to show: the web page is the way in. | |
| 65 | if (humanCheckOn(f.app.config)) { | |
| 66 | error = "Sign up on the web page: it carries a human check."; | |
| 67 | return; | |
| 68 | } | |
| 63 | 69 | const created = await signUp( |
| 64 | 70 | f.app, |
| 65 | 71 | { username, email, password }, |
src/web/layout.ts +7 −1
| @@ -90,6 +90,11 @@ export interface PageOptions { | ||
| 90 | 90 | * origin" — so pages that do not ask for it keep the stricter policy. |
| 91 | 91 | */ |
| 92 | 92 | scriptAsset?: string; |
| 93 | /** | |
| 94 | * The one third-party origin a page may load script, frame and talk to | |
| 95 | * (Turnstile on /signup). Only the page that asks widens its policy. | |
| 96 | */ | |
| 97 | humanCheckOrigin?: string; | |
| 93 | 98 | /** Extra palette rows for this page's context (a repo's own tabs). */ |
| 94 | 99 | palette?: PaletteItem[]; |
| 95 | 100 | /** |
| @@ -642,12 +647,13 @@ export function page(options: PageOptions, status = 200): Response { | ||
| 642 | 647 | </body> |
| 643 | 648 | </html>`; |
| 644 | 649 | |
| 650 | const third = options.humanCheckOrigin; | |
| 645 | 651 | const headers = new Headers({ |
| 646 | 652 | "content-type": "text/html; charset=utf-8", |
| 647 | 653 | // The external stylesheet plus this page's own nonce'd <style>/<script> |
| 648 | 654 | // are the only resources allowed; connect-src 'self' lets the live |
| 649 | 655 | // event stream (EventSource) and fragment fetches reach same-origin. |
| 650 | "content-security-policy": `default-src 'none'; style-src 'self' 'nonce-${nonce}'; script-src 'nonce-${nonce}'${options.scriptAsset === undefined ? "" : " 'self'"}; connect-src 'self'; worker-src 'self'; manifest-src 'self'; img-src 'self' data:; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`, | |
| 656 | "content-security-policy": `default-src 'none'; style-src 'self' 'nonce-${nonce}'; script-src 'nonce-${nonce}'${options.scriptAsset === undefined ? "" : " 'self'"}${third ? ` ${third}` : ""}; connect-src 'self'${third ? ` ${third}` : ""}; ${third ? `frame-src ${third}; ` : ""}worker-src 'self'; manifest-src 'self'; img-src 'self' data:; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`, | |
| 651 | 657 | "referrer-policy": "same-origin", |
| 652 | 658 | "x-content-type-options": "nosniff", |
| 653 | 659 | // Markup is revalidated every time; only the hashed assets it names are |
src/web/pages/capacity.ts +2
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { changeCapacity, listCapacity } from "../../actions/capacity.ts"; |
| 10 | import { humanCheckLine } from "../../core/human-check.ts"; | |
| 10 | 11 | import { CAPACITY, type CapacityKey } from "../../core/capacity.ts"; |
| 11 | 12 | import type { Ctx } from "../../http/context.ts"; |
| 12 | 13 | import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts"; |
| @@ -113,6 +114,7 @@ export function capacityPage(ctx: Ctx): Response { | ||
| 113 | 114 | )} |
| 114 | 115 | ${card( |
| 115 | 116 | html`<div class="card-body"> |
| 117 | <p class="muted small" id="human-check">${humanCheckLine(ctx.app.config)}</p> | |
| 116 | 118 | <p class="muted small"> |
| 117 | 119 | How much the forge does at once. A change here takes effect on the |
| 118 | 120 | next crew run, check or app start — running work is left to finish — |
src/web/pages/site.ts +40 −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 { humanCheckOn, TURNSTILE_ORIGIN } from "../../core/human-check.ts"; | |
| 11 | 12 | import { mayView } from "../../core/sealed.ts"; |
| 12 | 13 | import { signOutHere } from "../../actions/signins.ts"; |
| 13 | 14 | import { clientAddress } from "../../http/peer.ts"; |
| @@ -312,6 +313,7 @@ interface AuthErrors { | ||
| 312 | 313 | username?: string; |
| 313 | 314 | email?: string; |
| 314 | 315 | password?: string; |
| 316 | humanCheck?: string; | |
| 315 | 317 | } |
| 316 | 318 | |
| 317 | 319 | function authForm( |
| @@ -392,6 +394,24 @@ function authForm( | ||
| 392 | 394 | : {}), |
| 393 | 395 | ...(errors.password ? { error: errors.password } : {}), |
| 394 | 396 | })} |
| 397 | ${when(humanCheckOn(ctx.app.config) && kind === "signup", () => { | |
| 398 | const check = ctx.app.config.humanCheck!; | |
| 399 | return html`<div class="mb"> | |
| 400 | <div | |
| 401 | class="cf-turnstile" | |
| 402 | data-sitekey="${check.siteKey}" | |
| 403 | data-response-field-name="human_token" | |
| 404 | ></div> | |
| 405 | ${errors.humanCheck | |
| 406 | ? html`<span class="error" role="alert">${errors.humanCheck}</span>` | |
| 407 | : null} | |
| 408 | <script | |
| 409 | src="${TURNSTILE_ORIGIN}/turnstile/v0/api.js" | |
| 410 | async | |
| 411 | defer | |
| 412 | ></script> | |
| 413 | </div>`; | |
| 414 | })} | |
| 395 | 415 | ${when( |
| 396 | 416 | firstAccount, |
| 397 | 417 | () => |
| @@ -438,8 +458,17 @@ export function signinPage(ctx: Ctx): Response { | ||
| 438 | 458 | return page({ ctx, title: "Sign in", body: authForm(ctx, "signin") }); |
| 439 | 459 | } |
| 440 | 460 | |
| 461 | /** Only the signup page, and only with the check on, admits Cloudflare. */ | |
| 462 | const signupOrigin = (ctx: Ctx) => | |
| 463 | humanCheckOn(ctx.app.config) ? { humanCheckOrigin: TURNSTILE_ORIGIN } : {}; | |
| 464 | ||
| 441 | 465 | export function signupPage(ctx: Ctx): Response { |
| 442 | return page({ ctx, title: "Sign up", body: authForm(ctx, "signup") }); | |
| 466 | return page({ | |
| 467 | ctx, | |
| 468 | title: "Sign up", | |
| 469 | body: authForm(ctx, "signup"), | |
| 470 | ...signupOrigin(ctx), | |
| 471 | }); | |
| 443 | 472 | } |
| 444 | 473 | |
| 445 | 474 | /** |
| @@ -552,7 +581,12 @@ export async function signupSubmit(ctx: Ctx): Promise<Response> { | ||
| 552 | 581 | const email = field(form, "email"); |
| 553 | 582 | const created = await signUp( |
| 554 | 583 | ctx.app, |
| 555 | { username, email, password: field(form, "password") }, | |
| 584 | { | |
| 585 | username, | |
| 586 | email, | |
| 587 | password: field(form, "password"), | |
| 588 | humanToken: field(form, "human_token"), | |
| 589 | }, | |
| 556 | 590 | { address: clientAddress(ctx.request) }, |
| 557 | 591 | ); |
| 558 | 592 | if (!created.ok) { |
| @@ -564,12 +598,16 @@ export async function signupSubmit(ctx: Ctx): Promise<Response> { | ||
| 564 | 598 | username_reserved: { username: "That name is reserved." }, |
| 565 | 599 | email_taken: { email: "That email already has an account." }, |
| 566 | 600 | weak_password: { password: "Passwords need at least 10 characters." }, |
| 601 | human_check: { | |
| 602 | humanCheck: "Please complete the human check and try again.", | |
| 603 | }, | |
| 567 | 604 | }[created.error] ?? { username: "Something went wrong." }; |
| 568 | 605 | return page( |
| 569 | 606 | { |
| 570 | 607 | ctx, |
| 571 | 608 | title: "Sign up", |
| 572 | 609 | body: authForm(ctx, "signup", { values: { username, email }, errors }), |
| 610 | ...signupOrigin(ctx), | |
| 573 | 611 | }, |
| 574 | 612 | 422, |
| 575 | 613 | ); |
test/human-check.test.ts added +192
| @@ -0,0 +1,192 @@ | ||
| 1 | import { afterEach, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { createSession } from "../src/core/sessions.ts"; | |
| 7 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 10 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 11 | import { settleApp } from "./helpers/tui.ts"; | |
| 12 | ||
| 13 | // Signup's Turnstile check: on only with both keys, verified by the server | |
| 14 | // before an account is made, widening the CSP of /signup alone. | |
| 15 | describe("signup human check", () => { | |
| 16 | let dir = ""; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let base = ""; | |
| 20 | let seen: (string | undefined)[] = []; | |
| 21 | ||
| 22 | const boot = async (on: boolean) => { | |
| 23 | dir = await mkdtemp(join(tmpdir(), "forge-human-")); | |
| 24 | app = createApp({ | |
| 25 | dataDir: dir, | |
| 26 | port: 0, | |
| 27 | ...(on | |
| 28 | ? { | |
| 29 | humanCheck: { | |
| 30 | siteKey: "site-key-123", | |
| 31 | secret: "very-secret-value", | |
| 32 | verify: async (token: string) => { | |
| 33 | seen.push(token); | |
| 34 | return token === "good"; | |
| 35 | }, | |
| 36 | }, | |
| 37 | } | |
| 38 | : {}), | |
| 39 | }); | |
| 40 | servers = await serve(app); | |
| 41 | base = servers.url; | |
| 42 | }; | |
| 43 | afterEach(async () => { | |
| 44 | servers?.stop(); | |
| 45 | app?.close(); | |
| 46 | seen = []; | |
| 47 | await rm(dir, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | const form = (fields: Record<string, string>) => | |
| 51 | fetch(`${base}/signup`, { | |
| 52 | method: "POST", | |
| 53 | headers: { origin: base, "content-type": "application/x-www-form-urlencoded" }, | |
| 54 | body: new URLSearchParams({ | |
| 55 | username: "newbie", | |
| 56 | email: "[email protected]", | |
| 57 | password: "a-good-passphrase", | |
| 58 | ...fields, | |
| 59 | }), | |
| 60 | redirect: "manual", | |
| 61 | }); | |
| 62 | ||
| 63 | test("the page renders the widget, with the origin in its CSP alone", async () => { | |
| 64 | await boot(true); | |
| 65 | const res = await fetch(`${base}/signup`); | |
| 66 | const body = await res.text(); | |
| 67 | const csp = res.headers.get("content-security-policy") ?? ""; | |
| 68 | expect(body).toContain('data-sitekey="site-key-123"'); | |
| 69 | expect(body).not.toContain("very-secret-value"); | |
| 70 | expect(csp).toContain("https://challenges.cloudflare.com"); | |
| 71 | const other = await fetch(`${base}/signin`); | |
| 72 | expect(other.headers.get("content-security-policy")).not.toContain( | |
| 73 | "cloudflare", | |
| 74 | ); | |
| 75 | expect(await other.text()).not.toContain("cf-turnstile"); | |
| 76 | }); | |
| 77 | ||
| 78 | test("a missing or bad token makes no account", async () => { | |
| 79 | await boot(true); | |
| 80 | for (const fields of [{}, { human_token: "bad" }]) { | |
| 81 | const res = await form(fields); | |
| 82 | expect(res.status).toBe(422); | |
| 83 | expect(await res.text()).toContain( | |
| 84 | "Please complete the human check and try again.", | |
| 85 | ); | |
| 86 | } | |
| 87 | expect(getUserByName(app.db, "newbie")).toBeUndefined(); | |
| 88 | expect(seen).toEqual(["bad"]); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a good token makes the account", async () => { | |
| 92 | await boot(true); | |
| 93 | const res = await form({ human_token: "good" }); | |
| 94 | expect(res.status).toBe(303); | |
| 95 | expect(getUserByName(app.db, "newbie")).toBeDefined(); | |
| 96 | }); | |
| 97 | ||
| 98 | test("the API refuses without a token and takes human_token", async () => { | |
| 99 | await boot(true); | |
| 100 | const post = (extra: object) => | |
| 101 | fetch(`${base}/api/v1/users`, { | |
| 102 | method: "POST", | |
| 103 | headers: { "content-type": "application/json", origin: base }, | |
| 104 | body: JSON.stringify({ | |
| 105 | username: "apiuser", | |
| 106 | email: "[email protected]", | |
| 107 | password: "a-good-passphrase", | |
| 108 | ...extra, | |
| 109 | }), | |
| 110 | }); | |
| 111 | const refused = await post({}); | |
| 112 | expect(refused.status).toBe(403); | |
| 113 | expect(((await refused.json()) as { error: string }).error).toBe("human_check"); | |
| 114 | expect((await post({ human_token: "good" })).status).toBe(201); | |
| 115 | }); | |
| 116 | ||
| 117 | test("with no key the check is off and the admin's settings say so", async () => { | |
| 118 | await boot(false); | |
| 119 | const page = await (await fetch(`${base}/signup`)).text(); | |
| 120 | expect(page).not.toContain("cf-turnstile"); | |
| 121 | expect((await form({})).status).toBe(303); | |
| 122 | const admin = getUserByName(app.db, "newbie")!; | |
| 123 | const res = await fetch(`${base}/settings/capacity`, { | |
| 124 | headers: { cookie: `forge_session=${createSession(app.db, admin.id).token}` }, | |
| 125 | }); | |
| 126 | expect(await res.text()).toContain("Human check on signup: off."); | |
| 127 | }); | |
| 128 | ||
| 129 | test("the settings line says on when the keys are set", async () => { | |
| 130 | await boot(true); | |
| 131 | const made = await createUser(app.db, { | |
| 132 | username: "boss", | |
| 133 | email: "[email protected]", | |
| 134 | password: "a-good-passphrase", | |
| 135 | adminIfFirst: true, | |
| 136 | trusted: true, | |
| 137 | }); | |
| 138 | if (!made.ok) throw new Error(made.error); | |
| 139 | const res = await fetch(`${base}/settings/capacity`, { | |
| 140 | headers: { cookie: `forge_session=${createSession(app.db, made.value.id).token}` }, | |
| 141 | }); | |
| 142 | expect(await res.text()).toContain("Human check on signup: on"); | |
| 143 | }); | |
| 144 | ||
| 145 | // The terminal has no widget to show, so with the check on its signup is | |
| 146 | // refused; with it off the same keystrokes make the account. | |
| 147 | const terminalSignup = async () => { | |
| 148 | const { app: tui } = createForgeApp( | |
| 149 | app, | |
| 150 | { | |
| 151 | sshUsername: "git", | |
| 152 | remoteAddress: "127.0.0.1", | |
| 153 | fingerprint: "SHA256:unknown", | |
| 154 | }, | |
| 155 | { terminal: headlessTerminal(120, 32), exitHooks: false }, | |
| 156 | ); | |
| 157 | tui.start(); | |
| 158 | const type = async (text: string) => { | |
| 159 | for (const key of text) | |
| 160 | tui.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 161 | await settleApp(tui); | |
| 162 | }; | |
| 163 | try { | |
| 164 | await settleApp(tui); | |
| 165 | await type("s"); // an empty forge opens the screen in create mode | |
| 166 | await type("newbie"); | |
| 167 | tui.dispatch({ type: "key", key: "tab", ctrl: false, alt: false, shift: false }); | |
| 168 | await type("[email protected]"); | |
| 169 | tui.dispatch({ type: "key", key: "tab", ctrl: false, alt: false, shift: false }); | |
| 170 | await type("a-good-passphrase"); | |
| 171 | tui.dispatch({ type: "key", key: "enter", ctrl: false, alt: false, shift: false }); | |
| 172 | await settleApp(tui); | |
| 173 | return tui.screen.toText(); | |
| 174 | } finally { | |
| 175 | tui.stop(); | |
| 176 | } | |
| 177 | }; | |
| 178 | ||
| 179 | test("the terminal refuses signup while the check is on", async () => { | |
| 180 | await boot(true); | |
| 181 | const screen = await terminalSignup(); | |
| 182 | expect(getUserByName(app.db, "newbie")).toBeUndefined(); | |
| 183 | expect(screen).toContain("Sign up on the web page"); | |
| 184 | }); | |
| 185 | ||
| 186 | test("the terminal signs up when the check is off", async () => { | |
| 187 | await boot(false); | |
| 188 | const screen = await terminalSignup(); | |
| 189 | expect(getUserByName(app.db, "newbie")).toBeDefined(); | |
| 190 | expect(screen).not.toContain("Sign up on the web page"); | |
| 191 | }); | |
| 192 | }); |
test/vitals.test.ts +1 −1
| @@ -105,7 +105,7 @@ test("a full queue shows in queue, and an old one in needs", () => { | ||
| 105 | 105 | for (let i = 0; i < 3; i++) createRun(app.db, { repoId, kind: "agent" }); |
| 106 | 106 | const v = vitals({ db: app.db, dataDir }); |
| 107 | 107 | expect(v.queue).toEqual({ agent: 3 }); |
| 108 | expect(v.ok).toBe(true); | |
| 108 | expect(v.needs.join(" ")).not.toContain("agent queue"); | |
| 109 | 109 | const later = vitals({ db: app.db, dataDir, now: old + 2 * 3_600_000 + 3_600_000 }); |
| 110 | 110 | expect(later.ok).toBe(false); |
| 111 | 111 | expect(later.needs.join(" ")).toContain("agent queue"); |
new above
offline
This refusal is the terminal's half of the gate, and no test covers it. Add one with
humanCheckon: submit signup through this screen, then assert no account was made anderrorshows the refusal.