mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-27 09:17:39 +00:00
Merge PR #15842: fix(automation): reject malformed doc.render requests
This commit is contained in:
@@ -232,6 +232,15 @@ impl Headless {
|
||||
}
|
||||
"doc.inspect" => self.inspect(index_of(&p)?),
|
||||
"doc.render" => {
|
||||
// Invalid file requests must not fall back to a base64 response.
|
||||
if !p.is_object() {
|
||||
return Err(bad("doc.render params must be an object"));
|
||||
}
|
||||
let path = match p.get("path") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(Value::String(path)) => Some(path.as_str()),
|
||||
Some(_) => return Err(bad("`path` must be a string")),
|
||||
};
|
||||
let max = match p.get("maxSide") {
|
||||
None => 1024,
|
||||
Some(value) => {
|
||||
@@ -241,7 +250,7 @@ impl Headless {
|
||||
let png = self.render_png(index_of(&p)?, max)?;
|
||||
// A GPU to CPU fallback is reported with this reply, not on the next save.
|
||||
let warnings = self.drain_diagnostics();
|
||||
let mut reply = match str_of(&p, "path") {
|
||||
let mut reply = match path {
|
||||
Some(path) => {
|
||||
self.write_render(&PathBuf::from(path), &png)?;
|
||||
json!({"path": path, "bytes": png.len()})
|
||||
|
||||
@@ -176,6 +176,32 @@ fn batch_rejects_too_many_steps() {
|
||||
assert!(error.to_string().contains("maximum is 256"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_rejects_malformed_requests_and_keeps_missing_or_null_path_behavior() {
|
||||
let h = session();
|
||||
let mut g = h.lock().unwrap();
|
||||
g.handle("doc.new", json!({"width": 8, "height": 8})).unwrap();
|
||||
let before = g.inspect(None).unwrap();
|
||||
for params in [json!([]), json!([0]), json!(123), json!(false), json!("out.png")] {
|
||||
let error = g.handle("doc.render", params.clone()).unwrap_err();
|
||||
assert!(matches!(&error, AutomationError::BadRequest(_)), "{params}: {error}");
|
||||
assert!(error.to_string().contains("doc.render params must be an object"), "{params}: {error}");
|
||||
assert_eq!(g.inspect(None).unwrap(), before, "invalid params {params} changed the document");
|
||||
}
|
||||
for path in [json!(123), json!(false), json!([]), json!({})] {
|
||||
let error = g.handle("doc.render", json!({"path": path})).unwrap_err();
|
||||
assert!(matches!(&error, AutomationError::BadRequest(_)), "{path}: {error}");
|
||||
assert!(error.to_string().contains("`path` must be a string"), "{path}: {error}");
|
||||
assert_eq!(g.inspect(None).unwrap(), before, "invalid path {path} changed the document");
|
||||
}
|
||||
for params in [Value::Null, json!({}), json!({"path": null})] {
|
||||
let reply = g.handle("doc.render", params.clone()).unwrap();
|
||||
assert_eq!(reply["mime"], "image/png", "{params}: {reply}");
|
||||
let png = base64::engine::general_purpose::STANDARD.decode(reply["base64"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(photocraft_codecs::decode(&png).unwrap().dimensions(), (8, 8));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_to_file_and_base64() {
|
||||
let h = session();
|
||||
|
||||
@@ -573,6 +573,10 @@ An `index` targets that one call: `doc.place`, `doc.exportLayers`, `doc.exportDa
|
||||
without making it the active one, so the next call without `index` still runs on the document
|
||||
that was active before (`doc.select` is the method that changes it).
|
||||
|
||||
`doc.render` treats omitted `params` and `"params": null` as `{}`. Pass a string `path`
|
||||
for a file reply. An omitted or null `path` requests an inline PNG. Other parameter
|
||||
container or path types return an error before rendering.
|
||||
|
||||
```sh
|
||||
printf '%s\n' \
|
||||
'{"id":1,"method":"doc.open","params":{"path":"in.jpg"}}' \
|
||||
|
||||
Reference in New Issue
Block a user