From 4d423fbdffdf849fc47225efd4b6793a6eff5af3 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Wed, 28 Oct 2026 09:48:03 -0500 Subject: [PATCH] fix(id): uniquify colliding upload stored names (#1549) upload_handler set the blob tag under the supplied filename unconditionally, so a second paste named image.png silently re-pointed the name at the new blob and un-named the first image. Per the triage ruling on #1549 (archive-first rejected: it renames the FIRST image), the handler now resolves the stored name after the blob hash is known: the intended name is kept when free or already naming the same blob (idempotent re-upload of identical bytes); when a different blob owns it, the upload is stored as stem-N.ext with the smallest free N >= 1. The paste-{secs} fallback passes through the same uniquifier. Tag, filename-keyed metadata, and UploadResponse name/url all use the stored name. Regression tests: two same-name uploads with different bytes (both listed and resolvable, first name still resolves to the first hash, second response carries image-1.png, identical re-upload idempotent) and two nameless uploads never sharing a stored name. --- pkgs/id/src/web/routes.rs | 194 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) diff --git a/pkgs/id/src/web/routes.rs b/pkgs/id/src/web/routes.rs index b2082870d..50c1a02b5 100644 --- a/pkgs/id/src/web/routes.rs +++ b/pkgs/id/src/web/routes.rs @@ -1451,6 +1451,50 @@ fn image_too_large_response() -> Response { /// /// Accepts a single file field, validates the MIME type against allowed /// image types, stores the blob, creates a named tag, and sets metadata. +/// The hash `name` currently points at, if the name is taken (#1549). +async fn upload_name_owner( + store: &iroh_blobs::api::Store, + name: &str, +) -> Result, String> { + store + .tags() + .get(name) + .await + .map(|info| info.map(|tag| tag.hash)) + .map_err(|err| err.to_string()) +} + +/// Choose the stored name for an upload of `hash` intended as +/// `intended` (#1549): the intended name itself when it is free or +/// already names this exact blob (an idempotent re-upload of identical +/// bytes keeps its name), otherwise `stem-N.ext` with the smallest +/// N >= 1 that is free or already names this blob — so a later upload +/// never silently un-names an earlier, different image. +async fn uniquify_upload_name( + store: &iroh_blobs::api::Store, + intended: &str, + hash: iroh_blobs::Hash, +) -> Result { + match upload_name_owner(store, intended).await? { + None => return Ok(intended.to_owned()), + Some(owner) if owner == hash => return Ok(intended.to_owned()), + Some(_) => {} + } + let (stem, ext) = match intended.rfind('.') { + Some(dot) if dot > 0 => (&intended[..dot], &intended[dot..]), + _ => (intended, ""), + }; + for n in 1.. { + let candidate = format!("{stem}-{n}{ext}"); + match upload_name_owner(store, &candidate).await? { + None => return Ok(candidate), + Some(owner) if owner == hash => return Ok(candidate), + Some(_) => {} + } + } + unreachable!("the counter always finds a free name") +} + async fn upload_handler(State(state): State, mut multipart: Multipart) -> Response { // Read the first field from the multipart body let field = match multipart.next_field().await { @@ -1548,6 +1592,25 @@ async fn upload_handler(State(state): State, mut multipart: Multipart) let hash = outcome.hash; let hash_str = hash.to_string(); + // #1549: resolve the stored name before tagging anything. The + // intended name is kept when free or already naming this blob; a + // name owned by a different blob is uniquified (stem-N.ext), and + // the nameless paste-{ts} fallback passes through the same + // uniquifier, so two uploads can never land on the same stored + // name. The tag, the filename-keyed metadata, and the response + // below all use the stored name. + let filename = match uniquify_upload_name(&state.store, &filename, hash).await { + Ok(name) => name, + Err(err) => { + tracing::error!("[routes] Failed to resolve upload name: {}", err); + return ( + StatusCode::INTERNAL_SERVER_ERROR, + "Failed to resolve file name", + ) + .into_response(); + } + }; + // Set named tag let tag = iroh_blobs::api::Tag::from(filename.clone()); if let Err(err) = state.store.tags().set(tag, hash).await { @@ -2447,6 +2510,137 @@ mod upload_router_tests { (status, String::from_utf8_lossy(&bytes).into_owned()) } + async fn list_images(router: Router) -> Vec { + let response = router + .oneshot( + Request::builder() + .method("GET") + .uri("/api/images") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + serde_json::from_slice(&bytes).unwrap() + } + + fn multipart_body_noname(content_type: &str, file_bytes: &[u8]) -> Vec { + let mut body = Vec::new(); + body.extend_from_slice( + format!( + "--{BOUNDARY}\r\nContent-Disposition: form-data; name=\"file\"\r\n\ + Content-Type: {content_type}\r\n\r\n" + ) + .as_bytes(), + ); + body.extend_from_slice(file_bytes); + body.extend_from_slice(format!("\r\n--{BOUNDARY}--\r\n").as_bytes()); + body + } + + // #1549: a second upload under the same filename with different + // bytes must not silently un-name the first: the second is stored + // uniquified, both stay listed and resolvable, and the first name + // still resolves to the first hash. Re-uploading identical bytes + // under the same name is idempotent. + #[tokio::test] + async fn upload_same_name_different_bytes_uniquifies_second() { + let (router, _dir, _endpoint, _docs) = router().await; + let (status, first) = upload( + router.clone(), + multipart_body_named("image.png", "image/png", b"first-bytes"), + ) + .await; + assert_eq!(status, StatusCode::OK, "body: {first}"); + assert!(first.contains("\"name\":\"image.png\""), "body: {first}"); + let first_hash = uploaded_hash(&first); + + let (status, second) = upload( + router.clone(), + multipart_body_named("image.png", "image/png", b"second-bytes"), + ) + .await; + assert_eq!(status, StatusCode::OK, "body: {second}"); + assert!( + second.contains("\"name\":\"image-1.png\""), + "second upload stored uniquified: {second}" + ); + assert!(second.contains("filename=image-1.png"), "body: {second}"); + let second_hash = uploaded_hash(&second); + assert_ne!(first_hash, second_hash); + + let (status, again) = upload( + router.clone(), + multipart_body_named("image.png", "image/png", b"first-bytes"), + ) + .await; + assert_eq!(status, StatusCode::OK, "body: {again}"); + assert!( + again.contains("\"name\":\"image.png\""), + "identical re-upload keeps the plain name: {again}" + ); + assert_eq!(uploaded_hash(&again), first_hash); + + let images = list_images(router.clone()).await; + let by_name = |name: &str| { + images + .iter() + .find(|entry| entry["name"] == name) + .unwrap_or_else(|| panic!("{name} listed in {images:?}")) + }; + assert_eq!(by_name("image.png")["hash"], first_hash); + assert_eq!(by_name("image-1.png")["hash"], second_hash); + + let (_, _, bytes) = get_blob(router.clone(), &format!("/blob/{first_hash}")).await; + assert_eq!(bytes, b"first-bytes"); + let (_, _, bytes) = get_blob(router, &format!("/blob/{second_hash}")).await; + assert_eq!(bytes, b"second-bytes"); + } + + // #1549: the nameless paste-{secs} fallback passes through the + // same uniquifier, so two nameless uploads in the same second can + // never land on the same stored name. + #[tokio::test] + async fn upload_nameless_uploads_never_share_a_stored_name() { + let (router, _dir, _endpoint, _docs) = router().await; + let (status, first) = upload( + router.clone(), + multipart_body_noname("image/png", b"nameless-one"), + ) + .await; + assert_eq!(status, StatusCode::OK, "body: {first}"); + let (status, second) = upload( + router.clone(), + multipart_body_noname("image/png", b"nameless-two"), + ) + .await; + assert_eq!(status, StatusCode::OK, "body: {second}"); + let name_of = |body: &str| { + serde_json::from_str::(body).unwrap()["name"] + .as_str() + .unwrap() + .to_owned() + }; + let (first_name, second_name) = (name_of(&first), name_of(&second)); + assert!(first_name.starts_with("paste-"), "first: {first_name}"); + assert_ne!( + first_name, second_name, + "nameless uploads must not share a stored name" + ); + let images = list_images(router).await; + let hash_of = |name: &str| { + images + .iter() + .find(|entry| entry["name"] == name) + .map(|entry| entry["hash"].as_str().unwrap().to_owned()) + .unwrap_or_else(|| panic!("{name} listed in {images:?}")) + }; + assert_eq!(hash_of(&first_name), uploaded_hash(&first)); + assert_eq!(hash_of(&second_name), uploaded_hash(&second)); + } + // #751: a body over UPLOAD_BODY_LIMIT must surface as the clear 413, // not a generic 400. On PR #747's original code this returned 400, // because is_body_limit_error substring-matched MultipartError's