From e757979f1a2afdbe7146066345bb9acd19d3b151 Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 17:09:59 +0000 Subject: comments: say why, not what MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every module-level `//!` header is gone, and the comments that remained either re-explained the line below them or had grown into paragraphs. Density went from one comment line per 6.7 lines of code to one per 10. 827 insertions against 2194 deletions across 86 files. What survived: the invariants (I1-I12), the design cross-references into DESIGN.md, and the notes recording a bug that was found and fixed — those are the comments that carry weight, because they answer a question the code cannot answer about itself. What did not: restating a function signature in prose, and narration of the statement underneath. --- src/http/auth.rs | 45 ++++++++++++--------------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) (limited to 'src/http/auth.rs') diff --git a/src/http/auth.rs b/src/http/auth.rs index 8c56c1f..9689fd8 100644 --- a/src/http/auth.rs +++ b/src/http/auth.rs @@ -1,26 +1,11 @@ -//! Session and CSRF (§12: "form posts are plain `
` with a -//! CSRF token"). -//! -//! One operator, one shared credential, no user table (§14). A session is an -//! opaque random id held in memory; the CSRF token is derived from it, so a -//! cross-site form cannot produce one it has not seen, and there is no token -//! table to keep in step with the sessions. -//! -//! **The sid is the credential.** A sid is minted for anonymous visitors too, -//! because the CSRF token has to be derivable before anyone has typed a -//! password; what the password buys is the `authenticated` flag on one sid. -//! Nothing else in the request proves who you are, which is why there is no -//! separate "logged in" cookie to forge. - use std::collections::HashMap; use std::sync::Mutex; use std::sync::OnceLock; use crate::hash; -/// Live sids and whether each has passed the password gate. One map, so the -/// two facts cannot drift apart: dropping a session removes its authentication -/// with it. +/// Live sids and whether each has passed the password gate. One map, so +/// dropping a session removes its authentication with it. static SESSIONS: OnceLock>> = OnceLock::new(); fn sessions() -> &'static Mutex> { @@ -28,19 +13,14 @@ fn sessions() -> &'static Mutex> { } /// 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. +/// A session id is the whole credential (C-1), so "no RNG" is a refusal rather +/// than a weaker, guessable token. fn random_hex() -> Option { use std::io::Read; let mut buf = [0u8; 32]; 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. + // Mixed in 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()) @@ -68,8 +48,7 @@ pub fn is_valid_session(sid: &str) -> bool { sessions().lock().map(|s| s.contains_key(sid)).unwrap_or(false) } -/// Mark a session as having passed the password gate. Only ever called with a -/// sid this process minted, and it cannot mint one: an unknown sid stays +/// Mark a session as having passed the password gate. An unknown sid stays /// unknown rather than being inserted as a side effect of a login attempt. pub fn authenticate(sid: &str) { if let Ok(mut s) = sessions().lock() { @@ -79,8 +58,8 @@ pub fn authenticate(sid: &str) { } } -/// The gate's whole question: is this sid one we issued *and* did someone type -/// the password into a form on this session? +/// The gate's whole question: is this sid one we issued, and did someone type +/// the password into a form on it? pub fn is_authenticated(sid: &str) -> bool { sessions().lock().map(|s| s.get(sid).copied().unwrap_or(false)).unwrap_or(false) } @@ -91,8 +70,8 @@ pub fn drop_session(sid: &str) { } } -/// The CSRF token for a session. Derived rather than stored: it cannot drift -/// out of sync with the session, and it is cheap to recompute per request. +/// The CSRF token for a session. Derived rather than stored, so it cannot drift +/// out of sync with the session. pub fn csrf_token(sid: &str) -> String { hash::hex(format!("csrf|{sid}").as_bytes())[..32].to_string() } @@ -125,8 +104,8 @@ pub fn parse_cookies(header: &str) -> Vec<(String, String)> { .collect() } -/// Check the shared password. Empty password means the UI is open, which is the -// default on loopback; the CSRF gate still applies. +/// An empty password means the UI is open, which is the default on loopback; +/// the CSRF gate still applies. pub fn password_ok(cfg: &crate::config::Config, given: &str) -> bool { cfg.password.is_empty() || constant_time_eq(&cfg.password, given) } -- cgit v1.2.3