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.
164 lines
4.7 KiB
TypeScript
164 lines
4.7 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import NextAuth from "next-auth";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import { env } from "@/env";
|
|
import { getCachedJwtVersion } from "@/lib/auth/jwt-version-cache";
|
|
import {
|
|
getLoginUser,
|
|
invalidateLoginCache,
|
|
isEmailUnverified,
|
|
normalizeLoginInput,
|
|
runDummyHashCheck,
|
|
verifyLoginPassword,
|
|
} from "@/lib/auth/login-core";
|
|
import {
|
|
clearLoginLockout,
|
|
isLoginLocked,
|
|
recordLoginFailure,
|
|
} from "@/lib/auth/login-lockout";
|
|
|
|
export { invalidateLoginCache };
|
|
|
|
import { verify2faChallenge } from "@/lib/auth/twofactor-verification";
|
|
import { recordWebsiteLogin } from "@/lib/auth/website-login-log";
|
|
import { db, User } from "@/lib/db";
|
|
import { logger } from "@/lib/logger";
|
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
|
|
|
export const { handlers, signOut, auth } = NextAuth({
|
|
trustHost: true,
|
|
secret: env.AUTH_SECRET,
|
|
session: { strategy: "jwt", maxAge: 24 * 60 * 60 },
|
|
pages: { signIn: "/login", error: "/login" },
|
|
logger: {
|
|
error(error) {
|
|
logger.error("NextAuth error", {
|
|
error: error.message,
|
|
stack: error.stack,
|
|
});
|
|
},
|
|
},
|
|
providers: [
|
|
Credentials({
|
|
credentials: {
|
|
username: { label: "Username", type: "text" },
|
|
password: { label: "Password", type: "password" },
|
|
code: { label: "2FA code", type: "text" },
|
|
},
|
|
authorize: async (credentials) => {
|
|
const { username, password } = normalizeLoginInput(
|
|
credentials?.username,
|
|
credentials?.password,
|
|
);
|
|
if (!username || !password) return null;
|
|
|
|
const ip = await clientIp();
|
|
|
|
// Throttle login attempts per IP (10 per 5 min) against credential stuffing.
|
|
if (!(await rateLimit(`login:${ip}`, 10, 5 * 60_000)).ok) return null;
|
|
|
|
const user = await getLoginUser(username);
|
|
if (!user) {
|
|
// Prevent timing-based enumeration: always run a dummy hash check.
|
|
await runDummyHashCheck(password);
|
|
return null;
|
|
}
|
|
|
|
// Same per-account lockout the pre-check uses (same key and budget), so
|
|
// the two paths cannot be used to buy extra attempts, and a client that
|
|
// skips the pre-check is still bounded. Checked before the password is
|
|
// verified — clearing on success would unlock a locked account.
|
|
if (await isLoginLocked(user.id)) return null;
|
|
|
|
const res = await verifyLoginPassword(user, password);
|
|
if (!res.valid) {
|
|
await recordLoginFailure(user.id);
|
|
return null;
|
|
}
|
|
|
|
await clearLoginLockout(user.id);
|
|
|
|
if (await isEmailUnverified(user)) {
|
|
return null;
|
|
}
|
|
|
|
if (res.upgradedHash) {
|
|
await db
|
|
.update(User)
|
|
.set({ password: res.upgradedHash })
|
|
.where(eq(User.id, user.id));
|
|
invalidateLoginCache(username);
|
|
}
|
|
|
|
// Two-factor: if enabled, a valid TOTP or recovery code is required.
|
|
if (user.twoFactorConfirmedAt && user.twoFactorSecret) {
|
|
const code = String(credentials?.code ?? "").trim();
|
|
if (!code || !env.APP_KEY) return null;
|
|
|
|
// Per-user 2FA rate limit (5 attempts per 30s) — prevents TOTP brute-force
|
|
// even when the attacker rotates IPs or knows the password.
|
|
if (!(await rateLimit(`2fa:${user.id}`, 5, 30_000)).ok) return null;
|
|
|
|
if (!(await verify2faChallenge(user.id, code))) return null;
|
|
}
|
|
|
|
// Record the successful login for the user's "session logs" page.
|
|
await recordWebsiteLogin(user.id, ip);
|
|
|
|
const jwtVersion = await getCachedJwtVersion(user.id);
|
|
return {
|
|
id: String(user.id),
|
|
name: user.username,
|
|
rank: user.rank,
|
|
jwtVersion,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async jwt({ token, user, account }) {
|
|
if (user) {
|
|
token.jwtVersion =
|
|
(user as { jwtVersion?: number }).jwtVersion ?? token.jwtVersion ?? 0;
|
|
token.jwtCheckedAt = Date.now();
|
|
}
|
|
|
|
if (user && account?.provider === "credentials") {
|
|
token.rank = (user as { rank?: number }).rank;
|
|
token.sub = String((user as { id?: string }).id);
|
|
return token;
|
|
}
|
|
|
|
// Re-check jwt version at most once per minute (memory/Redis cached).
|
|
if (token.sub && !token.invalid) {
|
|
const lastCheck =
|
|
typeof token.jwtCheckedAt === "number" ? token.jwtCheckedAt : 0;
|
|
if (Date.now() - lastCheck >= 60_000) {
|
|
try {
|
|
const version = await getCachedJwtVersion(Number(token.sub));
|
|
if (version === null || (token.jwtVersion ?? 0) !== version) {
|
|
token.invalid = true;
|
|
delete token.sub;
|
|
return token;
|
|
}
|
|
token.jwtCheckedAt = Date.now();
|
|
} catch {
|
|
logger.warn("JWT version check failed, keeping session");
|
|
}
|
|
}
|
|
}
|
|
|
|
return token;
|
|
},
|
|
session({ session, token }) {
|
|
if (token.invalid || !token.sub) {
|
|
return session;
|
|
}
|
|
if (session.user) session.user.id = token.sub;
|
|
if (typeof token.rank === "number" && session.user)
|
|
session.user.rank = token.rank;
|
|
return session;
|
|
},
|
|
},
|
|
});
|