mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Judge a control client's prompt Save as file.save even without a session gate
Services with only the automation command callback (no automation_authorize) install no session gate for a control request, so answering the unsaved-changes prompt with Save over ui.dialog.confirm skipped the file.save check and wrote the document. `apply` now knows the answer came from a control client: its Save is judged by whatever policy is available and refused when there is none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
64efd3df25
commit
1743a3240a
2 files changed
+50
-21
No files matched your search
@@ -1646,7 +1646,8 @@ fn automation_confirming_a_file_writing_dialog_is_judged_as_its_file_command() {
|
||||
/// Answering the unsaved-changes prompt with Save writes the document back like File › Save,
|
||||
/// so automation is judged for `file.save` there too, through a control request or synthetic
|
||||
/// input alike: a client allowed to close a document can't write it over its own file. A
|
||||
/// user's Save still writes and closes.
|
||||
/// control answer is judged even when its services install no session gate, and fails closed
|
||||
/// with no policy at all. A user's Save still writes and closes.
|
||||
#[test]
|
||||
fn automation_saving_from_the_unsaved_changes_prompt_is_judged_as_file_save() {
|
||||
// The desktop policy (crates/automation/src/workspace.rs) lets automation close documents
|
||||
@@ -1674,7 +1675,7 @@ fn automation_saving_from_the_unsaved_changes_prompt_is_judged_as_file_save() {
|
||||
let (mut app, ctx, written, _) = prompted();
|
||||
let error = if synthetic {
|
||||
app.automation_input = true;
|
||||
let r = crate::discard_ui::answer_control(&mut app, &ctx, "save");
|
||||
let r = crate::discard_ui::answer_choice(&mut app, &ctx, "save", false);
|
||||
app.automation_input = false;
|
||||
r.err().unwrap_or_default()
|
||||
} else {
|
||||
@@ -1693,17 +1694,35 @@ fn automation_saving_from_the_unsaved_changes_prompt_is_judged_as_file_save() {
|
||||
let r = call(&mut app, &ctx, "ui.dialog.confirm", json!({"dialog": crate::discard_ui::CONTROL_ID, "choice": "save"}));
|
||||
assert!(r["error"].as_str().is_some_and(|e| e.contains("file.save")), "{r}");
|
||||
assert!(written.borrow().is_empty() && app.discard.is_some(), "the gate alone keeps the file unwritten");
|
||||
// Synthetic input with no policy at all fails closed instead of saving unjudged.
|
||||
// Services with only the command callback install no session gate for the request: the
|
||||
// control answer is still judged as `file.save` by that callback.
|
||||
let (mut app, ctx, written, _) = prompted();
|
||||
app.services.automation_command = None;
|
||||
app.services.automation_authorize = None;
|
||||
app.automation_input = true;
|
||||
let r = crate::discard_ui::answer_control(&mut app, &ctx, "save");
|
||||
app.automation_input = false;
|
||||
assert!(r.is_err() && written.borrow().is_empty(), "no policy, no Save: {r:?}");
|
||||
let r = call(&mut app, &ctx, "ui.dialog.confirm", json!({"dialog": crate::discard_ui::CONTROL_ID, "choice": "save"}));
|
||||
assert!(r["error"].as_str().is_some_and(|e| e.contains("file.save")), "{r}");
|
||||
assert!(written.borrow().is_empty(), "the callback alone keeps the file unwritten: {:?}", written.borrow());
|
||||
assert!(app.discard.is_some() && app.session.documents().len() == 1, "the prompt and the document stay");
|
||||
// Synthetic input or a control answer with no policy at all fails closed instead of saving
|
||||
// unjudged.
|
||||
for synthetic in [true, false] {
|
||||
let (mut app, ctx, written, _) = prompted();
|
||||
app.services.automation_command = None;
|
||||
app.services.automation_authorize = None;
|
||||
let r = if synthetic {
|
||||
app.automation_input = true;
|
||||
let r = crate::discard_ui::answer_choice(&mut app, &ctx, "save", false);
|
||||
app.automation_input = false;
|
||||
r.err().unwrap_or_default()
|
||||
} else {
|
||||
let r = call(&mut app, &ctx, "ui.dialog.confirm", json!({"dialog": crate::discard_ui::CONTROL_ID, "choice": "save"}));
|
||||
r["error"].as_str().unwrap_or_default().to_string()
|
||||
};
|
||||
assert!(r.contains("file.save") && written.borrow().is_empty(), "synthetic={synthetic}: no policy, no Save: {r:?}");
|
||||
assert!(app.discard.is_some(), "synthetic={synthetic}: the prompt stays up");
|
||||
}
|
||||
// The user answering Save writes the document back and closes it.
|
||||
let (mut app, ctx, written, own_file) = prompted();
|
||||
crate::discard_ui::answer_control(&mut app, &ctx, "save").unwrap();
|
||||
crate::discard_ui::answer_choice(&mut app, &ctx, "save", false).unwrap();
|
||||
assert_eq!(*written.borrow(), vec![own_file]);
|
||||
assert!(app.discard.is_none());
|
||||
assert!(app.session.documents().is_empty());
|
||||
|
||||
@@ -337,21 +337,22 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
answer = buttons.iter().find(|b| b.0 == ButtonRole::Default).map(|b| b.4);
|
||||
}
|
||||
if let Some(answer) = answer
|
||||
&& let Err(e) = apply(app, ctx, doc, answer)
|
||||
&& let Err(e) = apply(app, ctx, doc, answer, false)
|
||||
{
|
||||
app.ui.status = e;
|
||||
app.ui.status_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Acts on the answer about `doc`, the document the prompt is asking about. Save is refused
|
||||
/// when automation may not run File › Save (see [`authorize_save`]); the prompt stays up.
|
||||
fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, doc: DocId, answer: Answer) -> Result<(), String> {
|
||||
/// Acts on the answer about `doc`, the document the prompt is asking about. `from_control` says
|
||||
/// a control client gave it (`ui.dialog.confirm`). Save is refused when automation may not run
|
||||
/// File › Save (see [`authorize_save`]); the prompt stays up.
|
||||
fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, doc: DocId, answer: Answer, from_control: bool) -> Result<(), String> {
|
||||
match answer {
|
||||
Answer::Cancel => app.discard = None,
|
||||
Answer::Discard => advance(app, ctx),
|
||||
Answer::Save => {
|
||||
authorize_save(app)?;
|
||||
authorize_save(app, from_control)?;
|
||||
if save(app, ctx, doc) {
|
||||
advance(app, ctx);
|
||||
}
|
||||
@@ -361,12 +362,14 @@ fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, doc: DocId, answer: Answe
|
||||
}
|
||||
|
||||
/// Save writes the document back over its own file, as File › Save does, so automation (a
|
||||
/// control request's gate or synthetic input) is judged for `file.save` before it runs: a client
|
||||
/// allowed to close a document can't use the prompt to write a file its policy keeps it from.
|
||||
/// The installed gate (or the services' policy) and the command callback both judge it, and
|
||||
/// with neither available the Save is refused rather than run unjudged.
|
||||
fn authorize_save(app: &PhotocraftApp) -> Result<(), String> {
|
||||
if !(app.automation_input || app.session.authorize.is_some()) {
|
||||
/// control client's answer, a control request's gate or synthetic input) is judged for
|
||||
/// `file.save` before it runs: a client allowed to close a document can't use the prompt to
|
||||
/// write a file its policy keeps it from. A control answer counts as automation even when its
|
||||
/// services install no session gate (only the command callback). The installed gate (or the
|
||||
/// services' policy) and the command callback both judge it, and with neither available the
|
||||
/// Save is refused rather than run unjudged.
|
||||
fn authorize_save(app: &PhotocraftApp, from_control: bool) -> Result<(), String> {
|
||||
if !(from_control || app.automation_input || app.session.authorize.is_some()) {
|
||||
return Ok(());
|
||||
}
|
||||
let params = json!({});
|
||||
@@ -409,7 +412,14 @@ pub fn pending_command(app: &PhotocraftApp) -> Option<(String, Value)> {
|
||||
|
||||
/// Answers the prompt for a control client: `save`, `dontSave` or `cancel` (Revert takes
|
||||
/// `dontSave` or `cancel`). Like the buttons, Save may first ask for a path in the save dialog.
|
||||
/// Save is always judged as automation for `file.save`, whatever gate the request installed.
|
||||
pub fn answer_control(app: &mut PhotocraftApp, ctx: &egui::Context, choice: &str) -> Result<Value, String> {
|
||||
answer_choice(app, ctx, choice, true)
|
||||
}
|
||||
|
||||
/// Answers the prompt with `choice` (see [`answer_control`]); `from_control` says a control
|
||||
/// client chose it, so its Save is judged as automation even without a session gate.
|
||||
pub(crate) fn answer_choice(app: &mut PhotocraftApp, ctx: &egui::Context, choice: &str, from_control: bool) -> Result<Value, String> {
|
||||
let Some(doc) = app.discard.as_ref().and_then(|p| p.docs.first().copied()) else {
|
||||
return Err("no unsaved-changes prompt is open".into());
|
||||
};
|
||||
@@ -426,7 +436,7 @@ pub fn answer_control(app: &mut PhotocraftApp, ctx: &egui::Context, choice: &str
|
||||
return Err(format!("unknown choice `{other}`: {choices}"));
|
||||
}
|
||||
};
|
||||
apply(app, ctx, doc, answer)?;
|
||||
apply(app, ctx, doc, answer, from_control)?;
|
||||
Ok(json!({"open": app.discard.is_some(), "fileDialog": app.file_dialog_open()}))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user