Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkgs/id/src/fileops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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.
Expand Down
69 changes: 69 additions & 0 deletions pkgs/id/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,9 @@ async fn rename_handler(State(state): State<AppState>, Json(req): Json<RenameReq
Err(FileOpError::EmptyName) => {
(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",
Expand Down Expand Up @@ -1934,6 +1937,9 @@ async fn copy_handler(State(state): State<AppState>, Json(req): Json<CopyRequest
Err(FileOpError::EmptyName) => {
(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",
Expand Down