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:
@@ -765,12 +765,13 @@ async fn start_reconstitute(
|
|||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
Json(req): Json<ReconstituteRequest>,
|
Json(req): Json<ReconstituteRequest>,
|
||||||
) -> Result<Json<ReconstituteStartResponse>, StatusCode> {
|
) -> Result<Json<ReconstituteStartResponse>, StatusCode> {
|
||||||
let actor = admin_write(&state, peer, &headers)?;
|
admin_write(&state, peer, &headers)?;
|
||||||
let destination = if actor.browser {
|
// Every caller, browser or API token, may only name a file inside the hub's exports directory,
|
||||||
browser_export_path(&state, &req.target_path)?
|
// and never one that already exists.
|
||||||
} else {
|
let destination = browser_export_path(&state, &req.target_path)?;
|
||||||
req.target_path.clone().into()
|
if destination.symlink_metadata().is_ok() {
|
||||||
};
|
return Err(StatusCode::CONFLICT);
|
||||||
|
}
|
||||||
let vault = state.vault.read().await;
|
let vault = state.vault.read().await;
|
||||||
if vault
|
if vault
|
||||||
.get_snapshot(req.snapshot_id)
|
.get_snapshot(req.snapshot_id)
|
||||||
@@ -1600,6 +1601,36 @@ mod tests {
|
|||||||
assert_eq!(r.status(), StatusCode::UNAUTHORIZED);
|
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]
|
#[tokio::test]
|
||||||
async fn a_failed_denial_audit_write_still_answers_401() {
|
async fn a_failed_denial_audit_write_still_answers_401() {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ impl Offsite {
|
|||||||
)?;
|
)?;
|
||||||
let size = file.as_file().metadata()?.len();
|
let size = file.as_file().metadata()?.len();
|
||||||
file.as_file().sync_all()?;
|
file.as_file().sync_all()?;
|
||||||
file.persist(dest)?;
|
file.persist_noclobber(dest)?;
|
||||||
Ok(size)
|
Ok(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -655,7 +655,8 @@ impl Vault {
|
|||||||
}
|
}
|
||||||
let len = temp.as_file().metadata()?.len();
|
let len = temp.as_file().metadata()?.len();
|
||||||
temp.as_file().sync_all()?;
|
temp.as_file().sync_all()?;
|
||||||
temp.persist(dest)?;
|
// never replace a file that is already there
|
||||||
|
temp.persist_noclobber(dest)?;
|
||||||
Ok(len)
|
Ok(len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
tests/fixtures/capture-mysql.py
vendored
4
tests/fixtures/capture-mysql.py
vendored
@@ -89,8 +89,8 @@ def main():
|
|||||||
assert result["phase"] == "dump_failed" and result["snapshot_id"] is None, result
|
assert result["phase"] == "dump_failed" and result["snapshot_id"] is None, result
|
||||||
continue
|
continue
|
||||||
assert result["status"] == "completed", (name, result)
|
assert result["status"] == "completed", (name, result)
|
||||||
target = root / f"{name}.sql"
|
target = root / "hub" / "exports" / f"{name}.sql"
|
||||||
session = api("reconstitute", {"snapshot_id": result["snapshot_id"], "target_path": str(target)})
|
session = api("reconstitute", {"snapshot_id": result["snapshot_id"], "target_path": target.name})
|
||||||
wait_until(lambda: api(f"reconstitute/{session['session_id']}")["state"] == "completed")
|
wait_until(lambda: api(f"reconstitute/{session['session_id']}")["state"] == "completed")
|
||||||
sql = target.read_text()
|
sql = target.read_text()
|
||||||
assert all(marker in sql for marker in ("sample_view", "sample_routine", "sample_event", "backup 🛰"))
|
assert all(marker in sql for marker in ("sample_view", "sample_routine", "sample_event", "backup 🛰"))
|
||||||
|
|||||||
4
tests/fixtures/capture-postgres.py
vendored
4
tests/fixtures/capture-postgres.py
vendored
@@ -125,8 +125,8 @@ def main():
|
|||||||
assert failed["snapshot_id"] is None
|
assert failed["snapshot_id"] is None
|
||||||
snapshots = api("snapshots")
|
snapshots = api("snapshots")
|
||||||
assert {snap["id"] for snap in snapshots} == {successful["snapshot_id"], through_bastion["snapshot_id"]}
|
assert {snap["id"] for snap in snapshots} == {successful["snapshot_id"], through_bastion["snapshot_id"]}
|
||||||
dump = root / "backup.dump"
|
dump = root / "hub" / "exports" / "backup.dump"
|
||||||
session = api("reconstitute", {"snapshot_id": successful["snapshot_id"], "target_path": str(dump)})
|
session = api("reconstitute", {"snapshot_id": successful["snapshot_id"], "target_path": dump.name})
|
||||||
wait_until(lambda: api(f"reconstitute/{session['session_id']}")["state"] == "completed")
|
wait_until(lambda: api(f"reconstitute/{session['session_id']}")["state"] == "completed")
|
||||||
command("pg_restore", *pg, "-d", "target_db", "--exit-on-error", dump)
|
command("pg_restore", *pg, "-d", "target_db", "--exit-on-error", dump)
|
||||||
restored = command("psql", *pg, "-d", "target_db", "-Atc",
|
restored = command("psql", *pg, "-d", "target_db", "-Atc",
|
||||||
|
|||||||
Reference in New Issue
Block a user