[core] Share links and repo layout: HMAC tokens with epoch, prefix check, exact hosts/<hostname>.toml, error codes; edge config v2

This commit is contained in:
2026-11-07 17:32:44 +01:00
parent 73a9f3da45
commit 94ba18d38b
8 changed files with 348 additions and 48 deletions
+76 -8
View File
@@ -276,6 +276,9 @@ fn decode(token: &str, prefix: &str) -> Result<(Claims, Vec<u8>, Vec<u8>), Rejec
x.copy_from_slice(&p[21..29]);
let prefix = std::str::from_utf8(&p[FIXED_LEN..]).map_err(|_| Rejection::Malformed)?;
// A token whose prefix is not in normal form was never minted.
if check_prefix(prefix).ok().as_deref() != Some(prefix) {
return Err(Rejection::Malformed);
}
Ok((
Claims {
id: ShareId(id),
@@ -306,8 +309,15 @@ pub fn verify_link(
now: u64,
revocations: &Revocations,
) -> Result<Claims, Rejection> {
let _ = (key, host, password, now, revocations);
peek(token)
let (claims, p, mac) = decode(token, LINK_PREFIX)?;
let password = match (claims.password, password) {
(true, None) => return Err(Rejection::PasswordRequired),
(true, Some(pw)) => Some(pw),
(false, _) => None,
};
let msg = link_message(key, host, &p, &claims.id, password);
hmac::verify(&key.0, &msg, &mac).map_err(|_| Rejection::Invalid)?;
finish(claims, now, revocations)
}
fn finish(claims: Claims, now: u64, revocations: &Revocations) -> Result<Claims, Rejection> {
@@ -346,16 +356,45 @@ pub fn verify_session(
now: u64,
revocations: &Revocations,
) -> Result<Claims, Rejection> {
let _ = (key, host, now, revocations);
decode(value, SESSION_PREFIX).map(|(c, _, _)| c)
let (claims, p, mac) = decode(value, SESSION_PREFIX)?;
hmac::verify(&key.0, &session_message(host, &p), &mac).map_err(|_| Rejection::Invalid)?;
finish(claims, now, revocations)
}
/// Normalise and check a path prefix: starts with `/`, at most [`MAX_PREFIX_LEN`] bytes, only
/// unreserved characters and `!$&'()*+,=:@-._~`, no `%`, `;`, backslash, empty segment or dot
/// segment. A trailing `/` is dropped (except for `/` itself).
pub fn check_prefix(prefix: &str) -> Result<String, &'static str> {
// Tests first: not written yet.
Ok(prefix.to_owned())
if !prefix.starts_with('/') {
return Err("it must start with /");
}
if prefix.len() > MAX_PREFIX_LEN {
return Err("it is longer than 256 characters");
}
let ok = |c: u8| c.is_ascii_alphanumeric() || b"/!$&'()*+,=:@-._~".contains(&c);
if !prefix.bytes().all(ok) {
return Err("only letters, digits, / and -._~!$&'()*+,=:@ are allowed");
}
if prefix.contains("//") {
return Err("it has an empty segment (//)");
}
let trimmed = if prefix.len() > 1 {
prefix.strip_suffix('/').unwrap_or(prefix)
} else {
prefix
};
if trimmed == "/" {
return Ok("/".into());
}
for seg in trimmed[1..].split('/') {
if seg.is_empty() {
return Err("it has an empty segment (//)");
}
if seg.bytes().all(|c| c == b'.') {
return Err("it has a . or .. segment");
}
}
Ok(trimmed.to_owned())
}
/// Is `raw_path` (the request path exactly as sent, still percent-encoded) under `prefix`?
@@ -365,6 +404,35 @@ pub fn check_prefix(prefix: &str) -> Result<String, &'static str> {
/// forms of `.`, `/`, `\`, `%` and NUL. Other paths match on whole segments, case-sensitively:
/// prefix `/a` covers `/a` and `/a/x`, not `/ab` or `/A`.
pub fn path_within(prefix: &str, raw_path: &str) -> bool {
// Tests first: not written yet.
raw_path.starts_with(prefix)
if !raw_path.starts_with('/')
|| !raw_path.bytes().all(|c| c.is_ascii_graphic())
|| raw_path.contains('\\')
{
return false;
}
let lower = raw_path.to_ascii_lowercase();
if ["%2e", "%2f", "%5c", "%25", "%00"]
.iter()
.any(|bad| lower.contains(bad))
{
return false;
}
// An empty segment anywhere but at the end (a trailing slash) is `//`.
if raw_path.contains("//") {
return false;
}
for seg in raw_path[1..].split('/') {
// `..;x` is `..` to Tomcat and friends.
let name = seg.split(';').next().unwrap_or(seg);
if !name.is_empty() && name.bytes().all(|c| c == b'.') {
return false;
}
}
if prefix == "/" {
return true;
}
match raw_path.strip_prefix(prefix) {
Some(rest) => rest.is_empty() || rest.starts_with('/'),
None => false,
}
}