Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Web app: Import… reads shortcuts and theme files through vfs (#7643) (#7644)

semmelrindee0809b427ca4
diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs
index 2e1bb42586..e35336a255 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::<Value>(&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 db332f59b5..003f00a52b 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 4d4b7487da..1b064af94f 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 7638cd3df2..4cffd9bcae 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")) {