add filesize rate limiting

This commit is contained in:
Ishaan Dey 2024-05-05 12:58:45 -07:00
parent 7fba908b7c
commit dd400b1d2a
2 changed files with 11 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import {
} from "./utils"
import { IDisposable, IPty, spawn } from "node-pty"
import {
MAX_BODY_SIZE,
createFileRL,
deleteFileRL,
renameFileRL,
@ -116,6 +117,14 @@ io.on("connection", async (socket) => {
try {
await saveFileRL.consume(data.userId, 1)
if (Buffer.byteLength(body, "utf-8") > MAX_BODY_SIZE) {
socket.emit(
"rateLimit",
"Rate limited: file size too large. Please reduce the file size."
)
return
}
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
file.data = body

View File

@ -5,6 +5,8 @@ export const saveFileRL = new RateLimiterMemory({
duration: 1,
})
export const MAX_BODY_SIZE = 5 * 1024 * 1024
export const createFileRL = new RateLimiterMemory({
points: 3,
duration: 1,