Compare commits

...
20 Commits
Author SHA1 Message Date
Jason Volk af570c481e Bump 1.4.8
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-21 19:21:27 +00:00
Jason Volk dc9314de1f Fix missing validations for federation invites.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-21 12:34:58 +00:00
June Strawberry cf005ee537 bump ruma to fix join_rule being omitted on default value in /publicRooms
also updates complement results for now passing TestKnockRoomsInPublicRoomsDirectory

Signed-off-by: June Strawberry <june@vern.cc>
2025-12-20 22:22:40 -05:00
June Strawberry 6455ef72cd bump ldap3 fork, initialise aws_lc_rs with single function for ldap and direct tls
Signed-off-by: June Strawberry <june@vern.cc>
2025-12-20 22:16:27 -05:00
dasha_uwu 7115fb2796 Refactor join, alias services
Split knock, user register from api into services

Fix autojoin not working with v12 rooms

Fix 'm.login.registration_token/validity' for reloaded registration tokens

Change join servers order

Move autojoin for ldap
2025-12-18 18:41:23 +00:00
Jason Volk 959c559bd8 Log all ruma handler results at trace level.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 17:27:22 +00:00
Jason Volk a1bf188504 Update some links to project documentation.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 16:54:19 +00:00
Jason Volk 87770fefeb Bump dependencies.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 02:43:35 +00:00
dasha_uwu 0c7ba1dd5a Fix sync handling for appservices calling without device_id 2025-12-18 02:18:30 +00:00
dasha_uwu 7b2079f714 Check PDU formats 2025-12-18 01:53:09 +00:00
dasha_uwu c5508bba58 Fix appservices not receiving membership events to sender_localpart
Simplify sending PDUs to appservices
2025-12-18 01:49:54 +00:00
Jason Volk 88a6b72f0f Update config comment for default hydra backports.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 01:49:54 +00:00
Jason Volk 22c5f0207d Fix chat badge hyperlink too.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 01:49:45 +00:00
Jason Volk 3af78ac851 Fix readme documentation link; tweak wording. [ci skip]
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 14:34:14 +00:00
dasha_uwu b5f50c3fda Allow using OpenID userinfo endpoint with federation disabled, fixes #240
Even though it's in SS API this is not really related to federation, so
it makes sense to allow using this endpoint with allow_federation=false
2025-12-17 08:51:29 +00:00
Jason Volk c9a26a0280 Bump rust-rocksdb, jevmalloc. (fixes #238) (fixes #239)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 08:51:26 +00:00
Jason Volk 417e9ba052 Update README; additional docs link. (closes #235)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 06:53:06 +00:00
yefim 085cdb30f4 Fix newly created LDAP users not added to auto-join-rooms 2025-12-17 06:53:06 +00:00
Jason Volk 77474479b5 audit: Ignore unmaintained rustls-pemfile. (RUSTSEC-2025-0134)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 06:53:06 +00:00
Alexandru Scvorțov d244e8027c Update README to say matrix-tuwunel is in stable NixOS [ci skip] 2025-12-17 04:39:38 +00:00
66 changed files with 1656 additions and 1723 deletions

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
[advisories]
ignore = ["RUSTSEC-2024-0436", "RUSTSEC-2025-0014"] # advisory IDs to ignore e.g. ["RUSTSEC-2019-0001", ...]
ignore = ["RUSTSEC-2024-0436", "RUSTSEC-2025-0014", "RUSTSEC-2025-0134"] # advisory IDs to ignore e.g. ["RUSTSEC-2019-0001", ...]
informational_warnings = [] # warn for categories of informational advisories
severity_threshold = "none" # CVSS severity ("none", "low", "medium", "high", "critical")
Generated
+183 -218
View File
File diff suppressed because it is too large. Load diff
+6 -6
View File
@@ -29,7 +29,7 @@ license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/matrix-construct/tuwunel"
rust-version = "1.89.0"
version = "1.4.7"
version = "1.4.8"
[workspace.metadata.crane]
name = "tuwunel"
@@ -233,7 +233,7 @@ version = "0.14"
[workspace.dependencies.jevmalloc]
git = "https://github.com/matrix-construct/jevmalloc"
rev = "29fde02533ee2c6a5e9b87095324e6f7a5d43738"
rev = "93795449913f65ab533b7fa482333eef63fc3ae0"
[workspace.dependencies.jsonwebtoken]
version = "9.3"
@@ -242,7 +242,7 @@ features = ["use_pem"]
[workspace.dependencies.ldap3]
git = "https://github.com/matrix-construct/ldap3"
rev = "7d423314b9dbc66347284e38fc2b78c3d8f3d494"
rev = "fdfbba2bf916b53e5f73cdb1a495ebb649978079"
default-features = false
features = ["sync", "tls-rustls"]
@@ -324,7 +324,7 @@ default-features = false
[workspace.dependencies.ruma]
git = "https://github.com/matrix-construct/ruma"
rev = "f8aa292ce48f28ee6c82b29a811d26c6ccb5aad2"
rev = "214ab27fdc3d7004f6d46e0aa89fba573b59c66f"
features = [
"__compat",
"appservice-api-c",
@@ -364,7 +364,7 @@ features = [
[workspace.dependencies.rustls]
version = "0.23"
default-features = false
features = ["aws_lc_rs"]
features = ["aws_lc_rs", "logging", "tls12", "prefer-post-quantum"]
[workspace.dependencies.rustyline-async]
version = "0.4.6"
@@ -372,7 +372,7 @@ default-features = false
[workspace.dependencies.rust-rocksdb]
git = "https://github.com/matrix-construct/rust-rocksdb"
rev = "31e6f8dd4a531b379c132e82773efc9315b0489a"
rev = "c11395350bc1f2090a0152f2d15c8c5847821eba"
default-features = false
features = [
"bzip2",
+16 -15
View File
@@ -1,11 +1,11 @@
# Tuwunel<sup>💕</sup>
![GitHub License](https://img.shields.io/github/license/matrix-construct/tuwunel?style=flat&color=%238A2BE2)
![GitHub Created At](https://img.shields.io/github/created-at/matrix-construct/tuwunel?style=flat&color=%238A2BE2)
![GitHub Commit Activity](https://img.shields.io/github/commit-activity/m/matrix-construct/tuwunel?style=flat&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel%2Fpulse%2Fmonthly&color=%238A2BE2)
![Docker Pulls](https://img.shields.io/docker/pulls/jevolk/tuwunel?style=flat&color=8A2BE2)
![GitHub Repo Stars](https://img.shields.io/github/stars/matrix-construct/tuwunel?style=flat&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel&color=%238A2BE2)
[![CI/CD](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml/badge.svg?branch=main&style=flat)](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml)
![GitHub License](https://img.shields.io/github/license/matrix-construct/tuwunel?style=flat%2Dsquare&color=%238A2BE2)
![GitHub Created At](https://img.shields.io/github/created-at/matrix-construct/tuwunel?style=flat%2Dsquare&color=%238A2BE2)
![GitHub Commit Activity](https://img.shields.io/github/commit-activity/m/matrix-construct/tuwunel?style=flat%2Dsquare&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel%2Fpulse%2Fmonthly&color=%238A2BE2)
![Docker Pulls](https://img.shields.io/docker/pulls/jevolk/tuwunel?style=flat%2Dsquare&color=8A2BE2)
![GitHub Repo Stars](https://img.shields.io/github/stars/matrix-construct/tuwunel?style=flat%2Dsquare&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel&color=%238A2BE2)
[![CI/CD](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml/badge.svg?branch=main&style=flat%2Dsquare)](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml)
<!-- ANCHOR: catchphrase -->
@@ -15,19 +15,20 @@
<!-- ANCHOR: body -->
[![Documentation](https://img.shields.io/badge/documentation%2D_?color=%238A2BE2&style=for-the-badge&logo=mdBook&logoColor=white)](https://matrix-construct.github.io/tuwunel/)
[![Support Chat](https://img.shields.io/matrix/tuwunel%3Amatrix.org.svg?color=%238A2BE2&style=for-the-badge&label=Support%20Chat)](https://matrix.to/#/#tuwunel:grin.hu)
Tuwunel is a featureful [Matrix](https://matrix.org/) homeserver you can use instead of Synapse
with your favorite [client](https://matrix.org/ecosystem/clients/),
[bridge](https://matrix.org/ecosystem/bridges/) or
[bot](https://matrix.org/ecosystem/integrations/). It is written entirely in Rust to be a scalable,
lightweight, low-cost, community-driven alternative covering all but the most niche uses.
low-cost, enterprise-ready, community-driven alternative, fully implementing the
[Matrix Specification](https://spec.matrix.org/latest/) for all but the most niche uses.
This project is the official successor to conduwuit, which
was a featureful and high-performance fork of [Conduit](https://gitlab.com/famedly/conduit), all
community-lead homeservers implementing the compatible
[Matrix Specification](https://spec.matrix.org/latest/).
Tuwunel is operated by enterprise users with a vested interest in sponsoring its continued
development. It is now maintained by full-time staff.
This project is the official successor to [conduwuit](https://github.com/x86pup/conduwuit) after it
reached stability. Tuwunel is now used by many companies with a vested interest in its continued
development by full-time staff. It is primarily sponsored by the government of
Switzerland 🇨🇭 where it is currently deployed for citizens.
### Getting Started
@@ -38,7 +39,7 @@ development. It is now maintained by full-time staff.
- Static binaries available as [releases](https://github.com/matrix-construct/tuwunel/releases) or [build artifacts](https://github.com/matrix-construct/tuwunel/actions?query=branch%3Amain).
- Deb and RPM packages available as [releases](https://github.com/matrix-construct/tuwunel/releases) or [build artifacts](https://github.com/matrix-construct/tuwunel/actions?query=branch%3Amain).
- Arch package available as [tuwunel](https://aur.archlinux.org/packages/tuwunel) or [tuwunel-git](https://aur.archlinux.org/packages/tuwunel-git).
- Nix package still [needs some love](https://github.com/NixOS/nixpkgs/issues/415469).
- Nix package available as [`matrix-tuwunel`](https://search.nixos.org/packages?query=matrix-tuwunel) and NixOS module available as [`services.matrix-tuwunel`](https://search.nixos.org/options?query=services.matrix-tuwunel).
**1.** [Configure](https://matrix-construct.github.io/tuwunel/configuration.html) by
copying and editing the `tuwunel-example.toml`. The `server_name` and `database_path` must be
+5 -35
View File
@@ -1,39 +1,9 @@
# Tuwunel 1.4.7
# Tuwunel 1.4.8
December 3, 2025
December 21, 2025
Tuwunel is now deployed at scale serving the citizens of Switzerland in production. Some optimizations were requested to reduce operating costs from projected growth over product lifespan: this release delivers with markedly reduced CPU usage and improves responsiveness. However, complications during routine dependency upgrades consumed valuable time planned for features and issues which could not be completed for this release.
All federating deployments must upgrade to this patch for mitigations to severe vulnerabilities in Matrix protocol implementation logic. This is an off-schedule coordinated security release. Full release notes will be included with the next scheduled release.
### New Features
### Security Fixes
- Upgrade Support for Room Version 12 is now available. Though this room version has been supported for the entire 1.4 series, all Tuwunel servers have been protected by Hydra Backports on all room versions. As such, other work was able to be prioritized for the preceding releases.
### Enhancements
- Recursive relations have been enabled. This is an optimization which allows the server to gather more data using fewer client requests, for example, of a thread with many reactions and replies. The implementation is now optimal and utilizes the full capabilities of Tuwunel's asynchronous database.
- Several miscellaneous but significant optimizations took place at the direction of memory profiling. This reduced load on the allocator for database queries and JSON serialization of complex objects. Heroes calculations and the joined room hot-path on sync v3 were further optimized to reduce the database query load itself.
- Jemalloc has been repackaged with platform-specific optimizations enhancing the build. The upgrade to the dev branch of libjemalloc itself was considered as too much variability for the same release, it is planned for an upcoming release.
- Thanks to element-hq/synapse#18970 by @dasha-uwu, we have very slightly turned down the amount of randomness when selecting join-servers, More retries also occur within a single request if necessary. Thanks to @gogo199432 and @lifeofguenter for reporting problems in (#128) and (#205) respectively. More opportunities are still available to make large room joins robust.
### Bug Fixes
- Special thanks to @yefimg for fixing LDAP logout in (#231) from a report kindly made by @orhtej2 in (#97); thank you for your patience waiting for domain expertise to assist here.
- Thanks to @Radiant-Xyz the example configurations have been updated to remove `allow_check_for_updates`. This fixes any warnings for the item no longer existing. (#221)
- Thanks again to @Radiant-Xyz reporting in (#219) the `/whoami` endpoint is now returns spec-compliant errors for Mautrix bridges (fe12daead9). Thanks also to @bobobo1618 for confirming the fix is working.
- Relations responses were sometimes incorrect in the forwards direction. This was fixed by (5147b541) bringing those responses into full compliance. Note the prior release had also fixed compliance issues but in the backwards direction.
- Server selection for backfill struggled sometimes for version 12 rooms. These rooms might fail to load history after join. Additional servers are now found using `creators` and `additional_creators` instead.
- Room leave compliance has been fixed for an edge-case where a room becomes empty except for a locally invited user which does not have its leave event sent down `/sync`.
- Thanks to @grinapo for a report which lead to the discovery of events acquired over backfill not being checked for whether they already exist.
### Upcoming
- As stated in the summary, several planned items could not be cut into this release. These include SSO/OIDC support (#7), Element Call setup assistance and documentation (#217)(#215), User-level Admin Room and Media deletion (#192), and any other assigned issue. These items are on the short-list for the next cycle and mean a lot to us; to all participants: your issues are not being ignored and we hear you.
- Requests to the [Federation Invite API](https://spec.matrix.org/v1.17/server-server-api/#put_matrixfederationv2inviteroomideventid) lacked sufficient validation on all input fields. An attacker can use this route to process other kinds of events: upon acceptance, they are signed by the victim's server as specified by the Matrix protocol. The attacker can therefore forge events on behalf of the victim's authority to gain control of a room. This vulnerability was present in all versions and derivatives of Conduit.
+2 -2
View File
@@ -15,7 +15,7 @@ pub(crate) enum SyncCommand {
/// Show details of sliding sync connection by ID.
ShowConnection {
user_id: OwnedUserId,
device_id: OwnedDeviceId,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
},
@@ -43,7 +43,7 @@ pub(super) async fn list_connections(&self) -> Result {
pub(super) async fn show_connection(
&self,
user_id: OwnedUserId,
device_id: OwnedDeviceId,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
) -> Result {
let key = into_connection_key(user_id, device_id, conn_id);
+15 -155
View File
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, fmt::Write as _};
use std::collections::BTreeMap;
use futures::{FutureExt, StreamExt};
use ruma::{
@@ -13,10 +13,9 @@ use ruma::{
},
};
use tuwunel_core::{
Err, Result, debug, debug_warn, error, info, is_equal_to,
Err, Result, debug_warn, info,
matrix::{Event, pdu::PduBuilder},
utils::{self, ReadyExt},
warn,
};
use tuwunel_service::Services;
@@ -62,148 +61,11 @@ pub(super) async fn create_user(&self, username: String, password: Option<String
let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH));
// Create user
self.services
.users
.create(&user_id, Some(password.as_str()), None)
.full_register(&user_id, Some(&password), None, None, false, true)
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !self
.services
.server
.config
.new_user_displayname_suffix
.is_empty()
{
write!(
displayname,
" {}",
self.services
.server
.config
.new_user_displayname_suffix
)?;
}
self.services
.users
.set_displayname(&user_id, Some(displayname));
// Initial account data
self.services
.account_data
.update(
None,
&user_id,
ruma::events::GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: ruma::push::Ruleset::server_default(&user_id),
},
})?,
)
.await?;
if !self
.services
.server
.config
.auto_join_rooms
.is_empty()
{
for room in &self.services.server.config.auto_join_rooms {
let Ok(room_id) = self.services.alias.maybe_resolve(room).await else {
error!(
%user_id,
"Failed to resolve room alias to room ID when attempting to auto join {room}, skipping"
);
continue;
};
if !self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
let state_lock = self.services.state.mutex.lock(&room_id).await;
if let Some(room_server_name) = room.server_name() {
match self
.services
.membership
.join(
&user_id,
&room_id,
Some("Automatically joining this room upon registration".to_owned()),
&[
self.services.globals.server_name().to_owned(),
room_server_name.to_owned(),
],
&None,
&state_lock,
)
.await
{
| Ok(_response) => {
info!("Automatically joined room {room} for user {user_id}");
},
| Err(e) => {
// don't return this error so we don't fail registrations
error!(
"Failed to automatically join room {room} for user {user_id}: {e}"
);
self.services
.admin
.send_text(&format!(
"Failed to automatically join room {room} for user {user_id}: \
{e}"
))
.await;
},
}
drop(state_lock);
}
}
}
// we dont add a device since we're not the user, just the creator
// if this account creation is from the CLI / --execute, invite the first user
// to admin room
if let Ok(admin_room) = self.services.admin.get_admin_room().await {
if self
.services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
self.services
.admin
.make_user_admin(&user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
} else {
debug!("create_user admin command called without an admin room being available");
}
self.write_str(&format!("Created user with user_id: {user_id} and password: `{password}`"))
.await
}
@@ -403,7 +265,7 @@ pub(super) async fn list_joined_rooms(&self, user_id: String) -> Result {
#[admin_command]
pub(super) async fn force_join_list_of_local_users(
&self,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
yes_i_want_to_do_this: bool,
) -> Result {
if self.body.len() < 2
@@ -415,7 +277,7 @@ pub(super) async fn force_join_list_of_local_users(
if !yes_i_want_to_do_this {
return Err!(
"You must pass the --yes-i-want-to-do-this-flag to ensure you really want to force \
"You must pass the --yes-i-want-to-do-this flag to ensure you really want to force \
bulk join all specified local users.",
);
}
@@ -427,7 +289,7 @@ pub(super) async fn force_join_list_of_local_users(
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
if !self
@@ -505,9 +367,10 @@ pub(super) async fn force_join_list_of_local_users(
.join(
&user_id,
&room_id,
Some(&room),
Some(String::from(BULK_JOIN_REASON)),
&servers,
&None,
false,
&state_lock,
)
.await
@@ -534,7 +397,7 @@ pub(super) async fn force_join_list_of_local_users(
#[admin_command]
pub(super) async fn force_join_all_local_users(
&self,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
yes_i_want_to_do_this: bool,
) -> Result {
if !yes_i_want_to_do_this {
@@ -551,7 +414,7 @@ pub(super) async fn force_join_all_local_users(
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
if !self
@@ -600,9 +463,10 @@ pub(super) async fn force_join_all_local_users(
.join(
user_id,
&room_id,
Some(&room),
Some(String::from(BULK_JOIN_REASON)),
&servers,
&None,
false,
&state_lock,
)
.await
@@ -627,16 +491,12 @@ pub(super) async fn force_join_all_local_users(
}
#[admin_command]
pub(super) async fn force_join_room(
&self,
user_id: String,
room_id: OwnedRoomOrAliasId,
) -> Result {
pub(super) async fn force_join_room(&self, user_id: String, room: OwnedRoomOrAliasId) -> Result {
let user_id = parse_local_user_id(self.services, &user_id)?;
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
assert!(
@@ -648,7 +508,7 @@ pub(super) async fn force_join_room(
self.services
.membership
.join(&user_id, &room_id, None, &servers, &None, &state_lock)
.join(&user_id, &room_id, Some(&room), None, &servers, false, &state_lock)
.await?;
drop(state_lock);
+3 -3
View File
@@ -78,7 +78,7 @@ pub(super) enum UserCommand {
/// - Manually join a local user to a room.
ForceJoinRoom {
user_id: String,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
},
/// - Manually leave a local user from a room.
@@ -148,7 +148,7 @@ pub(super) enum UserCommand {
///
/// Requires the `--yes-i-want-to-do-this` flag.
ForceJoinListOfLocalUsers {
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
#[arg(long)]
yes_i_want_to_do_this: bool,
@@ -160,7 +160,7 @@ pub(super) enum UserCommand {
///
/// Requires the `--yes-i-want-to-do-this` flag.
ForceJoinAllLocalUsers {
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
#[arg(long)]
yes_i_want_to_do_this: bool,
+7 -17
View File
@@ -121,26 +121,16 @@ async fn room_available_servers(
// insert our server as the very first choice if in list, else check if we can
// prefer the room alias server first
match servers
if let Some(server_index) = servers
.iter()
.position(|server_name| services.globals.server_is_ours(server_name))
{
| Some(server_index) => {
servers.swap_remove(server_index);
servers.insert(0, services.globals.server_name().to_owned());
},
| _ => {
match servers
.iter()
.position(|server| server == room_alias.server_name())
{
| Some(alias_server_index) => {
servers.swap_remove(alias_server_index);
servers.insert(0, room_alias.server_name().into());
},
| _ => {},
}
},
servers.swap(0, server_index);
} else if let Some(alias_server_index) = servers
.iter()
.position(|server| server == room_alias.server_name())
{
servers.swap(0, alias_server_index);
}
servers
+3 -3
View File
@@ -32,8 +32,8 @@ pub(crate) async fn get_context_route(
State(services): State<crate::State>,
body: Ruma<get_context::v3::Request>,
) -> Result<get_context::v3::Response> {
let sender = body.sender();
let (sender_user, sender_device) = sender;
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let room_id = &body.room_id;
let event_id = &body.event_id;
let filter = &body.filter;
@@ -110,7 +110,7 @@ pub(crate) async fn get_context_route(
let lazy_loading_context = lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
device_id: sender_device,
room_id,
token: Some(base_count.into_unsigned()),
options: Some(&filter.lazy_load_options),
+4 -6
View File
@@ -2,14 +2,14 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::StreamExt;
use ruma::{
MilliSecondsSinceUnixEpoch, OwnedDeviceId,
MilliSecondsSinceUnixEpoch,
api::client::device::{
self, delete_device, delete_devices, get_device, get_devices, update_device,
},
};
use tuwunel_core::{Err, Result, debug, err, utils, utils::string::to_small_string};
use tuwunel_core::{Err, Result, debug, err, utils::string::to_small_string};
use crate::{Ruma, client::DEVICE_ID_LENGTH, router::auth_uiaa};
use crate::{Ruma, router::auth_uiaa};
/// # `GET /_matrix/client/r0/devices`
///
@@ -94,13 +94,11 @@ pub(crate) async fn update_device_route(
appservice.registration.id
);
let device_id = OwnedDeviceId::from(utils::random_string(DEVICE_ID_LENGTH));
services
.users
.create_device(
sender_user,
&device_id,
None,
(Some(&appservice.registration.as_token), None),
None,
None,
+6 -4
View File
@@ -23,7 +23,7 @@ pub(crate) async fn events_route(
State(services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let from = body
.body
@@ -62,9 +62,11 @@ pub(crate) async fn events_route(
.expect("configuration must limit maximum timeout");
loop {
let watchers = services
.sync
.watch(sender_user, sender_device, once(room_id).stream());
let watchers = services.sync.watch(
sender_user,
body.sender_device.as_deref(),
once(room_id).stream(),
);
let next_batch = services.globals.wait_pending().await?;
+2 -1
View File
@@ -35,7 +35,8 @@ pub(crate) async fn upload_keys_route(
State(services): State<crate::State>,
body: Ruma<upload_keys::v3::Request>,
) -> Result<upload_keys::v3::Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device()?;
let one_time_keys = body
.one_time_keys
+1 -2
View File
@@ -22,8 +22,7 @@ pub(crate) async fn invite_user_route(
invite_check(&services, sender_user, room_id).await?;
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
.await?;
banned_room_check(&services, sender_user, room_id, None, client).await?;
let invite_user::v3::InvitationRecipient::UserId { user_id } = &body.recipient else {
return Err!(Request(ThreepidDenied("Third party identifiers are not implemented")));
+17 -17
View File
@@ -2,13 +2,13 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use ruma::{
RoomId, RoomOrAliasId,
RoomId,
api::client::membership::{join_room_by_id, join_room_by_id_or_alias},
};
use tuwunel_core::{Result, warn};
use super::banned_room_check;
use crate::{Ruma, client::membership::get_join_params};
use crate::Ruma;
/// # `POST /_matrix/client/r0/rooms/{roomId}/join`
///
@@ -28,23 +28,20 @@ pub(crate) async fn join_room_by_id_route(
let room_id: &RoomId = &body.room_id;
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
.await?;
banned_room_check(&services, sender_user, room_id, None, client).await?;
let (room_id, servers) =
get_join_params(&services, sender_user, <&RoomOrAliasId>::from(room_id), &[]).await?;
let state_lock = services.state.mutex.lock(&room_id).await;
let state_lock = services.state.mutex.lock(room_id).await;
let mut errors = 0_usize;
while let Err(e) = services
.membership
.join(
sender_user,
&room_id,
room_id,
None,
body.reason.clone(),
&servers,
&body.appservice_info,
&[],
body.appservice_info.is_some(),
&state_lock,
)
.boxed()
@@ -62,7 +59,7 @@ pub(crate) async fn join_room_by_id_route(
drop(state_lock);
Ok(join_room_by_id::v3::Response { room_id })
Ok(join_room_by_id::v3::Response { room_id: room_id.to_owned() })
}
/// # `POST /_matrix/client/r0/join/{roomIdOrAlias}`
@@ -83,10 +80,12 @@ pub(crate) async fn join_room_by_id_or_alias_route(
let sender_user = body.sender_user();
let appservice_info = &body.appservice_info;
let (room_id, servers) =
get_join_params(&services, sender_user, &body.room_id_or_alias, &body.via).await?;
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client)
banned_room_check(&services, sender_user, &room_id, Some(&body.room_id_or_alias), client)
.await?;
let state_lock = services.state.mutex.lock(&room_id).await;
@@ -97,9 +96,10 @@ pub(crate) async fn join_room_by_id_or_alias_route(
.join(
sender_user,
&room_id,
Some(&body.room_id_or_alias),
body.reason.clone(),
&servers,
appservice_info,
appservice_info.is_some(),
&state_lock,
)
.boxed()
@@ -117,5 +117,5 @@ pub(crate) async fn join_room_by_id_or_alias_route(
drop(state_lock);
Ok(join_room_by_id_or_alias::v3::Response { room_id })
Ok(join_room_by_id_or_alias::v3::Response { room_id: room_id.clone() })
}
+16 -563
View File
@@ -1,45 +1,10 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::{FutureExt, StreamExt};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedServerName, RoomId,
RoomVersionId, UserId,
api::{
client::knock::knock_room,
federation::{
membership::RawStrippedState,
{self},
},
},
canonical_json::to_canonical_value,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
},
};
use tuwunel_core::{
Err, Result, debug, debug_info, debug_warn, err, extract_variant, info,
matrix::{
PduCount,
event::{Event, gen_event_id},
pdu::{PduBuilder, PduEvent},
},
trace,
utils::{self},
warn,
};
use tuwunel_service::{
Services,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
},
};
use ruma::api::client::knock::knock_room;
use tuwunel_core::Result;
use super::banned_room_check;
use crate::{Ruma, client::membership::get_join_params};
use crate::Ruma;
/// # `POST /_matrix/client/*/knock/{roomIdOrAlias}`
///
@@ -51,542 +16,30 @@ pub(crate) async fn knock_room_route(
body: Ruma<knock_room::v3::Request>,
) -> Result<knock_room::v3::Response> {
let sender_user = body.sender_user();
let body = &body.body;
let (room_id, servers) =
get_join_params(&services, sender_user, &body.room_id_or_alias, &body.via).await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client)
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
knock_room_by_id_helper(&services, sender_user, &room_id, body.reason.clone(), &servers).await
}
async fn knock_room_by_id_helper(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
) -> Result<knock_room::v3::Response> {
let state_lock = services.state.mutex.lock(room_id).await;
if services
.state_cache
.is_invited(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already invited in {room_id} but attempted to knock");
return Err!(Request(Forbidden(
"You cannot knock on a room you are already invited/accepted to."
)));
}
if services
.state_cache
.is_joined(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already joined in {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are already joined in.")));
}
if services
.state_cache
.is_knocked(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already knocked in {room_id}");
return Ok(knock_room::v3::Response { room_id: room_id.into() });
}
if let Ok(membership) = services
.state_accessor
.get_member(room_id, sender_user)
.await
{
if membership.membership == MembershipState::Ban {
debug_warn!("{sender_user} is banned from {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
}
let server_in_room = services
.state_cache
.server_in_room(services.globals.server_name(), room_id)
.await;
let local_knock = server_in_room
|| servers.is_empty()
|| (servers.len() == 1 && services.globals.server_is_ours(&servers[0]));
if local_knock {
knock_room_helper_local(services, sender_user, room_id, reason, servers, state_lock)
.boxed()
.await?;
} else {
knock_room_helper_remote(services, sender_user, room_id, reason, servers, state_lock)
.boxed()
.await?;
}
Ok(knock_room::v3::Response::new(room_id.to_owned()))
}
async fn knock_room_helper_local(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: RoomMutexGuard,
) -> Result {
debug_info!("We can knock locally");
let room_version_id = services.state.get_room_version(room_id).await?;
if matches!(
room_version_id,
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
) {
return Err!(Request(Forbidden("This room does not support knocking.")));
}
let content = RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason: reason.clone(),
..RoomMemberEventContent::new(MembershipState::Knock)
};
// Try normal knock first
let Err(error) = services
.timeline
.build_and_append_pdu(
PduBuilder::state(sender_user.to_string(), &content),
sender_user,
room_id,
&state_lock,
)
.await
else {
return Ok(());
};
if servers.is_empty() || (servers.len() == 1 && services.globals.server_is_ours(&servers[0]))
{
return Err(error);
}
warn!("We couldn't do the knock locally, maybe federation can help to satisfy the knock");
let (make_knock_response, remote_server) =
make_knock_request(services, sender_user, room_id, servers).await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_knock_response.event.get(),
)
.map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = services
.federation
.execute(&remote_server, send_knock_request)
banned_room_check(&services, sender_user, &room_id, Some(&body.room_id_or_alias), client)
.await?;
info!("send_knock finished");
let state_lock = services.state.mutex.lock(&room_id).await;
services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = services.globals.next_count();
services
.state_cache
.update_membership(
room_id,
.membership
.knock(
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
&room_id,
Some(&body.room_id_or_alias),
body.reason.clone(),
&servers,
&state_lock,
)
.await?;
Ok(())
}
drop(state_lock);
async fn knock_room_helper_remote(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: RoomMutexGuard,
) -> Result {
info!("Knocking {room_id} over federation.");
let (make_knock_response, remote_server) =
make_knock_request(services, sender_user, room_id, servers).await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub: CanonicalJsonObject =
serde_json::from_str(make_knock_response.event.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = services
.federation
.execute(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Going through send_knock response knock state events");
let state = send_knock_response
.knock_room_state
.iter()
.map(|event| {
serde_json::from_str::<CanonicalJsonObject>(
extract_variant!(event.clone(), RawStrippedState::Stripped)
.expect("Raw<AnyStrippedStateEvent>")
.json()
.get(),
)
})
.filter_map(Result::ok);
let mut state_map: HashMap<u64, OwnedEventId> = HashMap::new();
for event in state {
let Some(state_key) = event.get("state_key") else {
debug_warn!("send_knock stripped state event missing state_key: {event:?}");
continue;
};
let Some(event_type) = event.get("type") else {
debug_warn!("send_knock stripped state event missing event type: {event:?}");
continue;
};
let Ok(state_key) = serde_json::from_value::<String>(state_key.clone().into()) else {
debug_warn!("send_knock stripped state event has invalid state_key: {event:?}");
continue;
};
let Ok(event_type) = serde_json::from_value::<StateEventType>(event_type.clone().into())
else {
debug_warn!("send_knock stripped state event has invalid event type: {event:?}");
continue;
};
let event_id = gen_event_id(&event, &room_version_id)?;
let shortstatekey = services
.short
.get_or_create_shortstatekey(&event_type, &state_key)
.await;
services
.timeline
.add_pdu_outlier(&event_id, &event);
state_map.insert(shortstatekey, event_id.clone());
}
info!("Compressing state from send_knock");
let compressed: CompressedState = services
.state_compressor
.compress_state_events(
state_map
.iter()
.map(|(ssk, eid)| (ssk, eid.borrow())),
)
.collect()
.await;
debug!("Saving compressed state");
let HashSetCompressStateEvent {
shortstatehash: statehash_before_knock,
added,
removed,
} = services
.state_compressor
.save_state(room_id, Arc::new(compressed))
.await?;
debug!("Forcing state for new room");
services
.state
.force_state(room_id, statehash_before_knock, added, removed, &state_lock)
.await?;
let statehash_after_knock = services
.state
.append_to_state(&parsed_knock_pdu)
.await?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = services.globals.next_count();
services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
&state_lock,
)
.await?;
info!("Setting final room state for new room");
// We set the room state after inserting the pdu, so that we never have a moment
// in time where events in the current room state do not exist
services
.state
.set_room_state(room_id, statehash_after_knock, &state_lock);
Ok(())
}
async fn make_knock_request(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
servers: &[OwnedServerName],
) -> Result<(federation::membership::prepare_knock_event::v1::Response, OwnedServerName)> {
let mut make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
let mut make_knock_counter: usize = 0;
for remote_server in servers {
if services.globals.server_is_ours(remote_server) {
continue;
}
info!("Asking {remote_server} for make_knock ({make_knock_counter})");
let make_knock_response = services
.federation
.execute(remote_server, federation::membership::prepare_knock_event::v1::Request {
room_id: room_id.to_owned(),
user_id: sender_user.to_owned(),
ver: services
.server
.supported_room_versions()
.collect(),
})
.await;
trace!("make_knock response: {make_knock_response:?}");
make_knock_counter = make_knock_counter.saturating_add(1);
make_knock_response_and_server = make_knock_response.map(|r| (r, remote_server.clone()));
if make_knock_response_and_server.is_ok() {
break;
}
if make_knock_counter > 40 {
warn!(
"50 servers failed to provide valid make_knock response, assuming no server can \
assist in knocking."
);
make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
return make_knock_response_and_server;
}
}
make_knock_response_and_server
Ok(knock_room::v3::Response::new(room_id.clone()))
}
+38 -153
View File
@@ -8,15 +8,12 @@ mod leave;
mod members;
mod unban;
use std::{cmp::Ordering, net::IpAddr};
use std::net::IpAddr;
use axum::extract::State;
use futures::{FutureExt, StreamExt};
use ruma::{
OwnedRoomId, OwnedServerName, RoomId, RoomOrAliasId, ServerName, UserId,
api::client::membership::joined_rooms,
};
use tuwunel_core::{Err, Result, result::LogErr, utils::shuffle, warn};
use ruma::{RoomId, RoomOrAliasId, UserId, api::client::membership::joined_rooms};
use tuwunel_core::{Err, Result, result::LogErr, warn};
use tuwunel_service::Services;
pub(crate) use self::{
@@ -58,57 +55,42 @@ pub(crate) async fn joined_rooms_route(
pub(crate) async fn banned_room_check(
services: &Services,
user_id: &UserId,
room_id: Option<&RoomId>,
server_name: Option<&ServerName>,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
client_ip: IpAddr,
) -> Result {
if services.users.is_admin(user_id).await {
return Ok(());
}
// TODO: weird condition
if let Some(room_id) = room_id {
if services.metadata.is_banned(room_id).await
|| (room_id.server_name().is_some()
&& services
.config
.forbidden_remote_server_names
.is_match(
room_id
.server_name()
.expect("legacy room mxid")
.host(),
)) {
warn!(
"User {user_id} who is not an admin attempted to send an invite for or \
attempted to join a banned room or banned room server name: {room_id}"
);
// room id is banned ...
if services.metadata.is_banned(room_id).await
// ... or legacy room id server is banned ...
|| room_id.server_name().is_some_and(|server_name| {
services
.config
.forbidden_remote_server_names
.is_match(server_name.host())
})
// ... or alias server is banned
|| orig_room_id.is_some_and(|orig_room_id| {
orig_room_id.server_name().is_some_and(|orig_server_name|
services
.config
.forbidden_remote_server_names
.is_match(orig_server_name.host()))
}) {
warn!(
"User {user_id} who is not an admin attempted to send an invite for or attempted to \
join a banned room or banned room server name: {room_id}"
);
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
} else if let Some(server_name) = server_name {
if services
.config
.forbidden_remote_server_names
.is_match(server_name.host())
{
warn!(
"User {user_id} who is not an admin tried joining a room which has the server \
name {server_name} that is globally forbidden. Rejecting.",
);
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
return Err!(Request(Forbidden("This remote server is banned on this homeserver.")));
}
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
Ok(())
@@ -120,16 +102,15 @@ async fn maybe_deactivate(services: &Services, user_id: &UserId, client_ip: IpAd
.config
.auto_deactivate_banned_room_attempts
{
warn!("Automatically deactivating user {user_id} due to attempted banned room join");
let notice = format!(
"Automatically deactivating user {user_id} due to attempted banned room join from \
IP {client_ip}"
);
warn!("{notice}");
if services.server.config.admin_room_notices {
services
.admin
.send_text(&format!(
"Automatically deactivating user {user_id} due to attempted banned room \
join from IP {client_ip}"
))
.await;
services.admin.send_text(&notice).await;
}
services
@@ -141,99 +122,3 @@ async fn maybe_deactivate(services: &Services, user_id: &UserId, client_ip: IpAd
Ok(())
}
// TODO: should this be in services? banned check would have to resolve again if
// room_id is not available at callsite
async fn get_join_params(
services: &Services,
user_id: &UserId,
room_id_or_alias: &RoomOrAliasId,
via: &[OwnedServerName],
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
// servers tried first, additional_servers shuffled then tried after
let (room_id, mut primary_servers, mut additional_servers) =
match OwnedRoomId::try_from(room_id_or_alias.to_owned()) {
// if room id, shuffle via + room_id server_name ...
| Ok(room_id) => {
let mut additional_servers = via.to_vec();
if let Some(server) = room_id.server_name() {
additional_servers.push(server.to_owned());
}
(room_id, Vec::new(), additional_servers)
},
// ... if room alias, resolve and don't shuffle ...
| Err(room_alias) => {
let (room_id, servers) = services.alias.resolve_alias(&room_alias).await?;
(room_id, servers, Vec::new())
},
};
// either way, add invited vias
additional_servers.extend(
services
.state_cache
.servers_invite_via(&room_id)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
.await,
);
// either way, add invite senders' servers
additional_servers.extend(
services
.state_cache
.invite_state(user_id, &room_id)
.await
.unwrap_or_default()
.iter()
.filter_map(|event| event.get_field("sender").ok().flatten())
.filter_map(|sender: &str| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()),
);
primary_servers.sort_unstable();
primary_servers.dedup();
shuffle(&mut primary_servers);
// shuffle additionals, append to base servers
additional_servers.sort_unstable();
additional_servers.dedup();
shuffle(&mut additional_servers);
let mut servers: Vec<_> = room_id_or_alias
.server_name()
.filter(|_| room_id_or_alias.is_room_alias_id())
.map(ToOwned::to_owned)
.into_iter()
.chain(primary_servers.into_iter())
.chain(additional_servers.into_iter())
.collect();
// sort deprioritized servers last
servers.sort_by(|a, b| {
let a_matches = services
.server
.config
.deprioritize_joins_through_servers
.is_match(a.host());
let b_matches = services
.server
.config
.deprioritize_joins_through_servers
.is_match(b.host());
if a_matches && !b_matches {
Ordering::Greater
} else if !a_matches && b_matches {
Ordering::Less
} else {
Ordering::Equal
}
});
Ok((room_id, servers))
}
-3
View File
@@ -88,9 +88,6 @@ pub(super) use user_directory::*;
pub(super) use voip::*;
pub(super) use well_known::*;
/// generated device ID length
const DEVICE_ID_LENGTH: usize = 10;
/// generated user access token length
const TOKEN_LENGTH: usize = tuwunel_service::users::device::TOKEN_LENGTH;
+1 -1
View File
@@ -589,7 +589,7 @@ pub(crate) async fn set_pushers_route(
services
.pusher
.set_pusher(sender_user, body.sender_device(), &body.action)
.set_pusher(sender_user, body.sender_device()?, &body.action)
.await?;
Ok(set_pusher::v3::Response::new())
+52 -240
View File
@@ -2,7 +2,6 @@ use std::fmt::Write;
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use register::RegistrationKind;
use ruma::{
UserId,
@@ -13,15 +12,11 @@ use ruma::{
},
uiaa::{AuthFlow, AuthType, UiaaInfo},
},
events::GlobalAccountDataEventType,
push,
};
use tuwunel_core::{
Err, Error, Result, debug_info, debug_warn, error, info, is_equal_to, utils, warn,
};
use tuwunel_core::{Err, Error, Result, debug_info, debug_warn, info, utils};
use tuwunel_service::users::device::generate_refresh_token;
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH};
use super::SESSION_ID_LENGTH;
use crate::Ruma;
const RANDOM_USER_ID_LENGTH: usize = 10;
@@ -48,15 +43,10 @@ pub(crate) async fn get_register_available_route(
.appservice_info
.as_ref()
.is_some_and(|appservice| {
appservice.registration.id == "irc"
|| appservice
.registration
.id
.contains("matrix-appservice-irc")
|| appservice
.registration
.id
.contains("matrix_appservice_irc")
let id = &appservice.registration.id;
id == "irc"
|| id.contains("matrix-appservice-irc")
|| id.contains("matrix_appservice_irc")
});
if services
@@ -148,71 +138,32 @@ pub(crate) async fn register_route(
let is_guest = body.kind == RegistrationKind::Guest;
let emergency_mode_enabled = services.config.emergency_password.is_some();
let user = body.username.as_deref().unwrap_or("");
let device_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
if !services.config.allow_registration && body.appservice_info.is_none() {
match (body.username.as_ref(), body.initial_device_display_name.as_ref()) {
| (Some(username), Some(device_display_name)) => {
info!(
%is_guest,
user = %username,
device_name = %device_display_name,
"Rejecting registration attempt as registration is disabled"
);
},
| (Some(username), _) => {
info!(
%is_guest,
user = %username,
"Rejecting registration attempt as registration is disabled"
);
},
| (_, Some(device_display_name)) => {
info!(
%is_guest,
device_name = %device_display_name,
"Rejecting registration attempt as registration is disabled"
);
},
| (None, _) => {
info!(
%is_guest,
"Rejecting registration attempt as registration is disabled"
);
},
}
info!(
%is_guest,
%user,
%device_name,
"Rejecting registration attempt as registration is disabled"
);
return Err!(Request(Forbidden("Registration has been disabled.")));
}
if is_guest && !services.config.allow_guest_registration {
let display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
debug_warn!(
"Guest registration disabled / registration enabled with token configured, \
rejecting guest registration attempt, initial device name: \"{display_name}\""
%device_name,
"Guest registration disabled, rejecting guest registration attempt"
);
return Err!(Request(GuestAccessForbidden("Guest registration is disabled.")));
}
// forbid guests from registering if there is not a real admin user yet. give
// generic user error.
if is_guest && services.users.count().await < 2 {
let display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
warn!(
"Guest account attempted to register before a real admin user has been registered, \
rejecting registration. Guest's initial device name: \"{display_name}\""
);
return Err!(Request(Forbidden("Registration is temporarily disabled.")));
}
let user_id = match (body.username.as_ref(), is_guest) {
| (Some(username), false) => {
// workaround for https://github.com/matrix-org/matrix-appservice-irc/issues/1780 due to inactivity of fixing the issue
@@ -313,7 +264,13 @@ pub(crate) async fn register_route(
// UIAA
let mut uiaainfo;
let skip_auth = if services.globals.registration_token.is_some() && !is_guest {
let skip_auth = if !services
.globals
.get_registration_tokens()
.await
.is_empty()
&& !is_guest
{
// Registration token required
uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
@@ -378,45 +335,9 @@ pub(crate) async fn register_route(
let password = if is_guest { None } else { body.password.as_deref() };
// Create user
services
.users
.create(&user_id, password, None)
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !services
.config
.new_user_displayname_suffix
.is_empty()
&& body.appservice_info.is_none()
{
write!(displayname, " {}", services.server.config.new_user_displayname_suffix)?;
}
services
.users
.set_displayname(&user_id, Some(displayname.clone()));
// Initial account data
services
.account_data
.update(
None,
&user_id,
GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: push::Ruleset::server_default(&user_id),
},
})?,
)
.full_register(&user_id, password, None, body.appservice_info.as_ref(), is_guest, true)
.await?;
if (!is_guest && body.inhibit_login)
@@ -426,169 +347,56 @@ pub(crate) async fn register_route(
.is_some_and(|appservice| appservice.registration.device_management)
{
return Ok(register::v3::Response {
access_token: None,
user_id,
device_id: None,
access_token: None,
refresh_token: None,
expires_in: None,
});
}
// Generate new device id if the user didn't specify one
let device_id = if is_guest { None } else { body.device_id.clone() }
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
let device_id = if is_guest { None } else { body.device_id.as_deref() };
// Generate new token for the device
let (access_token, expires_in) = services
.users
.generate_access_token(body.body.refresh_token);
.generate_access_token(body.refresh_token);
// Generate a new refresh_token if requested by client
let refresh_token = expires_in.is_some().then(generate_refresh_token);
// Create device for this account
services
let device_id = services
.users
.create_device(
&user_id,
&device_id,
device_id,
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.clone(),
body.initial_device_display_name.as_deref(),
Some(client.to_string()),
)
.await?;
debug_info!(%user_id, %device_id, "User account was created");
let device_display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
if body.appservice_info.is_none() && (!is_guest || services.config.log_guest_registrations) {
let mut notice = String::from(if is_guest { "New guest user" } else { "New user" });
// log in conduit admin channel if a non-guest user registered
if body.appservice_info.is_none() && !is_guest {
if !device_display_name.is_empty() {
let notice = format!(
"New user \"{user_id}\" registered on this server from IP {client} and device \
display name \"{device_display_name}\""
);
write!(notice, " registered on this server from IP {client}")?;
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
} else {
let notice = format!("New user \"{user_id}\" registered on this server.");
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
if let Some(device_name) = body.initial_device_display_name.as_deref() {
write!(notice, " with device name {device_name}")?;
}
}
// log in conduit admin channel if a guest registered
if body.appservice_info.is_none() && is_guest && services.config.log_guest_registrations {
debug_info!("New guest user \"{user_id}\" registered on this server.");
if !device_display_name.is_empty() {
if services.server.config.admin_room_notices {
services
.admin
.notice(&format!(
"Guest user \"{user_id}\" with device display name \
\"{device_display_name}\" registered on this server from IP {client}"
))
.await;
}
if !is_guest {
info!("{notice}");
} else {
#[allow(clippy::collapsible_else_if)]
if services.server.config.admin_room_notices {
services
.admin
.notice(&format!(
"Guest user \"{user_id}\" with no device display name registered on \
this server from IP {client}",
))
.await;
}
debug_info!("{notice}");
}
}
// If this is the first real user, grant them admin privileges except for guest
// users
// Note: the server user is generated first
if !is_guest
&& services.config.grant_admin_to_first_user
&& let Ok(admin_room) = services.admin.get_admin_room().await
&& services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
services
.admin
.make_user_admin(&user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
if body.appservice_info.is_none()
&& !services.server.config.auto_join_rooms.is_empty()
&& (services.config.allow_guests_auto_join_rooms || !is_guest)
{
for room in &services.server.config.auto_join_rooms {
let Ok(room_id) = services.alias.maybe_resolve(room).await else {
error!(
"Failed to resolve room alias to room ID when attempting to auto join \
{room}, skipping"
);
continue;
};
if !services
.state_cache
.server_in_room(services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
if let Some(room_server_name) = room.server_name() {
let state_lock = services.state.mutex.lock(&room_id).await;
match services
.membership
.join(
&user_id,
&room_id,
Some("Automatically joining this room upon registration".to_owned()),
&[services.globals.server_name().to_owned(), room_server_name.to_owned()],
&body.appservice_info,
&state_lock,
)
.boxed()
.await
{
| Err(e) => {
// don't return this error so we don't fail registrations
error!(
"Failed to automatically join room {room} for user {user_id}: {e}"
);
},
| _ => {
info!("Automatically joined room {room} for user {user_id}");
},
}
drop(state_lock);
}
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
}
@@ -611,9 +419,13 @@ pub(crate) async fn check_registration_token_validity(
State(services): State<crate::State>,
body: Ruma<check_registration_token_validity::v1::Request>,
) -> Result<check_registration_token_validity::v1::Response> {
let Some(reg_token) = services.globals.registration_token.clone() else {
return Err!(Request(Forbidden("Server does not allow token registration")));
};
let tokens = services.globals.get_registration_tokens().await;
Ok(check_registration_token_validity::v1::Response { valid: reg_token == body.token })
if tokens.is_empty() {
return Err!(Request(Forbidden("Server does not allow token registration")));
}
let valid = tokens.contains(&body.token);
Ok(check_registration_token_validity::v1::Response { valid })
}
+1 -2
View File
@@ -113,8 +113,7 @@ pub(crate) async fn get_relating_events_route(
name = "relations",
level = "debug",
skip_all,
fields(room_id, target, from, to, dir, limit, recurse),
ret(level = "trace")
fields(room_id, target, from, to, dir, limit, recurse)
)]
async fn paginate_relations_with_filter(
services: &Services,
+1 -1
View File
@@ -55,7 +55,7 @@ pub(crate) async fn get_room_summary(
) -> Result<get_summary::v1::Response> {
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(body.via.clone()))
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
if services.metadata.is_banned(&room_id).await {
+1 -1
View File
@@ -51,7 +51,7 @@ pub(super) async fn ldap_login(
if !services.users.exists(lowercased_user_id).await {
services
.users
.create(lowercased_user_id, Some("*"), Some("ldap"))
.full_register(lowercased_user_id, Some("*"), Some("ldap"), None, false, false)
.await?;
}
+1 -1
View File
@@ -23,7 +23,7 @@ pub(crate) async fn logout_route(
) -> Result<logout::v3::Response> {
services
.users
.remove_device(body.sender_user(), body.sender_device())
.remove_device(body.sender_user(), body.sender_device()?)
.await;
Ok(logout::v3::Response::new())
+24 -28
View File
@@ -21,7 +21,7 @@ use ruma::api::client::session::{
v3::{DiscoveryInfo, HomeserverInfo, LoginInfo},
},
};
use tuwunel_core::{Err, Result, info, utils, utils::stream::ReadyExt};
use tuwunel_core::{Err, Result, info, utils::stream::ReadyExt};
use tuwunel_service::users::device::generate_refresh_token;
use self::{ldap::ldap_login, password::password_login};
@@ -30,7 +30,7 @@ pub(crate) use self::{
refresh::refresh_token_route,
token::login_token_route,
};
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
use super::TOKEN_LENGTH;
use crate::Ruma;
/// # `GET /_matrix/client/v3/login`
@@ -97,43 +97,39 @@ pub(crate) async fn login_route(
// Generate a new refresh_token if requested by client
let refresh_token = expires_in.is_some().then(generate_refresh_token);
// Generate new device id if the user didn't specify one
let device_id = body
.device_id
.clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
// Determine if device_id was provided and exists in the db for this user
let device_exists = services
.users
.all_device_ids(&user_id)
.ready_any(|v| v == device_id)
.await;
if !device_exists {
services
let device_id = if let Some(device_id) = &body.device_id
&& services
.users
.create_device(
&user_id,
&device_id,
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.clone(),
Some(client.to_string()),
)
.await?;
} else {
.all_device_ids(&user_id)
.ready_any(|v| v == device_id)
.await
{
services
.users
.set_access_token(
&user_id,
&device_id,
device_id,
&access_token,
expires_in,
refresh_token.as_deref(),
)
.await?;
}
device_id.clone()
} else {
services
.users
.create_device(
&user_id,
body.device_id.as_deref(),
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.as_deref(),
Some(client.to_string()),
)
.await?
};
info!("{user_id} logged in");
+2 -1
View File
@@ -50,7 +50,8 @@ pub(crate) async fn login_token_route(
// This route SHOULD have UIA
// TODO: How do we make only UIA sessions that have not been used before valid?
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device()?;
let password_flow = uiaa::AuthFlow { stages: vec![uiaa::AuthType::Password] };
+55 -30
View File
@@ -120,13 +120,15 @@ type PresenceUpdates = HashMap<OwnedUserId, PresenceEventContent>;
skip_all,
fields(
user_id = %body.sender_user(),
device_id = %body.sender_device.as_deref().map_or("<no device>", |x| x.as_str()),
)
)]
pub(crate) async fn sync_events_route(
State(services): State<crate::State>,
body: Ruma<sync_events::v3::Request>,
) -> Result<sync_events::v3::Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let filter: OptionFuture<_> = body
.body
@@ -195,7 +197,8 @@ pub(crate) async fn sync_events_route(
if since < next_batch || full_state {
let response = build_sync_events(
&services,
body.sender(),
sender_user,
sender_device,
since,
next_batch,
full_state,
@@ -216,7 +219,8 @@ pub(crate) async fn sync_events_route(
// Wait for activity
if time::timeout_at(stop_at, watchers).await.is_err() || services.server.is_stopping() {
let response = build_empty_response(&services, body.sender(), next_batch).await;
let response =
build_empty_response(&services, sender_user, sender_device, next_batch).await;
trace!(since, next_batch, "empty response");
return Ok(response);
}
@@ -235,14 +239,22 @@ pub(crate) async fn sync_events_route(
async fn build_empty_response(
services: &Services,
(sender_user, sender_device): (&UserId, &DeviceId),
sender_user: &UserId,
sender_device: Option<&DeviceId>,
next_batch: u64,
) -> sync_events::v3::Response {
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
sync_events::v3::Response {
device_one_time_keys_count: services
.users
.count_one_time_keys(sender_user, sender_device)
.await,
device_one_time_keys_count: device_one_time_keys_count
.await
.unwrap_or_default(),
..sync_events::v3::Response::new(to_small_string(next_batch))
}
@@ -251,7 +263,6 @@ async fn build_empty_response(
#[tracing::instrument(
name = "build",
level = INFO_SPAN_LEVEL,
ret(level = "trace"),
skip_all,
fields(
%since,
@@ -261,7 +272,8 @@ async fn build_empty_response(
)]
async fn build_sync_events(
services: &Services,
(sender_user, sender_device): (&UserId, &DeviceId),
sender_user: &UserId,
sender_device: Option<&DeviceId>,
since: u64,
next_batch: u64,
full_state: bool,
@@ -387,27 +399,38 @@ async fn build_sync_events(
.map(ToOwned::to_owned)
.collect::<HashSet<_>>();
let to_device_events = services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>();
let to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>()
})
.into();
let device_one_time_keys_count = services
.users
.count_one_time_keys(sender_user, sender_device);
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
// Remove all to-device events the device received *last time*
let remove_to_device_events =
services
.users
.remove_to_device_events(sender_user, sender_device, since);
let remove_to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.remove_to_device_events(sender_user, sender_device, since)
})
.into();
let (
account_data,
keys_changed,
device_one_time_keys_count,
((), to_device_events, presence_updates),
presence_updates,
(_, to_device_events, device_one_time_keys_count),
(
(joined_rooms, mut device_list_updates, left_encrypted_users),
left_rooms,
@@ -417,8 +440,8 @@ async fn build_sync_events(
) = join5(
account_data,
keys_changed,
device_one_time_keys_count,
join3(remove_to_device_events, to_device_events, presence_updates),
presence_updates,
join3(remove_to_device_events, to_device_events, device_one_time_keys_count),
join4(joined_rooms, left_rooms, invited_rooms, knocked_rooms),
)
.boxed()
@@ -454,7 +477,7 @@ async fn build_sync_events(
left: device_list_left,
changed: device_list_updates.into_iter().collect(),
},
device_one_time_keys_count,
device_one_time_keys_count: device_one_time_keys_count.unwrap_or_default(),
// Fallback keys are not yet supported
device_unused_fallback_key_types: None,
next_batch: to_small_string(next_batch),
@@ -465,7 +488,9 @@ async fn build_sync_events(
invite: invited_rooms,
knock: knocked_rooms,
},
to_device: ToDevice { events: to_device_events },
to_device: ToDevice {
events: to_device_events.unwrap_or_default(),
},
})
}
@@ -732,7 +757,7 @@ async fn load_left_room(
async fn load_joined_room(
services: &Services,
sender_user: &UserId,
sender_device: &DeviceId,
sender_device: Option<&DeviceId>,
ref room_id: OwnedRoomId,
since: u64,
next_batch: u64,
@@ -841,7 +866,7 @@ async fn load_joined_room(
let lazy_loading_context = &lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
device_id: sender_device,
room_id,
token: Some(since),
options: Some(&filter.room.state.lazy_load_options),
+5 -4
View File
@@ -37,7 +37,7 @@ use crate::Ruma;
struct SyncInfo<'a> {
services: &'a Services,
sender_user: &'a UserId,
sender_device: &'a DeviceId,
sender_device: Option<&'a DeviceId>,
}
#[derive(Clone, Debug)]
@@ -69,7 +69,7 @@ type ListIds = SmallVec<[ListId; 1]>;
skip_all,
fields(
user_id = %body.sender_user().localpart(),
device_id = %body.sender_device(),
device_id = %body.sender_device.as_deref().map_or("<no device>", |x| x.as_str()),
conn_id = ?body.body.conn_id.clone().unwrap_or_default(),
since = ?body.body.pos.clone().unwrap_or_default(),
)
@@ -78,7 +78,8 @@ pub(crate) async fn sync_events_v5_route(
State(ref services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let request = &body.body;
let since = request
.pos
@@ -108,7 +109,7 @@ pub(crate) async fn sync_events_v5_route(
let conn = conn_val.lock();
let ping_presence = services
.presence
.maybe_ping_presence(sender_user, Some(sender_device), &request.set_presence)
.maybe_ping_presence(sender_user, sender_device, &request.set_presence)
.inspect_err(inspect_log)
.ok();
@@ -32,6 +32,9 @@ pub(super) async fn collect(
conn: &Connection,
) -> Result<response::E2EE> {
let SyncInfo { services, sender_user, sender_device, .. } = sync_info;
let Some(sender_device) = sender_device else {
return Ok(response::E2EE::default());
};
let keys_changed = services
.users
@@ -9,6 +9,10 @@ pub(super) async fn collect(
SyncInfo { services, sender_user, sender_device, .. }: SyncInfo<'_>,
conn: &Connection,
) -> Result<Option<response::ToDevice>> {
let Some(sender_device) = sender_device else {
return Ok(None);
};
services
.users
.remove_to_device_events(sender_user, sender_device, conn.globalsince)
+1 -1
View File
@@ -27,7 +27,7 @@ pub(crate) async fn turn_server_route(
let turn_secret = &services.globals.turn_secret;
let (username, password) = if !turn_secret.is_empty() {
let (username, password) = if let Some(turn_secret) = turn_secret {
let expiry = SecondsSinceUnixEpoch::from_system_time(
SystemTime::now()
.checked_add(Duration::from_secs(services.config.turn_ttl))
+3 -1
View File
@@ -195,6 +195,9 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::room_initial_sync_route)
.route("/client/server.json", get(client::syncv3_client_server_json));
// SS endpoint not related to federation
router = router.ruma_route(&server::get_openid_userinfo_route);
if config.allow_federation {
router = router
.ruma_route(&server::get_server_version_route)
@@ -226,7 +229,6 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&server::get_profile_information_route)
.ruma_route(&server::get_keys_route)
.ruma_route(&server::claim_keys_route)
.ruma_route(&server::get_openid_userinfo_route)
.ruma_route(&server::get_hierarchy_route)
.ruma_route(&server::well_known_server)
.ruma_route(&server::get_content_route)
+7 -12
View File
@@ -40,11 +40,6 @@ pub(crate) struct Args<T> {
}
impl<T> Args<T> {
#[inline]
pub(crate) fn sender(&self) -> (&UserId, &DeviceId) {
(self.sender_user(), self.sender_device())
}
#[inline]
pub(crate) fn sender_user(&self) -> &UserId {
self.sender_user
@@ -52,19 +47,19 @@ impl<T> Args<T> {
.expect("user must be authenticated for this handler")
}
#[inline]
pub(crate) fn sender_device(&self) -> &DeviceId {
self.sender_device
.as_deref()
.expect("user must be authenticated and device identified")
}
#[inline]
pub(crate) fn origin(&self) -> &ServerName {
self.origin
.as_deref()
.expect("server must be authenticated for this handler")
}
#[inline]
pub(crate) fn sender_device(&self) -> Result<&DeviceId> {
self.sender_device
.as_deref()
.ok_or(err!(Request(Forbidden("user must be authenticated and device identified"))))
}
}
impl<T> Deref for Args<T>
+4 -2
View File
@@ -14,6 +14,8 @@ pub(crate) async fn auth_uiaa<T>(services: &Services, body: &Ruma<T>) -> Result<
where
T: IncomingRequest + Send + Sync,
{
let sender_device = body.sender_device()?;
let flows = [
AuthFlow::new([AuthType::Password].into()),
AuthFlow::new([AuthType::Jwt].into()),
@@ -51,7 +53,7 @@ where
let (worked, uiaainfo) = services
.uiaa
.try_auth(sender_user, body.sender_device(), auth, &uiaainfo)
.try_auth(sender_user, sender_device, auth, &uiaainfo)
.await?;
if !worked {
@@ -71,7 +73,7 @@ where
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services
.uiaa
.create(sender_user, body.sender_device(), &uiaainfo, json);
.create(sender_user, sender_device, &uiaainfo, json);
Err(Error::Uiaa(uiaainfo))
},
+8 -3
View File
@@ -9,7 +9,7 @@ use axum::{
use futures::{Future, TryFutureExt};
use http::Method;
use ruma::api::IncomingRequest;
use tuwunel_core::Result;
use tuwunel_core::{Result, trace};
use super::{Ruma, RumaResponse, State};
@@ -42,7 +42,7 @@ macro_rules! ruma_handler {
Fut: Future<Output = Result<Req::OutgoingResponse, Err>> + Send,
Req: IncomingRequest + Debug + Send + Sync + 'static,
Err: IntoResponse + Debug + Send,
<Req as IncomingRequest>::OutgoingResponse: Send,
<Req as IncomingRequest>::OutgoingResponse: Debug + Send,
$( $tx: FromRequestParts<State> + Send + Sync + 'static, )*
{
fn add_routes(&'static self, router: Router<State>) -> Router<State> {
@@ -53,8 +53,13 @@ macro_rules! ruma_handler {
}
fn add_route(&'static self, router: Router<State>, path: &str) -> Router<State> {
let action = |$($tx,)* req| self($($tx,)* req).map_ok(RumaResponse);
let method = method_to_filter(&Req::METADATA.method);
let action = |$($tx,)* req| {
self($($tx,)* req)
.inspect_ok(|result| trace!(?result))
.map_ok(RumaResponse)
};
router.route(path, on(method, action))
}
}
+51 -2
View File
@@ -3,13 +3,13 @@ use axum_client_ip::InsecureClientIp;
use base64::{Engine as _, engine::general_purpose};
use futures::StreamExt;
use ruma::{
CanonicalJsonValue, OwnedUserId, UserId,
CanonicalJsonValue, OwnedRoomId, OwnedUserId, RoomId, UserId,
api::{
client::error::ErrorKind,
federation::membership::{RawStrippedState, create_invite},
},
events::{
GlobalAccountDataEventType,
GlobalAccountDataEventType, StateEventType,
push_rules::PushRulesEvent,
room::member::{MembershipState, RoomMemberEventContent},
},
@@ -78,6 +78,26 @@ pub(crate) async fn create_invite_route(
let mut signed_event = utils::to_canonical_object(&body.event)
.map_err(|_| err!(Request(InvalidParam("Invite event is invalid."))))?;
let room_id: OwnedRoomId = signed_event
.get("room_id")
.try_into()
.map(RoomId::to_owned)
.map_err(|e| err!(Request(InvalidParam("Invalid room_id property: {e}"))))?;
if body.room_id != room_id {
return Err!(Request(InvalidParam("Event room_id does not match the request path.")));
}
let kind: StateEventType = signed_event
.get("type")
.and_then(CanonicalJsonValue::as_str)
.ok_or_else(|| err!(Request(BadJson("Missing type in event."))))?
.into();
if kind != StateEventType::RoomMember {
return Err!(Request(InvalidParam("Event must be m.room.member type.")));
}
let invited_user: OwnedUserId = signed_event
.get("state_key")
.try_into()
@@ -91,6 +111,19 @@ pub(crate) async fn create_invite_route(
return Err!(Request(InvalidParam("User does not belong to this homeserver.")));
}
let content: RoomMemberEventContent = signed_event
.get("content")
.cloned()
.map(Into::into)
.map(serde_json::from_value)
.transpose()
.map_err(|e| err!(Request(InvalidParam("Invalid content object in event: {e}"))))?
.ok_or_else(|| err!(Request(BadJson("Missing content in event."))))?;
if content.membership != MembershipState::Invite {
return Err!(Request(InvalidParam("Event membership must be invite.")));
}
// Make sure we're not ACL'ed from their room.
services
.event_handler
@@ -108,11 +141,27 @@ pub(crate) async fn create_invite_route(
// Add event_id back
signed_event.insert("event_id".into(), CanonicalJsonValue::String(event_id.to_string()));
let origin: Option<&str> = signed_event
.get("origin")
.and_then(CanonicalJsonValue::as_str);
let sender: &UserId = signed_event
.get("sender")
.try_into()
.map_err(|e| err!(Request(InvalidParam("Invalid sender property: {e}"))))?;
if sender.server_name() != body.origin() {
return Err!(Request(Forbidden("Can only send invites on behalf of your users.")));
}
if origin.is_some_and(|origin| origin != sender.server_name()) {
return Err!(Request(Forbidden("Your users can only be from your origin.")));
}
if origin.is_some_and(|origin| origin != body.origin()) {
return Err!(Request(Forbidden("Can only send events from your origin.")));
}
if services.metadata.is_banned(&body.room_id).await
&& !services.users.is_admin(&invited_user).await
{
+1 -6
View File
@@ -1006,7 +1006,7 @@ pub struct Config {
///
/// display: sensitive
#[serde(default)]
pub turn_secret: String,
pub turn_secret: Option<String>,
/// TURN secret to use that's read from the file path specified.
///
@@ -2111,11 +2111,6 @@ pub struct Config {
///
/// This option exists for developer and debug use, and as a failsafe in
/// lieu of hardcoding it.
///
/// This currently defaults to false as a matter of development until
/// real-world testing can shake out any implementation issues rather than
/// jeopardize existing rooms, but otherwise will default to true at the
/// next point release or patch.
#[serde(default = "true_fn")]
pub hydra_backports: bool,
+3
View File
@@ -6,6 +6,7 @@ use ruma::{
use crate::{
Result, extract_variant, is_equal_to,
matrix::{PduEvent, room_version},
state_res::{self},
};
pub fn into_outgoing_federation(
@@ -95,6 +96,8 @@ pub fn from_incoming_federation(
pdu_json.insert("event_id".into(), CanonicalJsonValue::String(event_id.into()));
}
state_res::check_pdu_format(pdu_json, &room_rules.event_format)?;
PduEvent::from_val(pdu_json)
}
+3 -5
View File
@@ -37,9 +37,7 @@ pub fn check_pdu_format(pdu: &CanonicalJsonObject, rules: &EventFormatRules) ->
.map_err(|e| err!(Request(BadJson("Failed to serialize canonical JSON: {e}"))))?;
if json.len() > MAX_PDU_BYTES {
return Err!(Request(InvalidParam(
"PDU is larger than maximum of {MAX_PDU_BYTES} bytes"
)));
return Err!(Request(TooLarge("PDU is larger than maximum of {MAX_PDU_BYTES} bytes")));
}
// Check the presence, type and length of the `type` field.
@@ -133,7 +131,7 @@ fn extract_optional_string_field<'a>(
match object.get(field) {
| Some(CanonicalJsonValue::String(value)) =>
if value.len() > ID_MAX_BYTES {
Err!(Request(InvalidParam(
Err!(Request(TooLarge(
"invalid `{field}` field in PDU: string length is larger than maximum of \
{ID_MAX_BYTES} bytes"
)))
@@ -177,7 +175,7 @@ fn extract_required_array_field<'a>(
match object.get(field) {
| Some(CanonicalJsonValue::Array(value)) =>
if value.len() > max_len {
Err!(Request(InvalidParam(
Err!(Request(TooLarge(
"invalid `{field}` field in PDU: array length is larger than maximum of \
{max_len}"
)))
+6 -2
View File
@@ -68,9 +68,13 @@ pub(crate) fn build(services: &Arc<Services>) -> Result<(Router, Guard)> {
.layer(RequestBodyTimeoutLayer::new(Duration::from_secs(
server.config.client_receive_timeout,
)))
.layer(TimeoutLayer::new(Duration::from_secs(server.config.client_request_timeout)))
.layer(TimeoutLayer::with_status_code(
StatusCode::REQUEST_TIMEOUT,
Duration::from_secs(server.config.client_request_timeout),
))
.layer(SetResponseHeaderLayer::if_not_present(
HeaderName::from_static("origin-agent-cluster"), // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin-Agent-Cluster
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin-Agent-Cluster
HeaderName::from_static("origin-agent-cluster"),
HeaderValue::from_static("?1"),
))
.layer(SetResponseHeaderLayer::if_not_present(
+2
View File
@@ -32,6 +32,8 @@ pub(super) async fn serve(
if cfg!(unix) && config.unix_socket_path.is_some() {
unix::serve(server, app, shutdown).await
} else if config.tls.certs.is_some() {
#[cfg(feature = "direct_tls")]
services.globals.init_rustls_provider()?;
#[cfg(feature = "direct_tls")]
return tls::serve(server, app, handle, addrs).await;
-6
View File
@@ -27,12 +27,6 @@ pub(super) async fn serve(
.as_ref()
.ok_or_else(|| err!(Config("tls.key", "Missing required value in tls config section")))?;
// we use ring for ruma and hashing state, but aws-lc-rs is the new default.
// without this, TLS mode will panic.
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.expect("failed to initialise aws-lc-rs rustls crypto provider");
info!(
"Note: It is strongly recommended that you use a reverse proxy instead of running \
tuwunel directly with TLS."
+1
View File
@@ -107,6 +107,7 @@ rand.workspace = true
regex.workspace = true
reqwest.workspace = true
ruma.workspace = true
rustls.workspace = true
rustyline-async.workspace = true
rustyline-async.optional = true
serde_json.workspace = true
+1 -1
View File
@@ -175,7 +175,7 @@ pub async fn create_admin_room(services: &Services) -> Result {
.build_and_append_pdu(
PduBuilder::state(String::new(), &RoomTopicEventContent {
topic_block: TopicContentBlock::default(),
topic: format!("Manage {} | Run commands prefixed with `!admin` | Run `!admin -h` for help | Documentation: https://github.com/matrix-construct/tuwunel/", services.config.server_name),
topic: format!("Manage {} | Run commands prefixed with `!admin` | Run `!admin -h` for help | Documentation: https://matrix-construct.github.io/tuwunel", services.config.server_name),
}),
server_user,
&room_id,
+1 -1
View File
@@ -145,7 +145,7 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result {
if self.services.server.config.admin_room_notices {
let welcome_message = String::from(
"## Thank you for trying out tuwunel!\n\nTuwunel is a continuation of conduwuit which was technically a hard fork of Conduit.\n\nHelpful links:\n> GitHub Repo: https://github.com/matrix-construct/tuwunel\n> Documentation: https://github.com/matrix-construct/tuwunel\n> Report issues: https://github.com/matrix-construct/tuwunel/issues\n\nFor a list of available commands, send the following message in this room: `!admin --help`"
"## Thank you for trying out tuwunel!\n\nTuwunel is a continuation of conduwuit which was technically a hard fork of Conduit.\n\nHelpful links:\n> GitHub Repo: https://github.com/matrix-construct/tuwunel\n> Documentation: https://matrix-construct.github.io/tuwunel\n> Report issues: https://github.com/matrix-construct/tuwunel/issues\n\nFor a list of available commands, send the following message in this room: `!admin --help`"
);
// Send welcome message
+44 -52
View File
@@ -8,7 +8,7 @@ use tuwunel_core::{
utils::ReadyExt,
};
use super::{NamespaceRegex, RegistrationInfo};
use super::RegistrationInfo;
/// Called by timeline::append() after accepting new PDU.
#[implement(super::Service)]
@@ -43,57 +43,7 @@ async fn append_pdu_to(
pdu_id: RawPduId,
pdu: &Pdu,
) -> Result {
if self
.services
.state_cache
.appservice_in_room(pdu.room_id(), appservice)
.await
{
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;
return Ok(());
}
// If the RoomMember event has a non-empty state_key, it is targeted at someone.
// If it is our appservice user, we send this PDU to it.
if *pdu.kind() == TimelineEventType::RoomMember {
if let Some(state_key_uid) = &pdu
.state_key
.as_ref()
.and_then(|state_key| UserId::parse(state_key.as_str()).ok())
{
let appservice_uid = appservice.registration.sender_localpart.as_str();
if state_key_uid == &appservice_uid {
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;
return Ok(());
}
}
}
let matching_users = |users: &NamespaceRegex| {
appservice.users.is_match(pdu.sender().as_str())
|| *pdu.kind() == TimelineEventType::RoomMember
&& pdu
.state_key
.as_ref()
.is_some_and(|state_key| users.is_match(state_key))
};
let matching_aliases = |aliases: NamespaceRegex| {
self.services
.alias
.local_aliases_for_room(pdu.room_id())
.ready_any(move |room_alias| aliases.is_match(room_alias.as_str()))
};
if matching_aliases(appservice.aliases.clone()).await
|| appservice.rooms.is_match(pdu.room_id().as_str())
|| matching_users(&appservice.users)
{
if self.should_append_to(appservice, pdu).await {
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;
@@ -101,3 +51,45 @@ async fn append_pdu_to(
Ok(())
}
#[implement(super::Service)]
async fn should_append_to(&self, appservice: &RegistrationInfo, pdu: &Pdu) -> bool {
if self
.services
.state_cache
.appservice_in_room(pdu.room_id(), appservice)
.await
{
return true;
}
if appservice.is_user_match(pdu.sender()) {
return true;
}
if *pdu.kind() == TimelineEventType::RoomMember
&& pdu
.state_key
.as_ref()
.and_then(|state_key| UserId::parse(state_key.as_str()).ok())
.is_some_and(|user_id| appservice.is_user_match(user_id))
{
return true;
}
if self
.services
.alias
.local_aliases_for_room(pdu.room_id())
.ready_any(|room_alias| appservice.aliases.is_match(room_alias.as_str()))
.await
{
return true;
}
if appservice.rooms.is_match(pdu.room_id().as_str()) {
return true;
}
false
}
+50 -30
View File
@@ -1,10 +1,10 @@
mod data;
use std::{ops::Range, sync::Arc};
use std::{collections::HashSet, ops::Range, sync::Arc};
use data::Data;
use ruma::{OwnedUserId, RoomAliasId, ServerName, UserId};
use tuwunel_core::{Result, Server, error};
use tuwunel_core::{Result, Server, err, error};
use crate::service;
@@ -13,8 +13,7 @@ pub struct Service {
server: Arc<Server>,
pub server_user: OwnedUserId,
pub turn_secret: String,
pub registration_token: Option<String>,
pub turn_secret: Option<String>,
}
impl crate::Service for Service {
@@ -22,32 +21,17 @@ impl crate::Service for Service {
let db = Data::new(args);
let config = &args.server.config;
let turn_secret = config.turn_secret_file.as_ref().map_or_else(
|| config.turn_secret.clone(),
|path| {
std::fs::read_to_string(path).unwrap_or_else(|e| {
error!("Failed to read the TURN secret file: {e}");
config.turn_secret.clone()
})
},
);
let registration_token = config
.registration_token_file
let turn_secret = config
.turn_secret_file
.as_ref()
.map_or_else(
|| config.registration_token.clone(),
|path| {
let Ok(token) = std::fs::read_to_string(path).inspect_err(|e| {
error!("Failed to read the registration token file: {e}");
}) else {
return config.registration_token.clone();
};
Some(token)
},
);
.and_then(|path| {
std::fs::read_to_string(path)
.inspect_err(|e| {
error!("Failed to read the TURN secret file: {e}");
})
.ok()
})
.or_else(|| config.turn_secret.clone());
Ok(Arc::new(Self {
db,
@@ -58,7 +42,6 @@ impl crate::Service for Service {
)
.expect("@conduit:server_name is valid"),
turn_secret,
registration_token,
}))
}
@@ -122,4 +105,41 @@ impl Service {
#[inline]
#[must_use]
pub fn is_read_only(&self) -> bool { self.db.db.is_read_only() }
pub async fn get_registration_tokens(&self) -> HashSet<String> {
let mut tokens = HashSet::new();
if let Some(file) = &self
.server
.config
.registration_token_file
.as_ref()
{
match std::fs::read_to_string(file) {
| Err(e) => error!("Failed to read the registration token file: {e}"),
| Ok(text) => {
text.split_ascii_whitespace().for_each(|token| {
tokens.insert(token.to_owned());
});
},
}
}
if let Some(token) = &self.server.config.registration_token {
tokens.insert(token.to_owned());
}
tokens
}
pub fn init_rustls_provider(&self) -> Result {
if rustls::crypto::CryptoProvider::get_default().is_none() {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.map_err(|_provider| {
err!(error!("Error initialising aws_lc_rs rustls crypto backend"))
})
} else {
Ok(())
}
}
}
+101 -9
View File
@@ -1,9 +1,14 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use std::{
borrow::Borrow,
collections::{HashMap, HashSet},
iter::once,
sync::Arc,
};
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, pin_mut};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedServerName, OwnedUserId, RoomId, RoomVersionId,
UserId,
CanonicalJsonObject, CanonicalJsonValue, OwnedServerName, OwnedUserId, RoomId, RoomOrAliasId,
RoomVersionId, UserId,
api::{client::error::ErrorKind, federation},
canonical_json::to_canonical_value,
events::{
@@ -20,13 +25,13 @@ use tuwunel_core::{
matrix::{event::gen_event_id_canonical_json, room_version},
pdu::{PduBuilder, format::from_incoming_federation},
state_res, trace,
utils::{self, IterStream, ReadyExt},
utils::{self, IterStream, ReadyExt, shuffle},
warn,
};
use super::Service;
use crate::{
appservice::RegistrationInfo,
Services,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
@@ -39,22 +44,27 @@ use crate::{
skip_all,
fields(%sender_user, %room_id)
)]
#[allow(clippy::too_many_arguments)]
pub async fn join(
&self,
sender_user: &UserId,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
reason: Option<String>,
servers: &[OwnedServerName],
appservice_info: &Option<RegistrationInfo>,
is_appservice: bool,
state_lock: &RoomMutexGuard,
) -> Result {
let servers =
get_servers_for_room(&self.services, sender_user, room_id, orig_room_id, servers).await?;
let user_is_guest = self
.services
.users
.is_deactivated(sender_user)
.await
.unwrap_or(false)
&& appservice_info.is_none();
&& !is_appservice;
if user_is_guest
&& !self
@@ -99,12 +109,12 @@ pub async fn join(
|| (servers.len() == 1 && self.services.globals.server_is_ours(&servers[0]));
if local_join {
self.join_local(sender_user, room_id, reason, servers, state_lock)
self.join_local(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await?;
} else {
// Ask a remote server if we are not participating in this room
self.join_remote(sender_user, room_id, reason, servers, state_lock)
self.join_remote(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await?;
}
@@ -225,6 +235,8 @@ pub async fn join_remote(
.server_keys
.gen_id_hash_and_sign_event(&mut join_event_stub, &room_version_id)?;
state_res::check_pdu_format(&join_event_stub, &room_version_rules.event_format)?;
// It has enough fields to be called a proper event now
let mut join_event = join_event_stub;
@@ -634,6 +646,8 @@ pub async fn join_local(
));
}
let room_version_rules = room_version::rules(&room_version_id)?;
let mut join_event_stub: CanonicalJsonObject =
serde_json::from_str(make_join_response.event.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_join event json received from server: {e:?}"))
@@ -699,6 +713,8 @@ pub async fn join_local(
.server_keys
.gen_id_hash_and_sign_event(&mut join_event_stub, &room_version_id)?;
state_res::check_pdu_format(&join_event_stub, &room_version_rules.event_format)?;
// It has enough fields to be called a proper event now
let join_event = join_event_stub;
@@ -832,3 +848,79 @@ async fn make_join_request(
make_join_response_and_server
}
pub(super) async fn get_servers_for_room(
services: &Services,
user_id: &UserId,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
via: &[OwnedServerName],
) -> Result<Vec<OwnedServerName>> {
// add invited vias
let mut additional_servers = services
.state_cache
.servers_invite_via(room_id)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
.await;
// add invite senders' servers
additional_servers.extend(
services
.state_cache
.invite_state(user_id, room_id)
.await
.unwrap_or_default()
.iter()
.filter_map(|event| event.get_field("sender").ok().flatten())
.filter_map(|sender: &str| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()),
);
let mut servers = Vec::from(via);
shuffle(&mut servers);
if let Some(server_name) = room_id.server_name() {
servers.insert(0, server_name.to_owned());
}
if let Some(orig_room_id) = orig_room_id
&& let Some(orig_server_name) = orig_room_id.server_name()
{
servers.insert(0, orig_server_name.to_owned());
}
shuffle(&mut additional_servers);
servers.extend_from_slice(&additional_servers);
// 1. (room alias server)?
// 2. (room id server)?
// 3. shuffle [via query + resolve servers]?
// 4. shuffle [invited via, inviters servers]?
info!("{servers:?}");
// dedup preserving order
let mut set = HashSet::new();
servers.retain(|x| set.insert(x.clone()));
info!("{servers:?}");
// sort deprioritized servers last
if !servers.is_empty() {
for i in 0..servers.len() {
if services
.server
.config
.deprioritize_joins_through_servers
.is_match(servers[i].host())
{
let server = servers.remove(i);
servers.push(server);
}
}
}
Ok(servers)
}
+655
View File
@@ -0,0 +1,655 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use futures::{FutureExt, StreamExt};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedServerName, RoomId,
RoomOrAliasId, RoomVersionId, UserId,
api::federation::{self, membership::RawStrippedState},
canonical_json::to_canonical_value,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
},
};
use tuwunel_core::{
Err, Event, PduCount, Result, debug, debug_info, debug_warn, err, extract_variant, implement,
info,
matrix::event::gen_event_id,
pdu::{PduBuilder, PduEvent},
trace, utils, warn,
};
use super::Service;
use crate::{
membership::join::get_servers_for_room,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
},
};
#[implement(Service)]
#[tracing::instrument(
level = "debug",
skip_all,
fields(%sender_user, %room_id)
)]
pub async fn knock(
&self,
sender_user: &UserId,
room_id: &RoomId,
orig_server_name: Option<&RoomOrAliasId>,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: &RoomMutexGuard,
) -> Result {
let servers =
get_servers_for_room(&self.services, sender_user, room_id, orig_server_name, servers)
.await?;
if self
.services
.state_cache
.is_invited(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already invited in {room_id} but attempted to knock");
return Err!(Request(Forbidden(
"You cannot knock on a room you are already invited/accepted to."
)));
}
if self
.services
.state_cache
.is_joined(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already joined in {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are already joined in.")));
}
if self
.services
.state_cache
.is_knocked(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already knocked in {room_id}");
return Ok(());
}
if let Ok(membership) = self
.services
.state_accessor
.get_member(room_id, sender_user)
.await
{
if membership.membership == MembershipState::Ban {
debug_warn!("{sender_user} is banned from {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
}
let server_in_room = self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), room_id)
.await;
let local_knock = server_in_room
|| servers.is_empty()
|| (servers.len() == 1 && self.services.globals.server_is_ours(&servers[0]));
if local_knock {
self.knock_room_helper_local(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await
} else {
self.knock_room_helper_remote(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await
}
}
#[implement(Service)]
async fn knock_room_helper_local(
&self,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: &RoomMutexGuard,
) -> Result {
debug_info!("We can knock locally");
let room_version_id = self
.services
.state
.get_room_version(room_id)
.await?;
if matches!(
room_version_id,
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
) {
return Err!(Request(Forbidden("This room does not support knocking.")));
}
let content = RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason: reason.clone(),
..RoomMemberEventContent::new(MembershipState::Knock)
};
// Try normal knock first
let Err(error) = self
.services
.timeline
.build_and_append_pdu(
PduBuilder::state(sender_user.to_string(), &content),
sender_user,
room_id,
state_lock,
)
.await
else {
return Ok(());
};
if servers.is_empty()
|| (servers.len() == 1 && self.services.globals.server_is_ours(&servers[0]))
{
return Err(error);
}
warn!("We couldn't do the knock locally, maybe federation can help to satisfy the knock");
let (make_knock_response, remote_server) = self
.make_knock_request(sender_user, room_id, servers)
.await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !self
.services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_knock_response.event.get(),
)
.map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
.globals
.server_name()
.as_str()
.to_owned(),
),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
self.services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: self
.services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = self
.services
.federation
.execute(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
self.services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = self.services.globals.next_count();
self.services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
self.services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
state_lock,
)
.await?;
Ok(())
}
#[implement(Service)]
async fn knock_room_helper_remote(
&self,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: &RoomMutexGuard,
) -> Result {
info!("Knocking {room_id} over federation.");
let (make_knock_response, remote_server) = self
.make_knock_request(sender_user, room_id, servers)
.await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !self
.services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub: CanonicalJsonObject =
serde_json::from_str(make_knock_response.event.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
.globals
.server_name()
.as_str()
.to_owned(),
),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
self.services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: self
.services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = self
.services
.federation
.execute(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
self.services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Going through send_knock response knock state events");
let state = send_knock_response
.knock_room_state
.iter()
.map(|event| {
serde_json::from_str::<CanonicalJsonObject>(
extract_variant!(event.clone(), RawStrippedState::Stripped)
.expect("Raw<AnyStrippedStateEvent>")
.json()
.get(),
)
})
.filter_map(Result::ok);
let mut state_map: HashMap<u64, OwnedEventId> = HashMap::new();
for event in state {
let Some(state_key) = event.get("state_key") else {
debug_warn!("send_knock stripped state event missing state_key: {event:?}");
continue;
};
let Some(event_type) = event.get("type") else {
debug_warn!("send_knock stripped state event missing event type: {event:?}");
continue;
};
let Ok(state_key) = serde_json::from_value::<String>(state_key.clone().into()) else {
debug_warn!("send_knock stripped state event has invalid state_key: {event:?}");
continue;
};
let Ok(event_type) = serde_json::from_value::<StateEventType>(event_type.clone().into())
else {
debug_warn!("send_knock stripped state event has invalid event type: {event:?}");
continue;
};
let event_id = gen_event_id(&event, &room_version_id)?;
let shortstatekey = self
.services
.short
.get_or_create_shortstatekey(&event_type, &state_key)
.await;
self.services
.timeline
.add_pdu_outlier(&event_id, &event);
state_map.insert(shortstatekey, event_id.clone());
}
info!("Compressing state from send_knock");
let compressed: CompressedState = self
.services
.state_compressor
.compress_state_events(
state_map
.iter()
.map(|(ssk, eid)| (ssk, eid.borrow())),
)
.collect()
.await;
debug!("Saving compressed state");
let HashSetCompressStateEvent {
shortstatehash: statehash_before_knock,
added,
removed,
} = self
.services
.state_compressor
.save_state(room_id, Arc::new(compressed))
.await?;
debug!("Forcing state for new room");
self.services
.state
.force_state(room_id, statehash_before_knock, added, removed, state_lock)
.await?;
let statehash_after_knock = self
.services
.state
.append_to_state(&parsed_knock_pdu)
.await?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = self.services.globals.next_count();
self.services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
self.services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
state_lock,
)
.await?;
info!("Setting final room state for new room");
// We set the room state after inserting the pdu, so that we never have a moment
// in time where events in the current room state do not exist
self.services
.state
.set_room_state(room_id, statehash_after_knock, state_lock);
Ok(())
}
#[implement(Service)]
async fn make_knock_request(
&self,
sender_user: &UserId,
room_id: &RoomId,
servers: &[OwnedServerName],
) -> Result<(federation::membership::prepare_knock_event::v1::Response, OwnedServerName)> {
let mut make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
let mut make_knock_counter: usize = 0;
for remote_server in servers {
if self
.services
.globals
.server_is_ours(remote_server)
{
continue;
}
info!("Asking {remote_server} for make_knock ({make_knock_counter})");
let make_knock_response = self
.services
.federation
.execute(remote_server, federation::membership::prepare_knock_event::v1::Request {
room_id: room_id.to_owned(),
user_id: sender_user.to_owned(),
ver: self
.services
.server
.supported_room_versions()
.collect(),
})
.await;
trace!("make_knock response: {make_knock_response:?}");
make_knock_counter = make_knock_counter.saturating_add(1);
make_knock_response_and_server = make_knock_response.map(|r| (r, remote_server.clone()));
if make_knock_response_and_server.is_ok() {
break;
}
if make_knock_counter > 40 {
warn!(
"50 servers failed to provide valid make_knock response, assuming no server can \
assist in knocking."
);
make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
return make_knock_response_and_server;
}
}
make_knock_response_and_server
}
+6 -1
View File
@@ -11,8 +11,9 @@ use ruma::{
};
use tuwunel_core::{
Err, Result, debug_info, debug_warn, err, implement,
matrix::PduCount,
matrix::{PduCount, room_version},
pdu::PduBuilder,
state_res,
utils::{self, FutureBoolExt, future::ReadyBoolExt},
warn,
};
@@ -278,6 +279,8 @@ async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
)));
}
let room_version_rules = room_version::rules(&room_version_id)?;
let mut leave_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_leave_response.event.get(),
)
@@ -312,6 +315,8 @@ async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
.server_keys
.gen_id_hash_and_sign_event(&mut leave_event_stub, &room_version_id)?;
state_res::check_pdu_format(&leave_event_stub, &room_version_rules.event_format)?;
// It has enough fields to be called a proper event now
let leave_event = leave_event_stub;
+1
View File
@@ -2,6 +2,7 @@ mod ban;
mod invite;
mod join;
mod kick;
mod knock;
mod leave;
mod unban;
+2 -2
View File
@@ -109,10 +109,10 @@ impl Service {
pub async fn maybe_resolve_with_servers(
&self,
room: &RoomOrAliasId,
servers: Option<Vec<OwnedServerName>>,
servers: Option<&[OwnedServerName]>,
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
match <&RoomId>::try_from(room) {
| Ok(room_id) => Ok((room_id.to_owned(), servers.unwrap_or_default())),
| Ok(room_id) => Ok((room_id.to_owned(), Vec::from(servers.unwrap_or_default()))),
| Err(alias) => self.resolve_alias(alias).await,
}
}
@@ -68,6 +68,8 @@ pub(super) async fn handle_outlier_pdu(
let room_rules = room_version::rules(room_version)?;
state_res::check_pdu_format(&pdu_json, &room_rules.event_format)?;
// Now that we have checked the signature and hashes we can make mutations and
// convert to our PduEvent type.
let event = from_incoming_federation(room_id, event_id, &mut pdu_json, &room_rules)?;
@@ -52,6 +52,8 @@ pub(super) async fn upgrade_outlier_to_timeline_pdu(
let timer = Instant::now();
let room_rules = room_version::rules(room_version)?;
state_res::check_pdu_format(&val, &room_rules.event_format)?;
// 10. Fetch missing state and auth chain events by calling /state_ids at
// backwards extremities doing all the checks in this list starting at 1.
// These are not timeline events.
+3 -11
View File
@@ -187,17 +187,7 @@ pub async fn create_hash_and_sign_event(
pdu.event_id = self
.services
.server_keys
.gen_id_hash_and_sign_event(&mut pdu_json, &room_version)
.map_err(|e| {
use Error::Signatures;
use ruma::signatures::Error::PduSize;
match e {
| Signatures(PduSize) => {
err!(Request(TooLarge("PDU exceeds 65535 bytes")))
},
| _ => err!(Request(Unknown(warn!("Signing event failed: {e}")))),
}
})?;
.gen_id_hash_and_sign_event(&mut pdu_json, &room_version)?;
// Room id is event id for V12+
if matches!(version_rules.room_id_format, RoomIdFormatVersion::V2)
@@ -207,6 +197,8 @@ pub async fn create_hash_and_sign_event(
pdu_json.insert("room_id".into(), CanonicalJsonValue::String(pdu.room_id.clone().into()));
}
state_res::check_pdu_format(&pdu_json, &version_rules.event_format)?;
// Generate short event id
let _shorteventid = self
.services
+10 -2
View File
@@ -1,6 +1,6 @@
use ruma::{CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, RoomVersionId};
use tuwunel_core::{
Result, implement,
Result, err, implement,
matrix::{event::gen_event_id, room_version},
};
@@ -73,7 +73,15 @@ pub fn hash_and_sign_event(
object,
&room_version_rules.redaction,
)
.map_err(Into::into)
.map_err(|e| {
use ruma::signatures::Error::PduSize;
match e {
| PduSize => {
err!(Request(TooLarge("PDU exceeds 65535 bytes")))
},
| _ => err!(Request(Unknown(warn!("Signing event failed: {e}")))),
}
})
}
#[implement(super::Service)]
+8 -4
View File
@@ -59,7 +59,7 @@ pub struct Room {
type Connections = TokioMutex<BTreeMap<ConnectionKey, ConnectionVal>>;
pub type ConnectionVal = Arc<TokioMutex<Connection>>;
pub type ConnectionKey = (OwnedUserId, OwnedDeviceId, Option<ConnectionId>);
pub type ConnectionKey = (OwnedUserId, Option<OwnedDeviceId>, Option<ConnectionId>);
pub type Subscriptions = BTreeMap<OwnedRoomId, request::ListConfig>;
pub type Lists = BTreeMap<ListId, request::List>;
@@ -107,7 +107,7 @@ pub async fn clear_connections(
.await
.retain(|(conn_user_id, conn_device_id, conn_conn_id), _| {
let retain = user_id.is_none_or(is_equal_to!(conn_user_id))
&& device_id.is_none_or(is_equal_to!(conn_device_id))
&& (device_id.is_none() || device_id == conn_device_id.as_deref())
&& (conn_id.is_none() || conn_id == conn_conn_id.as_ref());
if !retain {
@@ -215,13 +215,17 @@ pub async fn is_connection_stored(&self, key: &ConnectionKey) -> bool {
}
#[inline]
pub fn into_connection_key<U, D, C>(user_id: U, device_id: D, conn_id: Option<C>) -> ConnectionKey
pub fn into_connection_key<U, D, C>(
user_id: U,
device_id: Option<D>,
conn_id: Option<C>,
) -> ConnectionKey
where
U: Into<OwnedUserId>,
D: Into<OwnedDeviceId>,
C: Into<ConnectionId>,
{
(user_id.into(), device_id.into(), conn_id.map(Into::into))
(user_id.into(), device_id.map(Into::into), conn_id.map(Into::into))
}
#[implement(Connection)]
+15 -10
View File
@@ -8,25 +8,18 @@ use tuwunel_database::{Interfix, Separator, serialize_key};
pub async fn watch<'a, Rooms>(
&self,
user_id: &UserId,
device_id: &DeviceId,
device_id: Option<&DeviceId>,
rooms: Rooms,
) -> Result
where
Rooms: Stream<Item = &'a RoomId> + Send + 'a,
{
let userdeviceid_prefix = (user_id, device_id, Interfix);
let globaluserdata_prefix = (Separator, user_id, Interfix);
let roomuserdataid_prefix = (Option::<&RoomId>::None, user_id, Interfix);
let userid_prefix =
serialize_key((user_id, Interfix)).expect("failed to serialize watch prefix");
let watchers = [
// Return when *any* user changed their key
// TODO: only send for user they share a room with
self.db
.todeviceid_events
.watch_prefix(&userdeviceid_prefix)
.boxed(),
self.db
.userroomid_joined
.watch_raw_prefix(&userid_prefix)
@@ -72,8 +65,20 @@ where
.boxed(),
];
let mut futures = FuturesUnordered::new();
futures.extend(watchers.into_iter());
let device_watchers = device_id.into_iter().map(|device_id| {
// Return when *any* user changed their key
// TODO: only send for user they share a room with
let userdeviceid_prefix = (user_id, device_id, Interfix);
self.db
.todeviceid_events
.watch_prefix(&userdeviceid_prefix)
.boxed()
});
let mut futures: FuturesUnordered<_> = watchers
.into_iter()
.chain(device_watchers)
.collect();
pin_mut!(rooms);
while let Some(room_id) = rooms.next().await {
+6 -28
View File
@@ -1,5 +1,5 @@
use std::{
collections::{BTreeMap, HashSet},
collections::BTreeMap,
sync::{Arc, RwLock},
};
@@ -45,32 +45,6 @@ impl crate::Service for Service {
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
#[implement(Service)]
pub async fn read_tokens(&self) -> Result<HashSet<String>> {
let mut tokens = HashSet::new();
if let Some(file) = &self
.services
.config
.registration_token_file
.as_ref()
{
match std::fs::read_to_string(file) {
| Err(e) => error!("Failed to read the registration token file: {e}"),
| Ok(text) => {
text.split_ascii_whitespace().for_each(|token| {
tokens.insert(token.to_owned());
});
},
}
}
if let Some(token) = &self.services.config.registration_token {
tokens.insert(token.to_owned());
}
Ok(tokens)
}
/// Creates a new Uiaa session. Make sure the session token is unique.
#[implement(Service)]
pub fn create(
@@ -175,7 +149,11 @@ pub async fn try_auth(
uiaainfo.completed.push(AuthType::Password);
},
| AuthData::RegistrationToken(t) => {
let tokens = self.read_tokens().await?;
let tokens = self
.services
.globals
.get_registration_tokens()
.await;
if tokens.contains(t.token.trim()) {
uiaainfo
.completed
+2 -2
View File
@@ -51,10 +51,10 @@ pub async fn set_dehydrated_device(&self, user_id: &UserId, request: Request) ->
self.create_device(
user_id,
&request.device_id,
Some(&request.device_id),
(None, None),
None,
request.initial_device_display_name.clone(),
request.initial_device_display_name.as_deref(),
None,
)
.await?;
+13 -6
View File
@@ -19,6 +19,9 @@ use tuwunel_core::{
};
use tuwunel_database::{Deserialized, Ignore, Interfix, Json, Map};
/// generated device ID length
const DEVICE_ID_LENGTH: usize = 10;
/// generated user access token length
pub const TOKEN_LENGTH: usize = 32;
@@ -28,12 +31,16 @@ pub const TOKEN_LENGTH: usize = 32;
pub async fn create_device(
&self,
user_id: &UserId,
device_id: &DeviceId,
device_id: Option<&DeviceId>,
(access_token, expires_in): (Option<&str>, Option<Duration>),
refresh_token: Option<&str>,
initial_device_display_name: Option<String>,
initial_device_display_name: Option<&str>,
client_ip: Option<String>,
) -> Result {
) -> Result<OwnedDeviceId> {
let device_id = device_id
.map(ToOwned::to_owned)
.unwrap_or_else(|| OwnedDeviceId::from(utils::random_string(DEVICE_ID_LENGTH)));
if !self.exists(user_id).await {
return Err!(Request(InvalidParam(error!(
"Called create_device for non-existent user {user_id}"
@@ -42,18 +49,18 @@ pub async fn create_device(
let notify = true;
self.put_device_metadata(user_id, notify, &Device {
device_id: device_id.into(),
device_id: device_id.clone(),
display_name: initial_device_display_name.map(Into::into),
last_seen_ip: client_ip.map(Into::into),
last_seen_ts: Some(MilliSecondsSinceUnixEpoch::now()),
});
if let Some(access_token) = access_token {
self.set_access_token(user_id, device_id, access_token, expires_in, refresh_token)
self.set_access_token(user_id, &device_id, access_token, expires_in, refresh_token)
.await?;
}
Ok(())
Ok(device_id)
}
/// Removes a device from a user.
+8
View File
@@ -21,6 +21,10 @@ pub async fn search_ldap(&self, user_id: &UserId) -> Result<Vec<(String, bool)>>
.as_ref()
.ok_or_else(|| err!(Ldap(error!("LDAP URI is not configured."))))?;
if uri.scheme().starts_with("ldaps") {
self.services.globals.init_rustls_provider()?;
}
debug!(?uri, "LDAP creating connection...");
let (conn, mut ldap) = LdapConnAsync::new(uri.as_str())
.await
@@ -122,6 +126,10 @@ pub async fn auth_ldap(&self, user_dn: &str, password: &str) -> Result {
.as_ref()
.ok_or_else(|| err!(Ldap(error!("LDAP URI is not configured."))))?;
if uri.scheme().starts_with("ldaps") {
self.services.globals.init_rustls_provider()?;
}
debug!(?uri, "LDAP creating connection...");
let (conn, mut ldap) = LdapConnAsync::new(uri.as_str())
.await
+3 -4
View File
@@ -3,6 +3,7 @@ pub mod device;
mod keys;
mod ldap;
mod profile;
mod register;
use std::sync::Arc;
@@ -124,10 +125,8 @@ impl Service {
password: Option<&str>,
origin: Option<&str>,
) -> Result {
origin.map_or_else(
|| self.db.userid_origin.insert(user_id, "password"),
|origin| self.db.userid_origin.insert(user_id, origin),
);
let origin = origin.unwrap_or("password");
self.db.userid_origin.insert(user_id, origin);
self.set_password(user_id, password).await
}
+156
View File
@@ -0,0 +1,156 @@
use std::fmt::Write;
use futures::FutureExt;
use ruma::{UserId, events::GlobalAccountDataEventType, push};
use tuwunel_core::{Err, Result, error, implement, info, is_equal_to, warn};
use crate::appservice::RegistrationInfo;
/// Fully register a local user
///
/// Returns a device id and access token for the registered user
#[implement(super::Service)]
pub async fn full_register(
&self,
user_id: &UserId,
password: Option<&str>,
origin: Option<&str>,
appservice_info: Option<&RegistrationInfo>,
is_guest: bool,
grant_admin: bool,
) -> Result {
if !self.services.globals.user_is_local(user_id) {
return Err!("Cannot register remote user");
}
if self.services.users.exists(user_id).await {
return Err!(Request(UserInUse("User ID is not available.")));
}
// Create user
self.services
.users
.create(user_id, password, origin)
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !self
.services
.config
.new_user_displayname_suffix
.is_empty()
&& appservice_info.is_none()
{
write!(
displayname,
" {}",
self.services
.server
.config
.new_user_displayname_suffix
)?;
}
self.services
.users
.set_displayname(user_id, Some(displayname.clone()));
// Initial account data
self.services
.account_data
.update(
None,
user_id,
GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: push::Ruleset::server_default(user_id),
},
})?,
)
.await?;
// If this is the first real user, grant them admin privileges except for guest
// users
// Note: the server user is generated first
if !is_guest
&& grant_admin
&& self.services.config.grant_admin_to_first_user
&& let Ok(admin_room) = self.services.admin.get_admin_room().await
&& self
.services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
self.services
.admin
.make_user_admin(user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
if appservice_info.is_none()
&& (self.services.config.allow_guests_auto_join_rooms || !is_guest)
{
for room in &self.services.server.config.auto_join_rooms {
let Ok(room_id) = self.services.alias.maybe_resolve(room).await else {
error!(
"Failed to resolve room alias to room ID when attempting to auto join \
{room}, skipping"
);
continue;
};
if !self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
let state_lock = self.services.state.mutex.lock(&room_id).await;
match self
.services
.membership
.join(
user_id,
&room_id,
Some(room),
Some("Automatically joining this room upon registration".to_owned()),
&[],
false,
&state_lock,
)
.boxed()
.await
{
| Err(e) => {
// don't return this error so we don't fail registrations
error!("Failed to automatically join room {room} for user {user_id}: {e}");
},
| _ => {
info!("Automatically joined room {room} for user {user_id}");
},
}
drop(state_lock);
}
}
Ok(())
}
+2 -2
View File
@@ -225,8 +225,8 @@
{"Action":"fail","Test":"TestKeyChangesLocal/New_login_should_create_a_device_lists.changed_entry"}
{"Action":"fail","Test":"TestKeyClaimOrdering"}
{"Action":"pass","Test":"TestKeysQueryWithDeviceIDAsObjectFails"}
{"Action":"fail","Test":"TestKnockRoomsInPublicRoomsDirectory"}
{"Action":"fail","Test":"TestKnockRoomsInPublicRoomsDirectoryInMSC3787Room"}
{"Action":"pass","Test":"TestKnockRoomsInPublicRoomsDirectory"}
{"Action":"pass","Test":"TestKnockRoomsInPublicRoomsDirectoryInMSC3787Room"}
{"Action":"fail","Test":"TestKnocking"}
{"Action":"fail","Test":"TestKnocking/A_user_can_knock_on_a_room_without_a_reason"}
{"Action":"fail","Test":"TestKnocking/A_user_can_knock_on_a_room_without_a_reason#01"}
-5
View File
@@ -1812,11 +1812,6 @@
# This option exists for developer and debug use, and as a failsafe in
# lieu of hardcoding it.
#
# This currently defaults to false as a matter of development until
# real-world testing can shake out any implementation issues rather than
# jeopardize existing rooms, but otherwise will default to true at the
# next point release or patch.
#
#hydra_backports = true
# Delete rooms when the last user from this server leaves. This feature is