fix(content): validate provider download paths

This commit is contained in:
objz committed 2026-08-04 20:46:21 +02:00
1 parent fb976f1cb9
commit 95a25836a0
4 files changed
+177 -23

No files matched your search

+16
View File
@@ -389,6 +389,8 @@ pub async fn download_version_file(
destination: &std::path::Path,
) -> Result<DownloadOutcome, crate::net::NetError> {
let file = select_primary_file(version)?;
validate_path_component(&file.filename, "provider filename")?;
validate_path_component(&version.id, "provider version id")?;
let path = destination.join(&file.filename);
if path.exists() {
if verify_version_file(&path, file)? {
@@ -428,6 +430,8 @@ pub async fn download_version_file_for_update(
installed_path: &std::path::Path,
) -> Result<DownloadOutcome, crate::net::NetError> {
let file = select_primary_file(version)?;
validate_path_component(&file.filename, "provider filename")?;
validate_path_component(&version.id, "provider version id")?;
let target = destination.join(&file.filename);
if target != installed_path {
return download_version_file(client, version, destination).await;
@@ -464,6 +468,18 @@ pub async fn download_version_file_for_update(
Ok(DownloadOutcome::Downloaded(target))
}
fn validate_path_component(value: &str, label: &str) -> Result<(), crate::net::NetError> {
let mut components = std::path::Path::new(value).components();
if !matches!(components.next(), Some(std::path::Component::Normal(_)))
|| components.next().is_some()
{
return Err(crate::net::NetError::Parse(format!(
"Invalid {label} '{value}'"
)));
}
Ok(())
}
fn verify_version_file(
path: &std::path::Path,
expected: &VersionFile,
+26
View File
@@ -207,6 +207,32 @@ async fn content_download_skips_an_existing_filename() {
);
}
#[tokio::test]
async fn content_download_rejects_provider_path_components() {
let directory = tempfile::tempdir().unwrap();
let version = version_with_files(vec![VersionFile {
url: "https://example.test/escape.jar".to_owned(),
filename: "../escape.jar".to_owned(),
size: 1,
primary: true,
hashes: HashMap::new(),
}]);
let error = download_version_file(&crate::net::HttpClient::new(), &version, directory.path())
.await
.unwrap_err();
assert!(error.to_string().contains("Invalid provider filename"));
assert!(
!directory
.path()
.parent()
.unwrap()
.join("escape.jar")
.exists()
);
}
#[tokio::test]
async fn staged_update_replaces_the_old_file_and_cleans_its_backup() {
let directory = tempfile::tempdir().unwrap();