M11: a plugin can read the images a document holds (#2944)

New command image.data {image} or {ids?, target?, index?} returns {image, format, width, height, bytes, data}: an image the file holds, by its key (a paint's `image`) or a layer's image paint, with data as base64 of the original file, never re-encoded. It changes nothing, so plugins with read access may call it; a result is bounded by the 16 MB message limit like any other. MCP reaches it through run_command.
This commit is contained in:
bflatastic
2026-12-03 00:29:57 +07:00
parent 5975fa0ded
commit 2fd90b1d49
9 changed files with 88 additions and 2 deletions
+32
View File
@@ -57,6 +57,13 @@ pub fn specs() -> Vec<CommandSpec> {
true,
vectorize::vectorize
),
spec!(
"image.data",
"Image data",
"{image (the key a paint's `image` names) | ids? (one layer), target? (fill|stroke, default fill), index? (default its top image paint)} → {image, format (png|jpg|gif|webp|tiff|bmp), width, height, bytes, data (base64 of the file's original bytes)} reads an image the document holds, never re-encoded",
false,
data
),
spec!("image.presets", "Image presets", "{} → {builtIn: [{id, label, filters}], saved: [{name, filters}]}", false, |s, _| {
let built_in: Vec<Value> = ImageFilters::PRESETS.iter().map(|(id, label, f)| json!({"id": id, "label": label, "filters": f})).collect();
Ok(json!({"builtIn": built_in, "saved": s.doc()?.image_presets}))
@@ -205,6 +212,31 @@ fn replace(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"image": image, "width": width, "height": height, "changed": changed}))
}
fn data(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "image.data";
let key = match string(p, "image") {
Some(key) => key,
None => {
let [id] = req_targets(cmd, s, p)?[..] else { return Err(bad(cmd, "pass `image`, or one layer")) };
let stroke = strokes(cmd, p)?;
let n = s.doc()?.get(id)?;
let list = if stroke { &n.strokes } else { &n.fills };
let at = match get(p, "index") {
None | Some(Value::Null) => list.iter().rposition(|x| matches!(x.kind, PaintKind::Image(_))),
Some(_) => Some(req_index(cmd, p, "index")?),
};
match at.and_then(|i| list.get(i)).map(|x| &x.kind) {
Some(PaintKind::Image(img)) => img.image.clone(),
_ => return Err(bad(cmd, "no image paint there")),
}
}
};
let file = crate::asset_handoff::image_file(s.doc()?, &key).map_err(|why| bad(cmd, why))?;
use base64::Engine as _;
let data = base64::engine::general_purpose::STANDARD.encode(file.bytes.as_slice());
Ok(json!({"image": key, "format": file.ext, "width": file.data.width, "height": file.data.height, "bytes": file.bytes.len(), "data": data}))
}
/// The index of a layer's top visible image fill among the fills it shows (fills paint bottom to
/// top): a text's characters' shared list, so none while they are coloured differently.
pub fn top_image_fill(n: &Node) -> Option<usize> {
+6
View File
@@ -865,6 +865,12 @@ mod refusal_tests {
assert!(refusal(&m, "branch.list", &json!({})).is_none());
}
#[test]
fn read_only_plugin_can_read_image_bytes() {
let m = Manifest::parse(r#"{"id":"x.y","name":"X","main":"plugin.wasm"}"#).unwrap();
assert!(refusal(&m, "image.data", &json!({"image": "k"})).is_none());
}
#[test]
fn plugins_cannot_compare_against_a_saved_version() {
let m = Manifest::parse(r#"{"id":"x.y","name":"X","main":"plugin.wasm","permissions":{"access":"write"}}"#).unwrap();
+30
View File
@@ -0,0 +1,30 @@
//! `image.data`: a layer's image paint, or an image by its key, comes back as the file's own
//! bytes (never re-encoded), so a read-only plugin can inspect what `image.replace` would swap.
use base64::Engine as _;
use serde_json::json;
use crate::Session;
#[test]
fn image_data_returns_the_original_bytes_by_layer_and_by_key() {
let mut s = Session::new();
s.execute("file.new", &json!({})).unwrap();
let png = {
let image = image::RgbaImage::from_fn(3, 2, |x, y| image::Rgba([x as u8 * 80, y as u8 * 90, 7, 255]));
let mut out = std::io::Cursor::new(Vec::new());
image.write_to(&mut out, image::ImageFormat::Png).unwrap();
out.into_inner()
};
let b64 = base64::engine::general_purpose::STANDARD;
let placed = s.execute("image.place", &json!({"data": b64.encode(&png)})).unwrap();
let by_layer = s.execute("image.data", &json!({"ids": [placed["id"]]})).unwrap();
assert_eq!(b64.decode(by_layer["data"].as_str().unwrap()).unwrap(), png, "the bytes placed, unchanged");
assert_eq!((&by_layer["format"], &by_layer["width"], &by_layer["height"]), (&json!("png"), &json!(3), &json!(2)));
let by_key = s.execute("image.data", &json!({"image": placed["image"]})).unwrap();
assert_eq!(by_key, by_layer);
let rect = s.execute("node.create", &json!({"type": "rectangle", "width": 10, "height": 10})).unwrap();
assert!(s.execute("image.data", &json!({"ids": [rect["id"]]})).is_err(), "no image paint");
assert!(s.execute("image.data", &json!({"image": "nope"})).is_err(), "unknown key");
}
+2
View File
@@ -442,6 +442,8 @@ mod id_params_behaviour_tests;
#[cfg(test)]
mod image_crop_tests;
#[cfg(test)]
mod image_data_tests;
#[cfg(test)]
mod image_hit_tests;
#[cfg(test)]
mod image_readable_tests;
+3
View File
@@ -296,6 +296,8 @@ No alpha blockers: the alpha gate in [`roadmap.md`](roadmap.md#alpha-gate) passe
Reading the web (#1883, 2026-12-02): a plugin can read the exact hosts its manifest names
(`permissions.network`, plugin API 9) over HTTPS with `GET` or `HEAD`, through FigCraft, after
the user approves them on import.
Reading images (#2944, 2026-12-03): `image.data` returns an image the document holds as its
original bytes, so a read-only plugin can inspect or re-encode images itself.
- **Missing:** a public catalogue and Community files, publisher accounts and signatures, ratings
and install counts, background update checks, keeping plugins installed in the browser past the
page (the directory works there since 2026-10-31); widgets have no
@@ -723,6 +725,7 @@ No alpha blockers: the alpha gate in [`roadmap.md`](roadmap.md#alpha-gate) passe
| Date | Change | Summary |
|---|---|---|
| 2026-12-03 | minor | Gap 6, #2944: plugins can read the images a document holds: `image.data {image}` or `{ids?, target?, index?}` → `{image, format, width, height, bytes, data}` (base64 of the original file), allowed with read access and bounded by the 16 MB message limit; also through MCP `run_command`. Estimate unchanged. Tests: `image_data_tests`, `refusal_tests::read_only_plugin_can_read_image_bytes` |
| 2026-12-02 | minor | Gap 6, #1883: a plugin can read the web hosts its manifest names: `permissions.network` (plugin API 9) lists exact hosts and `GET`/`HEAD`, and `http_request` asks FigCraft for an `https://` address on one of them (no redirects followed, no credentials sent, 8 requests per run, 4 MB per response). Importing such a plugin needs the user's approval (`plugin.import` `confirm: true`, `check: true` to look first); Manage plugins lists the hosts; plugin directories refuse such plugins until an entry can show them; confined sessions make no requests. Estimate unchanged. Tests: `network_tests`, `plugin_network_tests` |
| 2026-12-01 | minor | Gap 24: M12, #1736: as the help describes, dragging the border between two rows of the selected board table sizes the row above it (it keeps at least that height, and still grows with its text), in one undo step through `board.tableEdit {action: resizeRow, index, size}`. A click in the selected table picks a cell (it shows an outline), and the bar's colours go on that cell, or on every cell with the table selected; `board.setColor` takes a cell's text id from `board.table`'s `cells`, or the table's id. Every cell is as tall as its row, so a colour reaches the row's line. Not built: the row and column grabbers, picking several cells, Tab between cells. Numbers unchanged. Tests: `board_table_size_tests`. |
| 2026-12-01 | minor | Gap 18, #1392: bounded automatic retries recover same-stamp read failures; warnings are deduplicated per file and failed replacement, clearing on recovery or a new stamp. No parity or estimate changes |
+3
View File
@@ -376,6 +376,9 @@ ArtCraft Launcher's agents switch for FigCraft.
`image.replace {ids?, target?: fill|stroke, index?, data | path}` puts another image in a paint
(by default each layer's top image paint), keeping its fill mode, crop, position and adjustments;
a paint at `index` that isn't an image becomes one, filling the layer.
`image.data {image | ids?, target?, index?}` reads an image the file holds, by its key (a paint's
`image`) or a layer's paint, as `{image, format, width, height, bytes, data}`: `data` is base64 of
the original file, never re-encoded. Plugins with read access may call it.
`image.savePreset {name, filters?}` saves the current (or given) adjustments in the file,
`image.deletePreset {name}` removes one and `image.presets` lists both kinds.
`image.crop {ids?, index?}` (one layer) switches its top visible image fill (or fill `index`) to
+2 -2
View File
@@ -1,8 +1,8 @@
# Parity checklist
> **Generated:** 2026-12-01 by `cargo xtask parity`. Do not edit by hand.
> **Generated:** 2026-12-02 by `cargo xtask parity`. Do not edit by hand.
FigCraft commands registered for the incumbent's main menu and toolbar (703 commands in the engine).
FigCraft commands registered for the incumbent's main menu and toolbar (704 commands in the engine).
**216 / 216 menu items (100.0%)**
+9
View File
@@ -326,6 +326,15 @@ does, but there is no folder to keep a plugin in, so it lasts as long as the pag
| `commands` | The menu commands, each `{id, label, params?}`. Without any, the plugin has one, `run`, unless it's a widget (a widget needs no command). |
| `params[]` | `{name, label?, type, default?, options?, min?, max?}`; `type` is `text`, `number` (clamped to `min`/`max`), `boolean`, `select` (one of `options`, a menu), `list` (one of `options`, shown as a list), `slider` (a number between `min` and `max`, both required, shown as a slider with its value beside it) or `color` (hex, `#RRGGBB` or `#RRGGBBAA`). A default that doesn't fit is dropped. |
## Reading images
A plugin can't read files, but it can read the images the document holds: `image.data {image}` (the
key a paint's `image` names in `node.get`) or `image.data {ids: [id], target?, index?}` (a layer's
top image paint, or the one at `index`) returns `{image, format, width, height, bytes, data}`, where
`data` is base64 of the original file, never re-encoded. It needs only read access. A result over 16
MB (an image of about 12 MB) is refused like any other. `image.replace` with `data` puts an image
back, keeping the paint's fill mode, crop and adjustments.
## What a plugin can't do
Whatever its manifest says, FigCraft refuses these calls (the plugin gets the refusal as an error
+1
View File
@@ -666,6 +666,7 @@ Carried over from the ROADMAP's "Shipped so far" (2026-10-10). The test count th
| Date | Change | Summary |
|---|---|---|
| 2026-12-03 | patch | M11, #2944: a plugin can read an image the document holds (`image.data`, the original file's bytes as base64) with read access. Numbers unchanged. Tests: `image_data_tests`. |
| 2026-12-02 | patch | M11, #1883: a plugin can read fresh data from the web hosts its manifest names (plugin API 9, `permissions.network`: exact hosts, `GET`/`HEAD`, HTTPS only, through FigCraft with no credentials and no redirects followed), after the user approves them on import. Older plugins unchanged. Numbers unchanged. Tests: `network_tests`, `plugin_network_tests`. |
| 2026-12-01 | patch | M11, #2752: a plugin can enable the libraries it ships (plugin API 8, `permissions.libraries`: `.figcraft` files in its `assets` folder), so an asset-browser plugin places its components without the user adding its library to each file first. Older plugins unchanged. Numbers unchanged. Tests: `libraries_tests`, `plugin_libraries_tests`. |
| 2026-12-01 | patch | M3, #2777: a frame that doesn't clip its content grew its drop shadows by their spread; the reference tool ignores the spread there, as its renders of a synthetic file show (a 160 × 120 frame, radius 24, with a shadow spread by 4 and one 6 to the left spread by -2: no ring above it, the bare outline moved 6; a rectangle and a clipping frame grow and shrink theirs). The canvas, PNG/JPG/WebP, PDF and SVG exports now draw an unclipped frame's drop shadows without their spread and leave no room for it in the export's area; clipping the content again brings the spread back. Inner shadows keep their spread (not measured). Numbers unchanged |