mirror of
https://github.com/spacebarchat/server.git
synced 2026-08-14 15:30:26 +00:00
Add register ratelimit
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
} from "@fosscord/api";
|
||||
import bcrypt from "bcrypt";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { MoreThan } from "typeorm";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
@@ -25,7 +26,7 @@ router.post(
|
||||
route({ body: "RegisterSchema" }),
|
||||
async (req: Request, res: Response) => {
|
||||
const body = req.body as RegisterSchema;
|
||||
const { register, security } = Config.get();
|
||||
const { register, security, limits } = Config.get();
|
||||
const ip = getIpAdress(req);
|
||||
|
||||
// email will be slightly modified version of the user supplied email -> e.g. protection against GMail Trick
|
||||
@@ -198,6 +199,19 @@ router.post(
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
limits.absoluteRate.register.enabled &&
|
||||
(await User.count({ where: { created_at: MoreThan(new Date(Date.now() - limits.absoluteRate.register.window)) } }))
|
||||
>= limits.absoluteRate.register.limit
|
||||
) {
|
||||
console.log(
|
||||
`Global register ratelimit exceeded for ${getIpAdress(req)}, ${req.body.username}, ${req.body.invite || "No invite given"}`
|
||||
);
|
||||
throw FieldErrors({
|
||||
email: { code: "TOO_MANY_REGISTRATIONS", message: req.t("auth:register.TOO_MANY_REGISTRATIONS") }
|
||||
});
|
||||
}
|
||||
|
||||
const user = await User.register({ ...body, req });
|
||||
|
||||
if (body.invite) {
|
||||
|
||||
Reference in New Issue
Block a user