feat: Ratelimit CSAPI endpoints

This commit is contained in:
Ginger authored and Ellis Git committed 2026-11-05 16:49:48 +00:00
1 parent 15f7aabca6
commit 4a3b0366ac
6 files changed
+102 -6

No files matched your search

+13 -4
View File
@@ -1,18 +1,21 @@
use std::ops::Deref;
use std::{
net::{IpAddr, Ipv4Addr},
ops::Deref,
};
use axum::{
RequestExt, RequestPartsExt,
body::Body,
extract::{FromRequest, Path, Query},
};
use conduwuit::{Error, Result, err};
use conduwuit::{Error, Result, err, error};
use ruma::{
CanonicalJsonObject,
api::{IncomingRequest, IncomingRequestExt, auth_scheme::AuthScheme},
};
use serde::Deserialize;
use crate::{State, router::auth::CheckAuth};
use crate::{State, client_ip::ClientIp, router::auth::CheckAuth};
/// Query parameters needed to authenticate requests
#[derive(Deserialize)]
@@ -87,6 +90,11 @@ where
let Path(path): Path<Vec<String>> = parts.extract().await?;
let borrowed_path: Vec<_> = path.iter().map(String::as_str).collect();
let Query(auth_query): Query<AuthQueryParams> = parts.extract().await?;
let ClientIp(ip) = parts
.extract_with_state(services)
.await
.inspect_err(|err| error!("Failed to extract client IP: {err:?}"))
.unwrap_or(ClientIp(IpAddr::V4(Ipv4Addr::UNSPECIFIED)));
// Assemble a new request from the read body and parts
let request = hyper::Request::from_parts(parts, body.as_ref());
@@ -102,7 +110,8 @@ where
})?;
let identity =
R::Authentication::check::<R>(services, &request, authentication, auth_query).await?;
R::Authentication::check::<R>(services, &request, ip, authentication, auth_query)
.await?;
// Deserialize the body
let body = R::try_from_http_request(request, &borrowed_path)
+37 -1
View File
@@ -1,6 +1,7 @@
use std::{
any::{Any, TypeId},
collections::BTreeSet,
net::IpAddr,
};
use conduwuit::{
@@ -18,7 +19,10 @@ use ruma::{
AuthScheme, NoAccessToken, NoAuthentication,
},
client,
error::{ErrorKind, InsufficientUserAuthenticationErrorData, UnknownTokenErrorData},
error::{
ErrorKind, InsufficientUserAuthenticationErrorData, LimitExceededErrorData,
UnknownTokenErrorData,
},
federation::authentication::ServerSignatures,
},
assign,
@@ -90,6 +94,7 @@ pub(crate) trait CheckAuth: AuthScheme {
fn check<R: IncomingRequest<Authentication = Self> + Any>(
services: &Services,
incoming_request: &hyper::Request<&[u8]>,
request_ip: IpAddr,
authentication: Self::Output,
query: AuthQueryParams,
) -> impl Future<Output = Result<Self::Identity>> + Send;
@@ -101,6 +106,7 @@ impl CheckAuth for ServerSignatures {
async fn check<R: IncomingRequest<Authentication = Self> + Any>(
services: &Services,
incoming_request: &hyper::Request<&[u8]>,
_request_ip: IpAddr,
authentication: Self::Output,
_query: AuthQueryParams,
) -> Result<Self::Identity> {
@@ -161,6 +167,7 @@ impl CheckAuth for AccessToken {
async fn check<R: IncomingRequest<Authentication = Self> + Any>(
services: &Services,
_incoming_request: &hyper::Request<&[u8]>,
_request_ip: IpAddr,
authentication: Self::Output,
query: AuthQueryParams,
) -> Result<Self::Identity> {
@@ -181,6 +188,7 @@ impl CheckAuth for AccessTokenOptional {
async fn check<R: IncomingRequest<Authentication = Self> + Any>(
services: &Services,
_incoming_request: &hyper::Request<&[u8]>,
request_ip: IpAddr,
authentication: Self::Output,
query: AuthQueryParams,
) -> Result<Self::Identity> {
@@ -195,6 +203,10 @@ impl CheckAuth for AccessTokenOptional {
.await
.map(Some)
} else {
if R::RATE_LIMITED {
check_unauthenticated_request_ratelimiting(services, request_ip)?;
}
Ok(None)
}
}
@@ -206,6 +218,7 @@ impl CheckAuth for AppserviceToken {
async fn check<R: IncomingRequest + Any>(
services: &Services,
_incoming_request: &hyper::Request<&[u8]>,
_request_ip: IpAddr,
authentication: Self::Output,
_query: AuthQueryParams,
) -> Result<Self::Identity> {
@@ -219,6 +232,7 @@ impl CheckAuth for AppserviceTokenOptional {
async fn check<R: IncomingRequest<Authentication = Self> + Any>(
services: &Services,
_incoming_request: &hyper::Request<&[u8]>,
_request_ip: IpAddr,
authentication: Self::Output,
_query: AuthQueryParams,
) -> Result<Self::Identity> {
@@ -238,6 +252,7 @@ impl CheckAuth for NoAuthentication {
fn check<R: IncomingRequest<Authentication = Self> + Any>(
_services: &Services,
_incoming_request: &hyper::Request<&[u8]>,
_request_ip: IpAddr,
_authentication: Self::Output,
_query: AuthQueryParams,
) -> impl Future<Output = Result<Self::Identity>> + Send {
@@ -251,6 +266,7 @@ impl CheckAuth for NoAccessToken {
async fn check<R: IncomingRequest<Authentication = Self> + Any>(
services: &Services,
incoming_request: &hyper::Request<&[u8]>,
request_ip: IpAddr,
_authentication: Self::Output,
query: AuthQueryParams,
) -> Result<Self::Identity> {
@@ -267,6 +283,10 @@ impl CheckAuth for NoAccessToken {
.await
.map(Some)
} else {
if R::RATE_LIMITED {
check_unauthenticated_request_ratelimiting(services, request_ip)?;
}
Ok(None)
}
}
@@ -408,3 +428,19 @@ async fn check_appservice_token(services: &Services, token: &str) -> Result<Regi
Ok(appservice_info)
}
fn check_unauthenticated_request_ratelimiting(services: &Services, ip: IpAddr) -> Result<()> {
if services
.config
.unauthenticated_request_ratelimit
.check_key(&ip)
.is_err()
{
return Err(Error::BadRequest(
ErrorKind::LimitExceeded(LimitExceededErrorData::new()),
"Rate limit exceeded.",
));
}
Ok(())
}