- 
                Notifications
    You must be signed in to change notification settings 
- Fork 360
feat: implement rate limiting to prevent brute-force attacks (#1210) #1222
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
      
      
            Avdhesh-Varshney
  merged 7 commits into
  Code-A2Z:main
from
100NikhilBro:feature/rate-limiting-1210
  
      
      
   
  Sep 7, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3f03361
              
                feat: implement rate limiting to prevent brute-force attacks (#1210)
              
              
                100NikhilBro 8752a16
              
                Update index.js
              
              
                100NikhilBro fce1fe6
              
                Update package.json
              
              
                100NikhilBro 6bbd115
              
                Added rate-limiter-flexible and applied middleware to routes
              
              
                100NikhilBro 6f6f4fa
              
                Fixed package.json - only added rate-limiter-flexible
              
              
                100NikhilBro 2b84fee
              
                Update index.js
              
              
                100NikhilBro 0fa317a
              
                Update package.json
              
              
                100NikhilBro 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
  
    
      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,7 @@ | ||
| import { RateLimiterMemory } from 'rate-limiter-flexible' | ||
|  | ||
| export const authLimiter = new RateLimiterMemory({ | ||
| points: 5, | ||
| duration: 15 * 60, | ||
| blockDuration: 15 * 60, | ||
| }) | 
  
    
      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,7 @@ | ||
| import { RateLimiterMemory } from 'rate-limiter-flexible' | ||
|  | ||
| export const generalLimiter = new RateLimiterMemory({ | ||
| points: 100, | ||
| duration: 15 * 60, | ||
| blockDuration: 5 * 60 | ||
| }) | 
  
    
      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,10 @@ | ||
| import { authLimiter } from './authLimiter.js' | ||
| import { generalLimiter } from './generalLimiter.js'; | ||
|  | ||
| export const authMiddleware = (req, res, next) => { | ||
| authLimiter.consume(req.ip).then(() => next()).catch(() => res.status(429).send("Too many requests to /auth")); | ||
| } | ||
|  | ||
| export const generalMiddleware = (req, res, next) => { | ||
| generalLimiter.consume(req.ip).then(() => next()).catch(() => res.status(429).send("Too many requests")); | ||
| } | 
  
    
      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 | 
|---|---|---|
| @@ -1,22 +1,26 @@ | ||
| import express from 'express'; | ||
| import authRoutes from './api/auth.routes.js'; | ||
| import userRoutes from './api/user.routes.js'; | ||
| import mediaRoutes from './api/media.routes.js'; | ||
| import projectRoutes from './api/project.routes.js'; | ||
| import userRoutes from './api/user.routes.js'; | ||
| import notificationRoutes from './api/notification.routes.js'; | ||
| import subscriberRoutes from './api/subscriber.routes.js'; | ||
| import collectionRoutes from './api/collections.routes.js'; | ||
| import collaborationRoutes from './api/collaboration.routes.js'; | ||
|  | ||
| import { authMiddleware, generalMiddleware } from '../Middlewares/rateLimit/index.js'; | ||
|  | ||
| const router = express.Router(); | ||
|  | ||
| router.use('/auth', authRoutes); | ||
| router.use('/user', userRoutes); | ||
| router.use('/media', mediaRoutes); | ||
| router.use('/project', projectRoutes); | ||
| router.use('/notification', notificationRoutes); | ||
| router.use('/subscriber', subscriberRoutes); | ||
| router.use("/collection", collectionRoutes); | ||
| router.use('/collaboration', collaborationRoutes); | ||
|  | ||
| router.use('/auth', authMiddleware, authRoutes); | ||
|  | ||
| router.use('/user', generalMiddleware, userRoutes); | ||
| router.use('/media', generalMiddleware, mediaRoutes); | ||
| router.use('/project', generalMiddleware, projectRoutes); | ||
| router.use('/notification', generalMiddleware, notificationRoutes); | ||
| router.use('/subscriber', generalMiddleware, subscriberRoutes); | ||
| router.use('/collection', generalMiddleware, collectionRoutes); | ||
| router.use('/collaboration', generalMiddleware, collaborationRoutes); | ||
|  | ||
| export default router; | 
      
      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.