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
// SPDX-License-Identifier: BUSL-1.1
//! Basic validate() + validate_or_reject() entry points.
use crate::CrdtAuthContext;
use crate::error::{CrdtError, Result};
use crate::policy::PolicyResolution;
use crate::state::CrdtState;
use super::core::Validator;
use super::types::{ProposedChange, ValidationOutcome};
impl Validator {
/// Validate a proposed change against all applicable constraints.
///
/// Returns `Accepted` if all constraints pass, or `Rejected` with
/// detailed violation information.
pub fn validate(&self, state: &CrdtState, change: &ProposedChange) -> ValidationOutcome {
let constraints = self.constraints.for_collection(&change.collection);
let mut violations = Vec::new();
for constraint in constraints {
if let Some(violation) = self.check_constraint(state, change, constraint) {
violations.push(violation);
}
}
if violations.is_empty() {
ValidationOutcome::Accepted
} else {
ValidationOutcome::Rejected(violations)
}
}
/// Validate and apply declarative policy resolution.
///
/// ## Replay protection
///
/// When `auth.delta_signature` is non-zero, the following steps execute
/// in this order to prevent replay attacks at minimum cost:
///
/// 1. **Cheap seq_no check** — `seq_no > last_seen[(user_id, device_id)]`.
/// Fails fast before any HMAC computation.
/// 2. **HMAC verification** — constant-time comparison prevents timing attacks.
/// 3. **Atomic seq update** — `last_seen` advances only on success.
///
/// For accepted changes, returns Ok(()).
/// For violations, applies policy and:
/// - If AutoResolved: returns Ok(())
/// - If Deferred/Webhook/Escalate: returns appropriate error
pub fn validate_or_reject(
&mut self,
state: &CrdtState,
peer_id: u64,
auth: CrdtAuthContext,
change: &ProposedChange,
delta_bytes: Vec<u8>,
) -> Result<()> {
// Check auth expiry: agents that accumulated deltas offline must
// re-authenticate before syncing.
if auth.auth_expires_at > 0 {
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
if now_ms > auth.auth_expires_at {
return Err(CrdtError::AuthExpired {
user_id: auth.user_id,
expired_at: auth.auth_expires_at,
});
}
}
// Replay protection + signature verification (signed path only).
//
// The unsigned path (all-zeros signature) bypasses replay protection.
// Old clients that send device_id=0 / seq_no=0 with a non-zero
// signature will be rejected by the seq_no check (0 is never > 0).
if auth.delta_signature != [0u8; 32]
&& let Some(ref verifier) = self.delta_verifier
{
// Step 1: cheap seq_no check before any HMAC computation.
verifier
.registry()
.check_seq(auth.user_id, auth.device_id, auth.seq_no)?;
// Step 2: constant-time HMAC verification.
verifier.verify(
auth.user_id,
auth.device_id,
auth.seq_no,
&delta_bytes,
&auth.delta_signature,
)?;
// Step 3: advance last_seen atomically on success.
verifier
.registry()
.commit_seq(auth.user_id, auth.device_id, auth.seq_no)?;
}
let hlc_timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
match self.validate_with_policy(state, peer_id, auth, change, delta_bytes, hlc_timestamp)? {
PolicyResolution::AutoResolved(_) => Ok(()),
PolicyResolution::Deferred { .. } => {
// Violation was deferred for retry; return error to signal this
// The deferred entry was already enqueued by validate_with_policy
let violations = match self.validate(state, change) {
ValidationOutcome::Rejected(v) => v,
_ => vec![],
};
if !violations.is_empty() {
let v = &violations[0];
Err(CrdtError::ConstraintViolation {
constraint: v.constraint_name.clone(),
collection: change.collection.clone(),
detail: format!("{} (deferred for retry)", v.reason),
})
} else {
Ok(())
}
}
PolicyResolution::WebhookRequired { .. } => {
// Webhook decision required; return error
let violations = match self.validate(state, change) {
ValidationOutcome::Rejected(v) => v,
_ => vec![],
};
if !violations.is_empty() {
let v = &violations[0];
Err(CrdtError::ConstraintViolation {
constraint: v.constraint_name.clone(),
collection: change.collection.clone(),
detail: format!("{} (webhook required)", v.reason),
})
} else {
Ok(())
}
}
PolicyResolution::Escalate => {
// Already enqueued to DLQ by validate_with_policy
let violations = match self.validate(state, change) {
ValidationOutcome::Rejected(v) => v,
_ => vec![],
};
if !violations.is_empty() {
let v = &violations[0];
Err(CrdtError::ConstraintViolation {
constraint: v.constraint_name.clone(),
collection: change.collection.clone(),
detail: v.reason.clone(),
})
} else {
Ok(())
}
}
}
}
}