use parking_lot::Mutex;
use std::convert::Infallible;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct SplitMix64 {
state: u64,
}
impl SplitMix64 {
#[must_use]
pub const fn new(seed: u64) -> Self {
Self { state: seed }
}
pub fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
pub enum Seed {
Value(u64),
#[cfg_attr(feature = "schema", schemars(skip))]
Unrecognized(serde_json::Value),
}
impl Seed {
#[must_use]
pub fn value(&self) -> Option<u64> {
match self {
Self::Value(seed) => Some(*seed),
Self::Unrecognized(_) => None,
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
pub enum RngKey {
Parts(Vec<String>),
#[cfg_attr(feature = "schema", schemars(skip))]
Unrecognized(serde_json::Value),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyPart {
Header(String),
Query(String),
Method,
Path,
Body(String),
}
fn valid_pointer(pointer: &str) -> bool {
if pointer.is_empty() {
return true;
}
if !pointer.starts_with('/') {
return false;
}
let bytes = pointer.as_bytes();
bytes
.iter()
.enumerate()
.all(|(i, b)| *b != b'~' || matches!(bytes.get(i + 1), Some(b'0' | b'1')))
}
impl RngKey {
pub fn parts(&self) -> Result<Vec<KeyPart>, String> {
let Self::Parts(parts) = self else {
return Err("_rift.scriptEngine.rngKey must be a list of strings".into());
};
if parts.is_empty() {
return Err("_rift.scriptEngine.rngKey must name at least one part".into());
}
parts
.iter()
.map(|part| {
let parsed = if part == "method" {
Some(KeyPart::Method)
} else if part == "path" {
Some(KeyPart::Path)
} else if let Some(name) = part.strip_prefix("header:").filter(|n| !n.is_empty()) {
Some(KeyPart::Header(name.to_ascii_lowercase()))
} else if let Some(name) = part.strip_prefix("query:").filter(|n| !n.is_empty()) {
Some(KeyPart::Query(name.to_owned()))
} else if let Some(pointer) = part.strip_prefix("body:") {
if !valid_pointer(pointer) {
return Err(format!(
"_rift.scriptEngine.rngKey part {part:?}: not an RFC 6901 pointer"
));
}
Some(KeyPart::Body(pointer.to_owned()))
} else {
None
};
parsed.ok_or_else(|| {
format!(
"_rift.scriptEngine.rngKey part {part:?} is not one of header:<name>, \
query:<name>, method, path, body:<pointer>"
)
})
})
.collect()
}
}
pub fn check_rng_key(seed: Option<&Seed>, rng_key: Option<&RngKey>) -> Result<(), String> {
let Some(key) = rng_key else {
return Ok(());
};
key.parts()?;
if seed.and_then(Seed::value).is_none() {
return Err(
"_rift.scriptEngine.rngKey needs _rift.scriptEngine.seed: without a seed it changes nothing"
.into(),
);
}
Ok(())
}
pub struct KeySource<'a> {
pub method: &'a str,
pub path: &'a str,
pub header: &'a dyn Fn(&str) -> Option<String>,
pub query: &'a dyn Fn(&str) -> Option<String>,
pub body: Option<&'a str>,
}
#[must_use]
pub fn key_digest(parts: &[KeyPart], request: &KeySource<'_>) -> Option<u64> {
let body_json: Option<serde_json::Value> = request
.body
.and_then(|b| serde_json::from_str(b).ok());
let values: Vec<String> = parts
.iter()
.map(|part| match part {
KeyPart::Method => request.method.to_owned(),
KeyPart::Path => request.path.to_owned(),
KeyPart::Header(name) => (request.header)(name).unwrap_or_default(),
KeyPart::Query(name) => (request.query)(name).unwrap_or_default(),
KeyPart::Body(pointer) => body_json
.as_ref()
.and_then(|json| json.pointer(pointer))
.map(|v| match v {
serde_json::Value::String(s) => s.clone(),
other => other.to_string(),
})
.unwrap_or_default(),
})
.collect();
if values.iter().all(String::is_empty) {
return None;
}
let mut hasher = crate::imposter::reconcile::Fnv1a::default();
for value in &values {
hasher.update(&(value.len() as u64).to_le_bytes());
hasher.update(value.as_bytes());
}
Some(hasher.finish())
}
const fn mix(mut z: u64) -> u64 {
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
#[must_use]
pub const fn keyed_state(seed: u64, digest: u64, occurrence: u32) -> u64 {
mix(seed ^ mix(digest) ^ mix(occurrence as u64 + 1))
}
#[derive(Debug)]
pub struct ImposterRng {
seed: Option<u64>,
stream: Mutex<SplitMix64>,
occurrences: Mutex<std::collections::HashMap<u64, u32>>,
}
fn entropy() -> u64 {
rand::random()
}
impl ImposterRng {
#[must_use]
pub fn new(seed: Option<u64>) -> Self {
Self {
seed,
stream: Mutex::new(SplitMix64::new(seed.unwrap_or_else(entropy))),
occurrences: Mutex::new(std::collections::HashMap::new()),
}
}
pub fn next_occurrence(&self, digest: u64) -> u32 {
let mut occurrences = self.occurrences.lock();
let slot = occurrences.entry(digest).or_insert(0);
let occurrence = *slot;
*slot = slot.saturating_add(1);
occurrence
}
#[must_use]
pub fn seed(&self) -> Option<u64> {
self.seed
}
pub fn reseed(&self) {
*self.stream.lock() = SplitMix64::new(self.seed.unwrap_or_else(entropy));
self.occurrences.lock().clear();
}
fn next_u64(&self) -> u64 {
self.stream.lock().next_u64()
}
}
#[derive(Debug, Clone)]
pub struct RequestRng(Arc<arc_swap::ArcSwap<ImposterRng>>);
impl RequestRng {
#[must_use]
pub fn new(imposter: Arc<ImposterRng>) -> Self {
Self(Arc::new(arc_swap::ArcSwap::new(imposter)))
}
#[must_use]
pub fn unseeded() -> Self {
Self::new(Arc::new(ImposterRng::new(None)))
}
pub fn rekey(&self, stream: ImposterRng) {
self.0.store(Arc::new(stream));
}
pub fn follow(&self, other: &Self) {
self.0.store(other.0.load_full());
}
pub fn seed_u128(&mut self) -> u128 {
(u128::from(self.next()) << 64) | u128::from(self.next())
}
fn next(&self) -> u64 {
self.0.load().next_u64()
}
}
impl rand::TryRng for RequestRng {
type Error = Infallible;
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
Ok((self.next() >> 32) as u32)
}
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
Ok(self.next())
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
for chunk in dst.chunks_mut(8) {
let bytes = self.next().to_le_bytes();
chunk.copy_from_slice(&bytes[..chunk.len()]);
}
Ok(())
}
}
tokio::task_local! {
static REQUEST_RNG: RequestRng;
}
std::thread_local! {
static THREAD_RNG: std::cell::RefCell<Option<RequestRng>> = const { std::cell::RefCell::new(None) };
}
#[must_use]
pub fn current() -> Option<RequestRng> {
REQUEST_RNG
.try_with(Clone::clone)
.ok()
.or_else(|| THREAD_RNG.with(|slot| slot.borrow().clone()))
}
pub async fn with_request_rng<F: std::future::Future>(rng: RequestRng, fut: F) -> F::Output {
REQUEST_RNG.scope(rng, fut).await
}
pub fn carry_request_rng<T>(rng: Option<RequestRng>, f: impl FnOnce() -> T) -> T {
struct Restore(Option<RequestRng>);
impl Drop for Restore {
fn drop(&mut self) {
let previous = self.0.take();
THREAD_RNG.with(|slot| *slot.borrow_mut() = previous);
}
}
let _restore = Restore(THREAD_RNG.with(|slot| slot.replace(rng)));
f()
}
#[cfg(test)]
mod tests {
use super::*;
use rand::{RngExt, TryRng};
#[test]
fn splitmix64_matches_the_reference_vectors() {
let mut s = SplitMix64::new(0);
assert_eq!(s.next_u64(), 0xE220_A839_7B1D_CDAF);
assert_eq!(s.next_u64(), 0x6E78_9E6A_A1B9_65F4);
assert_eq!(s.next_u64(), 0x06C4_5D18_8009_454F);
}
#[test]
fn a_seed_repeats_after_reseed_and_entropy_does_not() {
let seeded = Arc::new(ImposterRng::new(Some(42)));
let mut rng = RequestRng::new(Arc::clone(&seeded));
let first: Vec<u64> = (0..4).map(|_| rng.try_next_u64().unwrap_or(0)).collect();
seeded.reseed();
let again: Vec<u64> = (0..4).map(|_| rng.try_next_u64().unwrap_or(0)).collect();
assert_eq!(first, again);
let mut other = RequestRng::new(Arc::new(ImposterRng::new(Some(43))));
assert_ne!(first[0], other.try_next_u64().unwrap_or(0));
let mut a = RequestRng::unseeded();
let mut b = RequestRng::unseeded();
assert_ne!(a.try_next_u64().ok(), b.try_next_u64().ok());
}
#[test]
fn bytes_and_ranges_draw_from_the_stream() {
let mut a = RequestRng::new(Arc::new(ImposterRng::new(Some(7))));
let mut b = RequestRng::new(Arc::new(ImposterRng::new(Some(7))));
let (mut x, mut y) = ([0u8; 13], [0u8; 13]);
a.try_fill_bytes(&mut x).ok();
b.try_fill_bytes(&mut y).ok();
assert_eq!(x, y);
assert_eq!(a.random_range(0..=1000), b.random_range(0..=1000));
assert_eq!(a.seed_u128(), b.seed_u128());
}
#[test]
fn rng_keys_parse_and_refuse_what_they_cannot_read() {
let key = |v: serde_json::Value| -> RngKey { serde_json::from_value(v).expect("decodes") };
assert_eq!(
key(serde_json::json!([
"header:X-Rift-Rng-Key",
"query:id",
"method",
"path",
"body:/a~1b/0"
]))
.parts()
.expect("parses"),
vec![
KeyPart::Header("x-rift-rng-key".into()),
KeyPart::Query("id".into()),
KeyPart::Method,
KeyPart::Path,
KeyPart::Body("/a~1b/0".into())
]
);
for (bad, why) in [
(serde_json::json!([]), "at least one"),
(serde_json::json!(["cookie:x"]), "is not one of"),
(serde_json::json!(["header:"]), "is not one of"),
(serde_json::json!(["body:a/b"]), "RFC 6901"),
(serde_json::json!(["body:/a~2"]), "RFC 6901"),
(serde_json::json!("header:x"), "list of strings"),
] {
let err = key(bad.clone()).parts().expect_err("refused");
assert!(err.contains(why), "{bad}: {err}");
}
let seed = Seed::Value(1);
let ok = key(serde_json::json!(["path"]));
assert!(check_rng_key(Some(&seed), Some(&ok)).is_ok());
assert!(
check_rng_key(None, Some(&ok))
.expect_err("no seed")
.contains("needs")
);
assert!(check_rng_key(None, None).is_ok());
}
#[test]
fn digests_separate_parts_and_skip_an_empty_key() {
let none = |_: &str| None;
let source = |method: &'static str, path: &'static str| KeySource {
method,
path,
header: &none,
query: &none,
body: None,
};
let two = [KeyPart::Method, KeyPart::Path];
assert_ne!(
key_digest(&two, &source("a", "bc")),
key_digest(&two, &source("ab", "c"))
);
assert_eq!(
key_digest(&two, &source("a", "bc")),
key_digest(&two, &source("a", "bc"))
);
assert_eq!(
key_digest(&[KeyPart::Header("x".into())], &source("", "")),
None
);
let body = KeySource {
body: Some(r#"{"id":"abc","n":7}"#),
..source("", "")
};
assert!(key_digest(&[KeyPart::Body("/id".into())], &body).is_some());
assert_ne!(
key_digest(&[KeyPart::Body("/id".into())], &body),
key_digest(&[KeyPart::Body("/n".into())], &body)
);
assert_ne!(keyed_state(42, 7, 0), keyed_state(42, 7, 1));
assert_ne!(keyed_state(42, 7, 0), keyed_state(43, 7, 0));
}
#[test]
fn a_rekeyed_handle_moves_every_clone() {
let mut a = RequestRng::new(Arc::new(ImposterRng::new(Some(1))));
let mut b = a.clone();
a.rekey(ImposterRng::new(Some(99)));
let mut fresh = RequestRng::new(Arc::new(ImposterRng::new(Some(99))));
assert_eq!(b.try_next_u64().ok(), fresh.try_next_u64().ok());
assert_eq!(a.try_next_u64().ok(), fresh.try_next_u64().ok());
let imposter = ImposterRng::new(Some(1));
assert_eq!(
(
imposter.next_occurrence(5),
imposter.next_occurrence(5),
imposter.next_occurrence(6)
),
(0, 1, 0)
);
}
#[test]
fn a_seed_decodes_tolerantly() {
let seed: Seed = serde_json::from_value(serde_json::json!(42)).expect("decodes");
assert_eq!(seed.value(), Some(42));
for odd in [
serde_json::json!(-1),
serde_json::json!("42"),
serde_json::json!(1.5),
] {
let seed: Seed = serde_json::from_value(odd.clone()).expect("decodes");
assert_eq!(seed.value(), None, "{odd}");
assert_eq!(
serde_json::to_value(&seed).expect("encodes"),
odd,
"kept as written"
);
}
}
#[tokio::test]
async fn the_handle_reaches_its_task_and_a_carried_thread_only() {
assert!(current().is_none());
let rng = RequestRng::new(Arc::new(ImposterRng::new(Some(1))));
let seen = with_request_rng(rng, async {
let carried = current();
let on_thread = tokio::task::spawn_blocking(move || {
carry_request_rng(carried, || current().is_some())
})
.await
.expect("join");
let uncarried = tokio::task::spawn_blocking(|| current().is_some())
.await
.expect("join");
(current().is_some(), on_thread, uncarried)
})
.await;
assert_eq!(seen, (true, true, false));
assert!(current().is_none());
}
}