From a6b652985f40cf022d6c522415c36fad13bc663d Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 08:06:39 +0000 Subject: =?UTF-8?q?http:=20the=20review=20surface=20=E2=80=94=20server-ren?= =?UTF-8?q?dered,=20no=20JavaScript=20required=20(=C2=A712,=20=C2=A713)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- tests/http.rs | 369 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 369 insertions(+) create mode 100644 tests/http.rs (limited to 'tests/http.rs') diff --git a/tests/http.rs b/tests/http.rs new file mode 100644 index 0000000..20dcbcb --- /dev/null +++ b/tests/http.rs @@ -0,0 +1,369 @@ +//! The web surface (§12, §13): routes render, the CSRF gate holds, and a stale +//! approval comes back as a 409 with the current diff instead of a silent +//! retarget. +//! +//! These drive a real `tiny_http` server on an ephemeral loopback port over a +//! real fixture corpus — no mocked transport, because the transport is part of +//! what we are claiming works. + +mod common; + +use aur_build::db::model::*; +use aur_build::http::{self, Server}; +use aur_build::review::{candidates, diff}; +use common::*; + +struct Web { + env: Env, + fx: Fixture, + #[allow(dead_code)] + server: Server, + cookie: String, + csrf: String, +} + +struct Page { + status: u16, + body: String, +} + +impl Web { + fn new(name: &str) -> Self { + Self::with_password(name, "") + } + + fn with_password(name: &str, password: &str) -> Self { + let (mut env, fx) = env_with_fixture(name); + env.cfg.bind = "127.0.0.1:0".into(); + env.cfg.password = password.into(); + let server = http::spawn(env.cfg.clone(), env.db.clone()).unwrap(); + let mut web = Self { env, fx, server, cookie: String::new(), csrf: String::new() }; + // First contact mints the session cookie and the CSRF token. + let first = web.raw_get("/"); + web.cookie = first + .0 + .iter() + .find(|(k, _)| k == "set-cookie") + .and_then(|(_, v)| v.split(';').next().map(|s| s.to_string())) + .unwrap_or_default(); + let body = web.get("/"); + web.csrf = extract(&body.body, "name=\"csrf\" content=\"", "\"").unwrap_or_default(); + web + } + + fn url(&self, path: &str) -> String { + format!("http://{}{}", self.server.addr, path) + } + + fn raw_get(&self, path: &str) -> (Vec<(String, String)>, String) { + let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); + let r = match resp { + Ok(r) => r, + Err(ureq::Error::Status(_, r)) => r, + Err(e) => panic!("GET {path}: {e}"), + }; + let cookie = r.header("set-cookie").map(|s| s.to_string()).unwrap_or_default(); + (vec![("set-cookie".to_string(), cookie)], r.into_string().unwrap_or_default()) + } + + fn get(&self, path: &str) -> Page { + let resp = ureq::get(&self.url(path)).set("Cookie", &self.cookie).call(); + self.finish(resp, path) + } + + fn post(&self, path: &str, form: &[(&str, &str)]) -> Page { + let resp = ureq::post(&self.url(path)).set("Cookie", &self.cookie).send_form(form); + self.finish(resp, path) + } + + fn finish(&self, resp: Result, path: &str) -> Page { + match resp { + Ok(r) => Page { status: r.status(), body: r.into_string().unwrap_or_default() }, + Err(ureq::Error::Status(code, r)) => Page { status: code, body: r.into_string().unwrap_or_default() }, + Err(e) => panic!("{path}: {e}"), + } + } + + /// Subscribe, detect and render a review so an approval can be attempted. + fn open_review(&self, name: &str) -> CandidateRow { + aur_build::watch::detect::check_pkgbase(&self.env.cfg, &self.env.db, name).unwrap(); + let c = candidates::pending_candidates(&self.env.db).unwrap().remove(0); + let dd = diff::render_and_store(&self.env.db, &self.env.cfg, &c).unwrap(); + let _ = dd; + candidates::candidate_by_id(&self.env.db, c.id).unwrap().unwrap() + } +} + +fn extract(hay: &str, open: &str, close: &str) -> Option { + let i = hay.find(open)? + open.len(); + let rest = &hay[i..]; + let j = rest.find(close)?; + Some(unescape(&rest[..j])) +} + +/// Templates escape attribute values; a test comparing one back to a Rust +/// string has to undo that. +fn unescape(s: &str) -> String { + s.replace("&", "&") + .replace(""", "\"") + .replace("<", "<") + .replace(">", ">") + .replace("'", "'") +} + +#[test] +fn every_surface_renders() { + let web = Web::new("plain-make"); + web.open_review("plain-make"); + for path in [ + "/", + "/reviews", + "/reviews?state=pending", + "/subscriptions", + "/attention", + "/packages", + "/packages/plain-make", + "/builds", + "/audit", + "/settings", + ] { + let page = web.get(path); + assert_eq!(page.status, 200, "{path} did not render"); + assert!(page.body.contains(""), "{path} is not a page"); + assert!(page.body.contains("aur-build"), "{path} has no chrome"); + } +} + +#[test] +fn the_review_detail_shows_the_panels_and_the_hashes() { + let web = Web::new("plain-make"); + // Publish a first version so the review has something to diff against; + // every panel compares the accepted release to the candidate (§6). + let first = web.open_review("plain-make"); + let page = web.get(&format!("/reviews/{}", first.id)); + let dd = extract(&page.body, "name=\"diff_digest\" value=\"", "\"").unwrap(); + web.post( + &format!("/reviews/{}/approve", first.id), + &[("csrf", &web.csrf), ("identity_hash", &first.identity_hash), ("diff_digest", &dd)], + ); + let id = aur_build::pipeline::enqueue_for_candidate(&web.env.cfg, &web.env.db, &first).unwrap(); + aur_build::pipeline::run(&web.env.cfg, &web.env.db, &id).unwrap(); + web.fx.bump(&[("pkgver", "2.0")]); + let c = web.open_review("plain-make"); + let page = web.get(&format!("/reviews/{}", c.id)); + assert_eq!(page.status, 200); + // The panel set (§6), all comparing accepted → candidate. + for want in ["Recipe diff", "Version delta", "Source digest delta", "Dependency delta", "Build-env delta"] { + assert!(page.body.contains(want), "missing panel {want}"); + } + assert!(page.body.contains("previous accepted release"), "no base shown"); + // The approve form carries both hashes (§7.1a) and is a plain POST form, + // so it works with JavaScript switched off. + assert!(page.body.contains("name=\"identity_hash\"")); + assert!(page.body.contains("name=\"diff_digest\"")); + assert!(page.body.contains("