PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Plug-in names reject bidi controls, zero-width spaces and other invisible format characters, and need a visible character (#1011) (#1444)
MarekSpodBiedry746f2530e617
diff --git a/crates/plugins/src/manifest.rs b/crates/plugins/src/manifest.rsindex c87b64526..ccabf47d5 100644--- a/crates/plugins/src/manifest.rs+++ b/crates/plugins/src/manifest.rs@@ -107,7 +107,8 @@ impl Manifest {return Err(Error::Manifest(format!("invalid id {:?}: use 1-64 characters from A-Z a-z 0-9 . _ -", self.id)));}let name = self.name.trim();- if name.is_empty() || name.chars().count() > 64 || name.chars().any(char::is_control) {+ let printable = name.chars().all(|c| !c.is_control() && !hidden_format(c)) && name.chars().any(visible);+ if !printable || name.chars().count() > 64 {return Err(Error::Manifest("`name` must be 1-64 printable characters".into()));}if self.version.len() > 32 || self.description.len() > 1024 || self.author.len() > 128 {@@ -251,6 +252,62 @@ pub fn valid_id(id: &str) -> bool {(1..=64).contains(&id.len()) && id.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))}+/// The Unicode format characters (general category Cf, Unicode 15.1). Most draw nothing, and the bidi+/// controls among them (U+061C, U+200E-200F, U+202A-202E, U+2066-2069) reorder the text around them.+const FORMAT: &[(char, char)] = &[+ ('\u{AD}', '\u{AD}'),+ ('\u{600}', '\u{605}'),+ ('\u{61C}', '\u{61C}'),+ ('\u{6DD}', '\u{6DD}'),+ ('\u{70F}', '\u{70F}'),+ ('\u{890}', '\u{891}'),+ ('\u{8E2}', '\u{8E2}'),+ ('\u{180E}', '\u{180E}'),+ ('\u{200B}', '\u{200F}'),+ ('\u{202A}', '\u{202E}'),+ ('\u{2060}', '\u{2064}'),+ ('\u{2066}', '\u{206F}'),+ ('\u{FEFF}', '\u{FEFF}'),+ ('\u{FFF9}', '\u{FFFB}'),+ ('\u{110BD}', '\u{110BD}'),+ ('\u{110CD}', '\u{110CD}'),+ ('\u{13430}', '\u{1343F}'),+ ('\u{1BCA0}', '\u{1BCA3}'),+ ('\u{1D173}', '\u{1D17A}'),+ ('\u{E0001}', '\u{E0001}'),+ ('\u{E0020}', '\u{E007F}'),+];++/// Default-ignorable characters outside Cf (fillers, variation selectors): allowed in a name, but they+/// draw nothing on their own.+const BLANK: &[(char, char)] = &[+ ('\u{34F}', '\u{34F}'),+ ('\u{115F}', '\u{1160}'),+ ('\u{17B4}', '\u{17B5}'),+ ('\u{180B}', '\u{180D}'),+ ('\u{180F}', '\u{180F}'),+ ('\u{3164}', '\u{3164}'),+ ('\u{FE00}', '\u{FE0F}'),+ ('\u{FFA0}', '\u{FFA0}'),+ ('\u{E0100}', '\u{E01EF}'),+];++fn in_ranges(c: char, ranges: &[(char, char)]) -> bool {+ ranges.iter().any(|&(lo, hi)| (lo..=hi).contains(&c))+}++/// A format character a menu label must not carry: it hides in the label or reorders it. The zero-width+/// non-joiner and joiner stay allowed, since Persian, Indic scripts and emoji sequences need them and they+/// cannot reorder text.+fn hidden_format(c: char) -> bool {+ !matches!(c, '\u{200C}' | '\u{200D}') && in_ranges(c, FORMAT)+}++/// A character that draws something on its own.+fn visible(c: char) -> bool {+ !c.is_whitespace() && !c.is_control() && !in_ranges(c, FORMAT) && !in_ranges(c, BLANK)+}+fn valid_key(k: &str) -> bool {(1..=64).contains(&k.len()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') && !k.starts_with('_') && k != "id" && k != "layer"}@@ -331,4 +388,33 @@ mod tests {assert!(Manifest::parse(&[0xff, 0xfe]).is_err());assert!(Manifest::parse(&vec![b' '; MAX_MANIFEST_BYTES + 1]).is_err());}++ fn with_name(name: &str) -> Result<Manifest> {+ Manifest::parse(json!({"id": "org.example.tone", "name": name, "kind": "filter"}).to_string().as_bytes())+ }++ #[test]+ fn name_must_be_printable_unicode() {+ for bad in [+ "Tone\u{202E}evil",+ "\u{2067}Tone\u{2069}",+ "Tone\u{200F}",+ "\u{200B}\u{200B}\u{200B}",+ "Tone\u{00AD}",+ "\u{FEFF}Tone",+ "\u{200D}",+ "\u{3164}\u{FE0F}",+ " \u{3000} ",+ "Tone\u{7}",+ ] {+ assert!(with_name(bad).is_err(), "{bad:?}");+ }+ // Unicode names still install, with joiners where the script or emoji needs them.+ for good in ["Tone…", "Kontrast ±", "می\u{200C}خواهم", "\u{1F469}\u{200D}\u{1F4BB} Code", "\u{2764}\u{FE0F} Love", "色调"] {+ assert_eq!(with_name(good).map(|m| m.name).ok().as_deref(), Some(good));+ }+ // The documented limit counts characters, not bytes.+ assert!(with_name(&"é".repeat(64)).is_ok());+ assert!(with_name(&"é".repeat(65)).is_err());+ }}diff --git a/docs/plugins.md b/docs/plugins.mdindex bc75ff1ed..4ff552fba 100644--- a/docs/plugins.md+++ b/docs/plugins.md@@ -93,7 +93,10 @@ exports. All integers are `i32` unless noted; pointers are offsets into the modu- `id`: 1–64 characters from `A-Z a-z 0-9 . _ -` (reverse-DNS recommended). Installing a modulewith the same id replaces the old one.-- `name`: the menu label (1–64 printable characters). `kind`: `"filter"` (the only kind in v1).+- `name`: the menu label (1–64 printable characters). Any script works, but control characters and+ Unicode format characters (bidi overrides and marks, zero-width spaces, soft hyphens) are rejected,+ except the zero-width joiner and non-joiner, and at least one character must be visible.+ `kind`: `"filter"` (the only kind in v1).- `params`: in dialog order. Names are `[A-Za-z0-9_]`, not starting with `_`, not `id`/`layer`.The host validates the user's values before calling: missing values take the default, numbers areclamped to `min..max` (ints rounded), a wrong type or an unknown choice is an error.