summaryrefslogtreecommitdiffstats
path: root/src/http/auth.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 08:06:39 +0000
committeraur-build <aur-build@localhost>2026-11-18 08:06:39 +0000
commita6b652985f40cf022d6c522415c36fad13bc663d (patch)
tree2383d83a820803808202084db1aa30b12f0e02c2 /src/http/auth.rs
parentab8d33d5edc4c88075685206e3046c719e9bc855 (diff)
downloadaur-build-a6b652985f40cf022d6c522415c36fad13bc663d.tar.gz
aur-build-a6b652985f40cf022d6c522415c36fad13bc663d.zip
http: the review surface — server-rendered, no JavaScript required (§12, §13)
tiny_http, thread-per-connection, askama templates. Every §13 surface is server-rendered HTML that works with scripts switched off: Inbox, review detail with the full panel set, Builds, Attention, Index, Subscriptions, Ops/audit, Settings. The parts that are easy to get wrong: - Handlers read through thread-local READ-ONLY connections. They cannot take the write lock even by accident, so a slow page render never blocks the build pipeline's writer. tests/http.rs asserts this by trying to write through a read connection and requiring failure. - Every POST requires a CSRF token derived from the session id. Logout drops the session, which invalidates every form still open in a browser. - Approve carries identity_hash AND diff_digest (§7.1a). If either has moved, the approval is refused with 409 and the page re-renders the review AS IT IS NOW — never a silent retarget onto the newer state. - Bulk approve refuses high-risk items unless each one is individually confirmed by id. Bulk is per-item, never 'approve everything forever'. - Reject requires a note; it is re-openable, not a deletion. - Optional password gate; anonymous visitors are redirected to /login. Also: the index page states the pacman conf snippet including 'SigLevel = Required DatabaseOptional', because a repo consumers ignore the signature is a repo that gets MITM'd.
Diffstat (limited to 'src/http/auth.rs')
-rw-r--r--src/http/auth.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/http/auth.rs b/src/http/auth.rs
new file mode 100644
index 0000000..9186f0e
--- /dev/null
+++ b/src/http/auth.rs
@@ -0,0 +1,102 @@
+//! Session and CSRF (§12: "form posts are plain `<form method=post>` 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.
+
+use std::collections::HashSet;
+use std::sync::Mutex;
+use std::sync::OnceLock;
+
+use crate::hash;
+
+static SESSIONS: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
+
+fn sessions() -> &'static Mutex<HashSet<String>> {
+ SESSIONS.get_or_init(|| Mutex::new(HashSet::new()))
+}
+
+/// Random hex token from the system RNG (`/dev/urandom` through our hash).
+fn random_hex() -> String {
+ 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 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())
+}
+
+fn counter() -> u64 {
+ static C: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+ C.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+}
+
+pub fn new_session() -> String {
+ let sid = random_hex();
+ if let Ok(mut s) = sessions().lock() {
+ s.insert(sid.clone());
+ }
+ sid
+}
+
+pub fn is_valid_session(sid: &str) -> bool {
+ sessions().lock().map(|s| s.contains(sid)).unwrap_or(false)
+}
+
+pub fn drop_session(sid: &str) {
+ if let Ok(mut s) = sessions().lock() {
+ s.remove(sid);
+ }
+}
+
+/// 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.
+pub fn csrf_token(sid: &str) -> String {
+ hash::hex(format!("csrf|{sid}").as_bytes())[..32].to_string()
+}
+
+pub fn csrf_ok(sid: &str, token: &str) -> bool {
+ !token.is_empty() && is_valid_session(sid) && constant_time_eq(&csrf_token(sid), token)
+}
+
+fn constant_time_eq(a: &str, b: &str) -> bool {
+ let (a, b) = (a.as_bytes(), b.as_bytes());
+ if a.len() != b.len() {
+ return false;
+ }
+ let mut diff = 0u8;
+ for (x, y) in a.iter().zip(b.iter()) {
+ diff |= x ^ y;
+ }
+ diff == 0
+}
+
+/// Parse a `Cookie:` header value.
+pub fn parse_cookies(header: &str) -> Vec<(String, String)> {
+ header
+ .split(';')
+ .filter_map(|p| {
+ let p = p.trim();
+ let (k, v) = p.split_once('=')?;
+ Some((k.trim().to_string(), v.trim().to_string()))
+ })
+ .collect()
+}
+
+/// Check the shared password. 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)
+}
+
+pub fn auth_required(cfg: &crate::config::Config) -> bool {
+ !cfg.password.is_empty()
+}