From e0809b427ca41eb83807e2da52be46b5c88bb59f Mon Sep 17 00:00:00 2001 From: semmelrinde <124386289+semmelrinde@users.noreply.github.com> Date: Tue, 27 Oct 2026 12:36:25 +0000 Subject: [PATCH] =?UTF-8?q?Web=20app:=20Import=E2=80=A6=20reads=20shortcut?= =?UTF-8?q?s=20and=20theme=20files=20through=20vfs=20(#7643)=20(#7644)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * claim: #7643 * Web app: Import… reads shortcuts and theme files through vfs (#7643) Import… on the Keyboard Shortcuts page and the theme strip's Import… tile read the picked file with std::fs::read_to_string. In the browser a picked file is kept in vfs's in-memory files under /upload/, and std::fs on wasm32 is unsupported, so every import failed. Both now read through vfs::read_to_string, capped (a theme file at theme_files::MAX_BYTES, a shortcuts file at the new toolbar_custom::MAX_IMPORT_BYTES), like the other imports. Natively it is the same file on the disk. The new tests import a file outside a vfs folder scope (refused, nothing imported) and inside it (imported). With std::fs both fail: the file outside the scope is imported. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/keymap.rs | 39 +++++++++++++++++++++++++- crates/ui-egui/src/prefs.rs | 42 +++++++++++++++++++++++++++- crates/ui-egui/src/theme_files.rs | 2 +- crates/ui-egui/src/toolbar_custom.rs | 3 ++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs index 2e1bb4258..e35336a25 100644 --- a/crates/ui-egui/src/keymap.rs +++ b/crates/ui-egui/src/keymap.rs @@ -651,7 +651,8 @@ pub fn page(app: &mut SolveApp, ui: &mut egui::Ui) { Some((_, "import")) => { app.pick_open(|app, path| { let Some(path) = path else { return }; - let r = std::fs::read_to_string(&path) + // Through `vfs`, like every other import: in the web app a picked file is there. + let r = solidcraft_engine::io::vfs::read_to_string(&path, crate::toolbar_custom::MAX_IMPORT_BYTES) .map_err(|e| e.to_string()) .and_then(|t| serde_json::from_str::(&t).map_err(|e| e.to_string())) .and_then(|v| crate::toolbar_custom::import(app, &v)); @@ -1161,4 +1162,40 @@ mod card_tests { } assert_eq!(effective(&h.app, "solid.extrude").as_deref(), Some("E")); } + + /// Import… reads the shortcuts file through `vfs`, like the other imports: in the web app a + /// picked file is kept there, where `std::fs` can't read it (#7643). On the disk it is the same + /// file; a folder scope active while it is read refuses a path outside it, which shows the + /// read goes through `vfs`. + #[test] + fn shortcut_import_reads_through_vfs() { + use solidcraft_engine::io::vfs; + let mut h = Harness::new(); + h.app.home.open = false; + bind(&mut h.app, "solid.extrude", "Shift+E", true).unwrap(); + let text = crate::toolbar_custom::export(&h.app).to_string(); + reset(&mut h.app, None); + h.call("ui.prefs", json!({"open":"Keyboard Shortcuts"})); + h.frames(2); + let (root, out) = (h.dir.join("root"), h.dir.join("out")); + std::fs::create_dir_all(&root).unwrap(); + std::fs::create_dir_all(&out).unwrap(); + for (path, inside) in [(out.join("keys.json"), false), (root.join("keys.json"), true)] { + std::fs::write(&path, &text).unwrap(); + let answer = path.to_string_lossy().to_string(); + h.app.services.pick_open = Some(Box::new(move |_| Box::pin(std::future::ready(Some(answer.clone()))))); + vfs::scoped(Some(vfs::PathScope::new([root.clone()])), || { + h.step(&json!({"widget":"Import…"})).unwrap(); + h.frames(4); + }); + let message = h.app.keymap.message.clone(); + if inside { + assert_eq!(message, None); + assert_eq!(effective(&h.app, "solid.extrude").as_deref(), Some("Shift+E")); + } else { + assert!(message.as_deref().is_some_and(|m| m.contains("outside the folders this agent may use")), "{message:?}"); + assert_eq!(effective(&h.app, "solid.extrude").as_deref(), Some("E"), "nothing imported"); + } + } + } } diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index db332f59b..003f00a52 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -992,7 +992,10 @@ fn theme_row(app: &mut SolveApp, ui: &mut egui::Ui) { match action { Some("import") => app.pick_open_for(&["json"], |app, path| { let Some(path) = path else { return }; - let r = std::fs::read_to_string(&path).map_err(|e| e.to_string()).and_then(|text| import_theme(app, &serde_json::Value::String(text))); + // Through `vfs`, like every other import: in the web app a picked file is there. + let r = solidcraft_engine::io::vfs::read_to_string(&path, crate::theme_files::MAX_BYTES) + .map_err(|e| e.to_string()) + .and_then(|text| import_theme(app, &serde_json::Value::String(text))); match r { Ok(id) => { let name = crate::theme_files::find(&id, &app.preferences.custom_themes).map(|t| t.name.clone()).unwrap_or(id); @@ -3589,6 +3592,43 @@ mod tests { assert_eq!(read, crate::theme::ViewportTheme::Theme); } + /// The theme strip's Import… reads the theme file through `vfs`, like the other imports: in the + /// web app a picked file is kept there, where `std::fs` can't read it (#7643). On the disk it + /// is the same file; a folder scope active while it is read refuses a path outside it, which + /// shows the read goes through `vfs`. + #[test] + fn theme_import_reads_through_vfs() { + use serde_json::json; + use solidcraft_engine::io::vfs; + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + h.call("ui.prefs", json!({"theme": "dark", "open": "Interface"})); + h.frames(2); + super::reveal_preference(&mut h, "dialog:Import…"); + let (root, out) = (h.dir.join("root"), h.dir.join("out")); + std::fs::create_dir_all(&root).unwrap(); + std::fs::create_dir_all(&out).unwrap(); + let file = json!({"format": "solidcraft-theme/1", "name": "Paper", "base": "light", "colors": {"panel": "#fffdf0"}}); + for (path, inside) in [(out.join("Paper.json"), false), (root.join("Paper.json"), true)] { + std::fs::write(&path, file.to_string()).unwrap(); + let answer = path.to_string_lossy().to_string(); + h.app.services.pick_open = Some(Box::new(move |_| Box::pin(std::future::ready(Some(answer.clone()))))); + h.app.status = None; + vfs::scoped(Some(vfs::PathScope::new([root.clone()])), || { + h.step(&json!({"click":{"handle":"dialog:Import…"}})).unwrap(); + h.frames(4); + }); + let status = h.app.status.clone().map(|(text, ..)| text); + if inside { + assert_eq!(status.as_deref(), Some("Theme imported: Paper")); + assert_eq!(h.app.preferences.theme, "paper"); + } else { + assert!(status.as_deref().is_some_and(|s| s.contains("outside the folders this agent may use")), "{status:?}"); + assert!(h.app.preferences.custom_themes.is_empty(), "nothing imported"); + } + } + } + #[test] fn theme_files_import_apply_export_save_and_remove() { use crate::theme::{ColorVision, Tokens}; diff --git a/crates/ui-egui/src/theme_files.rs b/crates/ui-egui/src/theme_files.rs index 4d4b7487d..1b064af94 100644 --- a/crates/ui-egui/src/theme_files.rs +++ b/crates/ui-egui/src/theme_files.rs @@ -21,7 +21,7 @@ use crate::theme::{IconColors, Tokens}; /// The `format` tag of a theme file. pub const FORMAT: &str = "solidcraft-theme/1"; /// Hostile-input caps: file size, colours per file, name length, imported themes kept. -const MAX_BYTES: usize = 256 * 1024; +pub const MAX_BYTES: usize = 256 * 1024; pub const MAX_COLORS: usize = 512; const MAX_NAME: usize = 80; pub const MAX_CUSTOM: usize = 64; diff --git a/crates/ui-egui/src/toolbar_custom.rs b/crates/ui-egui/src/toolbar_custom.rs index 7638cd3df..4cffd9bca 100644 --- a/crates/ui-egui/src/toolbar_custom.rs +++ b/crates/ui-egui/src/toolbar_custom.rs @@ -302,6 +302,9 @@ pub fn export(app: &SolveApp) -> Value { json!({ "format": "solidcraft-shortcuts/1", "shortcuts": app.keymap.prefs(), "favourites": app.sbox.pinned, "toolbar": app.toolbar_custom.prefs() }) } +/// The largest shortcuts file Import… reads (hostile input; an export is a few tens of KiB). +pub const MAX_IMPORT_BYTES: usize = 1 << 20; + pub fn import(app: &mut SolveApp, v: &Value) -> Result<(), String> { // "solvecraft-shortcuts/1": files exported before the rename (compatibility shim). if !matches!(v.get("format").and_then(Value::as_str), Some("solidcraft-shortcuts/1" | "solvecraft-shortcuts/1")) {