Files
vectorcraft/crates/plugins/src/permissions.rs
T
dotCooCoo ccf5765419 Tests: assert counts and saved names in tool, path, 3D, brush and CAD tests
Tests whose all() passed on an empty list now check the list's length or
that it is not empty:
- huge_circles_bridge_with_finite_anchors_or_are_refused (pathops bridge)
- smooth_rounds_corners (pathops ops): the four corners stay, smoothed
- a_held_rotation_turns_the_art_around_its_pin (tools ARAP): one point
  per mesh vertex
- the_eraser_drag_area_matches_the_release_when_pressure_or_a_mode_starts_during_the_drag
  (tools gesture): one released point per press
- every_effect_casts_a_shadow, inflate_cells_across_a_hole_edge_are_cut_to_the_shape,
  a_plane_has_only_a_front_and_a_hollow_solid_none and
  perspective_runs_0_to_160_degrees_with_the_eye_outside_the_object (three-d)
- a_file_two_branches_include_is_followed_from_the_shallower_one
  (scriptcheck): one row per include line
- pens_pick_colors_and_widths (cad HP-GL): four widths
- junk_pressure_brushes_draw_finite_art (brush): all 12 cases draw

Round trips that hold by construction (parse finds the id in ALL) are
replaced with one saved name that must parse and one that must not:
follow_names_parse (pathops shared edge), the shape kinds (pathops
shapes), surfaces_parse_from_their_ids (three-d), the Quick Pen actions
(tools), names_parse_and_serialize_as_names (plugins) and the brush
variation modes, which also compare the serde names with the ids.

crop_keeps_an_instance_whose_art_reaches_the_region (cad) names the
entities written instead of checking that some were.
the_saved_engine_preference_is_found (app) pins the GPU choices the
preferences offer to the three the app reads.
timing_unite_1000_circles (pathops) gets an ignore reason and checks
that Unite gives one shape.

Each changed assertion fails when the code under test is broken (an id
renamed, a list emptied, a flush or instance dropped) and passes when it
is restored.
2026-12-01 17:35:21 -08:00

460 lines
23 KiB
Rust

