[core] Tests first: share tokens, prefix escapes, repo layout, exact host path, error codes (red)

This commit is contained in:
2026-11-07 17:32:09 +01:00
parent 027bf3a553
commit 73a9f3da45
13 changed files with 1342 additions and 106 deletions
+233
View File
@@ -0,0 +1,233 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! The routes repository holds exactly `ianitor.toml` and `hosts/<hostname>.toml`, and the
//! loader requires the exact path (ROADMAP §10: the check used to look at the base name only).
//! Validation errors carry a stable code, a field path and params for the UI (docs/ui-api.md §3.1).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use ianitor_core::routes::{EntryKind, RoutePath, check_path, check_tree};
use ianitor_core::{SourceFile, ValidationError, load};
#[test]
fn only_the_site_file_and_host_files_are_allowed() {
assert_eq!(
check_path("ianitor.toml", EntryKind::File).unwrap(),
RoutePath::Site
);
assert_eq!(
check_path("hosts/wiki.home.test.toml", EntryKind::File).unwrap(),
RoutePath::Host {
hostname: "wiki.home.test".into()
}
);
for bad in [
"README.md",
".gitattributes",
"hosts.toml",
"other/wiki.home.test.toml",
"hosts/sub/wiki.home.test.toml",
"hosts/wiki.home.test.toml/x",
"hosts/Wiki.home.test.toml",
"hosts/wiki.home.test.TOML",
"hosts/wiki.home.test",
"hosts/.toml",
"hosts/*.home.test.toml",
"hosts/wiki..test.toml",
"hosts/10.0.0.1.toml",
"hosts/wiki home.toml",
"hosts\\wiki.home.test.toml",
"hosts/wiki\\x.toml",
"IANITOR.toml",
"./ianitor.toml",
] {
let e = check_path(bad, EntryKind::File).unwrap_err();
assert!(
matches!(e, ValidationError::UnexpectedFile { ref file, .. } if file == bad),
"{bad}: {e:?}"
);
assert_eq!(e.code(), "repo.unexpected_file");
}
}
#[test]
fn links_executables_and_submodules_are_refused() {
for kind in [
EntryKind::Symlink,
EntryKind::Submodule,
EntryKind::Executable,
] {
assert!(check_path("hosts/a.test.toml", kind).is_err(), "{kind:?}");
assert!(check_path("ianitor.toml", kind).is_err(), "{kind:?}");
}
}
#[test]
fn a_tree_needs_the_site_file_and_reports_every_stray_file() {
let ok = [
("ianitor.toml", EntryKind::File),
("hosts/a.test.toml", EntryKind::File),
];
assert!(check_tree(ok).is_ok());
let errs = check_tree([
("hosts/a.test.toml", EntryKind::File),
("notes.txt", EntryKind::File),
("hosts/b/c.toml", EntryKind::File),
])
.unwrap_err();
assert_eq!(errs.len(), 3, "{errs:?}");
}
#[test]
fn the_loader_requires_the_exact_path() {
let site = SourceFile {
name: "ianitor.toml",
contents: "",
};
let body = "hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n";
for name in [
"app.test.toml",
"other/app.test.toml",
"hosts/x/app.test.toml",
"hosts\\app.test.toml",
"./hosts/app.test.toml",
] {
let errs = load(
&site,
&[SourceFile {
name,
contents: body,
}],
)
.unwrap_err();
assert!(
errs.errors()
.iter()
.any(|e| matches!(e, ValidationError::FileNameMismatch { .. })),
"{name}: {errs:?}"
);
}
assert!(
load(
&site,
&[SourceFile {
name: "hosts/app.test.toml",
contents: body
}]
)
.is_ok()
);
}
fn errors_for(body: &str) -> Vec<ValidationError> {
let site = SourceFile {
name: "ianitor.toml",
contents: "secrets = [\"api_key\"]\n",
};
load(
&site,
&[SourceFile {
name: "hosts/app.test.toml",
contents: body,
}],
)
.unwrap_err()
.into_errors()
}
#[test]
fn errors_have_codes_fields_and_params_for_the_ui() {
let cases: &[(&str, &str, Option<&str>)] = &[
(
"upstreams = [\"http://127.0.0.1:1\"]\n",
"host.hostname.missing",
Some("hostname"),
),
("hostname = \"app.test\"\n", "host.upstreams.missing", Some("upstreams")),
(
"hostname = \"App.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
"host.hostname.uppercase",
Some("hostname"),
),
(
"hostname = \"app test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
"host.hostname.spaces",
Some("hostname"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\", \"ftp://x\"]\n",
"host.upstream.scheme",
Some("upstreams[1]"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1/x\"]\n",
"host.upstream.path",
Some("upstreams[0]"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\", \"http://127.0.0.1:1\"]\n",
"host.upstream.duplicate",
Some("upstreams[1]"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\", \"https://127.0.0.1:2\"]\n",
"host.upstream.mixed_schemes",
Some("upstreams"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\ncolour = 1\n",
"host.field.unknown",
Some("colour"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nvalue = \"1\"\n[[request_headers]]\naction = \"set\"\nname = \"Host\"\nvalue = \"x\"\n",
"host.header.protected",
Some("request_headers[1].name"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[response_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"api_key\"\n",
"host.header.secret_response",
Some("response_headers[0]"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"nope\"\n",
"host.header.secret_unknown",
Some("request_headers[0].secret"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[health_check]\npath = \"/x\"\ninterval_secs = 0\n",
"host.health.interval",
Some("health_check.interval_secs"),
),
(
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\nauth = \"forward-auth\"\n",
"host.auth.not_configured",
Some("auth"),
),
];
for (body, code, field) in cases {
let errs = errors_for(body);
let e = errs
.iter()
.find(|e| e.code() == *code)
.unwrap_or_else(|| panic!("no {code} for {body:?}: {errs:?}"));
assert_eq!(e.field().as_deref(), *field, "{code}");
assert_eq!(e.params()["file"], "hosts/app.test.toml");
}
}
#[test]
fn params_carry_suggestions() {
let errs = errors_for("hostname = \"App.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n");
let e = errs
.iter()
.find(|e| e.code() == "host.hostname.uppercase")
.unwrap();
assert_eq!(e.params()["suggestion"], "app.test");
let errs = errors_for("hostname = \"app.test\"\nupstreams = [\"http://10.0.0.2:3000/app\"]\n");
let e = errs.iter().find(|e| e.code() == "host.upstream.path").unwrap();
assert_eq!(e.params()["suggestion"], "http://10.0.0.2:3000");
assert_eq!(e.params()["url"], "http://10.0.0.2:3000/app");
}
+35 -52
View File
@@ -148,11 +148,11 @@ value = "max-age=31536000"
#[test]
fn hosts_are_sorted_by_hostname_whatever_the_file_order() {
let a = SourceFile {
name: "b.test.toml",
name: "hosts/b.test.toml",
contents: "hostname = \"b.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
};
let b = SourceFile {
name: "a.test.toml",
name: "hosts/a.test.toml",
contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
};
let m1 = load(&site(), &[a, b]).unwrap();
@@ -175,7 +175,7 @@ fn empty_site_file_is_allowed() {
contents: "",
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
}],
);
@@ -236,7 +236,7 @@ fn rejects_unknown_field_in_site_file() {
contents: "secrets = []\nsecret_values = { a = \"b\" }\n",
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
}],
);
@@ -348,10 +348,11 @@ fn accepts_good_hostnames() {
}
#[test]
fn rejects_duplicate_hostnames_across_files() {
let r = load(
&site(),
&[
fn a_second_file_for_the_same_host_is_refused_by_its_path() {
// With the exact `hosts/<hostname>.toml` rule, two files can never hold one hostname:
// the second one is in the wrong place, whatever the file order.
for order in [false, true] {
let mut files = vec![
SourceFile {
name: "hosts/app.test.toml",
contents: MINIMAL,
@@ -360,38 +361,19 @@ fn rejects_duplicate_hostnames_across_files() {
name: "other/app.test.toml",
contents: MINIMAL,
},
],
);
let errs = r.unwrap_err();
assert_eq!(
errs.errors(),
[ValidationError::DuplicateHostname {
hostname: "app.test".into(),
first: "hosts/app.test.toml".into(),
second: "other/app.test.toml".into()
}]
);
}
#[test]
fn duplicate_detection_is_independent_of_file_order() {
let r = load(
&site(),
&[
SourceFile {
name: "other/app.test.toml",
contents: MINIMAL,
},
SourceFile {
name: "hosts/app.test.toml",
contents: MINIMAL,
},
],
);
let errs = r.unwrap_err();
assert!(
matches!(&errs.errors()[0], ValidationError::DuplicateHostname { first, second, .. } if first == "hosts/app.test.toml" && second == "other/app.test.toml")
);
];
if order {
files.reverse();
}
let errs = load(&site(), &files).unwrap_err();
assert_eq!(
errs.errors(),
[ValidationError::FileNameMismatch {
file: "other/app.test.toml".into(),
hostname: "app.test".into()
}]
);
}
}
#[test]
@@ -400,11 +382,11 @@ fn rejects_duplicate_source_file_names() {
&site(),
&[
SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
},
SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
},
],
@@ -485,7 +467,8 @@ fn rejects_undefined_secret_reference() {
e,
ValidationError::UndefinedSecret {
file: "hosts/app.test.toml".into(),
secret: "nope".into()
secret: "nope".into(),
field: "request_headers[0].secret".into()
}
);
}
@@ -498,7 +481,7 @@ fn undefined_secret_is_reported_when_site_declares_none() {
contents: "",
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: &format!(
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-K\"\nsecret = \"api_key\"\n"
),
@@ -532,7 +515,7 @@ fn rejects_bad_secret_names_in_site_file() {
contents: &format!("secrets = [{bad:?}]\n"),
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
}],
);
@@ -678,7 +661,7 @@ fn forward_auth_requires_site_configuration() {
contents: "",
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: &format!("{MINIMAL}auth = \"forward-auth\"\n"),
}],
);
@@ -704,7 +687,7 @@ fn rejects_bad_forward_auth_site_config() {
contents: &format!("[forward_auth]\nurl = {bad:?}\n"),
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
}],
);
@@ -743,15 +726,15 @@ fn collects_errors_from_every_file() {
&site(),
&[
SourceFile {
name: "a.test.toml",
name: "hosts/a.test.toml",
contents: "hostname = \"a.test\"\nupstreams = [\"ftp://x\"]\n",
},
SourceFile {
name: "B.test.toml",
name: "hosts/B.test.toml",
contents: "hostname = \"B.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
},
SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
},
],
@@ -830,7 +813,7 @@ fn rejects_wildcards_in_copy_headers() {
contents: "[forward_auth]\nurl = \"http://127.0.0.1:9091/x\"\ncopy_headers = [\"Remote-*\"]\n",
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
}],
);
@@ -861,7 +844,7 @@ fn rejects_oversized_route_files() {
contents: &big,
},
&[SourceFile {
name: "app.test.toml",
name: "hosts/app.test.toml",
contents: MINIMAL,
}],
);
+378
View File
@@ -0,0 +1,378 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Share-link tokens (ROADMAP §6 phase 2, tests first): a token is refused when expired, for
//! the wrong path, for the wrong host, tampered, without its password or with a wrong one,
//! and no path trick lets it escape its prefix.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::collections::BTreeSet;
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use ianitor_core::share::{
Claims, LinkSpec, Rejection, Revocations, ShareId, ShareKey, check_prefix, mint, path_within,
peek, session_value, verify_link, verify_session,
};
const NOW: u64 = 1_800_000_000;
const HOST: &str = "photos.home.test";
fn key() -> ShareKey {
ShareKey::new(b"0123456789abcdef0123456789abcdef-test-key").unwrap()
}
fn other_key() -> ShareKey {
ShareKey::new(b"fedcba9876543210fedcba9876543210-test-key").unwrap()
}
fn id(n: u8) -> ShareId {
ShareId([n; 16])
}
fn spec<'a>(prefix: &'a str, password: Option<&'a str>) -> LinkSpec<'a> {
LinkSpec {
id: id(7),
epoch: 3,
expires_at: NOW + 3600,
prefix,
password,
}
}
fn none() -> Revocations {
Revocations {
epoch: 3,
deny: BTreeSet::new(),
}
}
fn token(prefix: &str, password: Option<&str>) -> String {
mint(&key(), HOST, &spec(prefix, password)).unwrap()
}
#[test]
fn a_fresh_token_is_accepted_and_says_what_it_covers() {
let t = token("/albums/summer", None);
assert!(t.starts_with("ish1."), "{t}");
let c = verify_link(&key(), HOST, &t, None, NOW, &none()).unwrap();
assert_eq!(
c,
Claims {
id: id(7),
epoch: 3,
expires_at: NOW + 3600,
password: false,
prefix: "/albums/summer".into()
}
);
}
#[test]
fn an_expired_token_is_refused_as_expired() {
let t = token("/", None);
assert_eq!(
verify_link(&key(), HOST, &t, None, NOW + 3600, &none()),
Err(Rejection::Expired {
expires_at: NOW + 3600
})
);
assert_eq!(
verify_link(&key(), HOST, &t, None, NOW + 99_999, &none()),
Err(Rejection::Expired {
expires_at: NOW + 3600
})
);
}
#[test]
fn a_token_for_another_host_is_refused() {
let t = token("/", None);
for host in ["other.home.test", "PHOTOS.home.test", "photos.home.test.", "photos.home"] {
assert_eq!(
verify_link(&key(), host, &t, None, NOW, &none()),
Err(Rejection::Invalid),
"{host}"
);
}
}
#[test]
fn a_token_made_with_another_key_is_refused() {
let t = mint(&other_key(), HOST, &spec("/", None)).unwrap();
assert_eq!(
verify_link(&key(), HOST, &t, None, NOW, &none()),
Err(Rejection::Invalid)
);
}
#[test]
fn every_single_bit_flip_is_refused() {
let t = token("/albums", None);
let raw = URL_SAFE_NO_PAD.decode(t.strip_prefix("ish1.").unwrap()).unwrap();
for i in 0..raw.len() {
for bit in 0..8 {
let mut r = raw.clone();
r[i] ^= 1 << bit;
let forged = format!("ish1.{}", URL_SAFE_NO_PAD.encode(&r));
let got = verify_link(&key(), HOST, &forged, None, NOW, &none());
assert!(
matches!(
got,
Err(Rejection::Invalid | Rejection::Malformed | Rejection::PasswordRequired)
),
"byte {i} bit {bit}: {got:?}"
);
}
}
}
#[test]
fn a_longer_expiry_or_wider_prefix_cannot_be_spliced_in() {
let t = token("/albums/summer", None);
let raw = URL_SAFE_NO_PAD.decode(t.strip_prefix("ish1.").unwrap()).unwrap();
let (payload, mac) = raw.split_at(raw.len() - 32);
// Same MAC, prefix cut down to "/albums".
let mut p = payload[..30].to_vec();
p.extend_from_slice(b"/albums");
p.extend_from_slice(mac);
let forged = format!("ish1.{}", URL_SAFE_NO_PAD.encode(&p));
assert_eq!(
verify_link(&key(), HOST, &forged, None, NOW, &none()),
Err(Rejection::Invalid)
);
// Same MAC, expiry pushed out.
let mut p = payload.to_vec();
p[21..29].copy_from_slice(&(NOW + 10 * 3600).to_be_bytes());
p.extend_from_slice(mac);
let forged = format!("ish1.{}", URL_SAFE_NO_PAD.encode(&p));
assert_eq!(
verify_link(&key(), HOST, &forged, None, NOW, &none()),
Err(Rejection::Invalid)
);
}
#[test]
fn garbage_is_malformed_never_a_panic() {
for bad in [
"",
"ish1.",
"ish1.!!!!",
"ish2.AAAA",
"iss1.AAAA",
"ish1.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
&"ish1.".repeat(400),
"ish1.\u{0}",
] {
assert_eq!(
verify_link(&key(), HOST, bad, None, NOW, &none()),
Err(Rejection::Malformed),
"{bad:?}"
);
}
}
#[test]
fn a_password_link_needs_its_password() {
let t = token("/", Some("correct horse"));
assert!(peek(&t).unwrap().password);
assert_eq!(
verify_link(&key(), HOST, &t, None, NOW, &none()),
Err(Rejection::PasswordRequired)
);
for wrong in ["", "correct hors", "correct horse ", "Correct horse", "x"] {
assert_eq!(
verify_link(&key(), HOST, &t, Some(wrong), NOW, &none()),
Err(Rejection::Invalid),
"{wrong:?}"
);
}
let c = verify_link(&key(), HOST, &t, Some("correct horse"), NOW, &none()).unwrap();
assert!(c.password);
}
#[test]
fn the_token_carries_no_password_hash() {
// Two links that differ only in the password have the same payload; only the MAC differs.
let a = token("/", Some("password-one"));
let b = token("/", Some("password-two"));
let ra = URL_SAFE_NO_PAD.decode(a.strip_prefix("ish1.").unwrap()).unwrap();
let rb = URL_SAFE_NO_PAD.decode(b.strip_prefix("ish1.").unwrap()).unwrap();
assert_eq!(ra.len(), rb.len());
assert_eq!(ra[..ra.len() - 32], rb[..rb.len() - 32]);
assert_ne!(ra[ra.len() - 32..], rb[rb.len() - 32..]);
}
#[test]
fn a_dropped_password_flag_does_not_open_the_link() {
let t = token("/", Some("correct horse"));
let mut raw = URL_SAFE_NO_PAD.decode(t.strip_prefix("ish1.").unwrap()).unwrap();
raw[29] = 0;
let forged = format!("ish1.{}", URL_SAFE_NO_PAD.encode(&raw));
assert_eq!(
verify_link(&key(), HOST, &forged, None, NOW, &none()),
Err(Rejection::Invalid)
);
}
#[test]
fn passwords_are_8_to_128_characters() {
assert!(mint(&key(), HOST, &spec("/", Some("short"))).is_err());
assert!(mint(&key(), HOST, &spec("/", Some(&"x".repeat(129)))).is_err());
assert!(mint(&key(), HOST, &spec("/", Some("12345678"))).is_ok());
}
#[test]
fn stopped_links_and_older_epochs_are_revoked() {
let t = token("/", None);
let mut r = none();
r.deny.insert(id(7));
assert_eq!(
verify_link(&key(), HOST, &t, None, NOW, &r),
Err(Rejection::Revoked)
);
let r = Revocations {
epoch: 4,
deny: BTreeSet::new(),
};
assert_eq!(
verify_link(&key(), HOST, &t, None, NOW, &r),
Err(Rejection::Revoked)
);
// Another link on the deny list does not matter.
let mut r = none();
r.deny.insert(id(8));
assert!(verify_link(&key(), HOST, &t, None, NOW, &r).is_ok());
}
#[test]
fn session_cookies_and_link_tokens_are_not_interchangeable() {
let t = token("/albums", Some("correct horse"));
let c = verify_link(&key(), HOST, &t, Some("correct horse"), NOW, &none()).unwrap();
let s = session_value(&key(), HOST, &c);
assert!(s.starts_with("iss1."));
assert_eq!(verify_session(&key(), HOST, &s, NOW, &none()).unwrap(), c);
// A cookie is not a link, a link is not a cookie, even with the prefix swapped.
assert!(verify_link(&key(), HOST, &s, None, NOW, &none()).is_err());
assert!(verify_session(&key(), HOST, &t, NOW, &none()).is_err());
let swapped = s.replacen("iss1.", "ish1.", 1);
assert!(verify_link(&key(), HOST, &swapped, Some("correct horse"), NOW, &none()).is_err());
let swapped = t.replacen("ish1.", "iss1.", 1);
assert!(verify_session(&key(), HOST, &swapped, NOW, &none()).is_err());
// Cookies are host-bound, expire and are revoked like links.
assert_eq!(
verify_session(&key(), "other.home.test", &s, NOW, &none()),
Err(Rejection::Invalid)
);
assert!(matches!(
verify_session(&key(), HOST, &s, NOW + 3600, &none()),
Err(Rejection::Expired { .. })
));
let mut r = none();
r.deny.insert(id(7));
assert_eq!(
verify_session(&key(), HOST, &s, NOW, &r),
Err(Rejection::Revoked)
);
}
#[test]
fn short_keys_are_refused() {
assert!(ShareKey::new(b"short").is_err());
assert!(ShareKey::new(&[0u8; 31]).is_err());
assert!(ShareKey::new(&[0u8; 32]).is_ok());
assert!(!format!("{:?}", key()).contains("0123"));
}
#[test]
fn prefixes_are_normalised_or_refused() {
assert_eq!(check_prefix("/").unwrap(), "/");
assert_eq!(check_prefix("/a/").unwrap(), "/a");
assert_eq!(check_prefix("/albums/summer-2026").unwrap(), "/albums/summer-2026");
for bad in [
"", "a", "//", "/a//b", "/a/../b", "/..", "/.", "/a/./b", "/a%2fb", "/a\\b", "/a b",
"/a;b", "/ä", "/a?b", "/a#b",
] {
assert!(check_prefix(bad).is_err(), "{bad:?} accepted");
}
assert!(check_prefix(&format!("/{}", "a".repeat(256))).is_err());
// A prefix that is not in normal form is not minted.
assert!(mint(&key(), HOST, &spec("/a/", None)).is_err());
}
#[test]
fn ids_round_trip_as_hex() {
let i = ShareId([0xab; 16]);
assert_eq!(i.to_hex(), "ab".repeat(16));
assert_eq!(ShareId::from_hex(&i.to_hex()), Some(i));
for bad in ["", "ab", &"AB".repeat(16), &"zz".repeat(16), &"a".repeat(33)] {
assert_eq!(ShareId::from_hex(bad), None, "{bad:?}");
}
}
// ------------------------------------------------------------------ prefix escapes
#[test]
fn paths_under_the_prefix_are_within() {
for (prefix, path) in [
("/a", "/a"),
("/a", "/a/"),
("/a", "/a/b"),
("/a", "/a/b/c.jpg"),
("/a", "/a/b%20c"),
("/a", "/a/%E2%9C%93"),
("/", "/"),
("/", "/anything/at/all"),
("/a/b", "/a/b/c"),
("/a", "/a/b;v=1"),
("/a", "/a/.well"),
] {
assert!(path_within(prefix, path), "{prefix} should cover {path}");
}
}
#[test]
fn no_path_trick_escapes_the_prefix() {
let cases = [
("/a", "/ab"),
("/a", "/abc/d"),
("/a", "/A"),
("/a", "/A/b"),
("/a/b", "/a"),
("/a/b", "/a/bc"),
("/a", "/b"),
("/a", "/a/../b"),
("/a", "/a/.."),
("/a", "/a/./../b"),
("/a", "/a/%2e%2e/b"),
("/a", "/a/%2E%2E/b"),
("/a", "/a/.%2e/b"),
("/a", "/a/%2e./b"),
("/a", "/a/%252e%252e/b"),
("/a", "/a/%25252e/b"),
("/a", "/a%2f..%2fb"),
("/a", "/a%2F..%2Fb"),
("/a", "/a/..%2fb"),
("/a", "/a//b"),
("/a", "//a/b"),
("/a", "/a\\..\\b"),
("/a", "/a/..\\b"),
("/a", "/a%5c..%5cb"),
("/a", "/a/..;/b"),
("/a", "/a/.;/b"),
("/a", "/a/%00/b"),
("/a", "/a/b c"),
("/a", "/a/\tb"),
("/a", "a/b"),
("/a", ""),
("/a", "/a/ä"),
("/", "/../etc/passwd"),
("/", "//evil"),
("/", "/%2e%2e/x"),
];
for (prefix, path) in cases {
assert!(!path_within(prefix, path), "{prefix} must not cover {path:?}");
}
}