-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: Add rate limiter for login route.
Removed general rate limiter and added rate limiter specific for login route that limits per ip + user combination.
- Loading branch information
Showing
7 changed files
with
201 additions
and
70 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* esp32-remote-access | ||
* Copyright (C) 2024 Frederic Henrichs <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the | ||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
* Boston, MA 02111-1307, USA. | ||
*/ | ||
|
||
use std::num::NonZeroU32; | ||
|
||
use actix_governor::{KeyExtractor, SimpleKeyExtractionError}; | ||
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, ResponseError}; | ||
use governor::{clock::{Clock, QuantaClock, QuantaInstant}, state::InMemoryState, NotUntil, Quota, RateLimiter}; | ||
|
||
|
||
/** | ||
* The struct used to extract ip for ratelimiting | ||
*/ | ||
#[derive(Clone)] | ||
pub struct IPExtractor; | ||
|
||
impl KeyExtractor for IPExtractor { | ||
type Key = String; | ||
type KeyExtractionError = SimpleKeyExtractionError<&'static str>; | ||
|
||
fn extract(&self, req: &actix_web::dev::ServiceRequest) -> Result<Self::Key, Self::KeyExtractionError> { | ||
let info = req.connection_info(); | ||
if let Some(ip) = info.realip_remote_addr() { | ||
Ok(ip.to_string()) | ||
} else { | ||
Err(SimpleKeyExtractionError::new("Invalid real IP")) | ||
} | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"IPExtractor" | ||
} | ||
} | ||
|
||
impl IPExtractor { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||
pub struct LoginRateLimitKey { | ||
user: String, | ||
ip: String, | ||
} | ||
|
||
// RateLimiter for the login route | ||
pub struct LoginRateLimiter { | ||
rate_limiter: RateLimiter<LoginRateLimitKey, dashmap::DashMap<LoginRateLimitKey, InMemoryState>, QuantaClock, governor::middleware::NoOpMiddleware<governor::clock::QuantaInstant>>, | ||
} | ||
|
||
impl LoginRateLimiter { | ||
pub fn new() -> Self { | ||
Self { | ||
rate_limiter: RateLimiter::keyed(Quota::per_second(NonZeroU32::new(1).unwrap()).allow_burst(NonZeroU32::new(5).unwrap())), | ||
} | ||
} | ||
|
||
pub fn check(&self, email: String, req: &HttpRequest) -> actix_web::Result<()> { | ||
let ip = if let Some(ip) = req.connection_info().realip_remote_addr() { | ||
ip.to_string() | ||
} else { | ||
return Err(crate::error::Error::InternalError.into()) | ||
}; | ||
|
||
let key = LoginRateLimitKey { | ||
user: email, | ||
ip | ||
}; | ||
if let Err(err) = self.rate_limiter.check_key(&key) { | ||
log::warn!("RateLimiter triggered for {:?}", key); | ||
let now = self.rate_limiter.clock().now(); | ||
|
||
Err(RateLimitError::new(err, now).into()) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
|
||
#[derive(Debug)] | ||
struct RateLimitError { | ||
wait_time: NotUntil<QuantaInstant>, | ||
now: QuantaInstant, | ||
} | ||
|
||
impl RateLimitError { | ||
pub fn new(wait_time: NotUntil<QuantaInstant>, now: QuantaInstant) -> Self { | ||
Self { wait_time, now } | ||
} | ||
} | ||
|
||
impl std::fmt::Display for RateLimitError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let wait_time = self.wait_time.wait_time_from(self.now); | ||
write!(f, "Retry in {} seconds.", wait_time.as_secs()) | ||
} | ||
} | ||
|
||
impl ResponseError for RateLimitError { | ||
fn status_code(&self) -> actix_web::http::StatusCode { | ||
StatusCode::TOO_MANY_REQUESTS | ||
} | ||
|
||
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> { | ||
let wait_time = self.wait_time.wait_time_from(self.now); | ||
HttpResponse::TooManyRequests() | ||
.append_header(("retry-after", wait_time.as_secs())) | ||
.append_header(("x-retry-after", wait_time.as_secs())) | ||
.body(self.to_string()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use actix_web::test; | ||
|
||
use super::LoginRateLimiter; | ||
|
||
#[actix_web::test] | ||
async fn test_login_rate_limiter() { | ||
let limiter = LoginRateLimiter::new(); | ||
let req = test::TestRequest::get() | ||
.uri("/login") | ||
.insert_header(("X-Forwarded-For", "123.123.123.2")) | ||
.to_http_request(); | ||
let email = "[email protected]".to_string(); | ||
|
||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_ok()); | ||
|
||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_ok()); | ||
|
||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_ok()); | ||
|
||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_ok()); | ||
|
||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_ok()); | ||
|
||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_err()); | ||
|
||
let email2 = "[email protected]".to_string(); | ||
let ret = limiter.check(email2.clone(), &req); | ||
assert!(ret.is_ok()); | ||
|
||
let req = test::TestRequest::get() | ||
.uri("/login") | ||
.insert_header(("X-Forwarded-For", "123.123.123.3")) | ||
.to_http_request(); | ||
let ret = limiter.check(email.clone(), &req); | ||
assert!(ret.is_ok()); | ||
} | ||
} |
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