-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds rate-limiting to defend against buggy and malicious clients
- Loading branch information
1 parent
18b43d8
commit aabd26a
Showing
10 changed files
with
102 additions
and
15 deletions.
There are no files selected for viewing
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,42 @@ | ||
const { RateLimiterMemory, BurstyRateLimiter } = require('rate-limiter-flexible'); | ||
|
||
const SUSTAINED_REQ_PER_SEC = 8; | ||
const MAX_BURST = 50; // remotestorage.js appears to keep requesting until 10 failures | ||
|
||
const rateLimiterSustained = new RateLimiterMemory({ | ||
points: SUSTAINED_REQ_PER_SEC, | ||
duration: 1 | ||
}); | ||
|
||
const rateLimiterBurst = new RateLimiterMemory({ | ||
keyPrefix: 'burst', | ||
points: MAX_BURST - SUSTAINED_REQ_PER_SEC, | ||
duration: 10 | ||
}); | ||
|
||
async function rateLimiterPenalty (key, points = 1) { | ||
await rateLimiterSustained.penalty(key, points); | ||
await rateLimiterBurst.penalty(key, points); | ||
} | ||
|
||
async function rateLimiterBlock (key, secDuration) { | ||
await rateLimiterSustained.block(key, secDuration); | ||
await rateLimiterBurst.block(key, secDuration); | ||
} | ||
|
||
const rateLimiter = new BurstyRateLimiter( | ||
rateLimiterSustained, | ||
rateLimiterBurst | ||
); | ||
|
||
const rateLimiterMiddleware = async (req, res, next) => { | ||
try { | ||
await rateLimiter.consume(req.ip); | ||
next(); | ||
} catch (err) { | ||
res.set({ 'Retry-After': Math.ceil(err.msBeforeNext / 1000) }); | ||
res.status(429).end(); | ||
} | ||
}; | ||
|
||
module.exports = { rateLimiterPenalty, rateLimiterBlock, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
/** sanity check of username, to defend against ".." and whatnot */ | ||
module.exports = function sanityCheckUsername (req, res, next) { | ||
const { rateLimiterBlock } = require('./rateLimiterMiddleware'); | ||
module.exports = async function sanityCheckUsername (req, res, next) { | ||
const username = req.params.username || req.data.username || ''; | ||
if (username.length > 0 && !/\/|^\.+$/.test(username) && /[\p{L}\p{Nd}]{1,63}/u.test(username)) { | ||
return next(); | ||
} | ||
|
||
res.logNotes.add('invalid user'); | ||
res.status(400).end(); | ||
await rateLimiterBlock(req.ip, 61); | ||
}; |
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
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