feat: download snapshots as real compressed archives (auth M6a)

- the download format follows the file's first bytes and manifest hints: plain SQL ->
  .sql.gz, tar -> .tar.gz, SQLite -> .sqlite.gz, hub filesystem archive -> .tessera-fs.gz,
  unknown -> .dump.gz; pg_dump -Fc stays .dump; gzip/zstd/xz/bzip2/zip pass through
- gzip is streamed (async-compression) in 64 KiB frames; no Content-Encoding, no
  Content-Length on compressed bodies, application/gzip, nosniff, no-store; HEAD is 405
- order: authenticate, find, permission, then admission (export slots and free disk), so a
  caller without permission cannot use up the slots
- the export directory, the slot and the audit end-of-download row live with the response
  body: finished, client disconnect, and a sweep of tessera-export-* at start after a crash
- audit export.requested / ready / finished / disconnected

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 17:27:06 +07:00
parent 228ca42801
commit e72b72111d
14 changed files with 725 additions and 41 deletions

View File

@@ -95,8 +95,15 @@ async fn fixture(state: &HubState, name: &str) -> Fixture {
}
async fn world() -> World {
world_with(None).await
}
async fn world_with(exports: Option<crate::exports::ExportGate>) -> World {
let dir = tempfile::tempdir().unwrap();
let mut state = HubState::bootstrap(dir.path().to_str().unwrap(), "synthetic-vault").await.unwrap();
if let Some(gate) = exports {
state.exports = std::sync::Arc::new(gate);
}
state.sessions = SessionGate::without_operator(state.db.clone()).with_oidc("matrix-fp");
let a = fixture(&state, "orbit-a").await;
let b = fixture(&state, "orbit-b").await;
@@ -571,3 +578,25 @@ async fn display_preferences_stick_to_the_account_and_are_validated() {
*request.uri_mut() = prefs.parse().unwrap();
assert_eq!(app.oneshot(request).await.unwrap().status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn a_caller_who_may_not_download_cannot_use_up_the_export_slots() {
// One slot in total. If admission ran before the permission check, a capture-only user could starve everyone.
let w = world_with(Some(crate::exports::ExportGate::new(1, 0))).await;
let capturer = w.caller("capturer", false, &[]);
let downloader = w.caller("downloader", false, &[]);
let nobody = w.caller("nobody", false, &[]);
w.grant_role(capturer.user_id.as_ref().unwrap(), "capturer", Some(w.a.orbit));
w.grant_role(downloader.user_id.as_ref().unwrap(), "downloader", Some(w.a.orbit));
let uri = format!("/api/v1/snapshots/{}/download", w.a.snapshot);
for _ in 0..5 {
assert_eq!(w.send(&capturer, "GET", &uri, None).await.0, StatusCode::FORBIDDEN);
assert_eq!(w.send(&nobody, "GET", &uri, None).await.0, StatusCode::NOT_FOUND);
assert_eq!(w.send(&anonymous(), "GET", &uri, None).await.0, StatusCode::UNAUTHORIZED);
}
assert_eq!(w.send(&downloader, "GET", &uri, None).await.0, StatusCode::OK, "the slot was never taken by the refused callers");
let audited: i64 = rusqlite::Connection::open(w._dir.path().join("tessera.db")).unwrap()
.query_row("SELECT COUNT(*) FROM audit_events WHERE action='export.requested'", [], |r| r.get(0)).unwrap();
assert_eq!(audited, 1, "only the permitted request counts as an export request");
}