fix: verify downloads and bound imported metadata
This commit is contained in:
1 parent
2b6750a971
commit
3149b96971
25 files changed
+678
-215
No files matched your search
@@ -8,6 +8,22 @@ pub mod refresh;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
const MAX_PACK_MANIFEST_BYTES: u64 = 8 * 1024 * 1024;
|
||||
|
||||
fn read_pack_manifest(reader: impl std::io::Read) -> Result<Vec<u8>, String> {
|
||||
use std::io::Read;
|
||||
|
||||
let mut bytes = Vec::new();
|
||||
reader
|
||||
.take(MAX_PACK_MANIFEST_BYTES + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|error| error.to_string())?;
|
||||
if bytes.len() as u64 > MAX_PACK_MANIFEST_BYTES {
|
||||
return Err("Pack manifest exceeds 8 MiB".to_owned());
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
use crate::instance::manager::InstanceManager;
|
||||
use crate::instance::models::ModLoader;
|
||||
|
||||
@@ -270,6 +286,34 @@ async fn owned_files(summary: &ImportSummary) -> Result<Vec<PathBuf>, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn override_files(path: &Path, roots: &[&str]) -> Result<Vec<PathBuf>, String> {
|
||||
let file = std::fs::File::open(path).map_err(|error| error.to_string())?;
|
||||
let mut archive = zip::ZipArchive::new(file).map_err(|error| error.to_string())?;
|
||||
let mut files = Vec::new();
|
||||
for index in 0..archive.len() {
|
||||
let entry = archive.by_index(index).map_err(|error| error.to_string())?;
|
||||
if entry.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let Some(root) = roots
|
||||
.iter()
|
||||
.find(|root| entry.name().starts_with(&format!("{root}/")))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let relative = entry
|
||||
.enclosed_name()
|
||||
.ok_or_else(|| format!("Unsafe override path: {}", entry.name()))?
|
||||
.strip_prefix(root)
|
||||
.map_err(|error| error.to_string())?
|
||||
.to_owned();
|
||||
if !relative.as_os_str().is_empty() {
|
||||
files.push(relative);
|
||||
}
|
||||
}
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
fn cleanup_failed_import(manager: &InstanceManager, name: &str) {
|
||||
let instance_dir = manager.instances_dir.join(name);
|
||||
if let Err(error) = std::fs::remove_dir_all(&instance_dir)
|
||||
|
||||
Reference in new issue
Block a user