diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs index 582d8a9..836fcdf 100644 --- a/crates/automation/src/headless.rs +++ b/crates/automation/src/headless.rs @@ -1,5 +1,7 @@ //! The headless backend: a [`Session`] plus file I/O and windowless renders. Synchronous and UI-free; the MCP server, the RPC server and the CLI all drive it +use std::fs::File; +use std::io::Read; use std::path::Path; use materialcraft_engine::core::NodeId; @@ -9,7 +11,7 @@ use materialcraft_render::{Lighting, Maps, Relief, Scene, Settings, VIEW_TRIANGL use serde_json::{Value, json}; use crate::budgets::{HQ_TRIANGLES, MAX_LOCAL_RENDER_SIDE, MAX_PNG_SIDE, check_png, encode_png}; -use crate::workspace::authorize_command; +use crate::workspace::{MAX_READ_BYTES, authorize_command}; use crate::{AutomationError, bad}; pub use crate::{Filesystem, RenderOptions, Rendered}; @@ -26,11 +28,30 @@ pub struct Headless { fn read_file(fs: &Filesystem, path: &str) -> Result, AutomationError> { match fs { Filesystem::Denied => Err(bad("file access is not granted: start the server with --read-root ")), - Filesystem::TrustedLocal => std::fs::read(path).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}"))), + Filesystem::TrustedLocal => read_trusted(path), Filesystem::Workspace(w) => w.read(path), } } +/// Read a local file under the same bound as capability workspaces: trusted access widens where a +/// path may point, not how much memory one read may take. +fn read_trusted(path: &str) -> Result, AutomationError> { + let file = File::open(path).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}")))?; + let metadata = file.metadata().map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}")))?; + if !metadata.is_file() { + return Err(bad(format!("not a regular file: `{path}`"))); + } + if metadata.len() > MAX_READ_BYTES { + return Err(bad(format!("file too large: `{path}` is {} bytes (limit {MAX_READ_BYTES})", metadata.len()))); + } + let mut bytes = Vec::new(); + file.take(MAX_READ_BYTES + 1).read_to_end(&mut bytes).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}")))?; + if bytes.len() as u64 > MAX_READ_BYTES { + return Err(bad(format!("file too large: `{path}` exceeds the {MAX_READ_BYTES}-byte limit"))); + } + Ok(bytes) +} + impl Headless { pub fn new(session: Session, fs: Filesystem) -> Headless { Headless { session, fs, scene: None, light: None } diff --git a/crates/automation/src/headless.test.rs b/crates/automation/src/headless.test.rs index 4d9ee59..f6d99af 100644 --- a/crates/automation/src/headless.test.rs +++ b/crates/automation/src/headless.test.rs @@ -3,6 +3,28 @@ mod end_merge_tests { use crate::headless::{Filesystem, Headless}; use serde_json::json; + #[cfg(unix)] + #[test] + fn trusted_reads_are_bounded_and_regular_files_only() { + let dir = std::env::temp_dir().join(format!("materialcraft-read-cap-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let small = dir.join("small.txt"); + std::fs::write(&small, b"ok").unwrap(); + assert_eq!(crate::headless::read_file(&Filesystem::TrustedLocal, small.to_str().unwrap()).unwrap(), b"ok"); + + let directory = dir.join("directory"); + std::fs::create_dir(&directory).unwrap(); + let err = crate::headless::read_file(&Filesystem::TrustedLocal, directory.to_str().unwrap()).unwrap_err().to_string(); + assert!(err.contains("not a regular file"), "{err}"); + + let huge = dir.join("huge.bin"); + std::fs::File::create(&huge).unwrap().set_len(crate::workspace::MAX_READ_BYTES + 1).unwrap(); + let err = crate::headless::read_file(&Filesystem::TrustedLocal, huge.to_str().unwrap()).unwrap_err().to_string(); + assert!(err.contains("too large"), "{err}"); + + std::fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn two_param_requests_are_two_undo_steps() { let mut h = Headless::new(Headless::default_session(), Filesystem::TrustedLocal); diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index 52a72fa..e24c4ff 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -10,6 +10,9 @@ use serde_json::Value; use crate::{AutomationError, bad}; +/// Upper bound for one automation read, so a hostile path cannot force a multi-gigabyte allocation. +pub const MAX_READ_BYTES: u64 = 2 << 30; + /// Separate read and write directory capabilities. A missing authority fails closed #[derive(Clone, Default)] pub struct AuthorizedWorkspace { @@ -26,14 +29,20 @@ impl AuthorizedWorkspace { pub fn read(&self, path: &str) -> Result, AutomationError> { let rel = relative_path(path)?; let root = self.read.as_ref().ok_or_else(|| denied("read"))?; - let mut file = root.open(&rel).map_err(|e| file_error("read", path, e))?; + let file = root.open(&rel).map_err(|e| file_error("read", path, e))?; let metadata = file.metadata().map_err(|e| file_error("read", path, e))?; if !metadata.is_file() { return Err(bad(format!("not a regular file: `{path}`"))); } + if metadata.len() > MAX_READ_BYTES { + return Err(bad(format!("file too large: `{path}` is {} bytes (limit {MAX_READ_BYTES})", metadata.len()))); + } let mut bytes = Vec::new(); - file.read_to_end(&mut bytes).map_err(|e| file_error("read", path, e))?; + file.take(MAX_READ_BYTES + 1).read_to_end(&mut bytes).map_err(|e| file_error("read", path, e))?; + if bytes.len() as u64 > MAX_READ_BYTES { + return Err(bad(format!("file too large: `{path}` exceeds the {MAX_READ_BYTES}-byte limit"))); + } Ok(bytes) } @@ -220,6 +229,16 @@ mod tests { assert!(none.read("r.txt").is_err()); } + #[cfg(unix)] + #[test] + fn oversized_reads_are_rejected_without_allocating() { + let (inside, _, ws) = roots("oversized"); + let file = std::fs::File::create(inside.join("huge.bin")).unwrap(); + file.set_len(MAX_READ_BYTES + 1).unwrap(); + let err = ws.read("huge.bin").unwrap_err().to_string(); + assert!(err.contains("too large"), "{err}"); + } + #[cfg(unix)] #[test] fn symlink_escape_is_rejected() {