feat: readable download names and PUT /capture-profiles/:id
- downloads are named <orbit>_<time>_<engine>_<id8>.<ext> (schedule zone or UTC), orbit names are reduced to [a-z0-9_-], and the header carries filename and filename* - operators can change a profile's schedule and retention (absent = keep, null = clear), validated like registration, audited, and a changed schedule never replays slots that already passed Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,87 @@ pub const EXPORT_PREFIX: &str = "tessera-export-";
|
||||
const DEFAULT_CONCURRENCY: usize = 2;
|
||||
const DEFAULT_MARGIN: u64 = 512 * 1024 * 1024;
|
||||
|
||||
/// Reduce free text to `[a-z0-9_-]`: anything else becomes `_`, runs collapse, and the ends are trimmed.
|
||||
fn slug(text: &str, limit: usize) -> String {
|
||||
let mut out = String::new();
|
||||
for c in text.chars() {
|
||||
let c = if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
|
||||
c.to_ascii_lowercase()
|
||||
} else {
|
||||
'_'
|
||||
};
|
||||
if c == '_' && out.ends_with('_') {
|
||||
continue;
|
||||
}
|
||||
out.push(c);
|
||||
}
|
||||
let out = out.trim_matches(['_', '-']);
|
||||
out.chars()
|
||||
.take(limit)
|
||||
.collect::<String>()
|
||||
.trim_matches(['_', '-'])
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// A readable file name for a download: `<application>_<time>_<engine>_<id8>.<extension>`.
|
||||
///
|
||||
/// The application is the orbit name reduced to `[a-z0-9_-]`; the time is when the backup was taken in the
|
||||
/// profile's schedule zone (UTC with `Z` when there is none); the first eight characters of the snapshot id
|
||||
/// keep two backups from the same minute apart. Nothing from the orbit name can reach the header unfiltered.
|
||||
pub fn download_name(
|
||||
orbit: &str,
|
||||
taken_at: chrono::DateTime<chrono::Utc>,
|
||||
zone: Option<&str>,
|
||||
engine: Option<&str>,
|
||||
id: Uuid,
|
||||
extension: &str,
|
||||
) -> String {
|
||||
let when = match zone.and_then(|zone| zone.parse::<chrono_tz::Tz>().ok()) {
|
||||
Some(tz) => taken_at
|
||||
.with_timezone(&tz)
|
||||
.format("%Y-%m-%dT%H%M%z")
|
||||
.to_string(),
|
||||
None => taken_at.format("%Y-%m-%dT%H%MZ").to_string(),
|
||||
};
|
||||
let application = match slug(orbit, 48) {
|
||||
name if name.is_empty() => "snapshot".to_string(),
|
||||
name => name,
|
||||
};
|
||||
let mut name = format!("{application}_{when}");
|
||||
if let Some(engine) = engine
|
||||
.map(|engine| slug(engine, 16))
|
||||
.filter(|engine| !engine.is_empty())
|
||||
{
|
||||
name.push('_');
|
||||
name.push_str(&engine);
|
||||
}
|
||||
let short: String = id.simple().to_string().chars().take(8).collect();
|
||||
format!("{name}_{short}.{extension}")
|
||||
}
|
||||
|
||||
/// `Content-Disposition` with both the plain `filename` and the RFC 5987 `filename*` forms.
|
||||
pub fn content_disposition(name: &str) -> String {
|
||||
let plain: String = name
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || "-._+".contains(c) {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut encoded = String::new();
|
||||
for byte in name.bytes() {
|
||||
if byte.is_ascii_alphanumeric() || b"-._~".contains(&byte) {
|
||||
encoded.push(byte as char);
|
||||
} else {
|
||||
encoded.push_str(&format!("%{byte:02X}"));
|
||||
}
|
||||
}
|
||||
format!("attachment; filename=\"{plain}\"; filename*=UTF-8''{encoded}")
|
||||
}
|
||||
|
||||
/// How a snapshot is delivered.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Plan {
|
||||
@@ -237,6 +318,100 @@ impl<S> Drop for Guarded<S> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
fn taken() -> chrono::DateTime<Utc> {
|
||||
Utc.with_ymd_and_hms(2026, 11, 14, 19, 30, 5).unwrap()
|
||||
}
|
||||
fn id() -> Uuid {
|
||||
Uuid::parse_str("a1b2c3d4-0000-4000-8000-000000000000").unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_download_is_named_after_the_application_the_time_and_the_engine() {
|
||||
let name = download_name(
|
||||
"lti",
|
||||
taken(),
|
||||
Some("Asia/Jakarta"),
|
||||
Some("postgres"),
|
||||
id(),
|
||||
"dump",
|
||||
);
|
||||
assert_eq!(name, "lti_2026-11-15T0230+0700_postgres_a1b2c3d4.dump");
|
||||
let name = download_name("boiler", taken(), None, Some("mysql"), id(), "sql.gz");
|
||||
assert_eq!(name, "boiler_2026-11-14T1930Z_mysql_a1b2c3d4.sql.gz");
|
||||
// no engine (a folder snapshot): the part is left out
|
||||
assert_eq!(
|
||||
download_name("docs", taken(), None, None, id(), "tar.gz"),
|
||||
"docs_2026-11-14T1930Z_a1b2c3d4.tar.gz"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_time_zone_falls_back_to_utc() {
|
||||
let name = download_name(
|
||||
"lti",
|
||||
taken(),
|
||||
Some("Mars/Olympus"),
|
||||
Some("postgres"),
|
||||
id(),
|
||||
"dump",
|
||||
);
|
||||
assert!(name.contains("_2026-11-14T1930Z_"), "{name}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_snapshots_in_the_same_minute_get_different_names() {
|
||||
let other = Uuid::parse_str("ffffffff-0000-4000-8000-000000000000").unwrap();
|
||||
let a = download_name("lti", taken(), None, Some("postgres"), id(), "dump");
|
||||
let b = download_name("lti", taken(), None, Some("postgres"), other, "dump");
|
||||
assert_ne!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_orbit_names_cannot_escape_the_file_name_or_the_header() {
|
||||
for orbit in [
|
||||
"../../etc/passwd",
|
||||
"a\"b\r\nSet-Cookie: x=1",
|
||||
"name; filename=evil.exe",
|
||||
"Ünïcode データ",
|
||||
"\u{202e}gnp.exe",
|
||||
" ",
|
||||
"",
|
||||
&"x".repeat(500),
|
||||
] {
|
||||
let name = download_name(orbit, taken(), None, Some("postgres"), id(), "dump");
|
||||
assert!(
|
||||
name.bytes().all(|b| b.is_ascii_lowercase()
|
||||
|| b.is_ascii_digit()
|
||||
|| b"_-.+TZ".contains(&b)),
|
||||
"{name:?}"
|
||||
);
|
||||
assert!(!name.contains(".."), "{name:?}");
|
||||
assert!(name.len() <= 100, "{name:?}");
|
||||
let header = content_disposition(&name);
|
||||
let value = axum::http::HeaderValue::from_str(&header).expect("a valid header value");
|
||||
// the only `=` signs are the two the header itself writes: nothing from the name adds a parameter
|
||||
assert_eq!(header.matches("filename=").count(), 1, "{header}");
|
||||
assert_eq!(header.matches("filename*=").count(), 1, "{header}");
|
||||
assert_eq!(header.matches(';').count(), 2, "{header}");
|
||||
assert_eq!(value.to_str().unwrap(), header);
|
||||
assert!(!header.contains(['\r', '\n']));
|
||||
}
|
||||
let name = download_name("", taken(), None, None, id(), "dump");
|
||||
assert!(name.starts_with("snapshot_"), "{name}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_header_carries_both_filename_forms() {
|
||||
let header = content_disposition("lti_2026-11-15T0230+0700_postgres_a1b2c3d4.dump");
|
||||
assert_eq!(
|
||||
header,
|
||||
"attachment; filename=\"lti_2026-11-15T0230+0700_postgres_a1b2c3d4.dump\"; \
|
||||
filename*=UTF-8''lti_2026-11-15T0230%2B0700_postgres_a1b2c3d4.dump"
|
||||
);
|
||||
}
|
||||
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user