406 lines
12 KiB
Rust
406 lines
12 KiB
Rust
// 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:?}"
|
|
);
|
|
}
|
|
}
|