Commit c4f579b577

c4f579b5775582754d86ebb2336c9eb6fcb1dc84

parent: c142db3205

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-09-19 22:49 UTC
committer: <strk@kbt.io>

fix: refine undecryptable key guidance

Layout: unified · split

src/render.rs +45 −37
@@ -632,49 +632,44 @@ impl Render for RoomEncryptedEventContent {
632632 type RenderContext = WeechatRoomMember;
633633
634634 fn render(&self, sender: &Self::RenderContext) -> RenderedContent {
635 // The prefix already shows the sender, but naming them in the
636 // placeholder makes the guidance below unambiguous.
637 let message = format!(
638 "{}<{}Unable to decrypt message from {}{}>{}",
639 Weechat::color("chat_delimiters"),
640 Weechat::color("logger.color.backlog_line"),
641 sender.nick(),
642 Weechat::color("chat_delimiters"),
643 Weechat::color("reset"),
644 );
645
646 let tags = self.tags();
647 let mut lines = vec![RenderedLine {
648 message,
635 let line = RenderedLine {
636 message: encrypted_placeholder_message(&sender.nick()),
649637 // TODO: add tags that allow us decrypt the event at a later point
650638 // in time, sender key, algorithm, session id.
651 tags: tags.clone(),
652 }];
653
654 // The room key is missing, which usually means the sender's client is
655 // not sharing keys with us. Print actionable guidance: only the sender
656 // can help (re-send the message or forward the room key from another
657 // device). The event is retried and re-rendered automatically once the
658 // key arrives (see #274).
659 lines.push(RenderedLine {
660 message: format!(
661 "{}<{}Waiting for the room key from {}. Ask them to re-send \
662 their message or forward the room key from one of their \
663 other devices. This message will be decrypted automatically \
664 once the key arrives.{}>{}",
665 Weechat::color("chat_delimiters"),
666 Weechat::color("logger.color.backlog_line"),
667 sender.nick(),
668 Weechat::color("chat_delimiters"),
669 Weechat::color("reset"),
670 ),
671 tags,
672 });
639 tags: self.tags(),
640 };
673641
674 RenderedContent::new(lines)
642 RenderedContent::new(vec![line])
675643 }
676644}
677645
646fn encrypted_placeholder_message(sender_nick: &str) -> String {
647 // The prefix already shows the sender, but naming them in the placeholder
648 // makes the guidance unambiguous. Keep it one line: decrypted replacement
649 // rewrites placeholders in place and WeeChat cannot delete surplus lines.
650 format!(
651 "{}<{}{}{}>{} {}",
652 Weechat::color("chat_delimiters"),
653 Weechat::color("logger.color.backlog_line"),
654 encrypted_placeholder_body(sender_nick),
655 Weechat::color("chat_delimiters"),
656 Weechat::color("reset"),
657 encrypted_placeholder_guidance(sender_nick),
658 )
659}
660
661fn encrypted_placeholder_body(sender_nick: &str) -> String {
662 format!("Unable to decrypt message from {sender_nick}")
663}
664
665fn encrypted_placeholder_guidance(sender_nick: &str) -> String {
666 format!(
667 "Waiting for the room key from {}; this message will be decrypted \
668 automatically once the key arrives.",
669 sender_nick,
670 )
671}
672
678673impl Render for RedactedSyncMessageLikeEvent<RedactedRoomMessageEventContent> {
679674 type RenderContext = WeechatRoomMember;
680675 const TAGS: &'static [&'static str] = &["matrix_redacted"];
@@ -2013,6 +2008,19 @@ mod tests {
20132008 assert_eq!(rendered.lines[0].message, "literal <b> text");
20142009 }
20152010
2011 #[test]
2012 fn encrypted_placeholder_guidance_stays_on_one_line() {
2013 let message = format!(
2014 "{} {}",
2015 encrypted_placeholder_body("Alice"),
2016 encrypted_placeholder_guidance("Alice")
2017 );
2018
2019 assert_eq!(message.lines().count(), 1);
2020 assert!(message.contains("Unable to decrypt message from Alice"));
2021 assert!(message.contains("Waiting for the room key from Alice"));
2022 }
2023
20162024 #[test]
20172025 fn backlog_tags_suppress_notifications_and_highlights() {
20182026 let rendered = RenderedEvent {
src/server.rs +146 −51
@@ -123,6 +123,80 @@ fn secure_set_token_command(name: &str, token: &str) -> Option<String> {
123123 safe.then(|| format!("/secure set {name} {token}"))
124124}
125125
126fn room_key_withheld_message(
127 code: &matrix_sdk::ruma::events::room_key::withheld::RoomKeyWithheldCodeInfo,
128 sender: &UserId,
129 own_user_id: Option<&UserId>,
130 reason: Option<&str>,
131) -> String {
132 use matrix_sdk::ruma::events::room_key::withheld::RoomKeyWithheldCodeInfo;
133
134 let is_own_sender = own_user_id == Some(sender);
135 match code {
136 RoomKeyWithheldCodeInfo::Unavailable(_) if is_own_sender => {
137 format!(
138 "{sender} withheld the room key: your other device does not \
139 have this key. This device cannot recover that message from it."
140 )
141 }
142 RoomKeyWithheldCodeInfo::Unavailable(_) => format!(
143 "{sender} withheld the room key: the room key is not available on \
144 their device."
145 ),
146 RoomKeyWithheldCodeInfo::Unverified(_) if is_own_sender => {
147 format!(
148 "{sender} withheld the room key: this device is not verified \
149 by your other device."
150 )
151 }
152 RoomKeyWithheldCodeInfo::Unverified(_) => format!(
153 "{sender} withheld the room key: your device is not verified and \
154 they only share keys with verified devices."
155 ),
156 RoomKeyWithheldCodeInfo::Blacklisted(_) if is_own_sender => {
157 format!(
158 "{sender} withheld the room key: this device is blacklisted by \
159 your other device."
160 )
161 }
162 RoomKeyWithheldCodeInfo::Blacklisted(_) => format!(
163 "{sender} withheld the room key: your device is blacklisted."
164 ),
165 RoomKeyWithheldCodeInfo::Unauthorized(_) if is_own_sender => {
166 format!(
167 "{sender} withheld the room key: this device is not entitled to \
168 that room key."
169 )
170 }
171 RoomKeyWithheldCodeInfo::Unauthorized(_) => format!(
172 "{sender} withheld the room key: this device will not be given that \
173 room key by that sender."
174 ),
175 RoomKeyWithheldCodeInfo::NoOlm if is_own_sender => format!(
176 "{sender} withheld the room key: your other device could not \
177 establish an encrypted session with this device. Keep this client \
178 connected; new messages should decrypt once a session is \
179 established."
180 ),
181 RoomKeyWithheldCodeInfo::NoOlm => format!(
182 "{sender} withheld the room key: they could not establish an \
183 encrypted session with this device. Messages they sent before now \
184 cannot be recovered from them. Keep this client connected; new \
185 messages should decrypt once a session is established."
186 ),
187 other => {
188 let mut message = format!(
189 "{sender} withheld the room key with code: {}.",
190 other.code()
191 );
192 if let Some(reason) = reason {
193 message.push_str(&format!(" Reason given: {reason}"));
194 }
195 message
196 }
197 }
198}
199
126200fn tombstone_replacement_room_id(
127201 event: &SyncStateEvent<RoomTombstoneEventContent>,
128202) -> Option<OwnedRoomId> {
@@ -1696,54 +1770,15 @@ impl InnerServer {
16961770 _ => None,
16971771 };
16981772
1699 let message = match code {
1700 RoomKeyWithheldCodeInfo::Unavailable(_) => format!(
1701 "{} withheld the room key: the room key is not \
1702 available on their device. Ask them to re-send \
1703 their message or forward the room key from another \
1704 of their devices.",
1705 e.sender
1706 ),
1707 RoomKeyWithheldCodeInfo::Unverified(_) => format!(
1708 "{} withheld the room key: your device is not \
1709 verified and they only share keys with verified \
1710 devices. Verify your device with them so the room \
1711 key gets shared.",
1712 e.sender
1713 ),
1714 RoomKeyWithheldCodeInfo::Blacklisted(_) => format!(
1715 "{} withheld the room key: your device is \
1716 blacklisted. Ask them to unblacklist your device to \
1717 receive the room key.",
1718 e.sender
1719 ),
1720 RoomKeyWithheldCodeInfo::Unauthorized(_) => format!(
1721 "{} withheld the room key: your device is not \
1722 allowed to receive it. Ask them to share the room \
1723 key with you.",
1724 e.sender
1725 ),
1726 RoomKeyWithheldCodeInfo::NoOlm => format!(
1727 "{} withheld the room key: no Olm session could be \
1728 established with your device. Make sure both \
1729 devices are online so an Olm session can be \
1730 established, then ask them to share the room key \
1731 again.",
1732 e.sender
1733 ),
1734 other => {
1735 let mut message = format!(
1736 "{} withheld the room key with code: {}.",
1737 e.sender,
1738 other.code()
1739 );
1740 if let Some(reason) = &e.content.reason {
1741 message
1742 .push_str(&format!(" Reason given: {reason}"));
1743 }
1744 message
1745 }
1746 };
1773 let login_state = self.login_state.borrow();
1774 let own_user_id =
1775 login_state.as_ref().map(|state| state.user_id.as_ref());
1776 let message = room_key_withheld_message(
1777 code,
1778 &e.sender,
1779 own_user_id,
1780 e.content.reason.as_deref(),
1781 );
17471782
17481783 let buffer = session_data.and_then(|data| {
17491784 self.rooms
@@ -3018,10 +3053,15 @@ impl InnerServer {
30183053mod tests {
30193054 use super::{
30203055 create_room_request, missing_alias_action, room_id_join_servers,
3021 secure_set_token_command, with_entered_runtime_until_drop, InnerServer,
3022 MissingAliasAction,
3056 room_key_withheld_message, secure_set_token_command,
3057 with_entered_runtime_until_drop, InnerServer, MissingAliasAction,
3058 };
3059 use matrix_sdk::ruma::{
3060 events::room_key::withheld::{
3061 RoomKeyWithheldCodeInfo, RoomKeyWithheldSessionData,
3062 },
3063 owned_room_id, user_id, OwnedRoomAliasId, OwnedRoomId, OwnedUserId,
30233064 };
3024 use matrix_sdk::ruma::{OwnedRoomAliasId, OwnedRoomId, OwnedUserId};
30253065 use std::path::{Path, PathBuf};
30263066 use std::rc::Rc;
30273067 use std::sync::{
@@ -3163,6 +3203,61 @@ mod tests {
31633203 );
31643204 }
31653205
3206 fn withheld_session(code: &str) -> Box<RoomKeyWithheldSessionData> {
3207 RoomKeyWithheldSessionData::new(
3208 owned_room_id!("!room:example.org"),
3209 code.to_owned(),
3210 )
3211 .into()
3212 }
3213
3214 #[test]
3215 fn unavailable_key_from_own_device_does_not_blame_foreign_sender() {
3216 let own_user = user_id!("@me:example.org");
3217 let message = room_key_withheld_message(
3218 &RoomKeyWithheldCodeInfo::Unavailable(withheld_session("s1")),
3219 own_user,
3220 Some(own_user),
3221 None,
3222 );
3223
3224 assert!(message.contains("your other device does not have this key"));
3225 assert!(!message.contains("Ask them"));
3226 assert!(!message.contains("re-send"));
3227 }
3228
3229 #[test]
3230 fn unauthorised_key_refusal_says_device_is_not_entitled() {
3231 let own_user = user_id!("@me:example.org");
3232 let message = room_key_withheld_message(
3233 &RoomKeyWithheldCodeInfo::Unauthorized(withheld_session("s1")),
3234 own_user,
3235 Some(own_user),
3236 None,
3237 );
3238
3239 assert!(message.contains("this device is not entitled"));
3240 assert!(!message.contains("Ask them"));
3241 assert!(!message.contains("share the room key"));
3242 }
3243
3244 #[test]
3245 fn no_olm_guidance_describes_future_session_establishment() {
3246 let sender = user_id!("@alice:example.org");
3247 let own_user = user_id!("@me:example.org");
3248 let message = room_key_withheld_message(
3249 &RoomKeyWithheldCodeInfo::NoOlm,
3250 sender,
3251 Some(own_user),
3252 None,
3253 );
3254
3255 assert!(message.contains("could not establish an encrypted session"));
3256 assert!(message.contains("new messages should decrypt"));
3257 assert!(!message.contains("share the room key again"));
3258 assert!(!message.contains("both devices are online"));
3259 }
3260
31663261 #[test]
31673262 fn uses_room_id_origin_server_as_join_via() {
31683263 let room_id = "!opaque-room-id:origin.example"