Skip to content

Commit aa6436a

Browse files
committed
Allow banning registrations by IP address
1 parent 67468ca commit aa6436a

File tree

17 files changed

+190
-42
lines changed

17 files changed

+190
-42
lines changed

crates/handlers/src/graphql/mutations/user_email.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ impl UserEmailMutations {
398398
let state = ctx.state();
399399
let id = NodeType::User.extract_ulid(&input.user_id)?;
400400
let requester = ctx.requester();
401+
let requester_fingerprint = ctx.requester_fingerprint();
401402
let clock = state.clock();
402403
let mut rng = state.rng();
403404

@@ -427,6 +428,7 @@ impl UserEmailMutations {
427428
let res = policy
428429
.evaluate_email(mas_policy::EmailInput {
429430
email: &input.email,
431+
requester: requester_fingerprint.into(),
430432
})
431433
.await?;
432434
if !res.valid() {
@@ -559,6 +561,7 @@ impl UserEmailMutations {
559561
let mut rng = state.rng();
560562
let clock = state.clock();
561563
let requester = ctx.requester();
564+
let requester_fingerprint = ctx.requester_fingerprint();
562565
let limiter = state.limiter();
563566

564567
// Only allow calling this if the requester is a browser session
@@ -617,6 +620,7 @@ impl UserEmailMutations {
617620
let res = policy
618621
.evaluate_email(mas_policy::EmailInput {
619622
email: &input.email,
623+
requester: requester_fingerprint.into(),
620624
})
621625
.await?;
622626
if !res.valid() {

crates/handlers/src/oauth2/authorization/complete.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ pub(crate) async fn complete(
231231
client,
232232
scope: &grant.scope,
233233
grant_type: mas_policy::GrantType::AuthorizationCode,
234+
requester: mas_policy::Requester {
235+
ip_address: activity_tracker.ip(),
236+
},
234237
})
235238
.await?;
236239

crates/handlers/src/oauth2/consent.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ pub(crate) async fn get(
116116
client: &client,
117117
scope: &grant.scope,
118118
grant_type: mas_policy::GrantType::AuthorizationCode,
119+
requester: mas_policy::Requester {
120+
ip_address: activity_tracker.ip(),
121+
},
119122
})
120123
.await?;
121124

@@ -195,6 +198,9 @@ pub(crate) async fn post(
195198
client: &client,
196199
scope: &grant.scope,
197200
grant_type: mas_policy::GrantType::AuthorizationCode,
201+
requester: mas_policy::Requester {
202+
ip_address: activity_tracker.ip(),
203+
},
198204
})
199205
.await?;
200206

crates/handlers/src/oauth2/device/consent.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub(crate) async fn get(
8787
client: &client,
8888
scope: &grant.scope,
8989
user: Some(&session.user),
90+
requester: mas_policy::Requester {
91+
ip_address: activity_tracker.ip(),
92+
},
9093
})
9194
.await?;
9295
if !res.valid() {
@@ -167,6 +170,9 @@ pub(crate) async fn post(
167170
client: &client,
168171
scope: &grant.scope,
169172
user: Some(&session.user),
173+
requester: mas_policy::Requester {
174+
ip_address: activity_tracker.ip(),
175+
},
170176
})
171177
.await?;
172178
if !res.valid() {

crates/handlers/src/oauth2/registration.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use thiserror::Error;
2525
use tracing::info;
2626
use url::Url;
2727

28-
use crate::impl_from_error_for_route;
28+
use crate::{impl_from_error_for_route, BoundActivityTracker};
2929

3030
#[derive(Debug, Error)]
3131
pub(crate) enum RouteError {
@@ -195,6 +195,7 @@ pub(crate) async fn post(
195195
clock: BoxClock,
196196
mut repo: BoxRepository,
197197
mut policy: Policy,
198+
activity_tracker: BoundActivityTracker,
198199
State(encrypter): State<Encrypter>,
199200
body: Result<Json<ClientMetadata>, axum::extract::rejection::JsonRejection>,
200201
) -> Result<impl IntoResponse, RouteError> {
@@ -247,6 +248,9 @@ pub(crate) async fn post(
247248
let res = policy
248249
.evaluate_client_registration(mas_policy::ClientRegistrationInput {
249250
client_metadata: &metadata,
251+
requester: mas_policy::Requester {
252+
ip_address: activity_tracker.ip(),
253+
},
250254
})
251255
.await?;
252256
if !res.valid() {

crates/handlers/src/oauth2/token.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ async fn client_credentials_grant(
681681
client,
682682
scope: &scope,
683683
grant_type: mas_policy::GrantType::ClientCredentials,
684+
requester: mas_policy::Requester {
685+
ip_address: activity_tracker.ip(),
686+
},
684687
})
685688
.await?;
686689
if !res.valid() {

crates/handlers/src/rate_limit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ pub struct RequesterFingerprint {
5353
ip: Option<IpAddr>,
5454
}
5555

56+
impl From<RequesterFingerprint> for mas_policy::Requester {
57+
fn from(val: RequesterFingerprint) -> Self {
58+
mas_policy::Requester { ip_address: val.ip }
59+
}
60+
}
61+
5662
impl std::fmt::Display for RequesterFingerprint {
5763
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5864
if let Some(ip) = self.ip {

crates/handlers/src/upstream_oauth2/link.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use super::{
4343
UpstreamSessionsCookie,
4444
};
4545
use crate::{
46-
impl_from_error_for_route, views::shared::OptionalPostAuthAction, PreferredLanguage, SiteConfig,
46+
impl_from_error_for_route, views::shared::OptionalPostAuthAction, BoundActivityTracker,
47+
PreferredLanguage, SiteConfig,
4748
};
4849

4950
const DEFAULT_LOCALPART_TEMPLATE: &str = "{{ user.preferred_username }}";
@@ -199,6 +200,7 @@ pub(crate) async fn get(
199200
State(url_builder): State<UrlBuilder>,
200201
State(homeserver): State<BoxHomeserverConnection>,
201202
cookie_jar: CookieJar,
203+
activity_tracker: BoundActivityTracker,
202204
user_agent: Option<TypedHeader<headers::UserAgent>>,
203205
Path(link_id): Path<Ulid>,
204206
) -> Result<impl IntoResponse, RouteError> {
@@ -445,6 +447,9 @@ pub(crate) async fn get(
445447
registration_method: mas_policy::RegistrationMethod::UpstreamOAuth2,
446448
username: &localpart,
447449
email: None,
450+
requester: mas_policy::Requester {
451+
ip_address: activity_tracker.ip(),
452+
},
448453
})
449454
.await?;
450455

@@ -502,6 +507,7 @@ pub(crate) async fn post(
502507
user_agent: Option<TypedHeader<headers::UserAgent>>,
503508
mut policy: Policy,
504509
PreferredLanguage(locale): PreferredLanguage,
510+
activity_tracker: BoundActivityTracker,
505511
State(templates): State<Templates>,
506512
State(homeserver): State<BoxHomeserverConnection>,
507513
State(url_builder): State<UrlBuilder>,
@@ -760,6 +766,9 @@ pub(crate) async fn post(
760766
registration_method: mas_policy::RegistrationMethod::UpstreamOAuth2,
761767
username: &username,
762768
email: email.as_deref(),
769+
requester: mas_policy::Requester {
770+
ip_address: activity_tracker.ip(),
771+
},
763772
})
764773
.await?;
765774

crates/handlers/src/views/register/password.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ pub(crate) async fn post(
237237
registration_method: mas_policy::RegistrationMethod::Password,
238238
username: &form.username,
239239
email: Some(&form.email),
240+
requester: mas_policy::Requester {
241+
ip_address: activity_tracker.ip(),
242+
},
240243
})
241244
.await?;
242245

crates/policy/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ mod tests {
307307
registration_method: RegistrationMethod::Password,
308308
username: "hello",
309309
email: Some("[email protected]"),
310+
requester: Requester { ip_address: None },
310311
})
311312
.await
312313
.unwrap();
@@ -317,6 +318,7 @@ mod tests {
317318
registration_method: RegistrationMethod::Password,
318319
username: "hello",
319320
email: Some("[email protected]"),
321+
requester: Requester { ip_address: None },
320322
})
321323
.await
322324
.unwrap();
@@ -327,6 +329,7 @@ mod tests {
327329
registration_method: RegistrationMethod::Password,
328330
username: "hello",
329331
email: Some("[email protected]"),
332+
requester: Requester { ip_address: None },
330333
})
331334
.await
332335
.unwrap();

0 commit comments

Comments
 (0)