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
30 changes: 24 additions & 6 deletions pkgs/id/src/fileops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub enum FileOpError {
/// The new name was empty or whitespace.
#[error("name cannot be empty")]
EmptyName,
/// The name is rooted or contains a traversal component.
#[error("name must be a safe relative path: {0}")]
InvalidName(String),
/// Source and destination are the same name.
#[error("new name must differ from the current name")]
SameName,
Expand Down Expand Up @@ -194,11 +197,28 @@ impl<'a> FileOps<'a> {
}
}

fn validate_target(from: &str, to: &str) -> Result<String, FileOpError> {
let to = to.trim();
if to.is_empty() {
fn validate_name(name: &str) -> Result<(), FileOpError> {
if name.trim().is_empty() {
return Err(FileOpError::EmptyName);
}
let bytes = name.as_bytes();
let has_windows_drive_prefix =
bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':';
if name.starts_with('/')
|| name.starts_with('\\')
|| has_windows_drive_prefix
|| name
.split(['/', '\\'])
.any(|component| matches!(component, "." | ".."))
{
return Err(FileOpError::InvalidName(name.to_owned()));
}
Ok(())
}

fn validate_target(from: &str, to: &str) -> Result<String, FileOpError> {
let to = to.trim();
Self::validate_name(to)?;
if from == to {
return Err(FileOpError::SameName);
}
Expand All @@ -221,9 +241,7 @@ impl<'a> FileOps<'a> {
/// Sets the `created` tag only if absent and always refreshes `modified`,
/// then auto-tags `name`/`file`.
pub async fn put(&self, name: &str, hash: Hash) -> Result<(), FileOpError> {
if name.trim().is_empty() {
return Err(FileOpError::EmptyName);
}
Self::validate_name(name)?;
if !self.blob_present(hash).await? {
return Err(FileOpError::BlobMissing(hash));
}
Expand Down
119 changes: 119 additions & 0 deletions pkgs/id/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,125 @@ mod tests {
node.shutdown().await.unwrap();
}

#[tokio::test]
async fn fileops_reject_unsafe_names_without_mutating_files() {
let node = LocalNode::open(true).await.unwrap();
let source_hash = put(&node, "source.txt", b"source").await;
let upload = node
.blobs()
.add_bytes(b"candidate".to_vec())
.temp_tag()
.await
.unwrap();
let put_hash = upload.hash();
let invalid_names = [
"../escape.txt",
"nested/../escape.txt",
"./dot.txt",
"/rooted.txt",
r"..\escape.txt",
r"nested\..\escape.txt",
r"\rooted.txt",
r"\\server\share.txt",
r"C:\rooted.txt",
"C:/rooted.txt",
"C:relative.txt",
];

for invalid in invalid_names {
let put_response = node
.request(MetaRequest::Put {
filename: invalid.to_owned(),
hash: put_hash,
})
.await;
assert!(
put_response
.error_message()
.is_some_and(|message| message.contains("safe relative path")),
"put accepted unsafe name {invalid:?}: {put_response:?}"
);

let rename_response = node
.request(MetaRequest::Rename {
from: "source.txt".to_owned(),
to: invalid.to_owned(),
})
.await;
assert!(
rename_response
.error_message()
.is_some_and(|message| message.contains("safe relative path")),
"rename accepted unsafe name {invalid:?}: {rename_response:?}"
);

let copy_response = node
.request(MetaRequest::Copy {
from: "source.txt".to_owned(),
to: invalid.to_owned(),
})
.await;
assert!(
copy_response
.error_message()
.is_some_and(|message| message.contains("safe relative path")),
"copy accepted unsafe name {invalid:?}: {copy_response:?}"
);

assert_eq!(names(&node).await, vec!["source.txt".to_owned()]);
assert!(tags_of(&node, invalid).await.is_empty());
assert!(matches!(
node.request(MetaRequest::Get {
filename: "source.txt".to_owned(),
})
.await,
MetaResponse::Get { hash: Some(hash) } if hash == source_hash
));
}

node.shutdown().await.unwrap();
}

#[tokio::test]
async fn fileops_accept_safe_nested_names_for_rename_and_copy() {
let node = LocalNode::open(true).await.unwrap();
let hash = put(&node, "dir/a.txt", b"hello").await;

assert!(matches!(
node.request(MetaRequest::Rename {
from: "dir/a.txt".to_owned(),
to: "renamed/deep.txt".to_owned(),
})
.await,
MetaResponse::Rename { success: true }
));
assert!(matches!(
node.request(MetaRequest::Copy {
from: "renamed/deep.txt".to_owned(),
to: "copies/copy.txt".to_owned(),
})
.await,
MetaResponse::Copy { success: true }
));

assert!(matches!(
node.request(MetaRequest::Get {
filename: "renamed/deep.txt".to_owned(),
})
.await,
MetaResponse::Get { hash: Some(found) } if found == hash
));
assert!(matches!(
node.request(MetaRequest::Get {
filename: "copies/copy.txt".to_owned(),
})
.await,
MetaResponse::Get { hash: Some(found) } if found == hash
));

node.shutdown().await.unwrap();
}

#[tokio::test]
async fn deltag_without_value_deletes_every_value_of_the_key() {
// Regression: the handler used to treat `None` as "the key-only tag",
Expand Down
Loading