Compare commits

..
Author SHA1 Message Date
timedout 8ffa3e5367 fix: Wrap parsed & raw values
Rather than passing them around separately everywhere, just unify them into a single struct that is primarily fetched via a raw.
Also stores raw data directly into the database so that signatures can be verified later rather than only at receive time.
2026-11-18 15:10:51 +00:00
hexchen cec1f85f88 fix: verify_server_keys_response fails on non-standard json fields
Previously `verify_server_keys_response` would work with re-serialized 
ServerSigningKeys, which drops any extra, non-standard fields from the
origin server response. The signature is made over the whole response
though, meaning that any response including extra fields will not pass
signature verification.
2026-11-18 15:10:51 +00:00
Jade Ellis ad50b9a3b3 fix(complement): Enable using container builds as a base 2026-11-18 12:37:40 +00:00
timedout ceafc6f58d chore: Update SECURITY.md (#2549)
Reviewed-on: https://forgejo.ellis.link/continuwuation/continuwuity/pulls/2549
Reviewed-by: Ginger <ginger@gingershaped.computer>
Reviewed-by: Jade Ellis <jade@ellis.link>
2026-11-17 23:57:45 +00:00
timedout 467e335c90 perf: Remove unnecessary clones for PDU size check 2026-11-17 10:17:15 +00:00
timedout 902e54b525 fix: Off-by-one on PDU size check
Related: https://github.com/ruma/ruma/pull/2677
2026-11-17 10:14:32 +00:00
timedout 4ef487f40e chore: Add changelog frags for quickfixes 2026-11-17 09:30:49 +00:00
timedout 32bfe5f129 fix: Allow sync v2 to be interrupted by server shutdown
Fixes #2361
2026-11-17 09:26:22 +00:00
timedout 07d4c6f304 fix: Don't default to a 30-second sync timeout
Fixes #2462
2026-11-17 09:14:37 +00:00
timedout b3d099a52a fix: Always advertise unstable feature for MSC4354
Fixes #2542
2026-11-17 09:07:42 +00:00
Renovate Bot 92f9531def chore(deps): update https://github.com/taiki-e/install-action digest to ab49d3c 2026-11-17 05:01:16 +00:00
Renovate Bot a3d23c3718 chore(deps): update ghcr.io/renovatebot/renovate docker tag to v44.241.0 2026-11-16 05:05:23 +00:00
Erwan Leboucher 055c430e77 refactor(sync): Build device list updates separately from joined rooms 2026-11-15 20:24:36 +00:00
Erwan Leboucher df5e03f3b4 fix(sync): Apply the room and account data filters in legacy sync 2026-11-15 20:24:36 +00:00
31 changed files with 510 additions and 470 deletions

No files matched your search

+1 -1
View File
@@ -71,7 +71,7 @@ runs:
- name: Install timelord-cli and git-warp-time
if: steps.check-binaries.outputs.need-install == 'true'
uses: https://github.com/taiki-e/install-action@cfdb7ed4ccc7c7dce37759eae89dc5674b469b39 # v2
uses: https://github.com/taiki-e/install-action@ab49d3c64680143e184118bed66a46419fc15519 # v2
with:
tool: git-warp-time,timelord-cli@3.0.1
+1 -1
View File
@@ -43,7 +43,7 @@ jobs:
name: Renovate
runs-on: ubuntu-latest
container:
image: ghcr.io/renovatebot/renovate:44.227.4@sha256:e09d40686f73d43b2c1e4667cb47d3c1d24a6ac730b74ce7aced9c84f68594f1
image: ghcr.io/renovatebot/renovate:44.241.0@sha256:22d22b4ca7d768b5b469309d34379783bf91cd94a58faecf1cd0f4528a65d21c
options: --tmpfs /tmp:exec
steps:
- name: Checkout
Generated
+1
View File
@@ -949,6 +949,7 @@ dependencies = [
"sha1 0.11.0",
"tokio",
"tracing",
"wildmatch",
]
[[package]]
+3
View File
@@ -272,6 +272,9 @@ version = "0.5.2"
[workspace.dependencies.libloading]
version = "0.9.0"
[workspace.dependencies.wildmatch]
version = "2.7.0"
# Validating urls in config, was already a transitive dependency
[workspace.dependencies.url]
version = "2.5.8"
+8 -3
View File
@@ -23,12 +23,15 @@ to encourage users to stay up-to-date with the latest release, and we have no co
We appreciate the efforts of security researchers and the community in identifying and reporting vulnerabilities. To
ensure that potential vulnerabilities are addressed properly, please follow these guidelines:
1. **Contact members of the team directly** over E2EE private message.
1. **Contact _all_ members of the team** over E2EE private message on Matrix.
Add each contact to the same encrypted room. **Do not message maintainers individually**.
If you cannot reach us from matrix.org, please try [another server][sl] (e.g. continuwuity.rocks).
- [@jade:ellis.link](https://matrix.to/#/@jade:ellis.link)
- [@nex:nexy7574.co.uk](https://matrix.to/#/@nex:nexy7574.co.uk)
- [@ginger:gingershaped.computer](https://matrix.to/#/@ginger:gingershaped.computer)
2. **Email the security team** at [security@continuwuity.org](mailto:security@continuwuity.org). This is not E2EE, so
don't include sensitive details.
don't include sensitive details. Do not ask where to send sensitive details either; that
information is included in this security policy. We may not respond to emails that do this.
3. **Do not disclose the vulnerability publicly** until a fix has been pushed to the main branch.
4. **Provide detailed information** about the vulnerability, including:
- A clear description of the issue
@@ -42,6 +45,8 @@ ensure that potential vulnerabilities are addressed properly, please follow thes
If you have any doubts about a potential security vulnerability, contact us via private channels first! We'd prefer that
you bother us, instead of having a vulnerability disclosed without a fix.
[sl]: https://servers.joinmatrix.org
### Terms for credit
Before reporting a vulnerability, please remember that we are a small team maintaining a large codebase depended upon by
@@ -105,4 +110,4 @@ When security vulnerabilities are identified:
---
This security policy was last updated on May 25, 2025.
This security policy was last updated on November 17th, 2026.
+17 -3
View File
@@ -52,8 +52,10 @@ else
fi
# The base docker image to use for complement tests
# You can build the default with `docker build -t continuwuity:complement -f ./docker/complement.Dockerfile .`
# after running `cargo build -p conduwuit --features openreg`. Only the debug binary is used.
# On Linux, you can build the default with `docker build -t continuwuity:complement -f ./docker/complement.Dockerfile .`
# after running `cargo build -p conduwuit --features openreg`.
# On non-Linux platforms (or to use a container build), build `docker/Dockerfile` with debuginfo and openreg `--build-arg RUST_PROFILE=dev --build-arg CARGO_FEATURES=default,openreg`
# and then run `docker build -t continuwuity:complement --target container -f ./docker/complement.Dockerfile .`.
COMPLEMENT_BASE_IMAGE="${COMPLEMENT_BASE_IMAGE:-continuwuity:complement}"
# Complement tests that are skipped due to flakiness/reliability issues or we don't implement such features and won't for a long time
@@ -83,6 +85,13 @@ for command in docker go jq; do
fi
done
if [ -z "${DOCKER_HOST:-}" ]; then
DOCKER_HOST="$(docker context inspect --format '{{.Endpoints.docker.Host}}' 2>/dev/null || true)"
if [ -n "$DOCKER_HOST" ]; then
export DOCKER_HOST
fi
fi
if [ -n "$IMAGE_ARCHIVE" ]; then
if [ ! -f "$IMAGE_ARCHIVE" ]; then
echo "Docker image archive not found: $IMAGE_ARCHIVE"
@@ -99,7 +108,12 @@ fi
if ! docker image inspect "$COMPLEMENT_BASE_IMAGE" > /dev/null 2>&1; then
echo "Complement base image not found: $COMPLEMENT_BASE_IMAGE"
echo "Build it with: cargo build -p conduwuit --features openreg && docker build -t $COMPLEMENT_BASE_IMAGE -f docker/complement.Dockerfile ."
if [ "$(uname -s)" != "Linux" ]; then
echo "Build it with: docker buildx build --load -t continuwuity:latest-openreg --build-arg RUST_PROFILE=dev --build-arg CARGO_FEATURES=default,openreg -f docker/Dockerfile . && docker build -t $COMPLEMENT_BASE_IMAGE --target container -f docker/complement.Dockerfile ."
else
echo "Build it with: cargo build -p conduwuit --features openreg && docker build -t $COMPLEMENT_BASE_IMAGE -f docker/complement.Dockerfile ."
echo "Or from a container build: docker buildx build --load -t continuwuity:latest-openreg --build-arg RUST_PROFILE=dev --build-arg CARGO_FEATURES=default,openreg -f docker/Dockerfile . && docker build -t $COMPLEMENT_BASE_IMAGE --target container -f docker/complement.Dockerfile ."
fi
exit 1
fi
+1
View File
@@ -0,0 +1 @@
Legacy `/sync` now honours the `room.rooms`, `room.not_rooms`, `account_data.types` and `account_data.not_types` filter fields instead of returning every room and all account data. Contributed by @eleboucher.
+1
View File
@@ -0,0 +1 @@
Requests waiting on `/_matrix/client/v3/sync` can now be interrupted by the server shutdown, unblocking the 30-second shutdown delay imposed on a quiet server.
+1
View File
@@ -0,0 +1 @@
Fix remote server key signature verification when extra fields exist. Contributed by @hexchen
+1
View File
@@ -0,0 +1 @@
`/_matrix/client/v3/sync` no longer waits 30 seconds if the `timeout` parameter is omitted or set to zero, correctly reflecting the specification's rule that such requests should return immediately, even if they are empty.
+1
View File
@@ -0,0 +1 @@
The sticky events feature flag is correctly advertised in `/_matrix/client/versions` again after it was accidentally removed in #2517.
+21 -3
View File
@@ -1,11 +1,29 @@
FROM ubuntu:latest
ARG CONTINUWUITY_IMAGE=continuwuity:latest-openreg
ARG BINARY_SOURCE=host
FROM ${CONTINUWUITY_IMAGE} AS container-binary
FROM scratch AS host-binary
ARG BINARY_PATH=target/debug/conduwuit
COPY ${BINARY_PATH} /sbin/conduwuit
FROM ubuntu:latest AS base
EXPOSE 8008
EXPOSE 8448
RUN apt-get update && apt-get install -y ca-certificates liburing2 && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /etc/continuwuity /var/lib/continuwuity /usr/local/bin/
COPY complement/complement-entrypoint.sh /usr/local/bin/complement-entrypoint.sh
COPY complement/complement.config.toml /etc/continuwuity/config.toml
COPY target/debug/conduwuit /usr/local/bin/conduwuit
RUN chmod +x /usr/local/bin/conduwuit /usr/local/bin/complement-entrypoint.sh
RUN chmod +x /usr/local/bin/complement-entrypoint.sh
#HEALTHCHECK --interval=30s --timeout=5s CMD curl --fail http://localhost:8008/_continuwuity/server_version || exit 1
ENTRYPOINT ["/usr/local/bin/complement-entrypoint.sh"]
FROM base AS container
COPY --from=container-binary /sbin/conduwuit /usr/local/bin/conduwuit
RUN chmod +x /usr/local/bin/conduwuit
FROM base AS host
COPY --from=host-binary /sbin/conduwuit /usr/local/bin/conduwuit
RUN chmod +x /usr/local/bin/conduwuit
FROM ${BINARY_SOURCE}
+14
View File
@@ -20,6 +20,20 @@ docker build -t continuwuity:complement -f docker/complement.Dockerfile .
The `openreg` feature is required: without it the server refuses the open
registrations Complement uses to create test users, and almost every test fails.
On non-Linux (or to build inside a container instead of on the host), build the Continuwuity image with the `openreg` feature and `RUST_PROFILE=dev` first (open
registration is only enabled when `debug_assertions` are active), then build
the Complement image using the `container` target:
```bash
docker buildx build --load -t continuwuity:latest-openreg --build-arg RUST_PROFILE=dev --build-arg CARGO_FEATURES=default,openreg -f docker/Dockerfile .
docker build -t continuwuity:complement --target container -f docker/complement.Dockerfile .
./bin/complement
```
Pass `--build-arg CONTINUWUITY_IMAGE=your-image:tag` when building
`docker/complement.Dockerfile` if your Continuwuity container image uses a
different tag.
The run can take up to an hour. The runner writes the full Go test stream to
`tests/test_results/complement/test_logs.jsonl` and a sorted pass/fail/skip
summary to `tests/test_results/complement/test_results.jsonl`. It exits nonzero
+2 -2
View File
@@ -142,11 +142,11 @@ impl crate::Context<'_> {
.await?;
self.write_str("| ------ | ---------- | --------------------------- |\n")
.await?;
for (key_id, key) in &response.verify_keys {
for (key_id, key) in &response.parsed.verify_keys {
self.write_str(&format!("| {key_id} | {} | |\n", key.key))
.await?;
}
for (key_id, old_key) in &response.old_verify_keys {
for (key_id, old_key) in &response.parsed.old_verify_keys {
self.write_str(&format!(
"| {key_id} | {} | {:0<13} |\n",
old_key.key,
+1
View File
@@ -87,6 +87,7 @@ serde.workspace = true
sha1.workspace = true
tokio.workspace = true
tracing.workspace = true
wildmatch.workspace = true
[lints]
workspace = true
+75 -52
View File
@@ -64,7 +64,7 @@ pub(super) async fn load_joined_room(
services: &Services,
sync_context: SyncContext<'_>,
ref room_id: OwnedRoomId,
) -> Result<(JoinedRoom, DeviceListUpdates)> {
) -> Result<JoinedRoom> {
/*
Building a sync response involves many steps which all depend on each other.
To parallelize the process as much as possible, each step is divided into its own function,
@@ -80,7 +80,6 @@ pub(super) async fn load_joined_room(
sticky,
summary,
notification_counts,
device_list_updates,
},
) = try_join3(
build_account_data(services, sync_context, room_id),
@@ -117,7 +116,7 @@ pub(super) async fn load_joined_room(
unread_thread_notifications: BTreeMap::new(),
});
Ok((joined_room, device_list_updates))
Ok(joined_room)
}
/// Collect changes to the syncing user's account data events.
@@ -263,7 +262,6 @@ struct StateAndTimeline {
sticky: Sticky,
summary: Option<RoomSummary>,
notification_counts: Option<UnreadNotificationsCount>,
device_list_updates: DeviceListUpdates,
}
async fn build_sticky_events(
@@ -377,24 +375,14 @@ async fn build_state_and_timeline(
debug_warn!("timeline for newly joined room is empty");
}
let (summary, device_list_updates) = try_join(
build_room_summary(
services,
sync_context,
room_id,
shortstatehashes,
&timeline,
&state_events,
joined_since_last_sync,
),
build_device_list_updates(
services,
sync_context,
room_id,
shortstatehashes,
&state_events,
joined_since_last_sync,
),
let summary = build_room_summary(
services,
sync_context,
room_id,
shortstatehashes,
&timeline,
&state_events,
joined_since_last_sync,
)
.await?;
@@ -430,7 +418,6 @@ async fn build_state_and_timeline(
sticky,
summary,
notification_counts,
device_list_updates,
})
}
@@ -854,35 +841,34 @@ async fn build_heroes(
.await
}
/// Collect updates to users' device lists for E2EE.
#[tracing::instrument(level = "debug", skip_all)]
async fn build_device_list_updates(
/// Collect updates to users' device lists for E2EE in a joined room.
#[tracing::instrument(level = "debug", skip_all, fields(%room_id))]
pub(super) async fn load_device_list_updates(
services: &Services,
SyncContext {
sync_context: SyncContext<'_>,
room_id: &RoomId,
) -> Result<DeviceListUpdates> {
let SyncContext {
syncing_user,
last_sync_end_count,
current_count,
..
}: SyncContext<'_>,
room_id: &RoomId,
ShortStateHashes { current_shortstatehash, .. }: ShortStateHashes,
state_events: &Vec<PduEvent>,
joined_since_last_sync: bool,
) -> Result<DeviceListUpdates> {
let is_encrypted_room = services
.rooms
.state_accessor
.state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")
.is_ok();
} = sync_context;
let mut device_list_updates = DeviceListUpdates::new();
// initial syncs don't include device updates, and rooms which aren't
// encrypted don't affect them, so return early in either of those cases
if last_sync_end_count.is_none() || !(is_encrypted_room.await) {
return Ok(DeviceListUpdates::new());
if last_sync_end_count.is_none()
|| !services
.rooms
.state_accessor
.is_encrypted_room(room_id)
.await
{
return Ok(device_list_updates);
}
let mut device_list_updates = DeviceListUpdates::new();
// add users with changed keys to the `changed` list
services
.users
@@ -893,19 +879,56 @@ async fn build_device_list_updates(
})
.await;
// add users who now share encrypted rooms to `changed` and
// users who no longer share encrypted rooms to `left`
for state_event in state_events {
if state_event.kind == RoomMember {
let Some(content): Option<RoomMemberEventContent> = state_event.get_content().ok()
let shortstatehashes = fetch_shortstatehashes(services, sync_context, room_id).await?;
let ShortStateHashes {
current_shortstatehash,
last_sync_end_shortstatehash,
} = shortstatehashes;
if check_joined_since_last_sync(services, shortstatehashes, sync_context).await? {
// the syncing user now shares an encrypted room with every member
services
.rooms
.state_cache
.room_members(room_id)
.ready_for_each(|user_id| {
device_list_updates.changed.insert(user_id);
})
.await;
} else if let Some(last_sync_end_shortstatehash) = last_sync_end_shortstatehash
&& last_sync_end_shortstatehash != current_shortstatehash
{
// add users who now share encrypted rooms to `changed` and
// users who no longer share encrypted rooms to `left`
let state_added = services
.rooms
.state_accessor
.state_added((last_sync_end_shortstatehash, current_shortstatehash))
.await?;
for (shortstatekey, _) in state_added {
let Ok((StateEventType::RoomMember, state_key)) = services
.rooms
.short
.get_statekey_from_short(shortstatekey)
.await
else {
continue;
};
let Some(user_id): Option<OwnedUserId> = state_event
.state_key
.as_ref()
.and_then(|key| key.parse().ok())
let Ok(user_id) = OwnedUserId::try_from(state_key.as_str()) else {
continue;
};
let Ok(content): Result<RoomMemberEventContent> = services
.rooms
.state_accessor
.state_get_content(
current_shortstatehash,
&StateEventType::RoomMember,
user_id.as_str(),
)
.await
else {
continue;
};
@@ -921,7 +944,7 @@ async fn build_device_list_updates(
| Leave if !shares_encrypted_room => {
device_list_updates.left.insert(user_id);
},
| Join if joined_since_last_sync || shares_encrypted_room => {
| Join if shares_encrypted_room => {
device_list_updates.changed.insert(user_id);
},
| _ => (),
+117 -21
View File
@@ -3,7 +3,6 @@ mod left;
mod state;
use std::{
cmp::{self},
collections::{BTreeMap, HashMap, HashSet},
time::Duration,
};
@@ -21,7 +20,7 @@ use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture};
use ruma::{
DeviceId, OwnedUserId, RoomId, UserId,
api::client::{
filter::FilterDefinition,
filter::{self, FilterDefinition, RoomFilter},
sync::sync_events::{
self, DeviceLists,
v3::{
@@ -39,13 +38,17 @@ use service::{
account_data::AnyRawAccountDataEvent,
rooms::lazy_loading::{self, MemberSet, Options as _},
};
use wildmatch::WildMatch;
use super::{load_timeline, share_encrypted_room};
use crate::{
Ruma,
client::{
is_ignored_invite,
sync::v3::{joined::load_joined_room, left::load_left_room},
sync::v3::{
joined::{load_device_list_updates, load_joined_room},
left::load_left_room,
},
},
client_ip::ClientIp,
};
@@ -203,7 +206,12 @@ pub(crate) async fn sync_events_route(
let mut wake_receiver = services.sync.subscribe_to_wake(sender_user);
let response = build_sync_events(&services, &body).await?;
if body.body.full_state
let timeout_duration = body.body.timeout.unwrap_or_default();
// If the timeout is zero, request has full_state=true, or there's
// meaningful data to return, do so immediately.
if timeout_duration.is_zero()
|| body.body.full_state
|| !(response.rooms.is_empty()
&& response.presence.is_empty()
&& response.account_data.is_empty()
@@ -213,11 +221,15 @@ pub(crate) async fn sync_events_route(
return Ok(response);
}
// Hang a few seconds so requests are not spammed
// Stop hanging if new info arrives
let default = Duration::from_secs(30);
let duration = cmp::min(body.body.timeout.unwrap_or(default), default);
_ = tokio::time::timeout(duration, wake_receiver.changed()).await;
// Otherwise, wait for a wake signal to rebuild changes. Cap timeout at 30
// seconds (this isn't specced, but a reasonable cap since most clients have
// a 30-second polling interval).
let duration = timeout_duration.min(Duration::from_secs(30));
let mut shutdown_signal = services.server.signal.subscribe();
tokio::select! {
_ = tokio::time::timeout(duration, wake_receiver.changed()) => {},
_ = shutdown_signal.recv() => return Err!(Request(Unknown("Server is shutting down"), SERVICE_UNAVAILABLE)),
};
// Retry returning data
build_sync_events(&services, &body).await
@@ -271,34 +283,47 @@ pub(crate) async fn build_sync_events(
.rooms
.state_cache
.rooms_joined(syncing_user)
.ready_filter(|room_id| room_selected(&filter.room, room_id))
.broad_filter_map(|room_id| async {
let joined_room = load_joined_room(services, context, room_id.clone()).await;
match joined_room {
| Ok((room, updates)) => Some((room_id, room, updates)),
| Ok(room) => Some((room_id, room)),
| Err(err) => {
error!(?err, %room_id, "error loading joined room");
None
},
}
})
.ready_fold(
(BTreeMap::new(), DeviceListUpdates::new()),
|(mut joined_rooms, mut all_updates), (room_id, joined_room, updates)| {
all_updates.merge(updates);
.ready_fold(BTreeMap::new(), |mut joined_rooms, (room_id, joined_room)| {
if !joined_room.is_empty() {
joined_rooms.insert(room_id, joined_room);
}
if !joined_room.is_empty() {
joined_rooms.insert(room_id, joined_room);
}
joined_rooms
});
(joined_rooms, all_updates)
},
);
// a filtered out room still contributes its device list updates
let device_list_updates = services
.rooms
.state_cache
.rooms_joined(syncing_user)
.broad_filter_map(|room_id| async move {
load_device_list_updates(services, context, &room_id)
.await
.inspect_err(|err| error!(?err, %room_id, "error loading device list updates"))
.ok()
})
.ready_fold(DeviceListUpdates::new(), |mut all_updates, updates| {
all_updates.merge(updates);
all_updates
});
let left_rooms = services
.rooms
.state_cache
.rooms_left(syncing_user)
.ready_filter(|(room_id, _)| room_selected(&filter.room, room_id))
.broad_filter_map(|(room_id, leave_pdu)| async {
let left_room = load_left_room(services, context, room_id.clone(), leave_pdu).await;
@@ -317,6 +342,7 @@ pub(crate) async fn build_sync_events(
.rooms
.state_cache
.rooms_invited(syncing_user)
.ready_filter(|(room_id, _)| room_selected(&filter.room, room_id))
.wide_filter_map(async |(room_id, invite_state)| {
if is_ignored_invite(services, syncing_user, &room_id).await {
None
@@ -347,6 +373,7 @@ pub(crate) async fn build_sync_events(
.rooms
.state_cache
.rooms_knocked(syncing_user)
.ready_filter(|(room_id, _)| room_selected(&filter.room, room_id))
.fold_default(|mut knocked_rooms: BTreeMap<_, _>, (room_id, knock_state)| async move {
let knock_count = services
.rooms
@@ -376,6 +403,13 @@ pub(crate) async fn build_sync_events(
.account_data
.changes_since(None, syncing_user, last_sync_end_count, Some(current_count))
.ready_filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Global))
.ready_filter(|event| {
event
.get_field::<String>("type")
.ok()
.flatten()
.is_some_and(|event_type| type_selected(&filter.account_data, &event_type))
})
.collect();
let to_device_events = services
@@ -404,7 +438,8 @@ pub(crate) async fn build_sync_events(
.list_unused_fallback_key_types(syncing_user, syncing_device);
let (
(joined_rooms, mut device_list_updates),
joined_rooms,
mut device_list_updates,
left_rooms,
invited_rooms,
knocked_rooms,
@@ -417,6 +452,7 @@ pub(crate) async fn build_sync_events(
) = async {
futures::join!(
joined_rooms,
device_list_updates,
left_rooms,
invited_rooms,
knocked_rooms,
@@ -465,6 +501,24 @@ pub(crate) async fn build_sync_events(
Ok(response)
}
fn room_selected(filter: &RoomFilter, room_id: &RoomId) -> bool {
!filter.not_rooms.iter().any(|not_room| not_room == room_id)
&& filter
.rooms
.as_ref()
.is_none_or(|rooms| rooms.iter().any(|room| room == room_id))
}
fn type_selected(filter: &filter::Filter, event_type: &str) -> bool {
let matches = |pattern: &String| WildMatch::new(pattern).matches(event_type);
!filter.not_types.iter().any(matches)
&& filter
.types
.as_ref()
.is_none_or(|types| types.iter().any(matches))
}
#[tracing::instrument(name = "presence", level = "debug", skip_all)]
async fn process_presence_updates(
services: &Services,
@@ -528,3 +582,45 @@ async fn prepare_lazily_loaded_members(
lazily_loaded_members
}
#[cfg(test)]
mod tests {
use ruma::{owned_room_id, room_id};
use super::*;
#[test]
fn rooms_filter_selects_rooms() {
let room_id = room_id!("!a:example.com");
let mut filter = RoomFilter::default();
assert!(room_selected(&filter, room_id));
filter.rooms = Some(Vec::new());
assert!(!room_selected(&filter, room_id));
filter.rooms = Some(vec![room_id.to_owned()]);
assert!(room_selected(&filter, room_id));
filter.not_rooms = vec![owned_room_id!("!a:example.com")];
assert!(!room_selected(&filter, room_id));
}
#[test]
fn types_filter_selects_account_data() {
let mut filter = filter::Filter::default();
assert!(type_selected(&filter, "m.direct"));
filter.types = Some(Vec::new());
assert!(!type_selected(&filter, "m.direct"));
filter.types = Some(vec!["m.direct".to_owned()]);
assert!(type_selected(&filter, "m.direct"));
filter.types = Some(vec!["m.*".to_owned()]);
assert!(type_selected(&filter, "m.direct"));
assert!(!type_selected(&filter, "im.ponies.user_emotes"));
filter.not_types = vec!["m.direct".to_owned()];
assert!(!type_selected(&filter, "m.direct"));
}
}
+3 -3
View File
@@ -34,7 +34,7 @@ pub(crate) async fn get_server_keys_route(
// We need to verify that this response is still trusted based on
// the current configuration. It's possible we got this response
// from a notary who we no longer trust.
if resp.valid_until_ts > in_one_week() {
if resp.parsed.valid_until_ts > in_one_week() {
continue;
}
let trusted = services.config.trusted_servers.iter().any(|notary| {
@@ -47,8 +47,8 @@ pub(crate) async fn get_server_keys_route(
if !trusted {
continue;
}
for (old_key_id, old_key) in resp.old_verify_keys {
if old_key.expired_ts > resp.valid_until_ts {
for (old_key_id, old_key) in resp.parsed.old_verify_keys {
if old_key.expired_ts > resp.parsed.valid_until_ts {
// This key expired in the future which is probably illegal
continue;
}
+51 -27
View File
@@ -44,10 +44,7 @@ use ruma::{
to_device::DeviceIdOrAllDevices,
};
use service::{
rooms::{
event_handler::{DagBuilderTree, build_local_dag},
queue::QueueJob,
},
rooms::event_handler::{DagBuilderTree, build_local_dag},
transactions::{FederationTxnState, TransactionError, TxnKey, WrappedTransactionResponse},
};
use tokio::sync::watch::{Receiver, Sender};
@@ -335,6 +332,13 @@ async fn handle_room(
room_id: OwnedRoomId,
pdus: impl Iterator<Item = Pdu> + Send,
) -> std::result::Result<Vec<(OwnedEventId, Result)>, TransactionError> {
let _room_lock = services
.rooms
.event_handler
.mutex_federation
.lock(room_id.as_str())
.await;
let room_id = &room_id;
let mut pdu_map: HashMap<OwnedEventId, CanonicalJsonObject> = pdus
.into_iter()
@@ -354,30 +358,50 @@ async fn handle_room(
let value = pdu_map
.remove(&event_id)
.expect("sorted event IDs must be from the original map");
let room_id = match value
.get("room_id")
.and_then(CanonicalJsonValue::as_str)
.map(|v| v.try_into().map_err(Into::into))
{
| Some(Ok(v)) => v,
| Some(Err(e)) => {
results.push((event_id, Err(e)));
continue;
},
| None => {
results
.push((event_id, Err!(Request(BadJson("PDU is missing a room_id field")))));
continue;
},
};
let job = QueueJob {
room_id,
event_id: event_id.clone(),
origin: origin.to_owned(),
object: value,
};
services
.server
.check_running()
.map_err(|_| TransactionError::ShuttingDown)?;
let event_type = KeyValue::new(
"type",
pdu_type_label(value.get("type").and_then(CanonicalJsonValue::as_str)),
);
let origin_server_ts = value
.get("origin_server_ts")
.and_then(CanonicalJsonValue::as_integer)
.and_then(|ts| u64::try_from(i64::from(ts)).ok());
results.push((event_id, services.rooms.queue.new_job(job).await));
let start = Instant::now();
let result = services
.rooms
.event_handler
.handle_incoming_pdu(origin, room_id, &event_id, value, false)
.boxed()
.await
.map(|_| ());
let outcome = KeyValue::new("outcome", match &result {
| Ok(()) => "accepted",
| Err(Error::BadRequest(ErrorKind::Forbidden, ..)) => "forbidden",
| Err(Error::BadRequest(ErrorKind::InvalidParam | ErrorKind::BadJson, ..)) =>
"invalid",
| Err(_) => "error",
});
let instruments = &services.server.metrics.instruments;
instruments
.federation_incoming_pdu_duration
.record(start.elapsed().as_secs_f64(), std::slice::from_ref(&outcome));
if let (Ok(()), Some(ts)) = (&result, origin_server_ts) {
let lag = millis_since_unix_epoch().saturating_sub(ts);
instruments
.federation_incoming_pdu_lag
.record(Duration::from_millis(lag).as_secs_f64(), &[]);
}
instruments
.federation_incoming_pdus
.add(1, &[event_type, outcome]);
results.push((event_id, result));
}
Ok(results)
}
+5 -2
View File
@@ -35,10 +35,10 @@ pub fn versions() -> Vec<String> {
pub fn unstable_features(experiments: &Experiments) -> BTreeMap<String, bool> {
BTreeMap::from_iter([
// MatrixRTC (https://github.com/matrix-org/matrix-spec-proposals/pull/4143)
// Expected for spec v1.20
// Expected for spec v1.20/v2.0
("org.matrix.msc4143".to_owned(), true),
// Simplified Sliding sync (https://github.com/matrix-org/matrix-spec-proposals/pull/4186)
// Expected for spec v1.20
// Expected for spec v1.20/v2.0
("org.matrix.simplified_msc3575".to_owned(), true),
("org.matrix.msc4155".to_owned(), experiments.msc4155.is_enabled()),
// profile change propagation (https://github.com/matrix-org/matrix-spec-proposals/pull/4466)
@@ -48,5 +48,8 @@ pub fn unstable_features(experiments: &Experiments) -> BTreeMap<String, bool> {
("org.continuwuity.msc4484.unstable".to_owned(), experiments.msc4484.is_enabled()),
("org.continuwuity.msc4560".to_owned(), experiments.msc4560.is_enabled()),
("fi.mau.msc2815".to_owned(), experiments.msc2815.is_enabled()),
// Sticky events (https://github.com/matrix-org/matrix-spec-proposals/pull/4354)
// Proposal merged, expected for spec v1.20/v2.0
("org.matrix.msc4354".to_owned(), true),
])
}
+1 -1
View File
@@ -25,7 +25,7 @@ http.workspace = true
ruma.workspace = true
serde = { workspace = true }
serde_json = { workspace = true }
wildmatch = "2.7.0"
wildmatch.workspace = true
[dev-dependencies]
+29 -30
View File
@@ -11,8 +11,8 @@ version.workspace = true
[lib]
path = "mod.rs"
crate-type = [
"rlib",
# "dylib",
"rlib",
# "dylib",
]
[features]
@@ -23,61 +23,61 @@ bindgen-static = [
"conduwuit-database/bindgen-static"
]
brotli_compression = [
"conduwuit-core/brotli_compression",
"reqwest/brotli",
"conduwuit-core/brotli_compression",
"reqwest/brotli",
]
console = [
"dep:rustyline-async",
"dep:termimad",
"dep:rustyline-async",
"dep:termimad",
]
element_hacks = []
gzip_compression = [
"conduwuit-core/gzip_compression",
"reqwest/gzip",
"conduwuit-core/gzip_compression",
"reqwest/gzip",
]
http3 = [
"reqwest/http3"
]
io_uring = [
"conduwuit-database/io_uring",
"conduwuit-database/io_uring",
]
jemalloc = [
"conduwuit-core/jemalloc",
"conduwuit-database/jemalloc",
"conduwuit-core/jemalloc",
"conduwuit-database/jemalloc",
]
jemalloc_conf = [
"conduwuit-core/jemalloc_conf",
"conduwuit-database/jemalloc_conf",
"conduwuit-core/jemalloc_conf",
"conduwuit-database/jemalloc_conf",
]
jemalloc_prof = [
"conduwuit-core/jemalloc_prof",
"conduwuit-database/jemalloc_prof",
"conduwuit-core/jemalloc_prof",
"conduwuit-database/jemalloc_prof",
]
jemalloc_stats = [
"conduwuit-core/jemalloc_stats",
"conduwuit-database/jemalloc_stats",
"conduwuit-core/jemalloc_stats",
"conduwuit-database/jemalloc_stats",
]
media_thumbnail = [
"dep:image",
"dep:image",
]
release_max_log_level = [
"conduwuit-core/release_max_log_level",
"conduwuit-database/release_max_log_level",
"log/max_level_trace",
"log/release_max_level_info",
"tracing/max_level_trace",
"tracing/release_max_level_info",
"conduwuit-core/release_max_log_level",
"conduwuit-database/release_max_log_level",
"log/max_level_trace",
"log/release_max_level_info",
"tracing/max_level_trace",
"tracing/release_max_level_info",
]
systemd = [
"dep:sd-notify",
"dep:sd-notify",
]
url_preview = [
"dep:image",
"dep:webpage",
"dep:image",
"dep:webpage",
]
zstd_compression = [
"conduwuit-core/zstd_compression",
"conduwuit-database/zstd_compression",
"conduwuit-core/zstd_compression",
"conduwuit-database/zstd_compression",
"reqwest/zstd",
]
@@ -135,7 +135,6 @@ openidconnect.workspace = true
# which is necessary to call `danger_accept_invalid_certs`.
reqwest_0_12.workspace = true
rand.workspace = true
opentelemetry.workspace = true
[target.'cfg(all(unix, target_os = "linux"))'.dependencies]
sd-notify.workspace = true
+1 -1
View File
@@ -534,7 +534,7 @@ impl Service {
return state;
},
};
if !pdu_fits(&value.clone()) {
if !pdu_fits(&value) {
warn!(
"dropping incoming PDU {event_id} in room {room_id} from room join \
because it exceeds 65535 bytes or is otherwise too large."
-2
View File
@@ -7,7 +7,6 @@ pub mod membership;
pub mod metadata;
pub mod outlier;
pub mod pdu_metadata;
pub mod queue;
pub mod read_receipt;
pub mod search;
pub mod short;
@@ -33,7 +32,6 @@ pub struct Service {
pub metadata: Arc<metadata::Service>,
pub outlier: Arc<outlier::Service>,
pub pdu_metadata: Arc<pdu_metadata::Service>,
pub queue: Arc<queue::Service>,
pub read_receipt: Arc<read_receipt::Service>,
pub search: Arc<search::Service>,
pub short: Arc<short::Service>,
-240
View File
@@ -1,240 +0,0 @@
use std::{
collections::{HashMap, hash_map::Entry},
sync::Arc,
time::{Duration, Instant},
};
use async_trait::async_trait;
use conduwuit::{
Error, Result, Server, SyncMutex, debug, err, error, info, utils::millis_since_unix_epoch,
};
use opentelemetry::KeyValue;
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedRoomId, OwnedServerName,
api::error::ErrorKind, events::TimelineEventType,
};
use tokio::{
sync::{
Notify, mpsc,
mpsc::{Sender, UnboundedReceiver, UnboundedSender},
},
task::JoinSet,
};
use crate::{
rooms,
service::{Args, Dep},
};
pub struct Services {
event_handler: Dep<rooms::event_handler::Service>,
server: Arc<Server>,
}
pub struct Service {
queues: SyncMutex<HashMap<OwnedRoomId, UnboundedSender<QueueJob>>>,
send: Sender<QueueJob>,
recv: tokio::sync::Mutex<mpsc::Receiver<QueueJob>>,
shutdown: Notify,
services: Services,
}
#[async_trait]
impl crate::Service for Service {
fn build(args: Args<'_>) -> Result<Arc<Self>>
where
Self: Sized,
{
let (send, recv) = mpsc::channel(1);
Ok(Arc::new(Self {
queues: SyncMutex::new(HashMap::new()),
shutdown: Notify::new(),
send,
recv: tokio::sync::Mutex::new(recv),
services: Services {
event_handler: args.depend("rooms::event_handler"),
server: args.server.clone(),
},
}))
}
async fn worker(self: Arc<Self>) -> Result<()> {
let mut recv = self.recv.lock().await;
let mut handles = JoinSet::new();
let (yielder, mut yielding) = mpsc::channel(5);
loop {
let job = tokio::select! {
() = self.shutdown.notified() => {
info!("Shutting down");
yielding.close();
recv.close();
break;
},
room_id = yielding.recv() => {
if let Some(room_id) = room_id {
self.queues.lock().remove(&room_id);
}
continue;
}
job = recv.recv() => job
};
let Some(job) = job else {
debug!("No job (channel closed)");
break;
};
let mut queues = self.queues.lock();
match queues.entry(job.room_id.clone()) {
| Entry::Occupied(entry) => {
entry.get().send(job).unwrap();
// TODO: handle error
// It might be possible for a self-reaping worker to close
// at the same time we try to send to it, which will cause
// this send to return a "channel closed" error.
},
| Entry::Vacant(entry) => {
let room_id = entry.key().to_owned();
let (sender, receiver) = mpsc::unbounded_channel();
sender.send(job).unwrap(); // TODO: handle error
handles.spawn_on(
Self::room_worker(self.clone(), room_id, receiver, yielder.clone()),
self.services.server.runtime(),
);
entry.insert(sender);
},
}
}
// If the loop broke, make sure that the shutdown signal was sent, so
// that any room workers are guaranteed to shut down as soon as
// possible.
self.interrupt();
while let Some(ret) = handles.join_next_with_id().await {
match ret {
| Ok((id, ())) => {
debug!(?id, "room queue worker finished");
},
| Err(error) => {
error!(id = ?error.id(), ?error, "room queue worker failed");
},
}
}
Ok(())
}
fn interrupt(&self) { self.shutdown.notify_waiters(); }
fn name(&self) -> &'static str { module_path!() }
}
impl Service {
pub async fn new_job(&self, job: QueueJob) -> Result<()> {
self.send
.send(job)
.await
.map_err(|e| err!("failed to send PDU to worker: {e}"))
}
#[tracing::instrument(skip(self,channel,yielder), fields(%room_id))]
async fn room_worker(
self: Arc<Self>,
room_id: OwnedRoomId,
mut channel: UnboundedReceiver<QueueJob>,
yielder: Sender<OwnedRoomId>,
) {
debug!("Room worker starting up");
loop {
let job = tokio::select! {
() = self.shutdown.notified() => {
debug!("Room worker shutting down");
channel.close();
return;
},
() = tokio::time::sleep(Duration::from_mins(5)) => {
debug!("Room worker self-reaping (no activity in 5 minutes)");
yielder.send(room_id).await.ok();
debug!("Room worker reaped. Goodbye.");
return;
},
job = channel.recv() => job
};
let Some(job) = job else {
debug!("Room worker channel closed without shutdown?");
break;
};
debug!(
event_id=%job.event_id,
origin=%job.origin,
backpressure=%channel.len(),
"Handling incoming job"
);
let _room_lock = self
.services
.event_handler
.mutex_federation
.lock(room_id.as_str())
.await;
debug!(event_id=%job.event_id, origin=%job.origin, "Acquired room federation lock");
let event_type = KeyValue::new(
"type",
pdu_type_label(job.object.get("type").and_then(CanonicalJsonValue::as_str)),
);
let origin_server_ts = job
.object
.get("origin_server_ts")
.and_then(CanonicalJsonValue::as_integer)
.and_then(|ts| u64::try_from(i64::from(ts)).ok());
let start = Instant::now();
let result = self
.services
.event_handler
.handle_incoming_pdu(&job.origin, &job.room_id, &job.event_id, job.object, false)
.await;
let outcome = KeyValue::new("outcome", match &result {
| Ok(_) => "accepted",
| Err(Error::BadRequest(ErrorKind::Forbidden, ..)) => "forbidden",
| Err(Error::BadRequest(ErrorKind::InvalidParam | ErrorKind::BadJson, ..)) =>
"invalid",
| Err(_) => "error",
});
let instruments = &self.services.server.metrics.instruments;
instruments
.federation_incoming_pdu_duration
.record(start.elapsed().as_secs_f64(), std::slice::from_ref(&outcome));
if let (Ok(_), Some(ts)) = (&result, origin_server_ts) {
let lag = millis_since_unix_epoch().saturating_sub(ts);
instruments
.federation_incoming_pdu_lag
.record(Duration::from_millis(lag).as_secs_f64(), &[]);
}
instruments
.federation_incoming_pdus
.add(1, &[event_type, outcome]);
debug!(event_id=%job.event_id, origin=%job.origin, "Job finished");
}
}
}
pub struct QueueJob {
pub origin: OwnedServerName,
pub event_id: OwnedEventId,
pub room_id: OwnedRoomId,
pub object: CanonicalJsonObject,
}
/// Known event types, or `other`, so remote servers cannot create new
/// attribute values.
fn pdu_type_label(event_type: Option<&str>) -> String {
match event_type.map(TimelineEventType::from) {
| Some(TimelineEventType::_Custom(_)) | None => "other".to_owned(),
| Some(event_type) => event_type.to_string(),
}
}
+4 -4
View File
@@ -54,7 +54,7 @@ pub fn pdu_fits(owned_obj: &CanonicalJsonObject) -> bool {
}
// Now check the full PDU size
match serde_json::to_string(owned_obj) {
| Ok(s) => s.len() <= 65535,
| Ok(s) => s.len() <= 65536,
| Err(_) => false,
}
}
@@ -305,7 +305,7 @@ impl super::Service {
{
return match e {
| Error::SignatureJson(ruma::signatures::JsonError::PduTooLarge) => {
Err!(Request(TooLarge("Message/PDU is too long (exceeds 65535 bytes)")))
Err!(Request(TooLarge("Message/PDU is too long (exceeds 65536 bytes)")))
},
| _ => Err!(Request(Unknown(warn!("Signing event failed: {e}")))),
};
@@ -330,9 +330,9 @@ impl super::Service {
pdu_json
.insert("event_id".into(), CanonicalJsonValue::String(pdu.event_id.clone().into()));
// Verify that the *full* PDU isn't over 64KiB.
if !pdu_fits(&pdu_json.clone()) {
if !pdu_fits(&pdu_json) {
// feckin huge PDU mate
return Err!(Request(TooLarge("Message/PDU is too long (exceeds 65535 bytes)")));
return Err!(Request(TooLarge("Message/PDU is too long (exceeds 65536 bytes)")));
}
// Check with the policy server
+9 -9
View File
@@ -241,9 +241,9 @@ impl super::Service {
continue;
}
};
trace!(?origin_response, "Got signing key response from origin");
trace!(?origin_response.raw, "Got signing key response from origin");
self.add_signing_keys(&origin_response);
Self::track_result(&mut result, &mut still_missing, &origin_response);
Self::track_result(&mut result, &mut still_missing, &origin_response.parsed);
if still_missing.is_empty() {
break;
}
@@ -262,9 +262,9 @@ impl super::Service {
"Got signing keys response from a notary"
);
for server_keys in notary_response {
trace!(?server_keys, "Adding response from notary");
trace!(?server_keys.raw, "Adding response from notary");
self.add_signing_keys(&server_keys);
Self::track_result(&mut result, &mut still_missing, &server_keys);
Self::track_result(&mut result, &mut still_missing, &server_keys.parsed);
if still_missing.is_empty() {
break;
}
@@ -285,9 +285,9 @@ impl super::Service {
continue;
};
for server_keys in res {
trace!(?server_keys, "Adding response from notary");
trace!(?server_keys.raw, "Adding response from notary");
self.add_signing_keys(&server_keys);
Self::track_result(&mut result, &mut still_missing, &server_keys);
Self::track_result(&mut result, &mut still_missing, &server_keys.parsed);
}
if still_missing.is_empty() {
break;
@@ -300,9 +300,9 @@ impl super::Service {
debug_warn!(err=?res.err(), "Got error response from origin");
continue;
};
trace!(response=?res, "Got signing key response from origin");
trace!(response=?res.raw, "Got signing key response from origin");
self.add_signing_keys(&res);
Self::track_result(&mut result, &mut still_missing, &res);
Self::track_result(&mut result, &mut still_missing, &res.parsed);
if still_missing.is_empty() {
break;
}
@@ -400,7 +400,7 @@ impl super::Service {
.inspect_err(|e| info!("Could not acquire signing key from origin server: {e:?}"))
.ok()?;
verify_key_from_response(key_id, &response, min_valid_ts)
verify_key_from_response(key_id, &response.parsed, min_valid_ts)
.inspect(|_| self.add_signing_keys(&response))
}
}
+13 -7
View File
@@ -19,8 +19,10 @@ use ruma::{
ServerSigningKeyId,
api::federation::discovery::{ServerSigningKeys, VerifyKey},
room_version_rules::RoomVersionRules,
serde::Raw,
signatures::{Ed25519KeyPair, PublicKeyMap, PublicKeySet},
};
use verify::IncomingServerKeys;
use crate::{Dep, globals, sending, server_keys::util::required_keys};
@@ -83,19 +85,22 @@ impl Service {
}
/// Adds a signing key response to the database.
fn add_signing_keys(&self, new_keys: &ServerSigningKeys) {
fn add_signing_keys(&self, new_keys: &IncomingServerKeys) {
// TODO(nex): this results in massive amounts of data duplication.
// Perhaps a sliding approach would be better? where we just update the
// last known response if the only thing that changed was the
// valid_until_ts. This would require a more complex data structure
// though. For now, just storing the duplicated data is fine.
for key in new_keys
.parsed
.verify_keys
.keys()
.chain(new_keys.old_verify_keys.keys())
.chain(new_keys.parsed.old_verify_keys.keys())
{
let db_key = (new_keys.server_name.as_str(), key.as_str());
self.db.servernamekeyid_response.put(db_key, Json(new_keys));
let db_key = (new_keys.parsed.server_name.as_str(), key.as_str());
self.db
.servernamekeyid_response
.put(db_key, Json(&new_keys.raw));
}
}
@@ -170,7 +175,7 @@ impl Service {
let mut keys = self
.signing_keys_for(origin)
.ready_fold(BTreeMap::new(), |mut base, keys| {
base.extend(keys.verify_keys);
base.extend(keys.parsed.verify_keys);
base
})
.await;
@@ -186,11 +191,12 @@ impl Service {
pub fn signing_keys_for<'a>(
&'a self,
origin: &'a ServerName,
) -> impl Stream<Item = ServerSigningKeys> + Send + 'a {
) -> impl Stream<Item = IncomingServerKeys> + Send + 'a {
self.db
.servernamekeyid_response
.stream_prefix(&(origin, Interfix))
.ignore_err()
.map(|(_, v): (Ignore, ServerSigningKeys)| v)
.map(|(_, v): (Ignore, Raw<ServerSigningKeys>)| v.try_into().map_err(Into::into))
.ignore_err()
}
}
+36 -28
View File
@@ -4,7 +4,6 @@ use conduwuit::{Err, Result, trace, utils::millis_since_unix_epoch, warn};
use ruma::{
MilliSecondsSinceUnixEpoch, OwnedServerName, OwnedServerSigningKeyId, ServerName, UInt,
api::federation::discovery::{
ServerSigningKeys,
get_remote_server_keys_batch::{
self,
v2::{QueryCriteria, Response as NotaryResponse},
@@ -15,6 +14,8 @@ use ruma::{
uint,
};
use crate::server_keys::verify::IncomingServerKeys;
pub(crate) type ServerKeysQuery = BTreeMap<OwnedServerName, ServerKeysQuerySet>;
pub(crate) type ServerKeysQuerySet = BTreeMap<OwnedServerSigningKeyId, QueryCriteria>;
@@ -27,32 +28,33 @@ impl super::Service {
&self,
target: OwnedServerName,
min_valid_ts: MilliSecondsSinceUnixEpoch,
) -> Result<ServerSigningKeys> {
) -> Result<IncomingServerKeys> {
use get_server_keys::v2::Request;
let server_signing_key = self
let raw_server_key = self
.services
.sending
.send_unauthenticated_request(&target, Request::new())
.await
.map(|response| response.server_key)
.and_then(|key| key.deserialize().map_err(Into::into))?;
.map(|response| response.server_key)?;
if server_signing_key.server_name != target {
let server_signing_key: IncomingServerKeys = raw_server_key.try_into()?;
if server_signing_key.parsed.server_name != target {
return Err!(BadServerResponse(debug_warn!(
requested = ?target,
response = ?server_signing_key.server_name,
response = ?server_signing_key.parsed.server_name,
"Origin server responded with bogus server_name"
)));
}
if server_signing_key.valid_until_ts < min_valid_ts
|| server_signing_key.valid_until_ts > in_one_week()
if server_signing_key.parsed.valid_until_ts < min_valid_ts
|| server_signing_key.parsed.valid_until_ts > in_one_week()
{
return Err!(BadServerResponse(debug_warn!(
?target,
?min_valid_ts,
?server_signing_key.valid_until_ts,
?server_signing_key.parsed.valid_until_ts,
"Origin server responded with server keys that are not in-date"
)));
}
@@ -83,7 +85,7 @@ impl super::Service {
&self,
notary: &ServerName,
batch: ServerKeysQuery,
) -> Result<Vec<ServerSigningKeys>> {
) -> Result<Vec<IncomingServerKeys>> {
use get_remote_server_keys_batch::v2::Request;
let is_empty = batch.is_empty() || batch.values().all(BTreeMap::is_empty);
@@ -116,12 +118,12 @@ impl super::Service {
}
/// Parses a notary response, returning a vec of server signing key
/// responses. Drops any chunks that cannot be deserialized.
fn parse_notary_response(raw: &NotaryResponse) -> Vec<ServerSigningKeys> {
/// responses, each paired with its raw JSON (which is what the signatures
/// were made over). Drops any chunks that cannot be deserialized.
fn parse_notary_response(raw: &NotaryResponse) -> Vec<IncomingServerKeys> {
raw.server_keys
.iter()
.map(ruma::serde::Raw::deserialize)
.filter_map(Result::ok)
.filter_map(|chunk| chunk.try_into().ok())
.collect()
}
@@ -129,12 +131,15 @@ impl super::Service {
/// if the response should be dropped, true if it should be kept.
fn validate_notary_chunk(
batch: &ServerKeysQuery,
keys: &ServerSigningKeys,
incoming: &IncomingServerKeys,
notary_name: &ServerName,
notary_keys: &[Base64],
) -> bool {
let Some(expected_keys) = batch.get(&keys.server_name) else {
trace!("Dropping signing keys chunk for unrequested server: {}", keys.server_name);
let Some(expected_keys) = batch.get(&incoming.parsed.server_name) else {
trace!(
"Dropping signing keys chunk for unrequested server: {}",
incoming.parsed.server_name
);
return false;
};
let min_valid_ts = expected_keys
@@ -148,13 +153,15 @@ impl super::Service {
// If this notary response is older than any of the min_valid_ts we
// requested, then we should drop it.
if keys.valid_until_ts < min_valid_ts || keys.valid_until_ts > in_one_week() {
if incoming.parsed.valid_until_ts < min_valid_ts
|| incoming.parsed.valid_until_ts > in_one_week()
{
trace!(
?min_valid_ts,
?keys.valid_until_ts,
?incoming.parsed.valid_until_ts,
"Dropping signing keys chunk for server {} because it is older than the minimum \
valid timestamp we requested",
keys.server_name
incoming.parsed.server_name
);
return false;
}
@@ -162,38 +169,39 @@ impl super::Service {
// Does this chunk even contain any keys we asked for?
// Note that this enforces min_valid_ts per-key, instead of using the
// lowest denominator.
let all_key_ids = keys
let all_key_ids = incoming
.parsed
.verify_keys
.keys()
.chain(keys.old_verify_keys.keys())
.chain(incoming.parsed.old_verify_keys.keys())
.map(ToOwned::to_owned)
.collect::<Vec<_>>();
let interesting = expected_keys.iter().any(|(k, q)| {
all_key_ids.contains(k)
&& q.minimum_valid_until_ts
.is_none_or(|m| m <= keys.valid_until_ts)
.is_none_or(|m| m <= incoming.parsed.valid_until_ts)
});
if !interesting {
trace!(
"Dropping signing keys chunk for server {} because it does not contain any keys \
we requested",
keys.server_name
incoming.parsed.server_name
);
return false;
}
Self::verify_server_keys_response(keys, Some((notary_name, notary_keys)))
Self::verify_server_keys_response(incoming, Some((notary_name, notary_keys)))
.inspect_err(|e| {
trace!(
?e,
?keys,
?incoming.raw,
?notary_name,
?notary_keys,
"Dropping illegal server signing keys response chunk"
);
warn!(
%notary_name,
server_name=%keys.server_name,
server_name=%incoming.parsed.server_name,
"Dropping illegal server signing keys response chunk: {e:?}"
);
})
+92 -29
View File
@@ -5,11 +5,36 @@ use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, ServerName, SigningKeyAlgorithm,
api::federation::discovery::ServerSigningKeys,
room_version_rules::RoomVersionRules,
serde::{Base64, base64::Standard},
serde::{Base64, Raw, base64::Standard},
signatures::Verified,
};
use serde_json::value::RawValue as RawJsonValue;
pub struct IncomingServerKeys {
pub raw: Raw<ServerSigningKeys>,
pub parsed: ServerSigningKeys,
}
impl TryFrom<Raw<ServerSigningKeys>> for IncomingServerKeys {
type Error = serde_json::Error;
fn try_from(value: Raw<ServerSigningKeys>) -> std::result::Result<Self, Self::Error> {
value
.deserialize()
.map(|parsed| Self { raw: value, parsed })
}
}
impl TryFrom<&Raw<ServerSigningKeys>> for IncomingServerKeys {
type Error = serde_json::Error;
fn try_from(value: &Raw<ServerSigningKeys>) -> std::result::Result<Self, Self::Error> {
value
.deserialize()
.map(|parsed| Self { raw: value.to_owned(), parsed })
}
}
impl super::Service {
/// Validates the incoming event only using locally known verification keys.
/// Inserts the calculated event ID into the event_id field upon success.
@@ -58,34 +83,38 @@ impl super::Service {
/// `notary` must either be empty or have ONE entry. Extraneous notaries
/// will be ignored.
pub fn verify_server_keys_response(
server_keys: &ServerSigningKeys,
incoming: &IncomingServerKeys,
notary: Option<(&ServerName, &[Base64])>,
) -> Result<()> {
if let Some((notary_name, notary_keys)) = notary {
if !notary_keys.is_empty() {
Self::verify_notary_signature(server_keys, notary_name, notary_keys)?;
Self::verify_notary_signature(incoming, notary_name, notary_keys)?;
}
}
// NOTE: old_verify_keys aren't considered here because old keys can't
// be used to sign key responses.
let mut set = ruma::signatures::PublicKeySet::new();
server_keys.verify_keys.iter().for_each(|(key_id, key)| {
set.insert(key_id.to_string(), key.key.clone());
});
incoming
.parsed
.verify_keys
.iter()
.for_each(|(key_id, key)| {
set.insert(key_id.to_string(), key.key.clone());
});
let mut pubkey_map = ruma::signatures::PublicKeyMap::new();
pubkey_map.insert(server_keys.server_name.to_string(), set);
pubkey_map.insert(incoming.parsed.server_name.to_string(), set);
let mut canonical = to_canonical_object(server_keys)?;
Self::strip_extraneous_signatures(&mut canonical, &server_keys.server_name, &[]);
let mut canonical = to_canonical_object(&incoming.raw)?;
Self::strip_extraneous_signatures(&mut canonical, &incoming.parsed.server_name, &[]);
ruma::signatures::verify_json(&pubkey_map, &canonical).map_err(Into::into)
}
/// Verifies that the notary has at least one valid signature present with
/// the provided signing keys.
fn verify_notary_signature(
server_keys: &ServerSigningKeys,
incoming: &IncomingServerKeys,
notary_name: &ServerName,
notary_keys: &[Base64],
) -> Result<()> {
@@ -94,19 +123,16 @@ impl super::Service {
// certificate has been compromised, which would otherwise allow
// them to produce attacker-controlled signing keys with valid
// self-signatures.
let Some(notary_signatures) = server_keys.signatures.get(notary_name) else {
let Some(notary_signatures) = incoming.parsed.signatures.get(notary_name) else {
return Err!(Request(Forbidden(
"Missing notary server signature from {notary_name}"
)));
};
let mut canonical_object = to_canonical_object(notary_signatures)?;
Self::strip_extraneous_signatures(&mut canonical_object, &server_keys.server_name, &[
notary_name,
]);
// The notary signed the response as the origin served it, so verify
// over the raw JSON, not a re-serialised `ServerSigningKeys`.
let for_verify = ruma::signatures::to_canonical_json_string_for_signing(
&to_canonical_object(server_keys).expect("server keys object must be canonical json"),
)
.expect("canonical JSON must be stringable");
&to_canonical_object(&incoming.raw)?,
)?;
let raw_notary_keys = notary_keys.iter().map(Base64::as_bytes).collect::<Vec<_>>();
let raw_notary_signatures = notary_signatures
@@ -126,7 +152,7 @@ impl super::Service {
trace!(
"Valid signature from {notary_name} present on signing keys response \
for {}",
server_keys.server_name
incoming.parsed.server_name
);
return Ok(());
}
@@ -181,9 +207,9 @@ mod tests {
}
}
}
)).map(Raw::from_json).unwrap();
)).map(Raw::from_json).unwrap().try_into().unwrap();
Service::verify_server_keys_response(&response_data.deserialize().unwrap(), None)
Service::verify_server_keys_response(&response_data, None)
.expect("Self-signature of real key response is valid");
}
@@ -208,9 +234,9 @@ mod tests {
}
}
}
)).map(Raw::from_json).unwrap();
)).map(Raw::from_json).unwrap().try_into().unwrap();
Service::verify_server_keys_response(&response_data.deserialize().unwrap(), None)
Service::verify_server_keys_response(&response_data, None)
.expect("Self-signature of real key response is valid");
}
@@ -235,11 +261,11 @@ mod tests {
}
}
}
)).map(Raw::from_json).unwrap();
)).map(Raw::from_json).unwrap().try_into().unwrap();
let starstruck_pubkey =
Base64::<Standard>::parse("Z4oHuEjdUbtC5W/z/gzc5B8raxNk+8gGwbNwgxHqW8Y").unwrap();
Service::verify_server_keys_response(
&response_data.deserialize().unwrap(),
&response_data,
Some((server_name!("starstruck.systems"), &[starstruck_pubkey])),
)
.expect("Server keys from notary are valid");
@@ -265,8 +291,8 @@ mod tests {
}
}
}
)).map(Raw::from_json).unwrap();
Service::verify_server_keys_response(&response_data.deserialize().unwrap(), None)
)).map(Raw::from_json).unwrap().try_into().unwrap();
Service::verify_server_keys_response(&response_data, None)
.expect_err("Tampered response should fail self-signature check");
}
@@ -303,8 +329,45 @@ mod tests {
}
}
}
)).map(Raw::from_json).unwrap();
Service::verify_server_keys_response(&response_data.deserialize().unwrap(), None)
)).map(Raw::from_json).unwrap().try_into().unwrap();
Service::verify_server_keys_response(&response_data, None)
.expect_err("Intercepted notary response should fail strict validation");
}
#[test]
fn verify_response_with_extra_fields() {
let response_data = serde_json::from_value(serde_json::json!(
{
"old_verify_keys": {},
"server_name": "origin.example",
"signatures": {
"notary.example": {
"ed25519:a_RBdc": "ozlc2T5TlUa9XAsmeam+ESr20573vqFJvYHonj3nG8x+m6v7K5TSlEKTAlTRrJYiC6T/aWB+yxMae6FYR6nqCA"
},
"origin.example": {
"ed25519:a_jUhC": "+8xKspAfqI8SfrioOVliQ/jCPRDZEuag6JfkN5dXx9gsCX2rSnbvVTJlTJXFiDYUSyIJZg8ilq1QaPsT85ltBA"
}
},
"tls_fingerprints": [],
"valid_until_ts": 1900000000000_i64,
"verify_keys": {
"ed25519:a_jUhC": {
"key": "idcPtcSpRB5Chnlv1azhL3rMlcTawgQhoGNZWKaNFGI"
}
}
}
)).map(Raw::from_json).unwrap().try_into().unwrap();
let notary_pubkey =
Base64::<Standard>::parse("0ocHGigwa/ap6gSZzzspDQhhOkVkloaf9apolOvwqo8").unwrap();
Service::verify_server_keys_response(&response_data, None)
.expect("Self-signature must verify over the raw response");
Service::verify_server_keys_response(
&response_data,
Some((server_name!("notary.example"), &[notary_pubkey])),
)
.expect("Notary signature must verify over the raw response");
}
}
-1
View File
@@ -99,7 +99,6 @@ impl Services {
metadata: build!(rooms::metadata::Service),
outlier: build!(rooms::outlier::Service),
pdu_metadata: build!(rooms::pdu_metadata::Service),
queue: build!(rooms::queue::Service),
read_receipt: build!(rooms::read_receipt::Service),
search: build!(rooms::search::Service),
short: build!(rooms::short::Service),