summary refs log tree commit diff
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-09-17 15:31:20 +0200
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-12-19 20:57:43 +1100
commit3227933f285dfe79cec3f8876cfddde952040117 (patch)
treee7b729743e36055657bdd7496b153115bfe9e8e2
parentcomments (diff)
downloadserver-3227933f285dfe79cec3f8876cfddde952040117.tar.xz
Add register ratelimit
-rw-r--r--.prettierignore3
-rw-r--r--assets/locales/en/auth.json4
-rw-r--r--src/api/routes/auth/register.ts16
-rw-r--r--src/util/config/types/LimitConfigurations.ts5
-rw-r--r--src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts10
-rw-r--r--src/util/config/types/subconfigurations/limits/index.ts1
6 files changed, 34 insertions, 5 deletions
diff --git a/.prettierignore b/.prettierignore
index 49bc63ad..b36ef3a2 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -1,2 +1,3 @@
 assets
-dist
\ No newline at end of file
+dist
+node_modules
\ No newline at end of file
diff --git a/assets/locales/en/auth.json b/assets/locales/en/auth.json
index a78d4d60..2178548e 100644
--- a/assets/locales/en/auth.json
+++ b/assets/locales/en/auth.json
@@ -13,6 +13,8 @@
 		"EMAIL_ALREADY_REGISTERED": "Email is already registered",
 		"DATE_OF_BIRTH_UNDERAGE": "You need to be {{years}} years or older",
 		"CONSENT_REQUIRED": "You must agree to the Terms of Service and Privacy Policy.",
-		"USERNAME_TOO_MANY_USERS": "Too many users have this username, please try another"
+		"USERNAME_TOO_MANY_USERS": "Too many users have this username, please try another",
+		"GUESTS_DISABLED": "Guest users are disabled",
+		"TOO_MANY_REGISTRATIONS": "Too many registrations, please try again later"
 	}
 }
diff --git a/src/api/routes/auth/register.ts b/src/api/routes/auth/register.ts
index 370d7c52..eba86f77 100644
--- a/src/api/routes/auth/register.ts
+++ b/src/api/routes/auth/register.ts
@@ -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) {
diff --git a/src/util/config/types/LimitConfigurations.ts b/src/util/config/types/LimitConfigurations.ts
index bcc2e7e2..105fd1d6 100644
--- a/src/util/config/types/LimitConfigurations.ts
+++ b/src/util/config/types/LimitConfigurations.ts
@@ -1,4 +1,4 @@
-import { ChannelLimits, GuildLimits, MessageLimits, RateLimits, UserLimits } from ".";
+import { ChannelLimits, GlobalRateLimits, GuildLimits, MessageLimits, RateLimits, UserLimits } from ".";
 
 export class LimitsConfiguration {
 	user: UserLimits = new UserLimits();
@@ -6,4 +6,5 @@ export class LimitsConfiguration {
 	message: MessageLimits = new MessageLimits();
 	channel: ChannelLimits = new ChannelLimits();
 	rate: RateLimits = new RateLimits();
-}
\ No newline at end of file
+	absoluteRate: GlobalRateLimits = new GlobalRateLimits();
+}
diff --git a/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts b/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts
new file mode 100644
index 00000000..85280d3a
--- /dev/null
+++ b/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts
@@ -0,0 +1,10 @@
+export class GlobalRateLimits {
+	register: GlobalRateLimit = { limit: 25, window: 60 * 60 * 1000, enabled: true };
+	sendMessage: GlobalRateLimit = { limit: 50, window: 60 * 1000, enabled: true };
+}
+
+export class GlobalRateLimit {
+	limit: number = 100;
+	window: number = 60 * 60 * 1000;
+	enabled: boolean = true;
+}
diff --git a/src/util/config/types/subconfigurations/limits/index.ts b/src/util/config/types/subconfigurations/limits/index.ts
index 0b7304f6..ab7d9a5b 100644
--- a/src/util/config/types/subconfigurations/limits/index.ts
+++ b/src/util/config/types/subconfigurations/limits/index.ts
@@ -1,4 +1,5 @@
 export * from "./ChannelLimits";
+export * from "./GlobalRateLimits";
 export * from "./GuildLimits";
 export * from "./MessageLimits";
 export * from "./RateLimits";