diff --git a/crates/ui-egui/src/dialogs/script_run.rs b/crates/ui-egui/src/dialogs/script_run.rs index ae13009775..2dec50814d 100644 --- a/crates/ui-egui/src/dialogs/script_run.rs +++ b/crates/ui-egui/src/dialogs/script_run.rs @@ -42,6 +42,7 @@ use craft::ui::text::TextField; use egui::Ui; use serde_json::{Value, json}; use vectorcraft_engine::cmd::scripts::{RUN_EXTS, TOO_LARGE, runs}; +use vectorcraft_engine::file_access::ScopeHandle; use vectorcraft_engine::script::dialogs::{MAX_PROMPT_BYTES, cut_to, shown_text}; use vectorcraft_engine::script::files::{MAX_ENTRIES, PickAnswer, PickerKind, ScriptPicker}; use vectorcraft_engine::script::{DialogAnswer, MAX_SOURCE_BYTES, ScriptDialog}; @@ -82,6 +83,11 @@ crate::dialogs::confirm::wrapped_dialog!(CloseQuestion, StandardPrompt, CLOSE_KI /// The tag of the warning: the params `script.run` takes once the warning is answered. struct RunWarning { params: Value, + /// The automation roots in force when asked: the script runs under them, and under any in force + /// when the warning is answered. + scope: ScopeHandle, + /// Automation asked ([`VectorcraftApp::automation_asked`]): the script runs as automation. + automated: bool, } /// What a script's `alert`, `confirm` or `prompt` dialog answers with. @@ -473,7 +479,7 @@ pub(crate) fn chosen(app: &mut VectorcraftApp, params: Value) -> Result() + && let Some(RunWarning { params, scope, automated }) = answer.take_tag::() { let stored = if answer.skip { app.run("prefs.set", json!({"key": "showExternalScriptWarning", "value": false})).map(drop) } else { Ok(()) }; - if let Err(e) = stored.and_then(|()| app.run("script.run", params).map(drop)) { + let run = |app: &mut VectorcraftApp| app.run("script.run", params).map(drop); + let automated = automated || app.automation_asked(); + if let Err(e) = stored.and_then(|()| scope.within_current().run(|| file_asks::as_automated(app, automated, run))) { app.status(e); } } diff --git a/crates/ui-egui/src/tests/script_run.rs b/crates/ui-egui/src/tests/script_run.rs index 4022cb7c65..42d47ab1cd 100644 --- a/crates/ui-egui/src/tests/script_run.rs +++ b/crates/ui-egui/src/tests/script_run.rs @@ -103,6 +103,46 @@ fn a_run_goes_frame_by_frame_and_is_one_undo_step() { let _ = std::fs::remove_dir_all(&dir); } +/// The warning automation asked while confined to a folder: Run runs the script as automation's, +/// with that folder still the only place it may read. +#[test] +fn the_warning_runs_the_script_under_the_access_it_was_asked_with() { + let mut app = app(); + let (inside, outside) = (folder("scope-in"), folder("scope-out")); + let path = outside.join("draw.js"); + std::fs::write(&path, DRAW).unwrap(); + let roots = vectorcraft_engine::file_access::AutomationRoots::new(&[inside.as_path()], &[inside.as_path()]).unwrap(); + vectorcraft_engine::file_access::confine(roots.as_ref(), || { + crate::file_asks::as_automated(&mut app, true, |app| { + menus::run_ui_command(app, "ui.runScript", &json!({"path": path.to_string_lossy()})).unwrap().unwrap(); + }) + }); + assert!(confirm(&mut app, KIND)); + assert!(app.ui.status.contains("outside the read root"), "the script outside the folder is refused: {}", app.ui.status); + assert!(!app.session.script_running()); + finish(&mut app); + assert_eq!(objects(&app), 0); + let _ = std::fs::remove_dir_all(&inside); + let _ = std::fs::remove_dir_all(&outside); +} + +/// The warning a person asked, answered by automation confined to a folder: the script runs with +/// that folder still the only place it may read. +#[test] +fn the_warning_answered_under_narrower_access_keeps_it() { + let mut app = app(); + let (inside, outside) = (folder("narrow-in"), folder("narrow-out")); + let path = outside.join("draw.js"); + std::fs::write(&path, DRAW).unwrap(); + menus::run_ui_command(&mut app, "ui.runScript", &json!({"path": path.to_string_lossy()})).unwrap().unwrap(); + let roots = vectorcraft_engine::file_access::AutomationRoots::new(&[inside.as_path()], &[inside.as_path()]).unwrap(); + assert!(vectorcraft_engine::file_access::confine(roots.as_ref(), || confirm(&mut app, KIND))); + assert!(app.ui.status.contains("outside the read root"), "the script outside the folder is refused: {}", app.ui.status); + assert!(!app.session.script_running()); + let _ = std::fs::remove_dir_all(&inside); + let _ = std::fs::remove_dir_all(&outside); +} + #[test] fn do_not_show_again_is_remembered_and_the_script_still_runs() { let mut app = app();