Skip to content

Commit c53e289

Browse files
committed
udp: cookie fixups as suggested by Jose
1 parent e3562f0 commit c53e289

File tree

5 files changed

+110
-148
lines changed

5 files changed

+110
-148
lines changed

src/bootstrap/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn check_seed() {
5858
let seed = keys::Current::get_seed();
5959
let instance = keys::Instance::get_seed();
6060

61-
assert_eq!(seed, instance, "maybe using zeroed see in production!?");
61+
assert_eq!(seed, instance, "maybe using zeroed seed in production!?");
6262
}
6363

6464
/// It initializes the application with the given configuration.

src/servers/udp/connection_cookie.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use aquatic_udp_protocol::ConnectionId as Cookie;
8181
use cookie_builder::{assemble, decode, disassemble, encode};
8282
use zerocopy::AsBytes;
8383

84-
use super::error::{self, Error};
84+
use super::error::Error;
8585
use crate::shared::crypto::keys::CipherArrayBlowfish;
8686

8787
/// Generates a new connection cookie.
@@ -106,6 +106,8 @@ pub fn make(fingerprint: u64, issue_at: f64) -> Result<Cookie, Error> {
106106
Ok(zerocopy::FromBytes::read_from(cookie.as_slice()).expect("it should be the same size"))
107107
}
108108

109+
use std::ops::Range;
110+
109111
/// Checks if the supplied `connection_cookie` is valid.
110112
///
111113
/// # Errors
@@ -114,32 +116,32 @@ pub fn make(fingerprint: u64, issue_at: f64) -> Result<Cookie, Error> {
114116
///
115117
/// # Panics
116118
///
117-
/// It would panic if cookie min value is larger than the max value.
118-
pub fn check(cookie: &Cookie, fingerprint: u64, min: f64, max: f64) -> Result<f64, Error> {
119-
assert!(min < max, "min is larger than max");
119+
/// It would panic if the range start is not smaller than it's end.
120+
pub fn check(cookie: &Cookie, fingerprint: u64, valid_range: Range<f64>) -> Result<f64, Error> {
121+
assert!(valid_range.start <= valid_range.end, "range start is larger than range end");
120122

121123
let cookie_bytes = CipherArrayBlowfish::from_slice(cookie.0.as_bytes());
122124
let cookie_bytes = decode(*cookie_bytes);
123125

124126
let issue_time = disassemble(fingerprint, cookie_bytes);
125127

126128
if !issue_time.is_normal() {
127-
return Err(Error::InvalidConnectionId {
128-
bad_id: error::ConnectionCookie(*cookie),
129+
return Err(Error::ConnectionIdNotNormal {
130+
not_normal_value: issue_time,
129131
});
130132
}
131133

132-
if issue_time < min {
134+
if issue_time < valid_range.start {
133135
return Err(Error::ConnectionIdExpired {
134-
bad_age: issue_time,
135-
min_age: min,
136+
expired_value: issue_time,
137+
min_value: valid_range.start,
136138
});
137139
}
138140

139-
if issue_time > max {
141+
if issue_time > valid_range.end {
140142
return Err(Error::ConnectionIdFromFuture {
141-
future_age: issue_time,
142-
max_age: max,
143+
future_value: issue_time,
144+
max_value: valid_range.end,
143145
});
144146
}
145147

@@ -262,7 +264,7 @@ mod tests {
262264
let min = issue_at - 10.0;
263265
let max = issue_at + 10.0;
264266

265-
let result = check(&cookie, fingerprint, min, max).unwrap();
267+
let result = check(&cookie, fingerprint, min..max).unwrap();
266268

267269
// we should have exactly the same bytes returned
268270
assert_eq!(result.to_ne_bytes(), issue_at.to_ne_bytes());
@@ -277,7 +279,7 @@ mod tests {
277279
let min = issue_at + 10.0;
278280
let max = issue_at + 20.0;
279281

280-
let result = check(&cookie, fingerprint, min, max).unwrap_err();
282+
let result = check(&cookie, fingerprint, min..max).unwrap_err();
281283

282284
match result {
283285
Error::ConnectionIdExpired { .. } => {} // Expected error
@@ -295,7 +297,7 @@ mod tests {
295297
let min = issue_at - 20.0;
296298
let max = issue_at - 10.0;
297299

298-
let result = check(&cookie, fingerprint, min, max).unwrap_err();
300+
let result = check(&cookie, fingerprint, min..max).unwrap_err();
299301

300302
match result {
301303
Error::ConnectionIdFromFuture { .. } => {} // Expected error

src/servers/udp/error.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ pub enum Error {
1616
#[error("the issue time should be a normal floating point number")]
1717
InvalidCookieIssueTime { invalid_value: f64 },
1818

19-
#[error("connection id was decoded, but could not be understood")]
20-
InvalidConnectionId { bad_id: ConnectionCookie },
19+
#[error("connection id did not produce a normal value")]
20+
ConnectionIdNotNormal { not_normal_value: f64 },
2121

22-
#[error("connection id was decoded, but was expired (too old)")]
23-
ConnectionIdExpired { bad_age: f64, min_age: f64 },
22+
#[error("connection id produced an expired value")]
23+
ConnectionIdExpired { expired_value: f64, min_value: f64 },
2424

25-
#[error("connection id was decoded, but was invalid (from future)")]
26-
ConnectionIdFromFuture { future_age: f64, max_age: f64 },
25+
#[error("connection id produces a future value")]
26+
ConnectionIdFromFuture { future_value: f64, max_value: f64 },
2727

2828
/// Error returned when the domain tracker returns an error.
2929
#[error("tracker server error: {source}")]

0 commit comments

Comments
 (0)