Skip to content

Commit

Permalink
feat: allow disabling ratelimits (for developement purposes)
Browse files Browse the repository at this point in the history
  • Loading branch information
romeq committed Jun 28, 2023
1 parent 0620719 commit 3459e7a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default {
secret: process.env.SECRET || "keyboard cat",
abuseipdb_key: process.env.ABUSEIPDB_KEY || "",
data_dir: process.env.DATA_DIR || "./data",
skipRatelimiters: process.env.SKIPRATELIMITERS == "true",
getMailer: async () => {
const dkim =
process.env.DKIM_ENABLED === "true"
Expand Down
4 changes: 3 additions & 1 deletion src/ratelimiters/pastes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import config from "../config"
import rateLimit from "express-rate-limit"

// Limit each IP to 20 new paste requests per `window` (here, 30 mins)
export const newPasteRateLimiter = rateLimit({
skip: () => config.skipRatelimiters,
windowMs: 30 * 60 * 1000,
max: 20,
message: {
title: "IP-osoitetta on rajoitettu",
message: "Liian monta liitettä on luotu IP-osoitteestasi. Kokeile myöhemmin uudelleen."
message: "Liian monta liitettä on luotu IP-osoitteestasi. Kokeile myöhemmin uudelleen.",
},
standardHeaders: true,
legacyHeaders: false,
Expand Down
6 changes: 4 additions & 2 deletions src/ratelimiters/users.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import config from "../config"
import rateLimit from "express-rate-limit"

// Limit each IP to 5 create account requests per `window` (here, per day)
// -> keep in mind that account creations can be unsuccesful but they should
// not because frontend tells if cannot be created
export const createAccountLimiter = rateLimit({
skip: () => config.skipRatelimiters,
windowMs: 60 * 60 * 1000 * 24, // 1 day
max: 5,
message:
"Tästä IP-osoitteesta on luotu liian monta käyttäjää aikarajan sisään. Yritäthän myöhemmin uudelleen.",
message: "Tästä IP-osoitteesta on luotu liian monta käyttäjää aikarajan sisään. Yritäthän myöhemmin uudelleen.",
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})

// Limit each IP to 20 login requests per 30 mins
export const loginAccountLimiter = rateLimit({
skip: () => config.skipRatelimiters,
windowMs: 30 * 60 * 1000,
max: 20,
message:
Expand Down
2 changes: 1 addition & 1 deletion src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Users extends Routes {
if (identity) return this.sendErrorResponse(res, 403, userErrors.userAlreadyLoggedIn)

const registeredAt = parseInt(req.cookies?.["registered_at"])
if (registeredAt !== 0 && registeredAt - 3600 * 24)
if (!config.skipRatelimiters && registeredAt !== 0 && registeredAt - 3600 * 24)
return this.sendErrorResponse(res, 401, userErrors.registrationFailedRatelimit)

const body = await req.body
Expand Down

0 comments on commit 3459e7a

Please sign in to comment.