//! What a plug-in may do: the permissions its manifest lists (`permissions`), which the user
//! approves when it is installed ([`Grant`], kept in [`crate::registry`]), and which the host
//! enforces on every run.
//!
//! `doc.read`, `doc.write`, `state` and `progress` are the names of the plug-in capability model in
//! storytold/photocraft#662; the other names are VectorCraft's own. A manifest without
//! `permissions` gets the ones its kind implies ([`Permission::implied`]): what every plug-in of
//! that kind could do before manifests listed them. `progress`, `settings`, `cache` and `commands`,
//! which let a plug-in use the host imports of API level 2 ([`crate::host`]), are never implied: a
//! manifest lists them.
use serde::{Deserialize, Serialize};
use crate::{Error, Kind, Result};
/// The plug-in API level this host implements. A manifest's `api` (1 when left out) may be at most
/// this. Level 2 adds the host imports ([`PROGRESS_API`]) and a live effect's `paintOutput`
/// ([`crate::painted`]); level 3 adds a filter's `writes` ([`WRITES_API`]), a live effect's
/// `appearanceOutput` ([`APPEARANCE_API`]) and a manifest's `context` ([`CONTEXT_API`]). Level 4
/// adds an importer's `outputKinds` ([`OUTPUT_KINDS_API`]).
pub const API_LEVEL: u32 = 4;
/// The API level that adds an importer's `outputKinds` ([`crate::OutputKind`]): type, embedded
/// images, layers and artboards in what it returns ([`crate::import`]).
pub const OUTPUT_KINDS_API: u32 = 4;
/// The API level that adds a painted live effect's `appearanceOutput`: opacity, a blending mode
/// and effects on the pieces it returns and on their fills and strokes ([`crate::painted`]).
pub const APPEARANCE_API: u32 = 3;
/// The API level that adds the host imports ([`crate::host::IMPORTS`]) and the `progress`,
/// `settings`, `cache` and `commands` permissions they need.
pub const PROGRESS_API: u32 = 2;
/// The API level that adds a filter's `inputs` ([`crate::InputKind`]).
pub const INPUTS_API: u32 = 2;
/// The API level that adds a filter's `writes`: the kinds among its `inputs` it may change.
pub const WRITES_API: u32 = 3;
/// The API level that reads a command's `menu` ([`crate::manifest::MenuPlace`]); below it the key
/// is ignored.
pub const MENU_API: u32 = 2;
/// The API level that adds a manifest's `context` ([`crate::ContextSection`]): the document's
/// settings, artboards, layers and swatches, given to a run with or without a selection.
pub const CONTEXT_API: u32 = 3;
/// Most permissions a manifest may list.
pub const MAX_PERMISSIONS: usize = 16;
/// Longest plug-in name a prompt shows, in characters.
pub const MAX_SHOWN_NAME: usize = 64;
/// Longest version or id a prompt shows, in characters.
pub const MAX_SHOWN_FIELD: usize = 64;
/// Names kept for host calls a later plug-in API adds. This host grants none of them, so a
/// manifest that lists one is refused when it is installed.
pub const RESERVED: &[&str] = &["ui.panel", "ui.tool"];
/// One thing a plug-in may do.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Permission {
/// Read the objects the host hands it: a filter's selected paths, the object a live effect is
/// on, the document an exporter writes; and the document information a manifest's `context`
/// asks for ([`crate::ContextSection`]).
#[serde(rename = "doc.read")]
DocRead,
/// Change, add and delete objects (a filter's output), as one undo step.
#[serde(rename = "doc.write")]
DocWrite,
/// Read and write the data it keeps with the document (a filter's `documentData`).
#[serde(rename = "state")]
State,
/// Change how the objects its live effect is applied to are drawn.
#[serde(rename = "effect.render")]
EffectRender,
/// Write the document as a file in its own format (an exporter).
#[serde(rename = "export.write")]
ExportWrite,
/// Read a file the user opens and make a new document from it (an importer).
#[serde(rename = "import.read")]
ImportRead,
/// Report how far a run has got, see that the user asked it to stop and write log lines: the
/// host imports of API level 2 ([`crate::host::IMPORTS`]).
#[serde(rename = "progress")]
Progress,
/// Keep settings of its own between runs and sessions ([`crate::storage`]).
#[serde(rename = "settings")]
Settings,
/// Keep a cache of its own, which the user can clear ([`crate::storage`]).
#[serde(rename = "cache")]
Cache,
/// Run the VectorCraft commands its manifest's `commandAllowlist` names, with `vc_call`
/// ([`crate::host::IMPORTS`]): a filter only.
#[serde(rename = "commands")]
Commands,
}
/// Every permission this host grants, in the order prompts list them.
pub const ALL: [Permission; 10] = [
Permission::DocRead,
Permission::DocWrite,
Permission::State,
Permission::EffectRender,
Permission::ExportWrite,
Permission::ImportRead,
Permission::Progress,
Permission::Settings,
Permission::Cache,
Permission::Commands,
];
/// The permissions of the host imports, which any kind may list and none implies; each needs plug-in
/// API level [`PROGRESS_API`]. Only a filter with a `commandAllowlist` may list `commands`
/// ([`crate::manifest::Manifest::command_allowlist`]).
pub const HOST: [Permission; 4] = [Permission::Progress, Permission::Settings, Permission::Cache, Permission::Commands];
impl Permission {
/// Its name in a manifest and in `plugin.install`'s `grant`.
pub fn name(self) -> &'static str {
match self {
Permission::DocRead => "doc.read",
Permission::DocWrite => "doc.write",
Permission::State => "state",
Permission::EffectRender => "effect.render",
Permission::ExportWrite => "export.write",
Permission::ImportRead => "import.read",
Permission::Progress => "progress",
Permission::Settings => "settings",
Permission::Cache => "cache",
Permission::Commands => "commands",
}
}
/// The permission named `name`.
pub fn parse(name: &str) -> Option<Permission> {
ALL.into_iter().find(|p| p.name() == name)
}
/// What it lets a plug-in do, in plain words (English; the app shows its own translation).
pub fn describe(self) -> &'static str {
match self {
Permission::DocRead => "Read the objects it is given",
Permission::DocWrite => "Change, add and delete objects in the document",
Permission::State => "Keep data of its own with the document",
Permission::EffectRender => "Change how the objects its effect is applied to are drawn",
Permission::ExportWrite => "Write the document as a file in its own format",
Permission::ImportRead => "Read files you open in its format and make documents from them",
Permission::Progress => "Show its progress, see Cancel and write log lines",
Permission::Settings => "Keep settings of its own",
Permission::Cache => "Keep a cache of its own, which you can clear",
Permission::Commands => "Run these commands",
}
}
/// May a plug-in of `kind` that declares `commands` or not list it: one [`Permission::allowed`]
/// gives it, or one of the host imports' ([`HOST`]), which any kind may list.
pub fn applies(self, kind: Kind, commands: bool) -> bool {
HOST.contains(&self) || Permission::allowed(kind, commands).contains(&self)
}
/// The permissions a manifest of `kind` without `permissions` gets. A manifest may also list
/// the host imports' ([`HOST`], [`Permission::applies`]).
pub fn implied(kind: Kind) -> &'static [Permission] {
match kind {
Kind::Filter => &[Permission::DocRead, Permission::DocWrite, Permission::State],
Kind::Effect => &[Permission::DocRead, Permission::EffectRender],
Kind::Exporter => &[Permission::DocRead, Permission::ExportWrite],
Kind::Importer => &[Permission::ImportRead],
Kind::Generator => &[Permission::DocWrite],
}
}
/// The permissions a plug-in of `kind` that declares `commands` or not may list besides
/// `progress`, which are also the ones its manifest gets without `permissions`: those `kind`
/// implies, and for a live effect with commands also `doc.write` and `state`, which only its
/// commands use (drawing the effect changes no object and keeps no data).
pub fn allowed(kind: Kind, commands: bool) -> &'static [Permission] {
match kind {
Kind::Effect if commands => &[Permission::DocRead, Permission::DocWrite, Permission::State, Permission::EffectRender],
_ => Permission::implied(kind),
}
}
/// The permissions a plug-in of `kind` can't work without, which its list must hold: a live
/// effect and an exporter are always handed objects, and only draw or write; an importer is
/// always handed the file being opened; a generator always adds its art to the document. A
/// filter may leave any of its permissions out.
fn required(kind: Kind) -> &'static [Permission] {
match kind {
Kind::Filter => &[],
Kind::Effect | Kind::Exporter | Kind::Importer | Kind::Generator => Permission::implied(kind),
}
}
}
/// Checks the `permissions` a manifest of `kind` lists → them, in the order [`ALL`] has; `commands`
/// is whether it declares commands ([`Permission::allowed`]). A name that is not a permission, a
/// reserved one, one that doesn't apply to the kind, one listed twice and a list without what the
/// kind needs are errors that name it.
pub(crate) fn validate(kind: Kind, commands: bool, names: &[String]) -> Result<Vec<Permission>> {
if names.len() > MAX_PERMISSIONS {
return Err(Error::Manifest(format!("at most {MAX_PERMISSIONS} `permissions`")));
}
let mut out: Vec<Permission> = Vec::with_capacity(names.len());
for name in names {
let shown = display_text(name, MAX_SHOWN_FIELD);
let Some(p) = Permission::parse(name) else {
if RESERVED.contains(&name.as_str()) {
return Err(Error::Manifest(format!("permission `{shown}` is reserved for a later plug-in API; this host doesn't grant it")));
}
return Err(Error::Manifest(format!("unknown permission `{shown}`")));
};
if !p.applies(kind, commands) {
return Err(Error::Manifest(format!("permission `{shown}` doesn't apply to a plug-in of kind `{}`", kind.name())));
}
if out.contains(&p) {
return Err(Error::Manifest(format!("permission `{shown}` is listed twice")));
}
out.push(p);
}
if let Some(missing) = Permission::required(kind).iter().find(|p| !out.contains(p)) {
return Err(Error::Manifest(format!("a plug-in of kind `{}` needs the permission `{}`", kind.name(), missing.name())));
}
out.sort();
Ok(out)
}
/// The names of `permissions`, as a manifest and `grant` list them.
pub fn names(permissions: &[Permission]) -> Vec<&'static str> {
permissions.iter().map(|p| p.name()).collect()
}
/// Is `c` a character that changes the direction text is laid out in, or a line or paragraph
/// separator, which a prompt must not let a manifest string use to disguise itself?
pub(crate) fn is_bidi_or_separator(c: char) -> bool {
matches!(c, '\u{061C}' | '\u{200E}' | '\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}' | '\u{2028}' | '\u{2029}')
}
/// `s` as a prompt shows a string from a manifest: control, bidirectional formatting and line
/// separator characters written as `\u{202E}` escapes, and at most `max` characters, the last of
/// them `…` when it was cut.
pub fn display_text(s: &str, max: usize) -> String {
use std::fmt::Write as _;
let mut out = String::new();
// Characters written so far, and the byte length of the longest prefix of whole pieces
// shorter than `max` characters, where the ellipsis goes when the text is cut.
let mut count = 0;
let mut cut = 0;
for c in s.chars() {
let start = out.len();
let n = if c.is_control() || is_bidi_or_separator(c) {
let _ = write!(out, "\\u{{{:04X}}}", c as u32);
out.len() - start
} else {
out.push(c);
1
};
if count + n > max {
out.truncate(cut);
out.push('…');
return out;
}
count += n;
if count < max {
cut = out.len();
}
}
out
}
/// The SHA-256 of `bytes`, in lower-case hexadecimal.
pub fn sha256_hex(bytes: &[u8]) -> String {
use sha2::{Digest, Sha256};
Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect()
}
/// Is `s` a SHA-256 as [`sha256_hex`] writes it: 64 lower-case hexadecimal digits?
fn is_sha256_hex(s: &str) -> bool {
s.len() == 64 && s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
/// What the user approved for a plug-in: its id, the SHA-256 of the module they approved
/// ([`sha256_hex`]) and the permissions it was granted.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Grant {
pub id: String,
pub hash: String,
pub permissions: Vec<Permission>,
}
impl Grant {
/// Does it grant every one of `wanted` to the module with this `id` and SHA-256 `hash`? A grant
/// whose own hash is not a valid SHA-256 ([`sha256_hex`]) covers nothing.
pub fn covers(&self, id: &str, hash: &str, wanted: &[Permission]) -> bool {
self.id == id && is_sha256_hex(&self.hash) && self.hash == hash && wanted.iter().all(|w| self.permissions.contains(w))
}
}
/// A [`Grant`] that [`crate::registry::grant`] records. The engine makes one where it records what
/// a person approved and where it loads the grants kept in its plug-in folder; tests that set up a
/// grant use the same constructors. `tests/grant_sites.rs` fails when other shipped code makes one.
/// Native code linked into the app can still call these constructors, so the token does not
/// protect against such code.
#[derive(Debug)]
pub struct GrantToken(Grant);
impl GrantToken {
/// The grant the engine records after a person approved the module it covers, or in a session
/// whose starter allowed requests to grant permissions (`--allow-plugin-grants`).
pub fn after_engine_approval(grant: Grant) -> Self {
Self(grant)
}
/// A grant the engine read from the grants file of its plug-in folder, for a module it loads.
pub fn from_engine_grants_file(grant: Grant) -> Self {
Self(grant)
}
/// The grant it holds.
pub fn grant(&self) -> &Grant {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_parse_and_serialize_as_names() {
assert_eq!(serde_json::to_value(ALL).unwrap(), serde_json::json!(names(&ALL)), "saved names are the names");
assert_eq!(Permission::parse("doc.write"), Some(Permission::DocWrite));
assert_eq!(Permission::parse("document.read"), None);
assert!(!RESERVED.is_empty() && RESERVED.iter().all(|r| Permission::parse(r).is_none()), "reserved names are not granted");
}
#[test]
fn lists_are_checked_against_the_kind() {
let list = |names: &[&str]| names.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert_eq!(validate(Kind::Filter, false, &list(&["doc.write", "doc.read"])).unwrap(), [Permission::DocRead, Permission::DocWrite]);
assert_eq!(validate(Kind::Filter, false, &[]).unwrap(), []);
assert!(validate(Kind::Effect, false, &list(&["doc.read", "effect.render"])).is_ok());
assert!(validate(Kind::Exporter, false, &list(&["export.write", "doc.read"])).is_ok());
assert_eq!(validate(Kind::Importer, false, &list(&["import.read"])).unwrap(), [Permission::ImportRead]);
// Any kind may list `progress`, which no kind implies.
assert_eq!(validate(Kind::Filter, false, &list(&["progress", "doc.read"])).unwrap(), [Permission::DocRead, Permission::Progress]);
assert!(validate(Kind::Effect, false, &list(&["doc.read", "effect.render", "progress"])).is_ok());
assert!(validate(Kind::Effect, true, &list(&["doc.read", "effect.render", "progress"])).is_ok());
assert!(validate(Kind::Exporter, false, &list(&["progress", "export.write", "doc.read"])).is_ok());
assert_eq!(validate(Kind::Importer, false, &list(&["progress", "import.read"])).unwrap(), [Permission::ImportRead, Permission::Progress]);
assert!(
[Kind::Filter, Kind::Effect, Kind::Exporter, Kind::Importer].iter().all(|k| !Permission::implied(*k).contains(&Permission::Progress))
);
// So may `settings` and `cache`, which no kind implies either.
assert_eq!(
validate(Kind::Effect, false, &list(&["cache", "settings", "doc.read", "effect.render"])).unwrap(),
[Permission::DocRead, Permission::EffectRender, Permission::Settings, Permission::Cache]
);
assert!(validate(Kind::Exporter, false, &list(&["settings", "export.write", "doc.read"])).is_ok());
assert_eq!(validate(Kind::Filter, false, &list(&["cache"])).unwrap(), [Permission::Cache]);
for kind in [Kind::Filter, Kind::Effect, Kind::Exporter, Kind::Importer] {
for commands in [false, true] {
assert!(HOST.iter().all(|p| p.applies(kind, commands) && !Permission::implied(kind).contains(p)));
}
}
// A live effect with commands may also change the document and keep data with it.
assert_eq!(
validate(Kind::Effect, true, &list(&["state", "effect.render", "doc.write", "doc.read"])).unwrap(),
[Permission::DocRead, Permission::DocWrite, Permission::State, Permission::EffectRender]
);
assert_eq!(Permission::allowed(Kind::Effect, false), Permission::implied(Kind::Effect));
assert_eq!(Permission::allowed(Kind::Filter, true), Permission::implied(Kind::Filter));
for (kind, commands, names, says) in [
(Kind::Filter, false, &["doc.read", "clipboard"][..], "unknown permission `clipboard`"),
(Kind::Filter, false, &["ui.panel"], "`ui.panel` is reserved"),
(Kind::Filter, false, &["progress", "progress"], "`progress` is listed twice"),
(Kind::Filter, false, &["export.write"], "doesn't apply to a plug-in of kind `filter`"),
(Kind::Exporter, false, &["doc.read", "export.write", "doc.read"], "`doc.read` is listed twice"),
(Kind::Effect, false, &["doc.read"], "needs the permission `effect.render`"),
(Kind::Effect, false, &["doc.read", "effect.render", "doc.write"], "`doc.write` doesn't apply to a plug-in of kind `effect`"),
(Kind::Effect, true, &["doc.read", "doc.write"], "needs the permission `effect.render`"),
(Kind::Effect, true, &["doc.read", "effect.render", "export.write"], "`export.write` doesn't apply"),
(Kind::Exporter, true, &["doc.read", "export.write", "doc.write"], "`doc.write` doesn't apply"),
(Kind::Exporter, false, &["export.write"], "needs the permission `doc.read`"),
(Kind::Filter, false, &["cache", "cache"], "`cache` is listed twice"),
(Kind::Filter, false, &["storage"], "unknown permission `storage`"),
(Kind::Importer, false, &["import.read", "doc.read"], "`doc.read` doesn't apply to a plug-in of kind `importer`"),
(Kind::Importer, true, &["import.read", "doc.write"], "`doc.write` doesn't apply to a plug-in of kind `importer`"),
(Kind::Importer, false, &[], "needs the permission `import.read`"),
(Kind::Exporter, false, &["export.write", "doc.read", "import.read"], "`import.read` doesn't apply to a plug-in of kind `exporter`"),
] {
let e = validate(kind, commands, &list(names)).unwrap_err().to_string();
assert!(e.contains(says), "{names:?}: {e}");
}
let many = list(&["doc.read"; MAX_PERMISSIONS + 1]);
assert!(validate(Kind::Filter, false, &many).unwrap_err().to_string().contains("at most"));
}
/// Control, bidirectional override and separator characters show as escapes, so a name can't
/// reverse or hide the text after it; long text is cut with an ellipsis.
#[test]
fn prompt_text_escapes_hidden_characters_and_is_capped() {
assert_eq!(display_text("Tidy\u{202E}fdp.exe", 64), "Tidy\\u{202E}fdp.exe");
assert_eq!(display_text("a\u{0007}b\nc\u{2066}d\u{200F}", 64), "a\\u{0007}b\\u{000A}c\\u{2066}d\\u{200F}");
assert_eq!(display_text("plain", 64), "plain");
assert_eq!(display_text("abcdef", 6), "abcdef");
assert_eq!(display_text("abcdefg", 6), "abcde…");
assert_eq!(display_text(&"x".repeat(500), MAX_SHOWN_NAME).chars().count(), MAX_SHOWN_NAME);
assert_eq!(display_text("ab\u{202E}", 6), "ab…", "an escape that doesn't fit is cut whole");
assert_eq!(display_text("ab\u{0007}", 10), "ab\\u{0007}", "an escape that ends exactly at the cap is kept");
let long = display_text(&"\u{202E}y".repeat(500_000), MAX_SHOWN_NAME);
assert_eq!(long.chars().count(), MAX_SHOWN_NAME);
assert!(long.starts_with("\\u{202E}y\\u{202E}y") && long.ends_with('…'), "{long}");
}
#[test]
fn hashes_are_sha256_in_hex() {
assert_eq!(sha256_hex(b"abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
}
#[test]
fn a_grant_covers_its_permissions_and_no_more() {
let hash = sha256_hex(b"module");
let g = Grant { id: "a".into(), hash: hash.clone(), permissions: vec![Permission::DocRead, Permission::DocWrite] };
assert!(g.covers("a", &hash, &[Permission::DocRead]));
assert!(g.covers("a", &hash, &[]));
assert!(!g.covers("a", &hash, &[Permission::State]));
assert!(!g.covers("a", &hash, &[Permission::DocRead, Permission::State]));
}
#[test]
fn a_grant_covers_only_the_module_that_was_approved() {
let hash = sha256_hex(b"module");
let wanted = [Permission::DocRead];
let g = Grant { id: "a".into(), hash: hash.clone(), permissions: vec![Permission::DocRead] };
assert!(g.covers("a", &hash, &wanted));
assert!(!g.covers("b", &hash, &wanted), "another id");
assert!(!g.covers("a", &sha256_hex(b"other"), &wanted), "other bytes");
assert!(!g.covers("a", &sha256_hex(b"other"), &[]), "other bytes, even when nothing is wanted");
}
/// Damaged data can't stand for every module, even when the hash asked about is just as damaged.
#[test]
fn a_grant_with_an_invalid_hash_covers_nothing() {
let upper = sha256_hex(b"module").to_uppercase();
let short = "ab".repeat(31);
let long = "ab".repeat(33);
let not_hex = "g".repeat(64);
let wide = "é".repeat(32);
for hash in ["", "00", upper.as_str(), short.as_str(), long.as_str(), not_hex.as_str(), wide.as_str()] {
let g = Grant { id: "a".into(), hash: hash.into(), permissions: vec![Permission::DocRead] };
assert!(!g.covers("a", hash, &[Permission::DocRead]), "{hash:?}");
assert!(!g.covers("a", hash, &[]), "{hash:?}");
}
}
}