[control] Previews: POST/GET/DELETE /api/v1/previews, tokens scoped to previews only, the upstream allow list, expiry; Windows drive paths in compile options (tests first)

This commit is contained in:
2026-11-17 13:46:33 +01:00
parent 83270e0746
commit fc9e16f1b2
21 changed files with 1677 additions and 43 deletions
+53 -17
View File
@@ -23,13 +23,22 @@ fn assert_safe_label(l: &str) {
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-'),
"{l:?} is not lowercase LDH"
);
assert!(!l.starts_with('-') && !l.ends_with('-'), "{l:?} has an edge dash");
assert!(l.get(2..4) != Some("--"), "{l:?} has -- in positions 3 and 4");
assert!(
!l.starts_with('-') && !l.ends_with('-'),
"{l:?} has an edge dash"
);
assert!(
l.get(2..4) != Some("--"),
"{l:?} has -- in positions 3 and 4"
);
}
#[test]
fn plain_names_stay_readable() {
assert_eq!(preview::label("mnovak/Arca", "main").unwrap(), "main--arca-4a93cc");
assert_eq!(
preview::label("mnovak/Arca", "main").unwrap(),
"main--arca-4a93cc"
);
assert_eq!(preview::label("shop", "fix-42").unwrap(), "fix-42--shop");
assert_eq!(
preview::hostname("shop", "fix-42", "preview.example.org").unwrap(),
@@ -107,11 +116,18 @@ fn different_branches_never_share_a_label() {
// Long branches that differ only after the cut.
let a = format!("{}-one", "release".repeat(10));
let b = format!("{}-two", "release".repeat(10));
assert_ne!(preview::label("p", &a).unwrap(), preview::label("p", &b).unwrap());
assert_ne!(
preview::label("p", &a).unwrap(),
preview::label("p", &b).unwrap()
);
// And a broad sweep: many similar names, all distinct.
let mut seen = BTreeMap::new();
for i in 0..2000 {
for b in [format!("feature/{i}"), format!("feature-{i}"), format!("Feature/{i}")] {
for b in [
format!("feature/{i}"),
format!("feature-{i}"),
format!("Feature/{i}"),
] {
let l = preview::label("p", &b).unwrap();
assert_safe_label(&l);
if let Some(prev) = seen.insert(l.clone(), b.clone()) {
@@ -127,21 +143,39 @@ fn same_input_same_label() {
if b.len() > preview::MAX_BRANCH_BYTES {
continue;
}
assert_eq!(preview::label("p/q", b).unwrap(), preview::label("p/q", b).unwrap());
assert_eq!(
preview::label("p/q", b).unwrap(),
preview::label("p/q", b).unwrap()
);
}
}
#[test]
fn unusable_names_are_refused_not_mangled() {
let too_long = "b".repeat(preview::MAX_BRANCH_BYTES + 1);
let bad_branches = ["", "tab\there", "with\nnewline", "nul\0byte", "bell\u{7}", too_long.as_str()];
let bad_branches = [
"",
"tab\there",
"with\nnewline",
"nul\0byte",
"bell\u{7}",
too_long.as_str(),
];
for b in bad_branches {
assert!(
matches!(preview::label("p", b), Err(PreviewError::Branch(_))),
"{b:?}"
);
}
let bad_projects = ["", "/", "owner/", "has space", "semi;colon", "ü/name", &"p".repeat(129)];
let bad_projects = [
"",
"/",
"owner/",
"has space",
"semi;colon",
"ü/name",
&"p".repeat(129),
];
for p in bad_projects {
assert!(
matches!(preview::label(p, "main"), Err(PreviewError::Project(_))),
@@ -153,7 +187,7 @@ fn unusable_names_are_refused_not_mangled() {
preview::hostname("p", "main", "bad domain"),
Err(PreviewError::Domain(_))
));
let deep = format!("{}.example.org", "d".repeat(60).repeat(1) + &".d".repeat(90));
let deep = format!("{}.example.org", "d".repeat(60) + &".d".repeat(90));
assert!(preview::hostname("p", "main", &deep).is_err());
}
@@ -211,7 +245,10 @@ fn the_model_carries_the_preview() {
let host = preview::hostname("shop", "fix-42", "pv.example.org").unwrap();
let m = load_one(&host, &preview_file(&host, "shop", "fix-42")).unwrap();
let p = m.hosts()[0].preview.as_ref().expect("a preview");
assert_eq!((p.project.as_str(), p.branch.as_str(), p.expires_at), ("shop", "fix-42", 1_900_000_000));
assert_eq!(
(p.project.as_str(), p.branch.as_str(), p.expires_at),
("shop", "fix-42", 1_900_000_000)
);
}
#[test]
@@ -220,8 +257,11 @@ fn a_preview_table_must_match_its_host_name() {
let host = preview::hostname("shop", "fix-42", "pv.example.org").unwrap();
let e = load_one(&host, &preview_file(&host, "shop", "other")).unwrap_err();
assert!(e.to_string().contains("preview"), "{e}");
let e = load_one("plain.pv.example.org", &preview_file("plain.pv.example.org", "shop", "fix-42"))
.unwrap_err();
let e = load_one(
"plain.pv.example.org",
&preview_file("plain.pv.example.org", "shop", "fix-42"),
)
.unwrap_err();
assert!(e.to_string().contains("preview"), "{e}");
// Unknown keys and bad values in the table are refused like everywhere else.
let text = preview_file(&host, "shop", "fix-42").replace("expires_at", "expires");
@@ -237,11 +277,7 @@ fn compilers_ignore_the_preview_table() {
let host = preview::hostname("shop", "fix-42", "pv.example.org").unwrap();
let with = load_one(&host, &preview_file(&host, "shop", "fix-42")).unwrap();
let text = preview_file(&host, "shop", "fix-42");
let without_text: String = text
.split("[preview]")
.next()
.unwrap()
.to_owned();
let without_text: String = text.split("[preview]").next().unwrap().to_owned();
let without = load_one(&host, &without_text).unwrap();
let o = ianitor_core::compile::CompileOptions {
http_listen: "0.0.0.0:80".parse().unwrap(),