Skip to content

Commit

Permalink
Check authorization when attempting to delete a database
Browse files Browse the repository at this point in the history
  • Loading branch information
gefjon committed Jul 24, 2023
1 parent 92743d2 commit bdbdde2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
18 changes: 14 additions & 4 deletions crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,22 @@ pub struct DeleteDatabaseParams {
pub async fn delete_database(
State(ctx): State<Arc<dyn ControlCtx>>,
Path(DeleteDatabaseParams { address }): Path<DeleteDatabaseParams>,
auth: SpacetimeAuthHeader,
) -> axum::response::Result<impl IntoResponse> {
// TODO(cloutiertyler): Validate that the creator has credentials for the identity of this database

ctx.delete_database(&address).await.map_err(log_and_500)?;
let auth = auth.get().ok_or((StatusCode::BAD_REQUEST, "Invalid credentials."))?;

Ok(())
match ctx.control_db().get_database_by_address(&address).await.map_err(log_and_500)? {
Some(db) => {
if db.identity != auth.identity {
Err((StatusCode::BAD_REQUEST, "Identity does not own this database.").into())
} else {
ctx.delete_database(&address).await.map_err(log_and_500).map_err(Into::into)
}
}
None => {
Ok(())
}
}
}

#[derive(Deserialize)]
Expand Down
19 changes: 19 additions & 0 deletions test/tests/permissions-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

if [ "$DESCRIBE_TEST" = 1 ] ; then
echo "This test checks to make sure that you cannot delete a database that you do not own."
exit
fi

set -euox pipefail

source "./test/lib.include"

run_test cargo run identity new --no-email
IDENT=$(grep IDENTITY "$TEST_OUT" | awk '{print $2}')
run_test cargo run identity set-default "$IDENT"
run_test cargo run publish -s -d --project-path="$PROJECT_PATH" --clear-database
ADDRESS="$(grep "reated new database" "$TEST_OUT" | awk 'NF>1{print $NF}')"

reset_config
if cargo run delete "$ADDRESS"; then exit 1; fi

0 comments on commit bdbdde2

Please sign in to comment.