diff options
Diffstat (limited to 'src/http/auth.rs')
| -rw-r--r-- | src/http/auth.rs | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/http/auth.rs b/src/http/auth.rs index 3d05cab..8c56c1f 100644 --- a/src/http/auth.rs +++ b/src/http/auth.rs @@ -27,20 +27,26 @@ fn sessions() -> &'static Mutex<HashMap<String, bool>> { SESSIONS.get_or_init(|| Mutex::new(HashMap::new())) } -/// Random hex token from the system RNG (`/dev/urandom` through our hash). -fn random_hex() -> String { +/// Random hex token from the system RNG, or `None` when there is no usable one. +/// +/// The old version read `/dev/urandom` into a zeroed buffer, ignored every +/// error, and hashed the result anyway (§6 m-13): a failed read produced a +/// session id derived from a buffer of zeros plus the clock and a counter, +/// which is guessable by anyone who can guess roughly when the process started +/// — and nothing anywhere said the RNG had failed. A session id is the whole +/// credential now (C-1), so "no RNG" is a refusal, not a weaker token. +fn random_hex() -> Option<String> { + use std::io::Read; let mut buf = [0u8; 32]; - if let Ok(mut f) = std::fs::File::open("/dev/urandom") { - use std::io::Read; - let _ = f.read_exact(&mut buf); - } - // Mix in a counter and the clock so two calls in the same instant differ. + let mut f = std::fs::File::open("/dev/urandom").ok()?; + f.read_exact(&mut buf).ok()?; + // Mixed in regardless, so two calls in the same instant still differ. let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0); let salt = format!("{:?}", (buf, nanos, std::process::id(), counter())); - hash::hex(salt.as_bytes()) + Some(hash::hex(salt.as_bytes())) } fn counter() -> u64 { @@ -50,12 +56,12 @@ fn counter() -> u64 { /// Mint an anonymous session: enough for a CSRF token, not enough to see a /// single page when a password is set. -pub fn new_session() -> String { - let sid = random_hex(); +pub fn new_session() -> Option<String> { + let sid = random_hex()?; if let Ok(mut s) = sessions().lock() { s.insert(sid.clone(), false); } - sid + Some(sid) } pub fn is_valid_session(sid: &str) -> bool { |
