1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
//! What an inbound body says about its own handling.
//!
//! Three questions get asked of every decrypted [`MessageBody`] before
//! anything acts on it: which relationship it concerns, which peer role may
//! have sent it, and which endpoints in it were chosen by the peer rather
//! than agreed at pairing. All three are answered by reading the body and
//! nothing else, so they are methods on the body.
//!
//! [`MessageBody`] is generated in `derec-proto`, so an inherent `impl` is
//! impossible from this crate. [`MessageBodyExt`] sidesteps the orphan rule the
//! same way every other trait in [`extensions`](super) does.
use derec_proto::{MessageBody, SenderKind, TransportProtocol};
/// The relationship an inbound body concerns.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Route {
/// Replica-bound, concerning this group member. Which member — author or
/// target — is the flow's reading; see [`MessageBodyExt::route`].
Replica(u64),
/// Helper-bound: no member named, so the owner↔helper role gate applies.
Helper,
}
/// Questions answered by reading an inbound body.
///
/// Bring the trait into scope (`use super::inbound::MessageBodyExt as _;`) and
/// call the methods on the body itself.
pub(crate) trait MessageBodyExt {
/// Which relationship this body concerns.
///
/// The discriminator is the payload's `replica_id`: present means the
/// message is replica-bound, absent means it is helper-bound. Decryption
/// is keyed by `channel_id` alone, so the channel it arrived on cannot
/// answer the question — every member of a group answers on the same one.
///
/// **Which** member the id names is the flow's to say, not this method's.
/// On every body but one it is the author, which is what a reply is
/// addressed to and what a response is attributed to. On `UnpairRequest`
/// it is the member being *removed*: a removal is announced to every
/// peer, so the target is the only thing they all agree on.
///
/// A body that carries no `replica_id` field at all is always
/// [`Route::Helper`]; it can only be an owner↔helper exchange.
fn route(&self) -> Route;
/// The peer role this body may only be sent by, or `None` when it does
/// not have exactly one.
///
/// A `Some` is gated before dispatch. A `None` means the body is
/// **multi-valued** — legal from either relationship and meaning
/// different things on each — so no single role can be required up
/// front, and the owning flow applies the gate once [`Self::route`] has
/// told it which relationship it is looking at.
///
/// Store-share, discovery, get-share and unpair-request are all
/// multi-valued for the same reason: an `Owner` peer drives them against
/// a helper, and a group member drives the corresponding replica flow
/// against another member. `UpdateChannelInfo` is `None` for a different
/// reason — it is role-blind, either side may initiate it.
fn expected_sender_role(&self) -> Option<SenderKind>;
/// Every transport endpoint this body carries that a **peer** chose.
///
/// One kind appears: `reply_to`, on the five request types that have
/// one. It *overrides* the endpoints agreed at pairing for a single
/// response, which is why checking only at pairing time would not be
/// enough.
///
/// # What is deliberately absent
///
/// `PairRequest`, `PrePairRequest` and `UpdateChannelInfoRequest` all
/// carry peer-chosen endpoints and none appears here. Each advertises a
/// `supportedTransports` list, and filtering that list — applying the
/// same `check_peer` to every entry — happens in its handler instead,
/// where a failing entry is skipped rather than aborting the whole
/// message. Gating them here would fail-fast on one bad entry even when
/// a later one would have served, which for `UpdateChannelInfo` would
/// additionally leave the peer's *stale* endpoints in place.
///
/// `PrePairRequest` could not be gated here in any case: it travels in
/// plaintext and takes its own dispatch path, which never reaches a
/// caller of this method.
///
/// Collected here so [`crate::transport::TransportPolicy`] has a single
/// application point for what remains, rather than a copy in each
/// response path and each handler.
fn peer_supplied_endpoints(&self) -> impl Iterator<Item = &TransportProtocol>;
/// The inner message's timestamp: the time it was sent.
///
/// The envelope carries the same value, and a receiver rejects a message
/// whose two copies differ. `None` when the field is absent.
fn timestamp(&self) -> Option<&prost_types::Timestamp>;
/// Stamp the inner message with the time it is being sent, so the
/// envelope can carry the same value.
fn set_timestamp(&mut self, ts: prost_types::Timestamp);
}
impl MessageBodyExt for MessageBody {
fn route(&self) -> Route {
let author = match self {
MessageBody::StoreShareRequest(r) => r.replica_id,
MessageBody::StoreShareResponse(r) => r.replica_id,
MessageBody::GetSecretIdsVersionsRequest(r) => r.replica_id,
MessageBody::GetSecretIdsVersionsResponse(r) => r.replica_id,
MessageBody::GetShareRequest(r) => r.replica_id,
MessageBody::GetShareResponse(r) => r.replica_id,
MessageBody::UnpairRequest(r) => r.replica_id,
_ => None,
};
match author {
Some(author) => Route::Replica(author),
None => Route::Helper,
}
}
fn expected_sender_role(&self) -> Option<SenderKind> {
match self {
MessageBody::VerifyShareRequest(_) => Some(SenderKind::Owner),
MessageBody::VerifyShareResponse(_) | MessageBody::UnpairResponse(_) => {
Some(SenderKind::Helper)
}
_ => None,
}
}
fn peer_supplied_endpoints(&self) -> impl Iterator<Item = &TransportProtocol> {
use crate::extensions::advertised_endpoints::ReplyToEndpoints as _;
let listed: Vec<&TransportProtocol> = match self {
MessageBody::StoreShareRequest(r) => r.reply_to_endpoints(),
MessageBody::VerifyShareRequest(r) => r.reply_to_endpoints(),
MessageBody::GetSecretIdsVersionsRequest(r) => r.reply_to_endpoints(),
MessageBody::GetShareRequest(r) => r.reply_to_endpoints(),
MessageBody::UnpairRequest(r) => r.reply_to_endpoints(),
_ => Vec::new(),
};
listed.into_iter()
}
fn timestamp(&self) -> Option<&prost_types::Timestamp> {
match self {
MessageBody::PairRequest(m) => m.timestamp.as_ref(),
MessageBody::PairResponse(m) => m.timestamp.as_ref(),
MessageBody::UnpairRequest(m) => m.timestamp.as_ref(),
MessageBody::UnpairResponse(m) => m.timestamp.as_ref(),
MessageBody::StoreShareRequest(m) => m.timestamp.as_ref(),
MessageBody::StoreShareResponse(m) => m.timestamp.as_ref(),
MessageBody::VerifyShareRequest(m) => m.timestamp.as_ref(),
MessageBody::VerifyShareResponse(m) => m.timestamp.as_ref(),
MessageBody::GetSecretIdsVersionsRequest(m) => m.timestamp.as_ref(),
MessageBody::GetSecretIdsVersionsResponse(m) => m.timestamp.as_ref(),
MessageBody::GetShareRequest(m) => m.timestamp.as_ref(),
MessageBody::GetShareResponse(m) => m.timestamp.as_ref(),
MessageBody::ErrorResponse(m) => m.timestamp.as_ref(),
MessageBody::UpdateChannelInfoRequest(m) => m.timestamp.as_ref(),
MessageBody::UpdateChannelInfoResponse(m) => m.timestamp.as_ref(),
MessageBody::PrePairRequest(m) => m.timestamp.as_ref(),
MessageBody::PrePairResponse(m) => m.timestamp.as_ref(),
}
}
fn set_timestamp(&mut self, ts: prost_types::Timestamp) {
let slot = match self {
MessageBody::PairRequest(m) => &mut m.timestamp,
MessageBody::PairResponse(m) => &mut m.timestamp,
MessageBody::UnpairRequest(m) => &mut m.timestamp,
MessageBody::UnpairResponse(m) => &mut m.timestamp,
MessageBody::StoreShareRequest(m) => &mut m.timestamp,
MessageBody::StoreShareResponse(m) => &mut m.timestamp,
MessageBody::VerifyShareRequest(m) => &mut m.timestamp,
MessageBody::VerifyShareResponse(m) => &mut m.timestamp,
MessageBody::GetSecretIdsVersionsRequest(m) => &mut m.timestamp,
MessageBody::GetSecretIdsVersionsResponse(m) => &mut m.timestamp,
MessageBody::GetShareRequest(m) => &mut m.timestamp,
MessageBody::GetShareResponse(m) => &mut m.timestamp,
MessageBody::ErrorResponse(m) => &mut m.timestamp,
MessageBody::UpdateChannelInfoRequest(m) => &mut m.timestamp,
MessageBody::UpdateChannelInfoResponse(m) => &mut m.timestamp,
MessageBody::PrePairRequest(m) => &mut m.timestamp,
MessageBody::PrePairResponse(m) => &mut m.timestamp,
};
*slot = Some(ts);
}
}
#[cfg(test)]
mod tests {
use super::*;
use derec_proto::{
GetSecretIdsVersionsRequestMessage, GetShareResponseMessage, StoreShareRequestMessage,
UnpairRequestMessage, UnpairResponseMessage, UpdateChannelInfoRequestMessage,
VerifyShareRequestMessage, VerifyShareResponseMessage,
};
/// Every body that carries the field routes on it, so a new replica-aware
/// message type cannot be added to the enum and silently take the helper
/// path.
#[test]
fn every_replica_bearing_body_routes_on_its_author() {
let bodies = [
MessageBody::StoreShareRequest(StoreShareRequestMessage {
replica_id: Some(7),
..Default::default()
}),
MessageBody::GetSecretIdsVersionsRequest(GetSecretIdsVersionsRequestMessage {
replica_id: Some(7),
..Default::default()
}),
MessageBody::GetShareResponse(GetShareResponseMessage {
replica_id: Some(7),
..Default::default()
}),
MessageBody::UnpairRequest(UnpairRequestMessage {
replica_id: Some(7),
..Default::default()
}),
];
for body in bodies {
assert_eq!(body.route(), Route::Replica(7), "{body:?}");
}
}
/// The same bodies with the field absent are helper-bound.
#[test]
fn an_absent_author_is_helper_bound() {
let body = MessageBody::StoreShareRequest(StoreShareRequestMessage {
replica_id: None,
..Default::default()
});
assert_eq!(body.route(), Route::Helper);
}
/// A body with no `replica_id` field at all can only be helper-bound.
#[test]
fn a_body_without_the_field_is_helper_bound() {
let body =
MessageBody::UpdateChannelInfoRequest(UpdateChannelInfoRequestMessage::default());
assert_eq!(body.route(), Route::Helper);
}
/// The gate applied before dispatch. Only the single-valued bodies name
/// a role; naming one for a multi-valued body would reject the replica
/// route of a message that is legal on it.
#[test]
fn only_single_valued_bodies_name_a_sender_role() {
assert_eq!(
MessageBody::VerifyShareRequest(VerifyShareRequestMessage::default())
.expected_sender_role(),
Some(SenderKind::Owner),
);
assert_eq!(
MessageBody::VerifyShareResponse(VerifyShareResponseMessage::default())
.expected_sender_role(),
Some(SenderKind::Helper),
);
assert_eq!(
MessageBody::UnpairResponse(UnpairResponseMessage::default()).expected_sender_role(),
Some(SenderKind::Helper),
);
}
/// Every body the owning flow gates for itself must report `None` here,
/// or it would be refused up front on a route where it is legal.
#[test]
fn multi_valued_bodies_name_no_sender_role() {
let multi_valued = [
MessageBody::StoreShareRequest(StoreShareRequestMessage::default()),
MessageBody::GetSecretIdsVersionsRequest(GetSecretIdsVersionsRequestMessage::default()),
MessageBody::GetShareResponse(GetShareResponseMessage::default()),
MessageBody::UnpairRequest(UnpairRequestMessage::default()),
MessageBody::UpdateChannelInfoRequest(UpdateChannelInfoRequestMessage::default()),
];
for body in multi_valued {
assert_eq!(body.expected_sender_role(), None, "{body:?}");
}
}
/// An `ErrorResponse` is stamped and read like every other body, so it
/// passes the envelope timestamp check instead of always failing it.
#[test]
fn an_error_response_carries_its_timestamp() {
let ts = prost_types::Timestamp {
seconds: 1_700_000_000,
nanos: 5,
};
let mut body = MessageBody::ErrorResponse(derec_proto::ErrorResponseMessage::default());
assert_eq!(body.timestamp(), None);
body.set_timestamp(ts);
assert_eq!(body.timestamp(), Some(&ts));
}
/// A `reply_to` is the one peer-chosen endpoint gated centrally, so a
/// request carrying one must surface it.
#[test]
fn a_reply_to_is_reported_as_peer_supplied() {
let endpoint = TransportProtocol {
uri: "https://peer.example".to_owned(),
protocol: derec_proto::Protocol::Https as i32,
};
let body = MessageBody::GetShareRequest(derec_proto::GetShareRequestMessage {
reply_to_transports: vec![endpoint.clone()],
..Default::default()
});
let found: Vec<_> = body.peer_supplied_endpoints().collect();
assert_eq!(found, vec![&endpoint]);
}
/// Responses carry no `reply_to`, and the pairing-time bodies are
/// filtered by their own handlers instead — see the method's docs.
#[test]
fn bodies_without_a_reply_to_surface_nothing() {
let bodies = [
MessageBody::VerifyShareResponse(VerifyShareResponseMessage::default()),
MessageBody::UpdateChannelInfoRequest(UpdateChannelInfoRequestMessage::default()),
];
for body in bodies {
assert_eq!(body.peer_supplied_endpoints().count(), 0, "{body:?}");
}
}
}