Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

home/desktop/gate1: verify_served_held — every held history tried, exactly one match names the app (#842's held and histories rows) (#843)

nyblnet6aa0bc9d218c
diff --git a/home/desktop/gate1/src/lib.rs b/home/desktop/gate1/src/lib.rs
index feb6d5bd..56af38e9 100644
--- a/home/desktop/gate1/src/lib.rs
+++ b/home/desktop/gate1/src/lib.rs
@@ -561,6 +561,29 @@ pub fn verify_served(doc: &[u8], history: &History, frame_ancestors: &str) -> Se
}
}
+/// A served file judged against EVERY history the host holds (the reference's
+/// verifyServedHeld). A fresh shell has an empty document block, so nothing in
+/// the file says which app it is: never pick the history by a name read from the
+/// file or from a directory. Exactly one held history must match; none, or more
+/// than one, is UNKNOWN. A genuine verdict names the app that matched.
+pub fn verify_served_held(doc: &[u8], held: &[History], frame_ancestors: &str) -> (ServedVerdict, Option<String>) {
+ let mut hits: Vec<(String, ServedVerdict)> = vec![];
+ for h in held {
+ let v = verify_served(doc, h, frame_ancestors);
+ if matches!(v, ServedVerdict::Genuine { .. }) { hits.push((h.app().unwrap_or("").to_string(), v)) }
+ }
+ if hits.len() == 1 {
+ let (app, v) = hits.pop().unwrap();
+ return (v, Some(app));
+ }
+ let reason = if hits.is_empty() {
+ "no held history matches".to_string()
+ } else {
+ format!("{} held histories match ({})", hits.len(), hits.iter().map(|(a, _)| a.as_str()).collect::<Vec<_>>().join(", "))
+ };
+ (ServedVerdict::Unknown { reason }, None)
+}
+
// --- the strict file:// fallback (security §15) ------------------------------------
//
// The desktop host always SERVES the bytes it verified, so it never needs this
diff --git a/home/desktop/gate1/tests/corpus.rs b/home/desktop/gate1/tests/corpus.rs
index d728afe7..a63094f7 100644
--- a/home/desktop/gate1/tests/corpus.rs
+++ b/home/desktop/gate1/tests/corpus.rs
@@ -123,3 +123,60 @@ fn every_corpus_file_is_covered_by_a_row() {
}
}
}
+
+/// #842: each history named by `histories`, verified with its own key.
+fn named_histories(exp: &Value) -> Option<std::collections::HashMap<String, History>> {
+ let hs = exp.get("histories")?.as_object()?;
+ let mut out = std::collections::HashMap::new();
+ for (name, spec) in hs {
+ let key = json(spec["key"].as_str().unwrap());
+ let env = String::from_utf8(read(spec["history"].as_str().unwrap())).unwrap();
+ let h = History::from_signed(&env, key["x"].as_str().unwrap(), key["y"].as_str().unwrap()).unwrap_or_else(|e| panic!("history {name}: {e}"));
+ out.insert(name.clone(), h);
+ }
+ Some(out)
+}
+
+#[test]
+fn held_rows() {
+ let exp = json("expected.json");
+ let (Some(rows), Some(named)) = (exp.get("held").and_then(Value::as_array), named_histories(&exp)) else {
+ println!("NOTE: this corpus has no `held`/`histories` sections yet (they arrive with kernel #842)");
+ return;
+ };
+ assert!(rows.len() >= 6, "the held rows shrank to {}", rows.len());
+ let mut wrong = vec![];
+ for r in rows {
+ let held: Vec<History> = r["held"].as_array().unwrap().iter().map(|n| named[n.as_str().unwrap()].clone()).collect();
+ let (v, app) = verify_served_held(&read(r["case"].as_str().unwrap()), &held, "'none'");
+ let got = match &v {
+ ServedVerdict::Genuine { release, .. } => format!("genuine {} {}", app.clone().unwrap_or_default(), release.version),
+ ServedVerdict::Unknown { .. } => "unknown".into(),
+ };
+ let want = match r["verdict"].as_str().unwrap() {
+ "genuine" => format!("genuine {} {}", r["app"].as_str().unwrap(), r["release"].as_str().unwrap()),
+ o => o.to_string(),
+ };
+ if got != want { wrong.push(format!("{}: want {want}, got {got} ({v:?})", r["name"])) }
+ }
+ assert!(wrong.is_empty(), "held rows the port reads differently:\n{}", wrong.join("\n"));
+}
+
+#[test]
+fn every_named_history_verifies_and_a_flipped_byte_does_not() {
+ let exp = json("expected.json");
+ let Some(hs) = exp.get("histories").and_then(Value::as_object) else {
+ println!("NOTE: no `histories` section yet (kernel #842)");
+ return;
+ };
+ for (name, spec) in hs {
+ let key = json(spec["key"].as_str().unwrap());
+ let mut env = json(spec["history"].as_str().unwrap());
+ let p = env["payload"].as_str().unwrap().to_string();
+ let i = p.find("\"version\"").unwrap() + 12;
+ let mut b = p.into_bytes();
+ b[i] = if b[i] == b'1' { b'2' } else { b'1' };
+ env["payload"] = Value::String(String::from_utf8(b).unwrap());
+ assert!(History::from_signed(&env.to_string(), key["x"].as_str().unwrap(), key["y"].as_str().unwrap()).is_err(), "{name}: a changed payload must not verify");
+ }
+}