fix: confine every reconstitute target to the exports directory and never overwrite an existing file

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 21:11:34 +07:00
parent c18815eb5d
commit edc74d79c0
5 changed files with 44 additions and 12 deletions

View File

@@ -765,12 +765,13 @@ async fn start_reconstitute(
headers: HeaderMap,
Json(req): Json<ReconstituteRequest>,
) -> Result<Json<ReconstituteStartResponse>, StatusCode> {
let actor = admin_write(&state, peer, &headers)?;
let destination = if actor.browser {
browser_export_path(&state, &req.target_path)?
} else {
req.target_path.clone().into()
};
admin_write(&state, peer, &headers)?;
// Every caller, browser or API token, may only name a file inside the hub's exports directory,
// and never one that already exists.
let destination = browser_export_path(&state, &req.target_path)?;
if destination.symlink_metadata().is_ok() {
return Err(StatusCode::CONFLICT);
}
let vault = state.vault.read().await;
if vault
.get_snapshot(req.snapshot_id)
@@ -1600,6 +1601,36 @@ mod tests {
assert_eq!(r.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn reconstitute_targets_stay_inside_the_exports_directory_for_every_caller() {
let dir = tempfile::tempdir().unwrap();
let mut state = HubState::bootstrap(dir.path().to_str().unwrap(), "synthetic-vault")
.await
.unwrap();
state.admin.token = Some("synthetic-admin-token-of-32-chars!".into());
let app = router(state);
let attempt = |path: &str| {
Request::post("/api/v1/reconstitute")
.extension(ConnectInfo("203.0.113.30:9000".parse::<SocketAddr>().unwrap()))
.header(header::AUTHORIZATION, "Bearer synthetic-admin-token-of-32-chars!")
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(
serde_json::json!({"snapshot_id": Uuid::new_v4(), "target_path": path}).to_string(),
))
.unwrap()
};
for path in ["/etc/passwd", "../escape", "a/b", "..", ""] {
let r = app.clone().oneshot(attempt(path)).await.unwrap();
assert_eq!(r.status(), StatusCode::BAD_REQUEST, "{path}");
}
// a plain file name passes confinement (the unknown snapshot is what stops it)
let r = app.clone().oneshot(attempt("dump.sql")).await.unwrap();
assert_eq!(r.status(), StatusCode::NOT_FOUND);
std::fs::write(dir.path().join("exports").join("taken.sql"), b"keep").unwrap();
let r = app.oneshot(attempt("taken.sql")).await.unwrap();
assert_eq!(r.status(), StatusCode::CONFLICT);
}
#[tokio::test]
async fn a_failed_denial_audit_write_still_answers_401() {
let dir = tempfile::tempdir().unwrap();