210 lines
8.1 KiB
Rust
210 lines
8.1 KiB
Rust
//! Deploy approvals (docs/SIGNING.md, "approval"): the canonical form is checked byte for byte, and the signature is
|
|
//! a prehashed minisign over the text that minisign-verify (what Tauri and the hub's tools check with) accepts.
|
|
|
|
use custos_sign::*;
|
|
|
|
const NOW: u64 = 1_793_000_000;
|
|
const COMMIT: &str = "3f2a9c1d0e4b5a6978877665544332211aabbccd";
|
|
|
|
/// The doc's example, with an expiry 10 minutes after NOW.
|
|
fn canonical() -> String {
|
|
format!(
|
|
r#"{{"v":1,"run":"r-42","repo":"Arca","commit":"{COMMIT}","pipeline":"release","version":"0.7.0","targets":["deploy to nixos-vm","publish to Aula"],"expires":{}}}"#,
|
|
NOW + 600
|
|
)
|
|
}
|
|
|
|
fn with(from: &str, to: &str) -> String {
|
|
let t = canonical();
|
|
assert!(t.contains(from), "{from:?}");
|
|
t.replacen(from, to, 1)
|
|
}
|
|
|
|
fn refused(text: &str, now: u64) -> String {
|
|
match approval_check(text, now) {
|
|
Ok(a) => panic!("accepted {text:?}: {a:?}"),
|
|
Err(e) => e.to_string(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_canonical_approval_is_accepted_and_reads_back() {
|
|
let a = approval_check(&canonical(), NOW).unwrap();
|
|
assert_eq!(a.run, "r-42");
|
|
assert_eq!(a.repo, "Arca");
|
|
assert_eq!(a.commit, COMMIT);
|
|
assert_eq!(a.pipeline, "release");
|
|
assert_eq!(a.version, "0.7.0");
|
|
assert_eq!(a.targets, ["deploy to nixos-vm", "publish to Aula"]);
|
|
assert_eq!(a.expires, NOW + 600);
|
|
assert_eq!(a.canonical(), canonical());
|
|
}
|
|
|
|
#[test]
|
|
fn reordered_keys_spaces_and_other_shapes_are_refused() {
|
|
let t = canonical();
|
|
let reordered = with(r#""run":"r-42","repo":"Arca","#, r#""repo":"Arca","run":"r-42","#);
|
|
let bad = [
|
|
reordered,
|
|
t.replace(',', ", "),
|
|
t.replace(':', ": "),
|
|
format!("{t}\n"),
|
|
format!(" {t}"),
|
|
t.replace("{\"v\"", "{\n\"v\""),
|
|
with(r#""v":1,"#, r#""v":1,"x":1,"#),
|
|
with(r#""v":1,"#, ""),
|
|
with(r#""v":1,"#, r#""v":1,"v":1,"#),
|
|
with(r#""v":1,"#, r#""v":2,"#),
|
|
with(r#""v":1,"#, r#""v":1.0,"#),
|
|
with(r#""v":1,"#, r#""v":"1","#),
|
|
format!("[{t}]"),
|
|
"not json".to_string(),
|
|
String::new(),
|
|
];
|
|
for b in &bad {
|
|
refused(b, NOW);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn escaping_must_be_minimal() {
|
|
// A quote and a backslash in a target are escaped the one way JSON allows; the result is canonical.
|
|
let q = with("publish to Aula", r#"say \"hi\" \\ there"#);
|
|
assert_eq!(approval_check(&q, NOW).unwrap().targets[1], r#"say "hi" \ there"#);
|
|
// Raw non-ASCII is canonical; the same text through \u escapes is not.
|
|
let raw = with("publish to Aula", "publikovat na Aulu é");
|
|
assert!(approval_check(&raw, NOW).is_ok());
|
|
for b in [
|
|
with("publish to Aula", r"publikovat na Aulu \u00e9"),
|
|
with("publish to Aula", r"publish to \u0041ula"),
|
|
with("publish to Aula", r"publish\/to Aula"),
|
|
with("r-42", r"r\u002d42"),
|
|
] {
|
|
refused(&b, NOW);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_bad_run_repo_pipeline_or_version_is_refused() {
|
|
for run in ["", "r 42", "r/42", "r\\\\42", "ř-42", &"r".repeat(65)] {
|
|
let e = refused(&with("\"run\":\"r-42\"", &format!("\"run\":\"{run}\"")), NOW);
|
|
assert!(e.contains("run"), "{run:?}: {e}");
|
|
}
|
|
let ok_run = "a.B_c-9".to_string() + &"x".repeat(57);
|
|
assert_eq!(ok_run.len(), 64);
|
|
assert!(approval_check(&with("\"run\":\"r-42\"", &format!("\"run\":\"{ok_run}\"")), NOW).is_ok());
|
|
for (member, value) in [("repo", "Arca"), ("pipeline", "release"), ("version", "0.7.0")] {
|
|
let from = format!("\"{member}\":\"{value}\"");
|
|
for bad in ["", "a b", "a/b", "a:b", &"a".repeat(65)] {
|
|
let e = refused(&with(&from, &format!("\"{member}\":\"{bad}\"")), NOW);
|
|
assert!(e.contains(member), "{member} {bad:?}: {e}");
|
|
}
|
|
let fine = format!("\"{member}\":\"A.b_c-1+x{}\"", "z".repeat(55));
|
|
assert!(approval_check(&with(&from, &fine), NOW).is_ok(), "{fine}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_bad_commit_is_refused() {
|
|
for bad in [COMMIT.to_uppercase(), COMMIT[..39].to_string(), format!("{COMMIT}0"), COMMIT.replace('a', "g")] {
|
|
let e = refused(&with(COMMIT, &bad), NOW);
|
|
assert!(e.contains("commit"), "{bad}: {e}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn targets_have_limits() {
|
|
let list = |ts: &[String]| ts.iter().map(|t| format!("\"{t}\"")).collect::<Vec<_>>().join(",");
|
|
let from = r#""targets":["deploy to nixos-vm","publish to Aula"]"#;
|
|
let set = |ts: &[String]| with(from, &format!("\"targets\":[{}]", list(ts)));
|
|
let n = |k: usize| (0..k).map(|i| format!("deploy to host-{i}")).collect::<Vec<_>>();
|
|
|
|
assert!(approval_check(&set(&n(1)), NOW).is_ok());
|
|
assert!(approval_check(&set(&n(16)), NOW).is_ok());
|
|
assert!(approval_check(&set(&["é".repeat(80)]), NOW).is_ok(), "80 characters, not bytes");
|
|
for bad in [
|
|
set(&[]),
|
|
set(&n(17)),
|
|
set(&[String::new()]),
|
|
set(&["x".repeat(81)]),
|
|
set(&["tab\\there".to_string()]),
|
|
set(&["line\\nbreak".to_string()]),
|
|
set(&["nul\\u0000".to_string()]),
|
|
set(&["del\u{7f}".to_string()]),
|
|
set(&["right-to-left \u{202e}evil".to_string()]),
|
|
set(&["zero\u{200b}width".to_string()]),
|
|
with(from, r#""targets":"deploy to nixos-vm""#),
|
|
with(from, r#""targets":[1]"#),
|
|
] {
|
|
let e = refused(&bad, NOW);
|
|
assert!(e.contains("target") || e.contains("form"), "{bad}: {e}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_expiry_is_after_now_and_within_an_hour() {
|
|
let at = |exp: u64| with(&format!("\"expires\":{}", NOW + 600), &format!("\"expires\":{exp}"));
|
|
assert!(approval_check(&at(NOW + 1), NOW).is_ok());
|
|
assert!(approval_check(&at(NOW + 3600), NOW).is_ok());
|
|
for exp in [NOW, NOW - 1, 0, NOW + 3601, u64::MAX] {
|
|
let e = refused(&at(exp), NOW);
|
|
assert!(e.contains("expire"), "{exp}: {e}");
|
|
}
|
|
// The same text expires as time passes.
|
|
assert!(refused(&canonical(), NOW + 600).contains("expire"));
|
|
for bad in
|
|
[format!("\"expires\":{}.0", NOW + 600), format!("\"expires\":-{}", NOW + 600), "\"expires\":\"1\"".into()]
|
|
{
|
|
refused(&with(&format!("\"expires\":{}", NOW + 600), &bad), NOW);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_oversized_text_is_refused_before_parsing() {
|
|
let huge = with("publish to Aula", &"x".repeat(MAX_APPROVAL));
|
|
assert!(refused(&huge, NOW).contains("larger"));
|
|
}
|
|
|
|
#[test]
|
|
fn the_approval_signature_verifies_with_minisign() {
|
|
let k = new_ed25519();
|
|
let text = canonical();
|
|
let sig = approval(&k.secret, &k.key_id, &text, NOW, "r-42").unwrap();
|
|
let public = minisign_verify::PublicKey::decode(&minisign_public(&k.public, &k.key_id)).unwrap();
|
|
let decoded = minisign_verify::Signature::decode(&sig).unwrap();
|
|
public.verify(text.as_bytes(), &decoded, false).unwrap();
|
|
assert!(public.verify(with("Arca", "Fabrica").as_bytes(), &decoded, false).is_err());
|
|
assert_eq!(decoded.trusted_comment(), format!("timestamp:{NOW}\tfile:approval-r-42.json\thashed"));
|
|
assert!(sig.ends_with('\n'));
|
|
}
|
|
|
|
#[test]
|
|
fn approval_signs_only_a_valid_text_for_its_own_run() {
|
|
let k = new_ed25519();
|
|
let e = approval(&k.secret, &k.key_id, &canonical(), NOW, "r-43").unwrap_err();
|
|
assert!(e.to_string().contains("run"), "{e}");
|
|
assert!(approval(&k.secret, &k.key_id, &format!("{}\n", canonical()), NOW, "r-42").is_err());
|
|
assert!(approval(&k.secret, &k.key_id, &canonical(), NOW + 600, "r-42").is_err(), "expired at signing time");
|
|
}
|
|
|
|
#[test]
|
|
fn parse_reads_the_form_without_the_clock() {
|
|
// For the audit of a refused or expired request: everything but the expiry is checked.
|
|
let a = approval_parse(&canonical()).unwrap();
|
|
assert_eq!(a, approval_check(&canonical(), NOW).unwrap());
|
|
assert!(approval_check(&canonical(), NOW + 10_000).is_err());
|
|
assert_eq!(approval_parse(&canonical()).unwrap().expires, NOW + 600);
|
|
assert!(approval_parse(&canonical().replace(',', ", ")).is_err());
|
|
assert!(approval_parse(&with("\"run\":\"r-42\"", "\"run\":\"r 42\"")).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn run_ids_follow_the_rule() {
|
|
for ok in ["r-42", "setup-1", "a.b_c", &"x".repeat(64)] {
|
|
assert!(run_id_ok(ok), "{ok}");
|
|
}
|
|
for bad in ["", "a b", "a/b", "ř", &"x".repeat(65)] {
|
|
assert!(!run_id_ok(bad), "{bad}");
|
|
}
|
|
}
|