From 8f7f04d2f3682e4b3078a7be8d219ad128da28b3 Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Sun, 29 Nov 2026 20:13:42 -0600 Subject: [PATCH 1/2] leonida-lua: validate write_file manifest and extension deny-list on normalized path (D3D12-LUA-WRITE-FILE-PATH-NORMALIZATION) write_file() previously checked lower == "mod.json" and the blocked executable extension list against the raw name string before safe_relative() normalized Path::components(). Because Path::components() strips trailing '/' and interior '.', names such as "mod.json/", "mod.json/.", "evil.exe/", or "sub/evil.dll/." bypassed both checks and wrote mod.json or blocked executables into the mod folder. Normalize with safe_relative(name) first and inspect the normalized relative path's file_name() and extension() instead. Includes a Red -> Green unit test covering all bypass forms. AI-assisted: Antigravity (Gemini 4 Neon) --- d3d12/crates/leonida-lua/src/api.rs | 64 +++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/d3d12/crates/leonida-lua/src/api.rs b/d3d12/crates/leonida-lua/src/api.rs index 2294c43ce0..0ac4919114 100644 --- a/d3d12/crates/leonida-lua/src/api.rs +++ b/d3d12/crates/leonida-lua/src/api.rs @@ -509,15 +509,23 @@ fn write_file(root: &Path, name: &str, data: &[u8]) -> std::result::Result<(), S if data.len() > MAX_WRITE { return Err(format!("a write is at most {MAX_WRITE} bytes")); } - let lower = name.to_ascii_lowercase(); - let extension = lower.rsplit_once('.').map_or("", |(_, e)| e); - if lower == "mod.json" - || [ - "exe", "dll", "bat", "cmd", "com", "ps1", "vbs", "msi", "scr", "lnk", "sys", "reg", - "hta", "jar", - ] - .contains(&extension) - { + let rel = safe_relative(name)?; + let is_manifest = rel.parent().is_none_or(|p| p.as_os_str().is_empty()) + && rel + .file_name() + .and_then(|f| f.to_str()) + .is_some_and(|f| f.eq_ignore_ascii_case("mod.json")); + let blocked_extension = rel + .extension() + .and_then(|e| e.to_str()) + .is_some_and(|e| { + [ + "exe", "dll", "bat", "cmd", "com", "ps1", "vbs", "msi", "scr", "lnk", "sys", "reg", + "hta", "jar", + ] + .contains(&e.to_ascii_lowercase().as_str()) + }); + if is_manifest || blocked_extension { return Err(format!("`{name}` cannot be written by a mod")); } let path = inside(root, name)?; @@ -915,3 +923,41 @@ fn memory_table(lua: &Lua, shared: &Rc_, platform: &Rc) -> )?; Ok(m) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn write_file_blocks_manifest_and_executables_after_path_normalization() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("mod.json"), r#"{"name":"orig"}"#).unwrap(); + + for bad in [ + "mod.json", + "MOD.JSON", + "mod.json/", + "mod.json/.", + "evil.exe", + "evil.exe/", + "evil.exe/.", + "sub/evil.dll/", + "sub/evil.bat/.", + "sub/./evil.ps1", + ] { + assert!( + write_file(dir.path(), bad, b"pwned").is_err(), + "`{bad}` must be refused by write_file" + ); + } + + assert_eq!( + std::fs::read_to_string(dir.path().join("mod.json")).unwrap(), + r#"{"name":"orig"}"# + ); + assert!(!dir.path().join("evil.exe").exists()); + assert!(!dir.path().join("sub").join("evil.dll").exists()); + assert!(!dir.path().join("sub").join("evil.bat").exists()); + assert!(!dir.path().join("sub").join("evil.ps1").exists()); + } +} From b24edaa990d6b7c9cc5f343dbdf9694d69e20ca3 Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:24:36 -0600 Subject: [PATCH 2/2] leonida-lua: block leading-dot executable extensions after normalization --- d3d12/crates/leonida-lua/src/api.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/d3d12/crates/leonida-lua/src/api.rs b/d3d12/crates/leonida-lua/src/api.rs index 0ac4919114..b6f38c87ea 100644 --- a/d3d12/crates/leonida-lua/src/api.rs +++ b/d3d12/crates/leonida-lua/src/api.rs @@ -510,15 +510,12 @@ fn write_file(root: &Path, name: &str, data: &[u8]) -> std::result::Result<(), S return Err(format!("a write is at most {MAX_WRITE} bytes")); } let rel = safe_relative(name)?; + let file_name = rel.file_name().and_then(|f| f.to_str()); let is_manifest = rel.parent().is_none_or(|p| p.as_os_str().is_empty()) - && rel - .file_name() - .and_then(|f| f.to_str()) - .is_some_and(|f| f.eq_ignore_ascii_case("mod.json")); - let blocked_extension = rel - .extension() - .and_then(|e| e.to_str()) - .is_some_and(|e| { + && file_name.is_some_and(|f| f.eq_ignore_ascii_case("mod.json")); + let blocked_extension = file_name + .and_then(|f| f.rsplit_once('.')) + .is_some_and(|(_, e)| { [ "exe", "dll", "bat", "cmd", "com", "ps1", "vbs", "msi", "scr", "lnk", "sys", "reg", "hta", "jar", @@ -941,6 +938,10 @@ mod tests { "evil.exe", "evil.exe/", "evil.exe/.", + ".exe", + ".exe/", + "sub/.bat", + "sub/.ps1/.", "sub/evil.dll/", "sub/evil.bat/.", "sub/./evil.ps1", @@ -956,6 +957,9 @@ mod tests { r#"{"name":"orig"}"# ); assert!(!dir.path().join("evil.exe").exists()); + assert!(!dir.path().join(".exe").exists()); + assert!(!dir.path().join("sub").join(".bat").exists()); + assert!(!dir.path().join("sub").join(".ps1").exists()); assert!(!dir.path().join("sub").join("evil.dll").exists()); assert!(!dir.path().join("sub").join("evil.bat").exists()); assert!(!dir.path().join("sub").join("evil.ps1").exists());