Gitea Actions Runner Test / test-job (push) Successful in 2s
CI / check (push) Successful in 28s
CI / tests-integration (push) Successful in 1m42s
CI / tests-unit (push) Failing after 1m45s
CI / tests-ui (push) Successful in 2m29s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
Closes the four HIGH/MEDIUM items left open after the previous pass. Login lockout - The only login limits were keyed on the client IP, so a distributed attempt could grind on one account indefinitely. Added a per-account lockout with a budget of 8 failures per 15 minutes. - The bucket is keyed on the RESOLVED account id, not on the submitted string: users may sign in with either username or e-mail and neither the lookup nor the input normaliser folds case, so an input-keyed bucket would hand out a fresh budget per spelling of the same account. - precheckLogin and NextAuth's authorize share the bucket, so the pre-check cannot be used to buy extra attempts and a client that skips it entirely is still bounded. Both check the lockout BEFORE verifying the password: the success path clears the counter, which would otherwise walk a locked account straight back in on the right password. - A successful login clears the failures, which needs two new primitives in rate-limit.ts: peekRateLimit (read-only, does not consume a unit) and clearRateLimit. - Fixed a latent inconsistency while doing so: the in-process bucket capped its counter at the limit while Redis' INCR kept climbing, so the two backends disagreed about how far over the limit a key was. Both now track the true count. Mail lookup index - Added an index on users.mail (0035). Password reset, e-mail verification and the resend cooldown all resolve a single account from a submitted address and were full table scans of `users`. Deliberately non-unique: legacy rows can hold the same address more than once, so a unique index would fail to apply. Resend captcha - /verify's resend form triggers real outbound mail and was reachable with only a cooldown. It now runs the configured captcha before the account lookup and before any send. Client message payload - The root layout serialised the whole catalogue into every page. pages.admin and admin are ~177 KB of the ~235 KB and are unreachable from the public route group, so that layout now installs its own provider with the staff namespaces removed. Nested providers replace rather than merge, which is why this has to live in the segment layout. /admin, /mod, /client and /admin-next keep the full set; a guard test fails if a public page ever references a staff namespace.
75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
"use server";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
import { sendVerification } from "@/lib/auth/email-verification";
|
|
import { db, User } from "@/lib/db";
|
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
|
import { captchaConfig, verifyCaptcha } from "@/lib/services/captcha";
|
|
|
|
export interface ResendVerificationState {
|
|
ok: boolean;
|
|
error: string | null;
|
|
}
|
|
|
|
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
|
|
/**
|
|
* Re-send a verification e-mail for an address the visitor typed on /verify.
|
|
*
|
|
* Deliberately reports success even when no matching unverified account exists:
|
|
* a distinct failure would let anyone probe which addresses are registered. The
|
|
* identical-privacy behaviour also applies to the e-mail templates, which are
|
|
* only sent for real accounts. Rate limiting plus captcha are the spam defence:
|
|
* this endpoint triggers real outbound mail, so an unverified address must not
|
|
* be usable as a free mail cannon.
|
|
*/
|
|
export async function resendVerification(
|
|
_prevState: ResendVerificationState,
|
|
formData: FormData,
|
|
): Promise<ResendVerificationState> {
|
|
const email = String(formData.get("email") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.toLowerCase();
|
|
if (!EMAIL_RE.test(email)) {
|
|
return { ok: false, error: "invalid" };
|
|
}
|
|
|
|
const ip = await clientIp();
|
|
if (!(await rateLimit(`verify:resend:${ip}`, 3, 10 * 60_000)).ok) {
|
|
return { ok: false, error: "rateLimited" };
|
|
}
|
|
// Same cooldown keyed on the address, so rotating IPs cannot be used to
|
|
// mail-bomb an arbitrary inbox with "verify your email".
|
|
if (!(await rateLimit(`verify:resend:email:${email}`, 3, 10 * 60_000)).ok) {
|
|
return { ok: false, error: "rateLimited" };
|
|
}
|
|
|
|
// Captcha runs before any lookup or send, and answers with the same
|
|
// `captcha` code the login form uses so the UI can point at the widget.
|
|
const cfg = await captchaConfig();
|
|
if (cfg.provider !== "none") {
|
|
const token = String(formData.get(cfg.field) ?? "")
|
|
.normalize("NFC")
|
|
.trim();
|
|
if (!(await verifyCaptcha(token || null, ip))) {
|
|
return { ok: false, error: "captcha" };
|
|
}
|
|
}
|
|
|
|
try {
|
|
const [user] = await db
|
|
.select({ id: User.id, mailVerified: User.mailVerified })
|
|
.from(User)
|
|
.where(eq(User.mail, email))
|
|
.limit(1);
|
|
if (user && user.mailVerified !== "1") {
|
|
await sendVerification(email);
|
|
}
|
|
} catch {
|
|
return { ok: false, error: "unavailable" };
|
|
}
|
|
|
|
return { ok: true, error: null };
|
|
}
|