A3: ui.screenshot answers with the absolute path and checks the file

A relative path is resolved against the app's working directory before
the capture, and the answer names that absolute path, so a file written
elsewhere than expected is visible. After writing, the file is read back
and a missing or short file is an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-31 14:58:36 +01:00
parent af24899ac5
commit 3c80ab87c9
4 changed files with 36 additions and 10 deletions

View file

@ -190,10 +190,13 @@ fn dispatch(request: Request, backend: &dyn Backend) -> Response {
Err(e) => Response::err(id, "internal", e.to_string()),
},
UI_SCREENSHOT => match serde_json::from_value::<ScreenshotParams>(params) {
Ok(p) => match backend.screenshot(&p.path) {
Ok(()) => Response::ok(id, json!({ "path": p.path })),
Err(message) => Response::err(id, codes::SCREENSHOT_FAILED, message),
},
Ok(p) => {
let path = std::path::absolute(&p.path).unwrap_or(p.path);
match backend.screenshot(&path) {
Ok(()) => Response::ok(id, json!({ "path": path })),
Err(message) => Response::err(id, codes::SCREENSHOT_FAILED, message),
}
}
Err(e) => Response::err(id, codes::INVALID_PARAMS, e.to_string()),
},
APP_AUTH => Response::ok(id, json!({ "authenticated": true })),
@ -256,10 +259,12 @@ mod tests {
}
fn screenshot(&self, path: &Path) -> Result<(), String> {
if path.as_os_str() == "panic" {
panic!("screenshot exploded");
assert!(path.is_absolute(), "{}", path.display());
match path.file_name().and_then(|n| n.to_str()) {
Some("panic") => panic!("screenshot exploded"),
Some("ok.png") => Ok(()),
_ => Err("no window".into()),
}
Err("no window".into())
}
fn subscribe(&self, _sink: crate::EventSink) {}
@ -332,6 +337,14 @@ mod tests {
);
}
#[test]
fn screenshot_reports_the_absolute_path() {
let r = call(r#"{"id":1,"method":"ui.screenshot","params":{"path":"shots/ok.png"}}"#);
let path = std::path::PathBuf::from(r.result.unwrap()["path"].as_str().unwrap());
assert!(path.is_absolute());
assert!(path.ends_with("shots/ok.png"));
}
#[test]
fn backend_panics_become_internal_errors() {
let r = call(r#"{"id":9,"method":"ui.screenshot","params":{"path":"panic"}}"#);