Scripts: Other Script's warning runs the script under the file access of the request that asked

The warning before a script runs (ui.runScript) now keeps the automation
roots and the automation flag in force when it was asked, and Run runs
script.run under them, narrowed to any roots in force when it is
answered. A request confined to its roots could otherwise open the
warning for a script outside them and have it run, with full file
access, once Run was pressed.
This commit is contained in:
dotCooCoo
2026-11-25 10:07:41 -08:00
parent 08425c9610
commit 69b627ddc7
2 changed files with 51 additions and 3 deletions
+11 -3
View File
@@ -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<Value, S
.dont_show_again()
.button(PromptChoice::Proceed, tl!("Run"))
.button(PromptChoice::Cancel, tl!("Cancel"))
.tag(RunWarning { params });
.tag(RunWarning { params, scope: ScopeHandle::capture(), automated: app.automation_asked() });
with_host(app, |host, _| host.open(RunQuestion(prompt))).ok_or_else(|| "there is no window to ask in".to_string())?;
Ok(Value::Null)
}
@@ -560,10 +566,12 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
}
if let Some(DialogResult::Ok(mut answer)) = run
&& answer.choice == PromptChoice::Proceed
&& let Some(RunWarning { params }) = answer.take_tag::<RunWarning>()
&& let Some(RunWarning { params, scope, automated }) = answer.take_tag::<RunWarning>()
{
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);
}
}
+40
View File
@@ -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();