Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update Rust core sanity checks

Leendert de Borst0b5b5802d8cd
diff --git a/core/rust/src/identity_generator/mod.rs b/core/rust/src/identity_generator/mod.rs
index 08854e0ea..bfdfa547a 100644
--- a/core/rust/src/identity_generator/mod.rs
+++ b/core/rust/src/identity_generator/mod.rs
@@ -228,10 +228,14 @@ fn random_birth_date<R: RngCore + ?Sized>(
Some(BirthdateOptions {
target_year,
year_deviation,
- }) => (
- first_day_of_year(target_year - year_deviation.abs()),
- last_day_of_year(target_year + year_deviation.abs()),
- ),
+ }) => {
+ // Sanity check for overflows in the calculation.
+ let deviation = year_deviation.saturating_abs();
+ (
+ first_day_of_year(target_year.saturating_sub(deviation)),
+ last_day_of_year(target_year.saturating_add(deviation)),
+ )
+ }
None => (years_ago(today, DEFAULT_MAX_AGE), years_ago(today, DEFAULT_MIN_AGE)),
};
diff --git a/core/rust/src/identity_generator/tests.rs b/core/rust/src/identity_generator/tests.rs
index 76c392b53..1f13089ea 100644
--- a/core/rust/src/identity_generator/tests.rs
+++ b/core/rust/src/identity_generator/tests.rs
@@ -123,6 +123,30 @@ fn birthdate_options_deviation_bounds_the_year_range() {
assert!(distinct_years.len() > 1, "expected varied years within the range");
}
+#[test]
+fn extreme_birthdate_options_do_not_overflow() {
+ // Test with values that would be able to cause overflow in the calculation.
+ let cases = [
+ (i32::MIN, i32::MIN),
+ (i32::MAX, i32::MAX),
+ (i32::MIN, i32::MAX),
+ (0, i32::MIN),
+ (i32::MAX, 1),
+ ];
+
+ for (target_year, year_deviation) in cases {
+ let mut req = request("en");
+ req.birthdate_options = Some(BirthdateOptions {
+ target_year,
+ year_deviation,
+ });
+
+ let identity = generate_from_request(&req);
+ assert!(!identity.birth_date.is_empty());
+ assert!(!identity.first_name.is_empty());
+ }
+}
+
#[test]
fn german_decade_names_follow_the_birth_year() {
for target_year in (1955..=2025).step_by(10) {
diff --git a/core/rust/src/rng.rs b/core/rust/src/rng.rs
index aa8e613ff..154e02cd6 100644
--- a/core/rust/src/rng.rs
+++ b/core/rust/src/rng.rs
@@ -30,7 +30,7 @@ fn parse_seed_hex(hex: &str) -> Option<[u8; 32]> {
}
/// Get an unbiased random index in `0..max` using rejection sampling over the given CSPRNG.
-/// Handles modulo bias by rejecting values above the largest multiple of `max` that fits in a `u32`.
+/// Handles modulo bias by rejecting values above the largest multiple of `max` that fits in a `u64`.
pub(crate) fn unbiased_index<R: RngCore + ?Sized>(rng: &mut R, max: usize) -> usize {
debug_assert!(max > 0, "unbiased_index requires max > 0");
if max <= 1 {
@@ -38,12 +38,52 @@ pub(crate) fn unbiased_index<R: RngCore + ?Sized>(rng: &mut R, max: usize) -> us
}
let max = max as u64;
- let limit = ((1u64 << 32) / max) * max;
+ let rem = max.wrapping_neg() % max;
loop {
- let value = rng.next_u32() as u64;
- if value < limit {
+ let value = rng.next_u64();
+ if rem == 0 || value < rem.wrapping_neg() {
return (value % max) as usize;
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn seeded_rng() -> StdRng {
+ StdRng::from_seed([7u8; 32])
+ }
+
+ #[test]
+ fn unbiased_index_stays_in_range() {
+ let mut rng = seeded_rng();
+ for max in [2usize, 3, 10, 36, 100] {
+ for _ in 0..1000 {
+ assert!(unbiased_index(&mut rng, max) < max);
+ }
+ }
+ }
+
+ #[test]
+ fn unbiased_index_handles_edge_maxes() {
+ let mut rng = seeded_rng();
+ // max <= 1 short-circuits to 0.
+ assert_eq!(unbiased_index(&mut rng, 1), 0);
+
+ // Powers of two divide 2^64 evenly (rem == 0): must accept immediately, never hang.
+ for shift in [1u32, 8, 16] {
+ let max = 1usize << shift;
+ assert!(unbiased_index(&mut rng, max) < max);
+ }
+
+ #[cfg(target_pointer_width = "64")]
+ {
+ assert!(unbiased_index(&mut rng, 1usize << 62) < (1usize << 62));
+ let big = (u32::MAX as usize) + 1;
+ assert!(unbiased_index(&mut rng, big) < big);
+ assert!(unbiased_index(&mut rng, usize::MAX) < usize::MAX);
+ }
+ }
+}