179 lines
5.8 KiB
Rust
179 lines
5.8 KiB
Rust
//! Shape of the object keys the worker accepts from the database (STORAGE.md §1.4). A key
|
|
//! is validated before it reaches a URL, so a corrupted or hostile row can never make the
|
|
//! worker read or delete outside the two prefixes.
|
|
|
|
use std::fmt;
|
|
|
|
/// Final avatar object: `profiles/<owner_id>/<asset_id>/avatar.webp`.
|
|
pub const FINAL_PREFIX: &str = "profiles";
|
|
/// Upload landing key: `quarantine/<owner_id>/<asset_id>/upload`.
|
|
pub const QUARANTINE_PREFIX: &str = "quarantine";
|
|
|
|
/// Why a key or an id was refused.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum KeyError {
|
|
Segments,
|
|
Prefix,
|
|
Uuid,
|
|
FileName,
|
|
}
|
|
|
|
impl fmt::Display for KeyError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
let text = match self {
|
|
Self::Segments => "key must have four segments",
|
|
Self::Prefix => "unknown key prefix",
|
|
Self::Uuid => "segment is not a lowercase uuid",
|
|
Self::FileName => "unexpected file name",
|
|
};
|
|
f.write_str(text)
|
|
}
|
|
}
|
|
|
|
/// Lowercase hyphenated uuid (36 characters).
|
|
pub fn is_uuid(text: &str) -> bool {
|
|
let bytes = text.as_bytes();
|
|
bytes.len() == 36
|
|
&& bytes.iter().enumerate().all(|(i, b)| {
|
|
if matches!(i, 8 | 13 | 18 | 23) {
|
|
*b == b'-'
|
|
} else {
|
|
b.is_ascii_digit() || (b'a'..=b'f').contains(b)
|
|
}
|
|
})
|
|
}
|
|
|
|
fn validate(key: &str, prefix: &str, file_name: &str) -> Result<(), KeyError> {
|
|
let segments: Vec<&str> = key.split('/').collect();
|
|
let [p, owner, asset, name] = segments.as_slice() else {
|
|
return Err(KeyError::Segments);
|
|
};
|
|
if *p != prefix {
|
|
return Err(KeyError::Prefix);
|
|
}
|
|
if !is_uuid(owner) || !is_uuid(asset) {
|
|
return Err(KeyError::Uuid);
|
|
}
|
|
if *name != file_name {
|
|
return Err(KeyError::FileName);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// `profiles/<uuid>/<uuid>/avatar.webp`.
|
|
pub fn validate_object_key(key: &str) -> Result<(), KeyError> {
|
|
validate(key, FINAL_PREFIX, "avatar.webp")
|
|
}
|
|
|
|
/// `quarantine/<uuid>/<uuid>/upload`.
|
|
pub fn validate_quarantine_key(key: &str) -> Result<(), KeyError> {
|
|
validate(key, QUARANTINE_PREFIX, "upload")
|
|
}
|
|
|
|
/// Either shape: the only keys the worker may delete.
|
|
pub fn validate_any_key(key: &str) -> Result<(), KeyError> {
|
|
if key.starts_with("profiles/") {
|
|
validate_object_key(key)
|
|
} else {
|
|
validate_quarantine_key(key)
|
|
}
|
|
}
|
|
|
|
/// Object key of a sheet drawing (bucket `anatomical-sheet-assets`): `<sha256 hex>.svg`,
|
|
/// lowercase (V1-2.3, M6 `get_sheet_verification_input`).
|
|
pub fn is_drawing_key(key: &str) -> bool {
|
|
key.strip_suffix(".svg").is_some_and(|hash| {
|
|
hash.len() == 64
|
|
&& hash
|
|
.bytes()
|
|
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
})
|
|
}
|
|
|
|
/// True when a (valid) key's owner and asset segments are exactly these ids.
|
|
pub fn belongs_to(key: &str, owner_id: &str, asset_id: &str) -> bool {
|
|
let mut segments = key.split('/');
|
|
segments.next();
|
|
segments.next() == Some(owner_id) && segments.next() == Some(asset_id)
|
|
}
|
|
|
|
/// The asset id embedded in a valid quarantine key.
|
|
pub fn asset_id_of_quarantine_key(key: &str) -> Option<&str> {
|
|
validate_quarantine_key(key).ok()?;
|
|
key.split('/').nth(2)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const U1: &str = "10000000-0000-0000-0000-00000000000a";
|
|
const U2: &str = "7f2f0c3e-1c7a-4b2b-9c8d-0123456789ab";
|
|
|
|
#[test]
|
|
fn valid_keys_are_accepted() {
|
|
assert_eq!(
|
|
validate_object_key(&format!("{FINAL_PREFIX}/{U1}/{U2}/avatar.webp")),
|
|
Ok(())
|
|
);
|
|
assert_eq!(
|
|
validate_quarantine_key(&format!("{QUARANTINE_PREFIX}/{U1}/{U2}/upload")),
|
|
Ok(())
|
|
);
|
|
assert_eq!(
|
|
asset_id_of_quarantine_key(&format!("{QUARANTINE_PREFIX}/{U1}/{U2}/upload")),
|
|
Some(U2)
|
|
);
|
|
assert_eq!(
|
|
validate_any_key(&format!("{FINAL_PREFIX}/{U1}/{U2}/avatar.webp")),
|
|
Ok(())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn deviations_are_refused() {
|
|
let cases: &[(&str, KeyError)] = &[
|
|
("profiles/x/avatar.webp", KeyError::Segments),
|
|
(
|
|
&format!("profiles/{U1}/{U2}/avatar.webp/"),
|
|
KeyError::Segments,
|
|
),
|
|
(&format!("avatars/{U1}/{U2}/avatar.webp"), KeyError::Prefix),
|
|
(&format!("profiles/{U1}/../avatar.webp"), KeyError::Uuid),
|
|
(
|
|
&format!("profiles/{}/{U2}/avatar.webp", U1.to_uppercase()),
|
|
KeyError::Uuid,
|
|
),
|
|
(
|
|
&format!("profiles/{U1}/{U2}/avatar.png"),
|
|
KeyError::FileName,
|
|
),
|
|
(&format!("profiles/{U1}/{U2}/upload"), KeyError::FileName),
|
|
(
|
|
&format!("quarantine/{U1}/{U2}/avatar.webp"),
|
|
KeyError::FileName,
|
|
),
|
|
];
|
|
for (key, expected) in cases {
|
|
assert_eq!(validate_any_key(key).err(), Some(*expected), "{key}");
|
|
}
|
|
assert_eq!(asset_id_of_quarantine_key("quarantine/a/b/upload"), None);
|
|
let key = format!("profiles/{U1}/{U2}/avatar.webp");
|
|
assert!(belongs_to(&key, U1, U2));
|
|
assert!(!belongs_to(&key, U2, U1));
|
|
assert!(!belongs_to(&key, U1, U1));
|
|
assert!(!is_uuid("10000000-0000-0000-0000-00000000000"));
|
|
}
|
|
|
|
#[test]
|
|
fn drawing_keys_are_a_lowercase_sha256_and_svg() {
|
|
let hash = "0123456789abcdef".repeat(4);
|
|
assert!(is_drawing_key(&format!("{hash}.svg")));
|
|
assert!(!is_drawing_key(&format!("{}.svg", hash.to_uppercase())));
|
|
assert!(!is_drawing_key(&format!("{hash}.png")));
|
|
assert!(!is_drawing_key(&format!("{}.svg", &hash[1..])));
|
|
assert!(!is_drawing_key(&format!("../{}.svg", &hash[3..])));
|
|
assert!(!is_drawing_key(&format!("x/{hash}.svg")));
|
|
}
|
|
}
|