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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
//! Inbound message processing.
//!
//! [`DeRecProtocol::process`] is the single entry point for anything arriving
//! off a transport. It decides whether a message is still fresh, which of the
//! two dispatch paths it takes — encrypted channel traffic, or the plaintext
//! pairing handshake — and hands it to [`handlers`](super::handlers).
use super::context::{local, pairing_config};
use super::error::ProcessError;
use super::events::DeRecEvent;
use super::stores::borrow_stores;
use super::traits::{
DeRecChannelStore, DeRecSecretStore, DeRecShareStore, DeRecStateStore, DeRecTransport,
DeRecUserSecretStore,
};
use super::types::{SecretKind, SecretValue};
use super::{DeRecProtocol, handlers};
use crate::extensions::channel_store::ChannelStoreExt as _;
use crate::{Error, Result, types::ChannelId};
use derec_proto::DeRecMessage;
use prost::Message;
#[cfg(target_arch = "wasm32")]
use crate::interop::wasm::now_secs;
use crate::protocol::IgnoreReason;
use crate::protocol_version::ProtocolVersion;
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::now_secs;
use crate::utils::verify_protocol_version;
use std::result;
impl<
Ch: DeRecChannelStore,
Sh: DeRecShareStore,
Ss: DeRecSecretStore,
Us: DeRecUserSecretStore,
T: DeRecTransport,
St: DeRecStateStore,
> DeRecProtocol<Ch, Sh, Ss, Us, St, T>
{
/// Feed any incoming wire bytes here regardless of which flow they belong to.
///
/// The library:
///
/// 1. Decodes the outer [`DeRecMessage`] envelope to read `channel_id`
/// 2. Looks up the channel's key material to determine the message kind
/// 3. Dispatches to the appropriate message handler based on the channel state
/// 4. Returns the events the application should react to
///
/// # Expired channel cleanup
///
/// Each call first removes `Pending` channels that have exceeded the
/// configured
/// [`ExpiredChannelCleanup`](crate::protocol::ExpiredChannelCleanup)
/// timeout, along with their pairing keys. Removal is **lazy**: it
/// happens only when this function runs, so an idle node retains
/// expired channels until the next inbound message arrives. The
/// removed ids are discarded on this path — call
/// [`remove_expired_channels`](Self::remove_expired_channels)
/// directly to observe them.
///
/// # Auto-accept
///
/// Any [`DeRecEvent::ActionRequired`] whose action kind the configured
/// [`AutoAcceptPolicy`](crate::protocol::AutoAcceptPolicy) opts into is replaced in place with
/// `AutoAccepted` plus the events a manual
/// [`accept`](Self::accept) would have produced, including a
/// [`DeRecEvent::RequestFailed`] when accepting fails.
///
/// # Failed requests
///
/// A request that arrived on an authenticated channel and then failed is
/// **not** an `Err`. It is answered exactly once, and reported as
/// [`DeRecEvent::RequestFailed`] inside `Ok`, so the other events of the
/// call are kept. Whether the library sends the answer or hands the
/// application a token for [`reject`](Self::reject) is
/// [`with_auto_respond_on_failure`](crate::protocol::DeRecProtocolBuilder::with_auto_respond_on_failure).
/// That includes a message that decrypts but whose body cannot be
/// decoded, which is answered with an `ErrorResponseMessage`. A received
/// `ErrorResponseMessage` is reported as
/// [`DeRecEvent::ErrorResponseReceived`] and never answered.
///
/// A `PairRequest` refused under a protocol rule is not an `Err` either:
/// the refusal is sent and reported as [`DeRecEvent::PairingRefused`].
///
/// [`ProcessError`] is left for what cannot be answered: an envelope that
/// does not decode, a channel with no key, a message that does not
/// decrypt, any other failure of the pairing handshake or of a response
/// this device received, and a store failure before the message could be
/// authenticated.
///
/// # Event ordering
///
/// Timeout events come first — sharing-round before unpair — then the
/// events this message produced. An application reading the returned
/// list in order therefore sees deadlines settle before new work.
///
/// A failed call still reports what it produced before failing, in
/// [`ProcessError::events`]: the timeout events at least, and every
/// event of the message when only the follow-up auto-publish failed.
/// Those deadlines are already settled and are never reported again.
///
/// # Security: replay and reflection
///
/// An envelope older than
/// [`Timeouts::inbound_message`](crate::protocol::types::Timeouts::inbound_message)
/// is ignored as `Expired`, but within that window this function keeps
/// no record of what it has processed: a copy of a message is processed
/// again. Both ends of a channel encrypt under one key, so a message this
/// device sent, delivered back to it, decrypts too. Detecting both is the
/// **application's** job, and how is its choice. One that keys on the
/// envelope's encrypted `message` field
/// ([`crate::derec_message::encrypted_payload`] reads it) gets a key that
/// cannot be changed
/// without failing decryption, and that differs for every message, since
/// each is sealed under a fresh nonce; a hash of the raw envelope does
/// not work, because `sequence` and `traceId` are not authenticated and a
/// copy can arrive as different bytes. Whatever the key, keep it for the
/// whole inbound window, measured from the later of arrival and the
/// envelope's `timestamp`, and across restarts.
///
/// A message held back and delivered late is a first delivery, so no
/// record catches it. Where its order matters to the protocol's own
/// state, the library enforces it: a `StoreShareRequest` older than the
/// newest version stored on its channel is ignored as `Superseded`.
/// Where it matters only to the application, the application enforces
/// it: the `timestamp` of the envelope that raised an `UpdateChannelInfo`
/// [`ActionRequired`](DeRecEvent::ActionRequired) is when the update was
/// sent, and an update older than the last one accepted on the channel
/// should be rejected.
///
/// # Security: bounding inbound message size
///
/// This function does **not** enforce an upper bound on `message.len()`,
/// and no library entry point that ingests peer wire bytes does either.
/// Legitimate envelopes span many orders of magnitude:
///
/// - Tens of bytes for empty acks / ping-class messages.
/// - A few KB for pairing material and verification proofs.
/// - Hundreds of KB to several MB for `StoreShareRequest` carrying a
/// share of a large secret.
/// - Many MB for `ReplicaSync` envelopes carrying an entire secret
/// (`O(num_secrets × num_helpers × max_secret_bytes)`).
///
/// Any cap tight enough to provide meaningful DoS resistance would risk
/// silently truncating a legitimate replica sync — at which point the
/// secret can become unrecoverable. The protocol therefore delegates
/// inbound-size bounding to the **application's transport layer**,
/// which knows the deployment's max secret size, helper count, and
/// replica fan-out and can pick a ceiling that fits.
///
/// Callers MUST refuse oversized envelopes upstream (e.g. enforce a
/// max HTTP body / WebSocket frame size consistent with their
/// configuration) before handing bytes to this function.
///
/// Malformed bytes — including truncation, varint overflow, and any
/// `prost`-level decode failure — surface as
/// [`ProcessError`] wrapping [`Error::ProtobufDecode`], except an inner
/// body that decrypts under the channel key and then fails to decode,
/// which is a failed request (see *Failed requests* above). This function
/// never panics on adversarial input. Protobuf recursion depth is
/// bounded by `prost`'s decoder; DeRec's schema is shallow (~3 levels),
/// so no additional caller-side recursion limit is required.
#[cfg_attr(
feature = "logging",
tracing::instrument(
skip_all,
fields(trace_id = tracing::field::Empty, message_len = message.len())
)
)]
pub async fn process(
&mut self,
message: &[u8],
) -> result::Result<Vec<DeRecEvent>, ProcessError> {
let mut events = self.run_timeout_sweeps().await;
let handled = self.handle_message(message).await;
let channel_id = match handled {
Ok((channel_id, mut message_events)) => {
events.append(&mut message_events);
channel_id
}
Err((channel_id, source)) => {
self.update_sharing_round(&mut events).await;
return Err(ProcessError {
channel_id,
source,
events,
});
}
};
self.update_sharing_round(&mut events).await;
match self.maybe_auto_publish_after_pair(&events).await {
Ok(auto_publish_events) => {
events.extend(auto_publish_events);
Ok(events)
}
Err(source) => Err(ProcessError {
channel_id: Some(channel_id),
source,
events,
}),
}
}
async fn handle_message(
&mut self,
message: &[u8],
) -> result::Result<(ChannelId, Vec<DeRecEvent>), (Option<ChannelId>, Error)> {
let envelope =
DeRecMessage::decode(message).map_err(|e| (None, Error::ProtobufDecode(e)))?;
let channel_id = ChannelId(envelope.channel_id);
#[cfg(feature = "logging")]
tracing::Span::current().record("trace_id", envelope.trace_id);
let events = self
.process_inner(&envelope, channel_id)
.await
.map_err(|source| (Some(channel_id), source))?;
let events = self
.apply_auto_accept(events)
.await
.map_err(|source| (Some(channel_id), source))?;
Ok((channel_id, events))
}
async fn is_pending_verification(&self, channel_id: ChannelId) -> Result<bool> {
use crate::protocol::types::{ChannelQuery, ChannelStatus, ReplicaFilter};
if let Some(record) = self
.channel_store
.load(self.secret_id, ChannelQuery::Helper { channel_id })
.await?
&& record.status() == ChannelStatus::Pending
{
return Ok(true);
}
Ok(self
.channel_store
.replicas_matching(
self.secret_id,
ReplicaFilter {
status: vec![ChannelStatus::Pending],
..Default::default()
},
)
.await?
.iter()
.any(|member| member.channel_id == channel_id))
}
pub(super) fn is_message_expired(
&self,
envelope: &DeRecMessage,
#[cfg_attr(not(feature = "logging"), allow(unused))] channel_id: ChannelId,
) -> bool {
let Some(ts) = &envelope.timestamp else {
return false;
};
let msg_secs = ts.seconds as u64;
let now = now_secs();
let age = now.saturating_sub(msg_secs);
let expired = age > self.timeouts.inbound_message.as_secs();
#[cfg(feature = "logging")]
if expired {
tracing::warn!(
channel_id = channel_id.0,
message_age_secs = age,
timeout_secs = self.timeouts.inbound_message.as_secs(),
"message discarded — older than configured timeout"
);
}
expired
}
async fn process_inner(
&mut self,
message: &DeRecMessage,
channel_id: ChannelId,
) -> Result<Vec<DeRecEvent>> {
let version = ProtocolVersion {
major: message.protocol_version_major,
minor: message.protocol_version_minor,
};
if version < self.min_protocol_version {
#[cfg(feature = "logging")]
tracing::warn!(
channel_id = channel_id.0,
major = version.major,
minor = version.minor,
"message ignored — protocol version below the configured minimum"
);
return Ok(vec![DeRecEvent::MessageIgnored {
channel_id,
reason: IgnoreReason::ProtocolVersionTooOld,
trace_id: message.trace_id,
}]);
}
if self.is_message_expired(message, channel_id) {
return Ok(vec![DeRecEvent::MessageIgnored {
channel_id,
reason: IgnoreReason::Expired,
trace_id: message.trace_id,
}]);
}
if let Some(events) = self.process_channel_message(message, channel_id).await? {
return Ok(events);
}
if let Some(events) = self.process_pairing_message(message, channel_id).await? {
return Ok(events);
}
#[cfg(feature = "logging")]
tracing::warn!(channel_id = channel_id.0, "no key material for channel");
Err(Error::InvalidInput(
"unknown channel_id: no shared key or pairing secret found",
))
}
async fn process_channel_message(
&mut self,
message: &DeRecMessage,
channel_id: ChannelId,
) -> Result<Option<Vec<DeRecEvent>>> {
let Some(SecretValue::SharedKey(shared_key)) = self
.secret_store
.load(self.secret_id, channel_id, SecretKind::SharedKey)
.await?
else {
return Ok(None);
};
if self.is_pending_verification(channel_id).await? {
#[cfg(feature = "logging")]
tracing::warn!(
channel_id = channel_id.0,
"message ignored — channel is pending fingerprint verification"
);
return Ok(Some(vec![DeRecEvent::MessageIgnored {
channel_id,
reason: IgnoreReason::PendingVerification,
trace_id: message.trace_id,
}]));
}
let events = handlers::handle(
&mut borrow_stores!(self),
&local!(self),
message,
channel_id,
&shared_key,
self.auto_respond_on_failure,
)
.await?;
Ok(Some(events))
}
/// Dispatch a pairing message, trying the plaintext leg before the
/// encrypted one.
///
/// `PrePair` envelopes carry a serialized `MessageBody` directly — no
/// shared or asymmetric key exists yet, so there is nothing to encrypt
/// under — and therefore decode without any crypto material. ECIES
/// ciphertext for the regular `Pair` flow will not realistically decode to
/// a valid `PrePair*` variant; if it ever did, it falls through to the
/// encrypted path.
///
/// A `PrePairRequest` is routed only when this device holds a correlation
/// record for the channel, and either kind will do:
/// [`SecretKind::PairingSecret`](crate::protocol::SecretKind::PairingSecret) from a `HashedKeys` contact, whose keys
/// the accept path publishes, or [`SecretKind::PairingContact`](crate::protocol::SecretKind::PairingContact) from a
/// `NoKeys` one, which has no keys until the accept path generates them.
/// Holding neither means the channel is unknown, and the message is
/// dropped silently. A `PrePairResponse` needs the stored contact
/// specifically, to check the keys against its binding hash.
async fn process_pairing_message(
&mut self,
message: &DeRecMessage,
channel_id: ChannelId,
) -> Result<Option<Vec<DeRecEvent>>> {
use derec_proto::MessageBody;
if let Ok((inner, payload_version)) = MessageBody::decode_versioned(&message.message) {
let version_mismatch = verify_protocol_version(message, payload_version)
.is_err()
.then(|| {
vec![DeRecEvent::MessageIgnored {
channel_id,
reason: IgnoreReason::ProtocolVersionMismatch,
trace_id: message.trace_id,
}]
});
match inner {
MessageBody::PrePairRequest(request) => {
let has_pairing_secret = matches!(
self.secret_store
.load(self.secret_id, channel_id, SecretKind::PairingSecret)
.await?,
Some(SecretValue::PairingSecret(_))
);
let has_pairing_contact = matches!(
self.secret_store
.load(self.secret_id, channel_id, SecretKind::PairingContact)
.await?,
Some(SecretValue::PairingContact(_))
);
if !has_pairing_secret && !has_pairing_contact {
return Ok(None);
}
if let Some(ignored) = version_mismatch {
return Ok(Some(ignored));
}
if let Some(events) = handlers::pairing::ignored_if_expired(
&mut borrow_stores!(self),
&local!(self),
&pairing_config!(self),
channel_id,
request.timestamp.as_ref(),
message.trace_id,
)
.await?
{
return Ok(Some(events));
}
let events = handlers::pairing::pre_pair::on_request(
channel_id,
&request,
message.trace_id,
)?;
return Ok(Some(events));
}
MessageBody::PrePairResponse(resp) => {
let Some(SecretValue::PairingContact(contact)) = self
.secret_store
.load(self.secret_id, channel_id, SecretKind::PairingContact)
.await?
else {
return Ok(None);
};
if let Some(ignored) = version_mismatch {
return Ok(Some(ignored));
}
let events = handlers::pairing::pre_pair::on_response(
&mut borrow_stores!(self),
&local!(self),
&pairing_config!(self),
channel_id,
&contact,
&resp,
)
.await?;
return Ok(Some(events));
}
_ => {} // Fall through to the encrypted Pair path.
}
}
let Some(SecretValue::PairingSecret(pairing_secret)) = self
.secret_store
.load(self.secret_id, channel_id, SecretKind::PairingSecret)
.await?
else {
return Ok(None);
};
let pairing_secret = pairing_secret.to_secret()?;
let events = handlers::handle_pairing(
&mut borrow_stores!(self),
&local!(self),
&pairing_config!(self),
message,
channel_id,
&pairing_secret,
)
.await?;
Ok(Some(events))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::derec_message;
use crate::derec_message::DeRecMessageBuilder;
use crate::protocol::builder::DeRecProtocolBuilder;
use crate::protocol::tests::{
InMemChannelStore, InMemSecretStore, InMemShareStore, InMemStateStore,
InMemUserSecretStore, RecordingTransport, run_async,
};
use crate::protocol::types::ReplicaMember;
use crate::protocol::types::ReplicaRole;
use crate::protocol::types::{ChannelRecord, ChannelStatus, HelperChannel, SecretValue};
use crate::types::ReplicaId;
use crate::types::SharedKey;
use derec_proto::MessageBody;
use derec_proto::Protocol;
use derec_proto::SenderKind;
use derec_proto::StatusEnum;
use derec_proto::StoreShareRequestMessage;
use derec_proto::TransportProtocol;
use derec_proto::VerifyShareRequestMessage;
use std::collections::HashMap;
const SECRET_ID: u64 = 0xAF;
const CHANNEL: ChannelId = ChannelId(808);
const SHARED_KEY: SharedKey = [9u8; 32];
fn endpoint() -> TransportProtocol {
TransportProtocol {
uri: "https://peer.example.com/derec".to_owned(),
protocol: Protocol::Https as i32,
}
}
/// A paired channel with the peer in the **Helper** role, so an inbound
/// request that requires an `Owner` peer fails the role gate. That is a
/// post-decrypt failure: authenticated, and therefore answerable.
async fn seed(channels: &mut InMemChannelStore, secrets: &mut InMemSecretStore) {
channels
.save(
SECRET_ID,
ChannelRecord::Helper(HelperChannel {
channel_id: CHANNEL,
transports: vec![endpoint()],
communication_info: HashMap::new(),
status: ChannelStatus::Paired,
created_at: now_secs(),
peer_role: SenderKind::Helper,
weight: 1,
}),
)
.await
.expect("seed channel");
secrets
.save(SECRET_ID, CHANNEL, SecretValue::SharedKey(SHARED_KEY))
.await
.expect("seed shared key");
}
fn build(
channels: InMemChannelStore,
secrets: InMemSecretStore,
transport: RecordingTransport,
auto_respond: bool,
) -> DeRecProtocol<
InMemChannelStore,
InMemShareStore,
InMemSecretStore,
InMemUserSecretStore,
InMemStateStore,
RecordingTransport,
> {
DeRecProtocolBuilder::new(SECRET_ID)
.with_channel_store(channels)
.with_share_store(InMemShareStore::default())
.with_secret_store(secrets)
.with_user_secret_store(InMemUserSecretStore::default())
.with_transport(transport)
.with_state_store(InMemStateStore)
.with_own_transports(["https://owner.example.com"])
.with_threshold(2)
.with_auto_respond_on_failure(auto_respond)
.build()
.expect("test protocol builds")
}
/// An inbound `VerifyShareResponse` from a peer recorded as a Helper
/// fails the role gate — a response is only legal from a Helper, so the
/// request direction is what this channel refuses.
fn failing_request() -> Vec<u8> {
let timestamp = derec_message::current_timestamp();
DeRecMessageBuilder::channel()
.channel_id(CHANNEL)
.timestamp(timestamp)
.message_body(MessageBody::VerifyShareRequest(VerifyShareRequestMessage {
secret_id: SECRET_ID,
version: 1,
nonce: 7,
timestamp: Some(timestamp),
reply_to_transports: Vec::new(),
}))
.encrypt(&SHARED_KEY)
.expect("encrypt")
.build()
.expect("build")
.encode_to_vec()
}
/// Disabled is the default: nothing is sent, and the caller gets the
/// failure as a `RequestFailed` carrying the token that answers it.
#[test]
fn disabled_sends_nothing_and_hands_over_a_token() {
run_async(async {
let channels = InMemChannelStore::default();
let secrets = InMemSecretStore::default();
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed(&mut c, &mut s).await;
let mut protocol = build(channels, secrets, transport.clone(), false);
let events = protocol
.process(&failing_request())
.await
.expect("an answerable failure is reported, not raised");
assert!(
transport.sent_envelopes().is_empty(),
"nothing may go on the wire when the setting is off"
);
let token = events
.into_iter()
.find_map(|e| match e {
DeRecEvent::RequestFailed {
responded: false,
action: Some(action),
..
} => Some(action),
_ => None,
})
.expect("a RequestFailed carrying a token");
protocol
.reject(token, StatusEnum::Rejected, "declined")
.await
.expect("the token answers the request");
assert_eq!(transport.sent_envelopes().len(), 1, "exactly one response");
});
}
/// Enabled tells the peer, and reports the failure as answered.
#[test]
fn enabled_sends_a_failure_response_and_reports_it() {
run_async(async {
let channels = InMemChannelStore::default();
let secrets = InMemSecretStore::default();
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed(&mut c, &mut s).await;
let mut protocol = build(channels, secrets, transport.clone(), true);
let events = protocol
.process(&failing_request())
.await
.expect("an answerable failure is reported, not raised");
assert!(
events.iter().any(|e| matches!(
e,
DeRecEvent::RequestFailed {
responded: true,
action: None,
..
}
)),
"{events:?}"
);
let sent = transport.sent_envelopes();
assert_eq!(sent.len(), 1, "exactly one failure response");
// It is a real, decryptable VerifyShareResponse carrying the
// role-mismatch status — not merely "some bytes were sent".
let envelope =
DeRecMessage::decode(sent[0].as_slice()).expect("the response is a DeRec envelope");
let inner = derec_message::extract_inner_message(&envelope.message, &SHARED_KEY)
.expect("the peer can decrypt it under the channel key");
let MessageBody::VerifyShareResponse(response) = inner else {
panic!("expected a VerifyShareResponse, got {inner:?}");
};
let status = response.result.expect("a result is present").status;
assert_eq!(status, StatusEnum::Rejected as i32);
});
}
/// A message that fails to decrypt is not authenticated, so nothing is
/// sent back even with the setting on — otherwise anyone able to reach
/// this device could use it as an oracle.
#[test]
fn an_undecryptable_message_is_never_answered() {
run_async(async {
let channels = InMemChannelStore::default();
let secrets = InMemSecretStore::default();
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed(&mut c, &mut s).await;
// Right channel, wrong key: decryption fails before dispatch.
let timestamp = derec_message::current_timestamp();
let envelope = DeRecMessageBuilder::channel()
.channel_id(CHANNEL)
.timestamp(timestamp)
.message_body(MessageBody::VerifyShareRequest(VerifyShareRequestMessage {
secret_id: SECRET_ID,
version: 1,
nonce: 7,
timestamp: Some(timestamp),
reply_to_transports: Vec::new(),
}))
.encrypt(&[1u8; 32])
.expect("encrypt")
.build()
.expect("build")
.encode_to_vec();
let mut protocol = build(channels, secrets, transport.clone(), true);
let result = protocol.process(&envelope).await;
assert!(result.is_err(), "an undecryptable message still errors");
assert!(
transport.sent_envelopes().is_empty(),
"an unauthenticated message must never be answered"
);
});
}
const PAIRING_CHANNEL: ChannelId = ChannelId(4040);
const GROUP_CHANNEL: ChannelId = ChannelId(5050);
const SOURCE_ID: u64 = 0x5_0C;
const DESTINATION_ID: u64 = 0xDE_57;
const TRACE: u64 = 0x7_7ACE;
async fn seed_member(
channels: &mut InMemChannelStore,
channel_id: ChannelId,
replica_id: u64,
role: ReplicaRole,
status: ChannelStatus,
) {
channels
.save(
SECRET_ID,
ChannelRecord::Replica(ReplicaMember {
channel_id,
replica_id: ReplicaId(replica_id),
transports: vec![endpoint()],
communication_info: HashMap::new(),
role,
status,
created_at: now_secs(),
handover_channel: None,
}),
)
.await
.expect("seed replica member");
}
async fn seed_unconfirmed_destination(
channels: &mut InMemChannelStore,
secrets: &mut InMemSecretStore,
) {
use crate::protocol::types::ReplicaRole;
seed_member(
channels,
PAIRING_CHANNEL,
DESTINATION_ID,
ReplicaRole::Destination,
ChannelStatus::Pending,
)
.await;
seed_member(
channels,
PAIRING_CHANNEL,
SOURCE_ID,
ReplicaRole::Source,
ChannelStatus::Pending,
)
.await;
secrets
.save(
SECRET_ID,
PAIRING_CHANNEL,
SecretValue::SharedKey(SHARED_KEY),
)
.await
.expect("seed pairing key");
}
fn replica_push(channel_id: ChannelId, sent_at: prost_types::Timestamp) -> Vec<u8> {
DeRecMessageBuilder::channel()
.channel_id(channel_id)
.timestamp(sent_at)
.trace_id(TRACE)
.message_body(MessageBody::StoreShareRequest(StoreShareRequestMessage {
secret_id: SECRET_ID,
version: 1,
timestamp: Some(sent_at),
replica_id: Some(SOURCE_ID),
..Default::default()
}))
.encrypt(&SHARED_KEY)
.expect("encrypt")
.build()
.expect("build")
.encode_to_vec()
}
fn build_with(
channels: InMemChannelStore,
secrets: InMemSecretStore,
user_secrets: InMemUserSecretStore,
transport: RecordingTransport,
) -> DeRecProtocol<
InMemChannelStore,
InMemShareStore,
InMemSecretStore,
InMemUserSecretStore,
InMemStateStore,
RecordingTransport,
> {
DeRecProtocolBuilder::new(SECRET_ID)
.with_channel_store(channels)
.with_share_store(InMemShareStore::default())
.with_secret_store(secrets)
.with_user_secret_store(user_secrets)
.with_transport(transport)
.with_state_store(InMemStateStore)
.with_own_transports(["https://destination.example.com"])
.with_threshold(2)
.with_replica_id(DESTINATION_ID)
.build()
.expect("test protocol builds")
}
fn ignored(reason: IgnoreReason, channel_id: ChannelId) -> impl Fn(&DeRecEvent) -> bool {
move |e| {
matches!(e, DeRecEvent::MessageIgnored { channel_id: c, reason: r, trace_id }
if *c == channel_id && *r == reason && *trace_id == TRACE)
}
}
#[test]
fn an_unconfirmed_replica_destination_ignores_the_mirror_push() {
run_async(async {
let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
let user_secrets = InMemUserSecretStore::default();
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed_unconfirmed_destination(&mut c, &mut s).await;
let members_before = channels.member_rows.lock().unwrap().len();
let secrets_before = secrets.data.lock().unwrap().len();
let mut protocol = build_with(
channels.clone(),
secrets.clone(),
user_secrets.clone(),
transport.clone(),
);
let events = protocol
.process(&replica_push(
PAIRING_CHANNEL,
derec_message::current_timestamp(),
))
.await
.expect("an ignored message is not an error");
assert!(
events
.iter()
.any(ignored(IgnoreReason::PendingVerification, PAIRING_CHANNEL)),
"expected MessageIgnored(PendingVerification) on the pairing channel, got {events:?}"
);
assert!(
!events.iter().any(|e| matches!(
e,
DeRecEvent::ReplicaSecretInstalled { .. }
| DeRecEvent::ReplicaSecretReceived { .. }
)),
"the copy must not be installed: {events:?}"
);
assert!(
transport.sent_envelopes().is_empty(),
"nothing may be acknowledged"
);
assert!(
user_secrets.data.lock().unwrap().is_empty(),
"no snapshot may be written"
);
assert_eq!(
channels.member_rows.lock().unwrap().len(),
members_before,
"no member may be added"
);
assert!(
channels
.member_rows
.lock()
.unwrap()
.values()
.all(|m| m.status == ChannelStatus::Pending),
"no member may be promoted"
);
assert!(
channels.helper_rows.lock().unwrap().is_empty(),
"no helper may be written"
);
assert_eq!(
secrets.data.lock().unwrap().len(),
secrets_before,
"no key may be written or dropped"
);
});
}
#[test]
fn a_pending_helper_channel_ignores_inbound_traffic() {
run_async(async {
let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed(&mut c, &mut s).await;
channels
.helper_rows
.lock()
.unwrap()
.values_mut()
.for_each(|h| h.status = ChannelStatus::Pending);
let mut protocol = build(channels, secrets, transport.clone(), true);
let events = protocol
.process(&failing_request())
.await
.expect("ignored, not failed");
assert!(
events.iter().any(|e| matches!(
e,
DeRecEvent::MessageIgnored {
channel_id,
reason: IgnoreReason::PendingVerification,
..
} if *channel_id == CHANNEL
)),
"expected MessageIgnored(PendingVerification), got {events:?}"
);
assert!(
transport.sent_envelopes().is_empty(),
"a pending channel is answered with nothing, not even a failure status"
);
});
}
#[test]
fn a_pending_pairing_does_not_gate_the_group_channel() {
run_async(async {
use crate::protocol::types::ReplicaRole;
let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed_unconfirmed_destination(&mut c, &mut s).await;
seed_member(
&mut c,
GROUP_CHANNEL,
0x07_4E4,
ReplicaRole::Source,
ChannelStatus::Paired,
)
.await;
s.save(SECRET_ID, GROUP_CHANNEL, SecretValue::SharedKey(SHARED_KEY))
.await
.expect("seed group key");
let mut protocol = build_with(
channels,
secrets,
InMemUserSecretStore::default(),
transport,
);
let outcome = protocol
.process(&replica_push(
GROUP_CHANNEL,
derec_message::current_timestamp(),
))
.await;
let gated = outcome.as_ref().is_ok_and(|events| {
events
.iter()
.any(|e| matches!(e, DeRecEvent::MessageIgnored { .. }))
});
assert!(
!gated,
"group traffic must not be gated by another channel's pairing: {outcome:?}"
);
});
}
#[test]
fn an_expired_message_is_reported_as_ignored() {
run_async(async {
let (channels, secrets) = (InMemChannelStore::default(), InMemSecretStore::default());
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed_unconfirmed_destination(&mut c, &mut s).await;
let mut protocol = build_with(
channels,
secrets,
InMemUserSecretStore::default(),
transport.clone(),
);
let stale = prost_types::Timestamp {
seconds: now_secs() as i64 - 24 * 60 * 60,
nanos: 0,
};
let events = protocol
.process(&replica_push(PAIRING_CHANNEL, stale))
.await
.expect("an expired message is not an error");
assert!(
events
.iter()
.any(ignored(IgnoreReason::Expired, PAIRING_CHANNEL)),
"expected MessageIgnored(Expired), got {events:?}"
);
assert!(transport.sent_envelopes().is_empty());
});
}
mod minimum_protocol_version {
use super::*;
use crate::derec_message::apply_trace_id;
use crate::primitives::pairing::request::{create_contact, produce_pre_pair_request};
use derec_proto::ContactMode;
fn with_floor(
channels: InMemChannelStore,
secrets: InMemSecretStore,
transport: RecordingTransport,
floor: ProtocolVersion,
) -> DeRecProtocol<
InMemChannelStore,
InMemShareStore,
InMemSecretStore,
InMemUserSecretStore,
InMemStateStore,
RecordingTransport,
> {
DeRecProtocolBuilder::new(SECRET_ID)
.with_channel_store(channels)
.with_share_store(InMemShareStore::default())
.with_secret_store(secrets)
.with_user_secret_store(InMemUserSecretStore::default())
.with_transport(transport)
.with_state_store(InMemStateStore)
.with_own_transports(["https://owner.example.com"])
.with_threshold(2)
.with_min_protocol_version(floor)
.build()
.expect("test protocol builds")
}
const OLDEST: ProtocolVersion = ProtocolVersion { major: 0, minor: 0 };
const CUTOFF: ProtocolVersion = ProtocolVersion { major: 0, minor: 1 };
/// `bytes` relabelled as version 0.0, the version an earlier release
/// writes.
fn as_version_0_0(bytes: &[u8]) -> Vec<u8> {
let mut envelope = DeRecMessage::decode(bytes).expect("envelope");
envelope.protocol_version_minor = 0;
envelope.encode_to_vec()
}
/// The request [`failing_request`] builds, as an earlier release
/// writes it: no version in the payload, version 0.0 on the envelope.
fn from_an_earlier_release() -> Vec<u8> {
let timestamp = derec_message::current_timestamp();
let body = MessageBody::VerifyShareRequest(VerifyShareRequestMessage {
secret_id: SECRET_ID,
version: 1,
nonce: 7,
timestamp: Some(timestamp),
reply_to_transports: Vec::new(),
});
DeRecMessage {
protocol_version_major: 0,
protocol_version_minor: 0,
sequence: 0,
channel_id: CHANNEL.0,
timestamp: Some(timestamp),
message: derec_cryptography::channel::encrypt_message(
&body.encode_to_vec(),
&SHARED_KEY,
&[1u8; 32],
)
.expect("encrypt"),
trace_id: 0,
}
.encode_to_vec()
}
fn ignored_as_mismatch(events: &[DeRecEvent]) -> bool {
matches!(
events,
[DeRecEvent::MessageIgnored {
reason: IgnoreReason::ProtocolVersionMismatch,
..
}]
)
}
async fn run(floor: ProtocolVersion, bytes: &[u8]) -> Result<Vec<DeRecEvent>> {
let channels = InMemChannelStore::default();
let secrets = InMemSecretStore::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed(&mut c, &mut s).await;
with_floor(channels, secrets, RecordingTransport::default(), floor)
.process(bytes)
.await
.map_err(|e| e.source)
}
fn processed(events: &[DeRecEvent]) -> bool {
events
.iter()
.any(|e| matches!(e, DeRecEvent::RequestFailed { .. }))
}
fn ignored_as_too_old(events: &[DeRecEvent]) -> bool {
matches!(
events,
[DeRecEvent::MessageIgnored {
reason: IgnoreReason::ProtocolVersionTooOld,
..
}]
)
}
#[test]
fn a_current_message_is_accepted_at_either_floor() {
run_async(async {
for floor in [OLDEST, CUTOFF] {
let events = run(floor, &failing_request()).await.expect("processed");
assert!(processed(&events), "{floor:?}: {events:?}");
}
});
}
#[test]
fn a_message_from_an_earlier_version_is_accepted_by_default() {
run_async(async {
let events = run(OLDEST, &from_an_earlier_release())
.await
.expect("processed");
assert!(processed(&events), "{events:?}");
});
}
#[test]
fn a_message_below_the_floor_is_ignored_unread() {
run_async(async {
let channels = InMemChannelStore::default();
let secrets = InMemSecretStore::default();
let transport = RecordingTransport::default();
let (mut c, mut s) = (channels.clone(), secrets.clone());
seed(&mut c, &mut s).await;
let events = with_floor(channels, secrets, transport.clone(), CUTOFF)
.process(&from_an_earlier_release())
.await
.expect("an ignored message is an outcome");
assert!(ignored_as_too_old(&events), "{events:?}");
assert!(transport.sent_envelopes().is_empty());
});
}
/// A current message relabelled 0.0 carries 0.1 in its encrypted
/// payload: it is ignored as a mismatch, or as too old once the floor
/// is raised, which is checked first.
#[test]
fn a_message_relabelled_to_an_earlier_version_is_ignored() {
run_async(async {
let relabelled = as_version_0_0(&failing_request());
let events = run(OLDEST, &relabelled).await.expect("ignored");
assert!(ignored_as_mismatch(&events), "{events:?}");
let events = run(CUTOFF, &relabelled).await.expect("ignored");
assert!(ignored_as_too_old(&events), "{events:?}");
});
}
/// A message from an earlier release relabelled 0.1 to pass the floor
/// carries no version in its payload, which reads as 0.0.
#[test]
fn an_earlier_message_relabelled_to_the_current_version_is_ignored() {
run_async(async {
let mut envelope =
DeRecMessage::decode(from_an_earlier_release().as_slice()).expect("envelope");
envelope.protocol_version_minor = 1;
let events = run(CUTOFF, &envelope.encode_to_vec())
.await
.expect("ignored");
assert!(ignored_as_mismatch(&events), "{events:?}");
});
}
/// `traceId` is re-stamped after sealing, by the library and by
/// applications, so it is not covered.
#[test]
fn a_trace_id_stamped_after_sealing_is_accepted() {
run_async(async {
let bytes = apply_trace_id(&failing_request(), 0xFEED).expect("restamp");
let events = run(OLDEST, &bytes).await.expect("processed");
assert!(processed(&events), "{events:?}");
});
}
/// The floor is checked before routing, so it covers pairing
/// messages too.
#[test]
fn a_pairing_message_below_the_floor_is_ignored() {
run_async(async {
let contact = create_contact(
ChannelId(9_999),
ContactMode::NoKeys,
vec![endpoint()],
None,
)
.expect("contact")
.contact_message;
let pre_pair = produce_pre_pair_request(vec![endpoint()], &contact)
.expect("pre-pair")
.envelope;
let events = run(CUTOFF, &as_version_0_0(&pre_pair))
.await
.expect("ignored");
assert!(ignored_as_too_old(&events), "{events:?}");
});
}
}
}