Skip to content

Commit 585e0e2

Browse files
authored
BM-239: Sanity check ImgId in Bento REST API (github#64)
This PR adds a compute-image-id check during the upload phase of the Bento API, this prevents badly calculated image id's from persisting in a broker object store that would need to be manually cleared.
1 parent 980517f commit 585e0e2

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async-trait = { workspace = true }
1212
axum = "0.7"
1313
bonsai-sdk = { workspace = true }
1414
clap = { workspace = true, features = ["env", "derive"] }
15+
risc0-zkvm = { workspace = true }
1516
serde = { workspace = true }
1617
serde_json = { workspace = true }
1718
sqlx = { workspace = true }

crates/api/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use bonsai_sdk::responses::{
1717
SnarkReq, SnarkStatusRes, UploadRes,
1818
};
1919
use clap::Parser;
20+
use risc0_zkvm::compute_image_id;
2021
use serde::{Deserialize, Serialize};
2122
use sqlx::{postgres::PgPoolOptions, PgPool};
2223
use std::sync::Arc;
@@ -84,6 +85,9 @@ pub enum AppError {
8485
#[error("The provided imageid already exists: {0}")]
8586
ImgAlreadyExists(String),
8687

88+
#[error("The image id does not match the computed id, req: {0} comp: {1}")]
89+
ImageIdMismatch(String, String),
90+
8791
#[error("The provided inputid already exists: {0}")]
8892
InputAlreadyExists(String),
8993

@@ -108,6 +112,7 @@ impl AppError {
108112
match self {
109113
Self::ImageInvalid(_) => "ImageInvalid",
110114
Self::ImgAlreadyExists(_) => "ImgAlreadyExists",
115+
Self::ImageIdMismatch(_, _) => "ImageIdMismatch",
111116
Self::InputAlreadyExists(_) => "InputAlreadyExists",
112117
Self::ReceiptAlreadyExists(_) => "ReceiptAlreadyExists",
113118
Self::ReceiptMissing(_) => "ReceiptMissing",
@@ -128,7 +133,7 @@ impl From<AnyhowErr> for AppError {
128133
impl IntoResponse for AppError {
129134
fn into_response(self) -> Response {
130135
let code = match self {
131-
Self::ImageInvalid(_) => StatusCode::BAD_REQUEST,
136+
Self::ImageInvalid(_) | Self::ImageIdMismatch(_, _) => StatusCode::BAD_REQUEST,
132137
Self::ImgAlreadyExists(_)
133138
| Self::InputAlreadyExists(_)
134139
| Self::ReceiptAlreadyExists(_) => StatusCode::NO_CONTENT,
@@ -266,6 +271,13 @@ async fn image_upload_put(
266271

267272
let body_bytes =
268273
to_bytes(body, MAX_UPLOAD_SIZE).await.context("Failed to convert body to bytes")?;
274+
275+
let comp_img_id =
276+
compute_image_id(&body_bytes).context("Failed to compute image id")?.to_string();
277+
if comp_img_id != image_id {
278+
return Err(AppError::ImageIdMismatch(image_id, comp_img_id));
279+
}
280+
269281
state
270282
.s3_client
271283
.write_buf_to_s3(&new_img_key, body_bytes.to_vec())

0 commit comments

Comments
 (0)