Files
faunosteo/crates/worker/src/keys.rs
T
bchanot cd2dbb3b79 fix(supabase): security pass on the files foundation — ceiling counts live bytes only, confirm checks the account, null sha256 refused, browser RPCs one role; worker: cleartext only on loopback or a service name, no env proxy, key ownership cross-check, no library text in logs
Security audit (PASS, 2 MEDIUM + LOW): the global ceiling summed every
non-deleted row, so rejected uploads ratcheted it toward storage_full with no
disk used → in-flight rows by declared size, clean rows by byte_size, nothing
else (pgTAP: rejected rows do not count, in-flight rows do); confirm refuses a
deactivated account like begin; complete_avatar_job refuses a null sha256;
begin / confirm / remove granted to authenticated only. Worker: SUPABASE_URL
over http:// accepted only for loopback or a single-label Docker service
name; ureq proxy(None); the job's keys must embed its owner and asset ids;
backend_failure / claim_failed / sweep_failure lines carry the closed kind and
the HTTP status, never the library's error text. e2e: uuid shape checked
before the psql query. Contract revision 2 (oracle fixes).
2026-10-13 19:49:58 +02:00

157 lines
4.9 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)
}
}
/// 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"));
}
}