-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#6530): add rate limiting for authentication requests
- Loading branch information
1 parent
f4a5f11
commit 1332879
Showing
19 changed files
with
539 additions
and
201 deletions.
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 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 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 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 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,34 @@ | ||
const auth = require('../auth'); | ||
const rateLimitService = require('../services/rate-limit'); | ||
const serverUtils = require('../server-utils'); | ||
|
||
const registerReqFinishHandler = (req, res) => { | ||
res.on('finish', () => { | ||
if (res.statusCode === 401 || res.statusCode === 429) { | ||
// log in failed - punish user | ||
rateLimitService.consume(req); | ||
} | ||
}); | ||
}; | ||
|
||
const isLoggingIn = (req) => { | ||
const basicAuth = auth.basicAuthCredentials(req); | ||
return req.body?.user || basicAuth?.username; | ||
}; | ||
|
||
const shouldLimit = async (req) => { | ||
if (isLoggingIn(req)) { | ||
return await rateLimitService.isLimited(req); | ||
} | ||
return false; | ||
}; | ||
|
||
const rateLimiterMiddleware = async (req, res, next) => { | ||
if (await shouldLimit(req)) { | ||
return serverUtils.rateLimited(req, res); | ||
} | ||
registerReqFinishHandler(req, res); | ||
next(); | ||
}; | ||
|
||
module.exports = rateLimiterMiddleware; |
This file contains 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 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 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 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,58 @@ | ||
const { RateLimiterMemory } = require('rate-limiter-flexible'); | ||
const auth = require('../auth'); | ||
|
||
const failedLoginLimit = new RateLimiterMemory({ | ||
keyPrefix: 'failed-login', | ||
points: 10, // 10 requests | ||
duration: 10, // per 10 seconds | ||
}); | ||
|
||
const isLimitedKey = async (key) => { | ||
const limit = await failedLoginLimit.get(key); | ||
return limit && limit.remainingPoints <= 0; | ||
}; | ||
|
||
const consumeKey = async (key) => { | ||
try { | ||
await failedLoginLimit.consume(key); | ||
} catch (e) { | ||
// ignore - the limit has already been reached | ||
} | ||
}; | ||
|
||
const getKeys = (req) => { | ||
const keys = [ req.ip ]; | ||
|
||
if (req.body?.user) { | ||
keys.push(req.body.user); | ||
} | ||
if (req.body?.password) { | ||
keys.push(req.body.password); | ||
} | ||
const basicAuth = auth.basicAuthCredentials(req); | ||
if (basicAuth?.username) { | ||
keys.push(basicAuth.username); | ||
} | ||
if (basicAuth?.password) { | ||
keys.push(basicAuth.password); | ||
} | ||
return keys; | ||
}; | ||
|
||
module.exports = { | ||
isLimited: async req => { | ||
const keys = getKeys(req); | ||
for (const key of keys) { | ||
if (await isLimitedKey(key)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
consume: async req => { | ||
const keys = getKeys(req); | ||
for (const key of keys) { | ||
await consumeKey(key); | ||
} | ||
} | ||
}; |
This file contains 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
Oops, something went wrong.