Repository navigation
OM-554: Rate Limiter #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
OM-554: Rate Limiter #194
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
efc33cb
OM-554: Rate limiter
SebaKisser 7f33873
Merge branch 'dev' of https://github.com/CrisNAC/BackendMarketplace i…
SebaKisser cfca8f9
OM-554: Correciones Code Rabbit
SebaKisser 81b2ddd
Merge branch 'dev' of https://github.com/CrisNAC/BackendMarketplace i…
SebaKisser 37cf756
OM-554: Correciones code rabbit
SebaKisser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { rateLimit } from 'express-rate-limit'; | ||
|
|
||
| const jsonResponse = (message) => ({ | ||
| success: false, | ||
| message, | ||
| }); | ||
|
|
||
| // Login: 5 intentos por IP cada 15 minutos | ||
| export const loginRateLimiter = rateLimit({ | ||
| windowMs: 15 * 60 * 1000, | ||
| limit: 5, | ||
| standardHeaders: 'draft-7', | ||
| legacyHeaders: false, | ||
| skip: () => process.env.NODE_ENV === 'test', | ||
| message: jsonResponse( | ||
| 'Demasiados intentos de inicio de sesión. Esperá 15 minutos e intentá de nuevo.' | ||
| ), | ||
| }); | ||
|
|
||
| // Recuperar contraseña: 3 solicitudes por IP cada hora | ||
| export const passwordResetRateLimiter = rateLimit({ | ||
| windowMs: 60 * 60 * 1000, | ||
| limit: 3, | ||
| standardHeaders: 'draft-7', | ||
| legacyHeaders: false, | ||
| skip: () => process.env.NODE_ENV === 'test', | ||
| message: jsonResponse( | ||
| 'Demasiadas solicitudes de recuperación de contraseña. Esperá 1 hora e intentá de nuevo.' | ||
| ), | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, it, expect, beforeEach } from "vitest"; | ||
| import express from "express"; | ||
| import request from "supertest"; | ||
| import { rateLimit } from "express-rate-limit"; | ||
|
|
||
| function buildApp(limiter) { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
| app.post("/test", limiter, (_req, res) => res.status(200).json({ ok: true })); | ||
| return app; | ||
| } | ||
|
|
||
| function makeTestLimiter(limit, message) { | ||
| return rateLimit({ | ||
| windowMs: 60 * 1000, | ||
| limit, | ||
| standardHeaders: "draft-7", | ||
| legacyHeaders: false, | ||
| message: { success: false, message }, | ||
| }); | ||
| } | ||
|
|
||
| describe("loginRateLimiter — lógica de rate limit", () => { | ||
| let app; | ||
|
|
||
| beforeEach(() => { | ||
| app = buildApp(makeTestLimiter(5, "Demasiados intentos de inicio de sesión. Esperá 15 minutos e intentá de nuevo.")); | ||
| }); | ||
|
|
||
| it("permite hasta 5 intentos", async () => { | ||
| for (let i = 0; i < 5; i++) { | ||
| const res = await request(app).post("/test").send({}); | ||
| expect(res.status).toBe(200); | ||
| } | ||
| }); | ||
|
|
||
| it("bloquea el 6° intento con 429", async () => { | ||
| for (let i = 0; i < 5; i++) { | ||
| await request(app).post("/test").send({}); | ||
| } | ||
| const res = await request(app).post("/test").send({}); | ||
| expect(res.status).toBe(429); | ||
| expect(res.body.success).toBe(false); | ||
| expect(res.body.message).toMatch(/demasiados intentos/i); | ||
| }); | ||
|
|
||
| it("incluye el header RateLimit en la respuesta", async () => { | ||
| const res = await request(app).post("/test").send({}); | ||
| expect(res.headers).toHaveProperty("ratelimit"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("passwordResetRateLimiter — lógica de rate limit", () => { | ||
| let app; | ||
|
|
||
| beforeEach(() => { | ||
| app = buildApp(makeTestLimiter(3, "Demasiadas solicitudes de recuperación de contraseña. Esperá 1 hora e intentá de nuevo.")); | ||
| }); | ||
|
|
||
| it("permite hasta 3 solicitudes", async () => { | ||
| for (let i = 0; i < 3; i++) { | ||
| const res = await request(app).post("/test").send({}); | ||
| expect(res.status).toBe(200); | ||
| } | ||
| }); | ||
|
|
||
| it("bloquea la 4° solicitud con 429", async () => { | ||
| for (let i = 0; i < 3; i++) { | ||
| await request(app).post("/test").send({}); | ||
| } | ||
| const res = await request(app).post("/test").send({}); | ||
| expect(res.status).toBe(429); | ||
| expect(res.body.success).toBe(false); | ||
| expect(res.body.message).toMatch(/demasiadas solicitudes/i); | ||
| }); | ||
|
|
||
| it("incluye el header RateLimit en la respuesta", async () => { | ||
| const res = await request(app).post("/test").send({}); | ||
| expect(res.headers).toHaveProperty("ratelimit"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.