fix: Don't allow set_unsigned to panic

While an error in this function is probably bad, detonating a metric tonne of TNT on the call tree is probably worse
This commit is contained in:
timedout committed 2026-09-27 17:32:47 +01:00
1 parent 90793eaaf4
commit 4812837d13
12 files changed
+73 -67

No files matched your search

+9 -12
View File
@@ -1,6 +1,7 @@
use axum::extract::State;
use conduwuit::{
Err, Event, Result, at, debug_warn, err, ref_at,
result::LogErr,
utils::{
IterStream,
future::TryExtExt,
@@ -103,12 +104,10 @@ pub(crate) async fn get_context_route(
.timeline
.get_unsigned_context(&pdu.1, Some(sender_user))
.await;
pdu.1.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
pdu.1
.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
if let Err(e) = services
.rooms
.pdu_metadata
@@ -136,12 +135,10 @@ pub(crate) async fn get_context_route(
.timeline
.get_unsigned_context(&pdu.1, Some(sender_user))
.await;
pdu.1.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
pdu.1
.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
if let Err(e) = services
.rooms
.pdu_metadata
+4 -6
View File
@@ -145,12 +145,10 @@ pub(crate) async fn get_message_events_route(
.timeline
.get_unsigned_context(&pdu.1, Some(sender_user))
.await;
pdu.1.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
pdu.1
.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
if let Err(e) = services
.rooms
.pdu_metadata
+1 -1
View File
@@ -63,7 +63,7 @@ pub(crate) async fn get_room_event_route(
.timeline
.get_unsigned_context(&event, Some(sender_user))
.await;
event.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because);
event.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)?;
Ok(get_room_event::v3::Response::new(event.into_format()))
}
+5 -6
View File
@@ -1,6 +1,7 @@
use axum::extract::State;
use conduwuit::{
Err, Event, Result, at, debug_warn,
result::LogErr,
utils::{BoolExt, stream::TryTools},
};
use futures::{FutureExt, TryStreamExt, future::try_join4};
@@ -58,12 +59,10 @@ pub(crate) async fn room_initial_sync_route(
.timeline
.get_unsigned_context(&pdu.1, Some(sender_user))
.await;
pdu.1.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
pdu.1
.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
if let Err(e) = services
.rooms
.pdu_metadata
+21 -13
View File
@@ -6,7 +6,9 @@ use std::collections::VecDeque;
use conduwuit::{
Event, PduCount, Result, debug_warn, err,
matrix::pdu::PduEvent,
ref_at, trace,
ref_at,
result::LogErr,
trace,
utils::stream::{BroadbandExt, ReadyExt, TryIgnore, WidebandExt},
};
use conduwuit_service::Services;
@@ -89,12 +91,15 @@ async fn load_timeline(
.timeline
.get_unsigned_context(&pdu.1, Some(sender_user))
.await;
pdu.1.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
pdu.1
.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
)
.log_err()
.ok();
Some(pdu)
})
.then(async move |mut pdu| {
@@ -124,12 +129,15 @@ async fn load_timeline(
.timeline
.get_unsigned_context(&pdu.1, Some(sender_user))
.await;
pdu.1.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
pdu.1
.set_unsigned(
ctx.user_id,
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
)
.log_err()
.ok();
Some(pdu)
})
.then(async move |mut pdu| {
+4 -1
View File
@@ -6,6 +6,7 @@ use conduwuit::{
Event,
pdu::{PduCount, PduEvent, sticky},
},
result::LogErr,
trace,
utils::{
BoolExt, IterStream, ReadyExt, TryFutureExtExt,
@@ -327,7 +328,9 @@ async fn build_sticky_events(
.timeline
.get_unsigned_context(&pdu, Some(sync_context.syncing_user))
.await;
pdu.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because);
pdu.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
events.push(Event::into_format(pdu));
}
+4 -1
View File
@@ -11,6 +11,7 @@ use conduwuit::{
Event, TypeStateKey,
pdu::{PduCount, PduEvent, sticky},
},
result::LogErr,
trace,
utils::{
BoolExt, FutureBoolExt, IterStream, ReadyExt, TryFutureExtExt,
@@ -1217,7 +1218,9 @@ async fn push_sticky_event(
.timeline
.get_unsigned_context(&pdu, Some(sender_user))
.await;
pdu.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because);
pdu.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
response
.rooms
.entry(room_id)
+2 -2
View File
@@ -74,7 +74,7 @@ fn redaction_removes_stickiness() {
fn sticky_ttl_is_added_to_unsigned() {
let mut pdu = sticky_pdu(&serde_json::json!({ "duration_ms": 300_000 }));
pdu.origin_server_ts = ruma::MilliSecondsSinceUnixEpoch::now().get();
pdu.set_unsigned(None, None, None, None);
pdu.set_unsigned(None, None, None, None).unwrap();
let unsigned: serde_json::Value =
serde_json::from_str(pdu.unsigned.as_deref().expect("unsigned is set").get())
@@ -99,7 +99,7 @@ fn set_unsigned_adds_the_sticky_ttl() {
// section
let mut pdu = sticky_pdu(&serde_json::json!({ "duration_ms": 300_000 }));
pdu.origin_server_ts = ruma::MilliSecondsSinceUnixEpoch::now().get();
pdu.set_unsigned(None, None, None, None);
pdu.set_unsigned(None, None, None, None).unwrap();
let unsigned: serde_json::Value =
serde_json::from_str(pdu.unsigned.as_deref().expect("unsigned is set").get())
+11 -22
View File
@@ -34,40 +34,29 @@ impl Pdu {
membership: Option<MembershipState>,
prev_content: Option<Box<RawJsonValue>>,
redacted_because: Option<Raw<AnyTimelineEvent>>,
) {
) -> crate::Result<Unsigned> {
// See: https://spec.matrix.org/v1.19/client-server-api/#definition-clientevent_unsigneddata
let Some(mut unsigned) = self.unsigned().map_or_else(
|| Some(Unsigned::new()),
|u| serde_json::from_str::<Unsigned>(u.get()).ok(),
) else {
return;
};
let mut unsigned = self
.unsigned()
.map_or_else(|| Ok(Unsigned::new()), |u| serde_json::from_str::<Unsigned>(u.get()))?;
let now = MilliSecondsSinceUnixEpoch::now().get();
let now_i: ruma::Int = now.into();
unsigned.insert(
"age".to_owned(),
to_raw_value(&now_i.saturating_sub(self.origin_server_ts.into())).unwrap(),
to_raw_value(&now_i.saturating_sub(self.origin_server_ts.into()))?,
);
if let Some(membership) = membership {
unsigned.insert("membership".to_owned(), to_raw_value(&membership).unwrap());
unsigned.insert("membership".to_owned(), to_raw_value(&membership)?);
}
if let Some(prev_content) = prev_content {
unsigned.insert(
"prev_content".to_owned(),
to_raw_value(&prev_content)
.expect("prev_content must be a valid JSON value for unsigned"),
);
unsigned.insert("prev_content".to_owned(), to_raw_value(&prev_content)?);
}
if let Some(redacted_because) = redacted_because {
unsigned.remove("org.continuwuity.redacted_by");
unsigned.insert(
"redacted_because".to_owned(),
to_raw_value(&redacted_because)
.expect("redacted_because must be a valid JSON value for unsigned"),
);
unsigned.insert("redacted_because".to_owned(), to_raw_value(&redacted_because)?);
}
// Remove transaction_id unless the user is the sender
@@ -82,11 +71,11 @@ impl Pdu {
{
unsigned.insert(
sticky::TTL_UNSIGNED_KEY.to_owned(),
to_raw_value(&expires_at.saturating_sub(u64::from(now)))
.expect("sticky event TTL must be a valid JSON value for unsigned"),
to_raw_value(&expires_at.saturating_sub(u64::from(now)))?,
);
}
self.unsigned = Some(to_raw_value(&unsigned).unwrap());
self.unsigned = Some(to_raw_value(&unsigned)?);
Ok(unsigned)
}
}
+4 -1
View File
@@ -3,6 +3,7 @@ use std::{mem::size_of, sync::Arc};
use conduwuit::{
arrayvec::ArrayVec,
matrix::PduCount,
result::LogErr,
utils::{
ReadyExt,
stream::{TryIgnore, WidebandExt},
@@ -99,7 +100,9 @@ impl Data {
.timeline
.get_unsigned_context(&pdu, Some(user_id))
.await;
pdu.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because);
pdu.set_unsigned(ctx.user_id, ctx.membership, ctx.prev_content, ctx.redacted_because)
.log_err()
.ok();
Some((shorteventid, pdu))
})
+4 -1
View File
@@ -5,6 +5,7 @@ use conduwuit::{
arrayvec::ArrayVec,
debug_warn,
matrix::event::{Event, Matches},
result::LogErr,
utils::{
ArrayVecExt, IterStream, ReadyExt, set,
stream::{TryIgnore, WidebandExt},
@@ -147,7 +148,9 @@ impl Service {
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
)
.log_err()
.ok();
Some(pdu)
})
+4 -1
View File
@@ -1,5 +1,6 @@
use std::{collections::BTreeMap, sync::Arc};
use conduwuit::result::LogErr;
use conduwuit_core::{
Event, Result, err,
matrix::pdu::{PduCount, PduEvent, PduId, RawPduId},
@@ -174,7 +175,9 @@ impl Service {
ctx.membership,
ctx.prev_content,
ctx.redacted_because,
);
)
.log_err()
.ok();
Some((pdu_id.shorteventid, pdu))
});