From ac26193ffee23de810035e5e64ab6d45bcbd1b84 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 20 Jun 2025 15:27:57 +0000 Subject: [PATCH] Check appservice registrations for unique as_token. (#72) Signed-off-by: Jason Volk --- src/service/appservice/mod.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/service/appservice/mod.rs b/src/service/appservice/mod.rs index 970af394..e0d69ccb 100644 --- a/src/service/appservice/mod.rs +++ b/src/service/appservice/mod.rs @@ -1,7 +1,11 @@ mod namespace_regex; mod registration_info; -use std::{collections::BTreeMap, iter::IntoIterator, sync::Arc}; +use std::{ + collections::{BTreeMap, HashSet}, + iter::IntoIterator, + sync::Arc, +}; use async_trait::async_trait; use futures::{Future, FutureExt, Stream, StreamExt, TryStreamExt}; @@ -47,6 +51,7 @@ impl crate::Service for Service { async fn worker(self: Arc) -> Result { self.init_registrations().await?; + self.check_registrations().await?; Ok(()) } @@ -88,6 +93,25 @@ impl Service { .await } + #[tracing::instrument(name = "check", skip(self))] + async fn check_registrations(&self) -> Result { + let regs = self.registration_info.read().await; + + let num_as_tokens = regs + .values() + .map(|info| &info.registration.as_token) + .collect::>() + .len(); + + if num_as_tokens < regs.len() { + return Err!( + "Conflicting Appservice registrations: each must have a unique as_token." + ); + } + + Ok(()) + } + /// Registers an appservice and returns the ID to the caller pub async fn register_appservice( &self,