Skip to content

Commit d5e43d9

Browse files
committed
feat: add rate limiting to /upload, /text, and /channels endpoints
1 parent 1ad9984 commit d5e43d9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

server/server.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ const loginLimiter = rateLimit({
284284
message: { error: "Too many attempts, try again later" }
285285
});
286286

287+
const dropLimiter = rateLimit({
288+
windowMs: 5 * 60 * 1000,
289+
max: 60,
290+
skip: () => process.env.NODE_ENV === 'test',
291+
message: { error: "Too many requests, try again later" }
292+
});
293+
294+
const channelLimiter = rateLimit({
295+
windowMs: 5 * 60 * 1000,
296+
max: 20,
297+
skip: () => process.env.NODE_ENV === 'test',
298+
message: { error: "Too many requests, try again later" }
299+
});
300+
287301
/* LOGIN POST */
288302
app.post("/login", loginLimiter, (req, res) => {
289303
if (!config.auth.passphrase) return res.redirect("/");
@@ -313,7 +327,7 @@ app.post("/logout", (req, res) => {
313327

314328

315329
/* FILE UPLOAD */
316-
app.post("/upload", upload.single("file"), (req, res) => {
330+
app.post("/upload", dropLimiter, upload.single("file"), (req, res) => {
317331
if (!req.file) {
318332
return res.status(400).json({ error: "No file received" });
319333
}
@@ -359,7 +373,7 @@ app.use((err, req, res, next) => {
359373
});
360374

361375
/* TEXT/LINK */
362-
app.post("/text", (req, res) => {
376+
app.post("/text", dropLimiter, (req, res) => {
363377
const { content, channel, uploader } = req.body;
364378

365379
const item = {
@@ -496,7 +510,7 @@ app.get("/channels", (req, res) => {
496510
});
497511

498512
/* ADD CHANNEL */
499-
app.post("/channels", (req, res) => {
513+
app.post("/channels", channelLimiter, (req, res) => {
500514

501515
const { name } = req.body;
502516
if (!name) return res.status(400).json({ error: "Name required" });

vitest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
env: {
6+
NODE_ENV: 'test'
7+
}
8+
}
9+
})

0 commit comments

Comments
 (0)