feat(worker): R1 job loop — claim, download, sandboxed decode, clean upload, commit, purge sweeps
Keyed parent on ureq 3 (rustls + ring, bundled roots), no async runtime: config from the environment (secret never printed, JWT expiry logged), JSON line logs with a field allowlist (child stderr escaped and truncated), strict key validation before any URL, Backend trait over PostgREST RPCs and Storage objects with closed error kinds, one job per claim with the STORAGE.md §3.4 mapping (content codes, processing_error on signals / timeout / overflow, Broken stops the loop with exit 20, a refused commit deletes nothing), sweeper (expire pending, purge queue, quarantine leftovers), per-stage watchdog (exit 21), WORKER_ONCE for tests, run --check extended. 11 loop tests on a fake backend and decoder; sandbox tests and seccomp derivation unchanged (the child still makes the same 7 syscalls); image 11.6 MB.
This commit is contained in:
1 parent
926dd7df8c
commit
0410af0c35
18 files changed
+2921
-42
No files matched your search
@@ -0,0 +1,145 @@
|
||||
//! 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)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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);
|
||||
assert!(!is_uuid("10000000-0000-0000-0000-00000000000"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user