diff --git a/pkgs/id/src/fileops.rs b/pkgs/id/src/fileops.rs index 7aa2b0092d..0d057b584b 100644 --- a/pkgs/id/src/fileops.rs +++ b/pkgs/id/src/fileops.rs @@ -51,6 +51,9 @@ pub enum FileOpError { /// The name is rooted or contains a traversal component. #[error("name must be a safe relative path: {0}")] InvalidName(String), + /// The name collides with the namespace used for system archives. + #[error("name is reserved for archives: {0}")] + ReservedArchiveName(String), /// Source and destination are the same name. #[error("new name must differ from the current name")] SameName, @@ -234,6 +237,9 @@ impl<'a> FileOps<'a> { { return Err(FileOpError::InvalidName(name.to_owned())); } + if is_archive_tag_name(name) { + return Err(FileOpError::ReservedArchiveName(name.to_owned())); + } Ok(()) } @@ -598,6 +604,24 @@ mod archive_name_tests { Hash::from([byte; 32]) } + #[test] + fn fileops_rejects_archive_shaped_names_but_allows_lookalikes() { + for name in [ + "report.archive.0", + "report.archive.1700000000", + "report.archive.18446744073709551616", + ] { + assert!(matches!( + FileOps::validate_name(name), + Err(FileOpError::ReservedArchiveName(found)) if found == name + )); + } + + for name in ["a.archive.notes", "a.archive.+1", "a.archive.١"] { + assert!(FileOps::validate_name(name).is_ok(), "{name:?}"); + } + } + /// Two replaces of one name within the same second must not share an /// archive tag (#1612): the second archive is bumped to the next free /// numeric suffix, and both replaced versions stay archived. diff --git a/pkgs/id/src/protocol.rs b/pkgs/id/src/protocol.rs index 0d7ae41cd1..ef5dc394d6 100644 --- a/pkgs/id/src/protocol.rs +++ b/pkgs/id/src/protocol.rs @@ -1563,6 +1563,75 @@ mod tests { node.shutdown().await.unwrap(); } + #[tokio::test] + async fn fileops_rejects_archive_names_without_overwriting_existing_archives() { + let node = LocalNode::open(true).await.unwrap(); + let archive_hash = put(&node, "report.txt", b"original").await; + assert!(matches!( + node.request(MetaRequest::Rename { + from: "report.txt".to_owned(), + to: "renamed.txt".to_owned(), + }) + .await, + MetaResponse::Rename { success: true } + )); + let archive_name = names(&node) + .await + .into_iter() + .find(|name| name.starts_with("report.txt.archive.")) + .expect("rename should create an archive"); + let replacement_hash = put(&node, "source.txt", b"replacement").await; + + let put_response = node + .request(MetaRequest::Put { + filename: archive_name.clone(), + hash: replacement_hash, + }) + .await; + assert!( + put_response + .error_message() + .is_some_and(|message| message.contains("reserved for archives")), + "put accepted archive-shaped name: {put_response:?}" + ); + + let rename_response = node + .request(MetaRequest::Rename { + from: "source.txt".to_owned(), + to: archive_name.clone(), + }) + .await; + assert!( + rename_response + .error_message() + .is_some_and(|message| message.contains("reserved for archives")), + "rename accepted archive-shaped name: {rename_response:?}" + ); + + let copy_response = node + .request(MetaRequest::Copy { + from: "source.txt".to_owned(), + to: archive_name.clone(), + }) + .await; + assert!( + copy_response + .error_message() + .is_some_and(|message| message.contains("reserved for archives")), + "copy accepted archive-shaped name: {copy_response:?}" + ); + + assert!(matches!( + node.request(MetaRequest::Get { + filename: archive_name.clone(), + }) + .await, + MetaResponse::Get { hash: Some(hash) } if hash == archive_hash + )); + assert!(names(&node).await.contains(&archive_name)); + node.shutdown().await.unwrap(); + } + #[tokio::test] async fn fileops_accept_safe_nested_names_for_rename_and_copy() { let node = LocalNode::open(true).await.unwrap(); diff --git a/pkgs/id/src/web/routes.rs b/pkgs/id/src/web/routes.rs index 9ddf46a3f0..578c593d18 100644 --- a/pkgs/id/src/web/routes.rs +++ b/pkgs/id/src/web/routes.rs @@ -1892,6 +1892,9 @@ async fn rename_handler(State(state): State, Json(req): Json { (StatusCode::BAD_REQUEST, "New name cannot be empty").into_response() } + Err(err @ FileOpError::ReservedArchiveName(_)) => { + (StatusCode::BAD_REQUEST, err.to_string()).into_response() + } Err(FileOpError::SameName) => ( StatusCode::BAD_REQUEST, "New name must differ from current name", @@ -1934,6 +1937,9 @@ async fn copy_handler(State(state): State, Json(req): Json { (StatusCode::BAD_REQUEST, "New name cannot be empty").into_response() } + Err(err @ FileOpError::ReservedArchiveName(_)) => { + (StatusCode::BAD_REQUEST, err.to_string()).into_response() + } Err(FileOpError::SameName) => ( StatusCode::BAD_REQUEST, "New name must differ from current name",