mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-22 23:40:54 +01:00
Integrate main's read limits and preserve the font, evaluator, converter, and measured preview repairs. Complete the scoped type, comment, and spacing cleanup. Reuse one validated XML document for library layout and graph reads. Associate cache maintenance completion with its submitted revision so old failures cannot consume new settings or clear requests.
91 lines
3.5 KiB
Rust
91 lines
3.5 KiB
Rust
#[cfg(not(target_arch = "wasm32"))]
|
|
use std::io::Read as _;
|
|
|
|
/// Largest linked resource read from beside an SBS; matches the archive member cap.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
const MAX_LINKED_RESOURCE_BYTES: u64 = 256 << 20;
|
|
|
|
/// Read resource paths relative to an SBS package, including portable library aliases.
|
|
pub fn reader(path: &str) -> impl Fn(&str) -> Option<Vec<u8>> + use<> {
|
|
let owner = path.to_string();
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
let folder = {
|
|
let parent = std::path::Path::new(path).parent().filter(|parent| !parent.as_os_str().is_empty()).unwrap_or(std::path::Path::new("."));
|
|
std::fs::canonicalize(parent).ok()
|
|
};
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
let allowed_root = if materialcraft_formats::sbs::library::contains_file(std::path::Path::new(path)) {
|
|
materialcraft_formats::sbs::library::root().canonicalize().ok()
|
|
} else {
|
|
folder.clone()
|
|
};
|
|
|
|
move |reference: &str| {
|
|
if reference.starts_with("sbs://") {
|
|
return materialcraft_formats::sbs::library::read(reference).ok();
|
|
}
|
|
|
|
if owner.starts_with("sbs://") {
|
|
let uri = materialcraft_formats::sbs::library::resolve(&owner, reference).ok()?;
|
|
return materialcraft_formats::sbs::library::read(&uri).ok();
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
let relative = reference.trim().replace('\\', "/");
|
|
let plain = !relative.is_empty() && !relative.contains(':') && !relative.starts_with('/');
|
|
if !plain
|
|
|| std::path::Path::new(&relative)
|
|
.components()
|
|
.any(|part| !matches!(part, std::path::Component::Normal(_) | std::path::Component::ParentDir | std::path::Component::CurDir))
|
|
{
|
|
return None;
|
|
}
|
|
|
|
let folder = folder.as_ref()?;
|
|
let file = std::fs::canonicalize(folder.join(&relative)).ok()?;
|
|
let metadata = std::fs::metadata(&file).ok()?;
|
|
|
|
// Parent references are legal inside the configured library, never beyond its root
|
|
if !file.starts_with(allowed_root.as_ref()?) || !metadata.is_file() || metadata.len() > MAX_LINKED_RESOURCE_BYTES {
|
|
return None;
|
|
}
|
|
|
|
let mut bytes = Vec::new();
|
|
std::fs::File::open(&file).ok()?.take(MAX_LINKED_RESOURCE_BYTES + 1).read_to_end(&mut bytes).ok()?;
|
|
(bytes.len() as u64 <= MAX_LINKED_RESOURCE_BYTES).then_some(bytes)
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
None
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::path::PathBuf;
|
|
|
|
fn temp(name: &str) -> PathBuf {
|
|
std::env::temp_dir().join(format!("mc-linked-{}-{name}", std::process::id()))
|
|
}
|
|
|
|
#[test]
|
|
fn linked_resources_over_the_cap_are_ignored() {
|
|
let dir = temp("cap");
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let sbs = dir.join("p.sbs");
|
|
std::fs::write(&sbs, b"x").unwrap();
|
|
let big = dir.join("big.bin");
|
|
std::fs::File::create(&big).unwrap().set_len(MAX_LINKED_RESOURCE_BYTES + 1).unwrap();
|
|
|
|
let read = reader(&sbs.to_string_lossy());
|
|
assert!(read("big.bin").is_none(), "a resource over the cap is not read");
|
|
|
|
std::fs::write(dir.join("small.bin"), b"ok").unwrap();
|
|
assert_eq!(read("small.bin").as_deref(), Some(&b"ok"[..]));
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
}
|