tilo-actions 0.5.0

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store, search index entries included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! The name and avatar a bot shows for one action, as a webhook does: a
//! bridge posts a Discord user's message under that user's name. The bot
//! stays the author; an appearance is presentation recorded in provenance,
//! never identity. A value that fails validation refuses the invocation; it is
//! never cleaned up, so what a reader sees is exactly what the bot sent.

use serde::{Deserialize, Serialize};
use unicode_normalization::{UnicodeNormalization, is_nfc};

/// The longest name, in characters, after trimming.
pub const MAX_NAME_CHARS: usize = 80;
/// The largest avatar image.
pub const MAX_AVATAR_BYTES: usize = 1_048_576;
const MAX_SERVICE_CHARS: usize = 32;
const MAX_ACCOUNT_CHARS: usize = 128;

/// What a bot showed for one action instead of its own name and icon.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Appearance {
    /// The shown name: trimmed, NFC-normalized, 1..=80 characters.
    pub name: String,
    /// BLAKE3 hex of an image in the workspace's content store.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub avatar: Option<String>,
    /// The outside account this stands for.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub persona: Option<Persona>,
}

/// An account on another service, such as `discord` / `80351110224678912`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Persona {
    /// The service, lowercase: `discord`.
    pub service: String,
    /// The account's stable id on that service.
    pub account: String,
    /// The workspace member (their root key, hex) the account is linked to, as
    /// the bot speaking for it keeps the link: the shown name is then that
    /// member's own, not one made to look like it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub linked: Option<String>,
}

/// Why an appearance was refused.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum AppearanceError {
    /// Nothing is left after trimming.
    #[error("the shown name is empty")]
    EmptyName,
    /// Longer than [`MAX_NAME_CHARS`].
    #[error("the shown name is longer than {MAX_NAME_CHARS} characters")]
    NameTooLong,
    /// Not in the form [`Appearance::new`] makes.
    #[error("the shown name is not trimmed and NFC-normalized")]
    NotNormalized,
    /// A character that hides or reorders text, by code point.
    #[error("the shown name contains the hidden or direction-changing character U+{0:04X}")]
    HiddenCharacter(u32),
    /// The avatar is no content hash.
    #[error("the avatar does not name an image by its content hash")]
    AvatarNotHash,
    /// Larger than [`MAX_AVATAR_BYTES`].
    #[error("the avatar is larger than 1 MiB")]
    AvatarTooLarge,
    /// Not a raster image.
    #[error("the avatar is not a PNG, JPEG, WebP or GIF image")]
    AvatarNotImage,
    /// Not in the workspace's content store.
    #[error("the avatar is not in the workspace's content store")]
    AvatarMissing,
    /// The bytes do not hash to the name.
    #[error("the avatar's bytes do not match its hash")]
    AvatarMismatch,
    #[error("the persona's service must be 1 to 32 lowercase letters, digits or '-'")]
    BadService,
    /// The account is malformed.
    #[error("the persona's account must be 1 to 128 visible characters")]
    BadAccount,
    /// The linked member is not named by a root key.
    #[error("the persona's linked member must be a root key in lowercase hex")]
    BadLinked,
}

impl Appearance {
    /// An appearance whose name is trimmed and NFC-normalized, then checked.
    pub fn new(
        name: &str,
        avatar: Option<String>,
        persona: Option<Persona>,
    ) -> Result<Self, AppearanceError> {
        let appearance = Self {
            name: name.trim().nfc().collect(),
            avatar,
            persona,
        };
        appearance.validate()?;
        Ok(appearance)
    }

    /// Check a value as received: it must already be in the form
    /// [`Self::new`] makes, so two readers never disagree on the name.
    pub fn validate(&self) -> Result<(), AppearanceError> {
        if let Some(hidden) = self.name.chars().find(|c| hidden(*c)) {
            return Err(AppearanceError::HiddenCharacter(u32::from(hidden)));
        }
        if self.name.trim() != self.name || !is_nfc(&self.name) {
            return Err(AppearanceError::NotNormalized);
        }
        match self.name.chars().count() {
            0 => return Err(AppearanceError::EmptyName),
            n if n > MAX_NAME_CHARS => return Err(AppearanceError::NameTooLong),
            _ => {}
        }
        if self
            .avatar
            .as_deref()
            .is_some_and(|hash| !tilo_content_types::valid_hash(hash))
        {
            return Err(AppearanceError::AvatarNotHash);
        }
        if let Some(persona) = &self.persona {
            let service = persona.service.chars().count();
            if !(1..=MAX_SERVICE_CHARS).contains(&service)
                || !persona
                    .service
                    .bytes()
                    .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
            {
                return Err(AppearanceError::BadService);
            }
            let account = persona.account.chars().count();
            if !(1..=MAX_ACCOUNT_CHARS).contains(&account)
                || persona
                    .account
                    .chars()
                    .any(|c| hidden(c) || c.is_whitespace())
            {
                return Err(AppearanceError::BadAccount);
            }
            if persona.linked.as_deref().is_some_and(|person| {
                person.len() != 64
                    || !person
                        .bytes()
                        .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
            }) {
                return Err(AppearanceError::BadLinked);
            }
        }
        Ok(())
    }
}

/// Characters that hide or reorder text: a name carrying one could render
/// as somebody else's.
fn hidden(c: char) -> bool {
    c.is_control()
        || matches!(
            c,
            '\u{202A}'..='\u{202E}'
                | '\u{2066}'..='\u{2069}'
                | '\u{200E}'
                | '\u{200F}'
                | '\u{061C}'
                | '\u{200B}'..='\u{200D}'
                | '\u{2060}'
                | '\u{FEFF}'
                | '\u{180E}'
        )
}

/// Check the bytes an avatar hash names: small, and a raster image. SVG is
/// refused because it is a document, not a picture.
///
/// # Errors
/// [`AppearanceError::AvatarTooLarge`] or [`AppearanceError::AvatarNotImage`].
pub fn check_avatar_bytes(bytes: &[u8]) -> Result<(), AppearanceError> {
    if bytes.len() > MAX_AVATAR_BYTES {
        return Err(AppearanceError::AvatarTooLarge);
    }
    match tilo_content_types::image_format(bytes) {
        Some(format) if matches!(format.extension, "png" | "jpg" | "webp" | "gif") => Ok(()),
        _ => Err(AppearanceError::AvatarNotImage),
    }
}

/// For an API that cannot check that its caller is a bot allowed to show
/// other names: refuse a request carrying an appearance instead of running
/// it under the caller's own name.
///
/// # Errors
/// [`crate::remote::ActionCallError::Refused`] when the request carries one.
pub fn refuse_here(
    request: &crate::remote::InvokeRequest,
) -> Result<(), crate::remote::ActionCallError> {
    match request.appearance {
        Some(_) => Err(crate::remote::ActionCallError::Refused {
            reason: "only a bot allowed to post under other names may carry an appearance".into(),
        }),
        None => Ok(()),
    }
}

/// Admit `appearance` on a request from `caller`, for the workspace-action API:
/// only a bot whose installation `allows_personas` may show another name, the
/// appearance must be valid, and its avatar must be an image `avatar` finds
/// in the workspace's content store under its own hash.
///
/// # Errors
/// [`crate::remote::ActionCallError::Refused`] for a caller that may not show
/// another name; [`crate::remote::ActionCallError::InvalidRequest`] for an
/// invalid appearance or avatar.
pub fn admit(
    caller: &crate::remote::Caller,
    appearance: &Appearance,
    allows_personas: impl FnOnce(&str) -> bool,
    avatar: impl FnOnce(&str) -> Option<Vec<u8>>,
) -> Result<Appearance, crate::remote::ActionCallError> {
    use crate::remote::ActionCallError;
    let Some(installation) = caller.acting().bot_id() else {
        return Err(ActionCallError::Refused {
            reason: "only a bot allowed to post under other names may carry an appearance".into(),
        });
    };
    if !allows_personas(installation) {
        return Err(ActionCallError::Refused {
            reason: "this bot's installation does not allow it to post under other names".into(),
        });
    }
    let invalid = |error: AppearanceError| ActionCallError::InvalidRequest {
        reason: error.to_string(),
    };
    appearance.validate().map_err(invalid)?;
    if let Some(hash) = &appearance.avatar {
        let bytes = avatar(hash).ok_or_else(|| invalid(AppearanceError::AvatarMissing))?;
        if !blake3::hash(&bytes).to_hex().eq_ignore_ascii_case(hash) {
            return Err(invalid(AppearanceError::AvatarMismatch));
        }
        check_avatar_bytes(&bytes).map_err(invalid)?;
    }
    Ok(appearance.clone())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn png() -> Vec<u8> {
        let mut bytes = b"\x89PNG\r\n\x1a\n".to_vec();
        bytes.extend_from_slice(&[0; 32]);
        bytes
    }

    tilo_test_env::scenario! {
        fn an_appearance_is_checked_before_it_is_shown;

        phase a_name_is_trimmed_and_nfc_normalized_once_and_then_must_stay_so;
        {
            // "e" + combining acute becomes the single precomposed "é".
            let made = Appearance::new("  Rene\u{301}  ", None, None).unwrap();
            assert_eq!(made.name, "Ren\u{e9}");
            let decomposed = Appearance {
                name: "Rene\u{301}".into(),
                avatar: None,
                persona: None,
            };
            assert_eq!(decomposed.validate(), Err(AppearanceError::NotNormalized));
            let padded = Appearance {
                name: " alice".into(),
                avatar: None,
                persona: None,
            };
            assert_eq!(padded.validate(), Err(AppearanceError::NotNormalized));
        }

        phase a_name_is_one_to_eighty_characters;
        {
            assert_eq!(
                Appearance::new("   ", None, None),
                Err(AppearanceError::EmptyName)
            );
            assert!(Appearance::new(&"é".repeat(80), None, None).is_ok());
            assert_eq!(
                Appearance::new(&"é".repeat(81), None, None),
                Err(AppearanceError::NameTooLong)
            );
        }

        phase hidden_and_direction_changing_characters_refuse_the_name_rather_than_being_cleaned;
        {
            for (name, hidden) in [
                ("a\u{200B}lice", 0x200B),
                ("alice\u{202E}", 0x202E),
                ("\u{2066}alice", 0x2066),
                ("ali\u{200D}ce", 0x200D),
                ("ali\u{FEFF}ce", 0xFEFF),
                ("ali\u{061C}ce", 0x061C),
                ("ali\u{180E}ce", 0x180E),
                ("ali\nce", 0x0A),
            ] {
                assert_eq!(
                    Appearance::new(name, None, None),
                    Err(AppearanceError::HiddenCharacter(hidden)),
                    "{name:?}"
                );
            }
        }

        phase an_avatar_names_a_content_hash_and_a_persona_a_service_and_account;
        {
            let hash = blake3::hash(b"x").to_hex().to_string();
            assert!(Appearance::new("alice", Some(hash), None).is_ok());
            assert_eq!(
                Appearance::new("alice", Some("../etc/passwd".into()), None),
                Err(AppearanceError::AvatarNotHash)
            );
            let discord = Persona {
                service: "discord".into(),
                account: "80351110224678912".into(),
                linked: None,
            };
            assert!(Appearance::new("alice", None, Some(discord)).is_ok());
            let shouting = Persona {
                service: "Discord".into(),
                account: "1".into(),
                linked: None,
            };
            assert_eq!(
                Appearance::new("alice", None, Some(shouting)),
                Err(AppearanceError::BadService)
            );
            let empty = Persona {
                service: "discord".into(),
                account: String::new(),
                linked: None,
            };
            assert_eq!(
                Appearance::new("alice", None, Some(empty)),
                Err(AppearanceError::BadAccount)
            );
            let linked = |person: &str| Persona {
                service: "discord".into(),
                account: "1".into(),
                linked: Some(person.into()),
            };
            assert!(Appearance::new("alice", None, Some(linked(&"ab".repeat(32)))).is_ok());
            assert_eq!(
                Appearance::new("alice", None, Some(linked("alice"))),
                Err(AppearanceError::BadLinked)
            );
        }

        phase avatar_bytes_are_a_small_raster_image;
        {
            assert_eq!(check_avatar_bytes(&png()), Ok(()));
            assert_eq!(
                check_avatar_bytes(b"<svg xmlns=\"http://www.w3.org/2000/svg\"/>"),
                Err(AppearanceError::AvatarNotImage)
            );
            assert_eq!(
                check_avatar_bytes(b"hello"),
                Err(AppearanceError::AvatarNotImage)
            );
            let mut big = png();
            big.resize(MAX_AVATAR_BYTES + 1, 0);
            assert_eq!(
                check_avatar_bytes(&big),
                Err(AppearanceError::AvatarTooLarge)
            );
        }

        phase an_appearance_round_trips_and_leaves_out_what_it_lacks;
        {
            let appearance = Appearance::new("alice", None, None).unwrap();
            let json = serde_json::to_string(&appearance).unwrap();
            assert_eq!(json, r#"{"name":"alice"}"#);
            assert_eq!(
                serde_json::from_str::<Appearance>(&json).unwrap(),
                appearance
            );
        }

        phase a_api_that_cannot_check_a_bot_refuses_an_appearance;
        {
            use crate::remote::{ActionCallError, InvokeArgument, InvokeRequest, RemoteActionTarget};
            let mut request = InvokeRequest {
                invocation: crate::InvocationId(uuid::Uuid::from_u128(1)),
                target: RemoteActionTarget {
                    provider: "p".into(),
                    address: crate::ActionAddress::new("r", "a"),
                },
                argument: InvokeArgument::None,
                context: None,
                confirmation: None,
                revision: None,
                on_behalf_of: None,
                wait_ms: None,
                appearance: None,
                signed: None,
            };
            assert_eq!(refuse_here(&request), Ok(()));
            request.appearance = Some(Appearance::new("alice", None, None).unwrap());
            assert!(matches!(
                refuse_here(&request),
                Err(ActionCallError::Refused { .. })
            ));
        }

        phase only_a_bot_whose_installation_allows_personas_is_admitted;
        {
            use crate::remote::{ActionCallError, Caller};
            let alice = Appearance::new("alice", None, None).unwrap();
            let bot = Caller::bot("7f1c", [1; 32], [2; 32]);
            assert_eq!(
                admit(
                    &bot,
                    &alice,
                    |installation| installation == "7f1c",
                    |_| None
                ),
                Ok(alice.clone())
            );
            assert!(matches!(
                admit(&bot, &alice, |_| false, |_| None),
                Err(ActionCallError::Refused { .. })
            ));
            let person = Caller::local_person("ada");
            assert!(matches!(
                admit(&person, &alice, |_| true, |_| None),
                Err(ActionCallError::Refused { .. })
            ));
            let hidden = Appearance {
                name: "a\u{200B}lice".into(),
                avatar: None,
                persona: None,
            };
            assert!(matches!(
                admit(&bot, &hidden, |_| true, |_| None),
                Err(ActionCallError::InvalidRequest { .. })
            ));
        }

        phase an_avatar_must_be_in_the_store_match_its_hash_and_be_an_image;
        {
            use crate::remote::{ActionCallError, Caller};
            let bot = Caller::bot("7f1c", [1; 32], [2; 32]);
            let image = png();
            let hash = blake3::hash(&image).to_hex().to_string();
            let shown = Appearance::new("alice", Some(hash.clone()), None).unwrap();
            let stored = |wanted: &str| (wanted == hash).then(|| image.clone());
            assert_eq!(admit(&bot, &shown, |_| true, stored), Ok(shown.clone()));
            let refused = |result: Result<Appearance, ActionCallError>, expected: AppearanceError| {
                assert_eq!(
                    result,
                    Err(ActionCallError::InvalidRequest {
                        reason: expected.to_string()
                    })
                );
            };
            refused(
                admit(&bot, &shown, |_| true, |_| None),
                AppearanceError::AvatarMissing,
            );
            refused(
                admit(&bot, &shown, |_| true, |_| Some(b"other".to_vec())),
                AppearanceError::AvatarMismatch,
            );
            let text = b"not an image".to_vec();
            let named = Appearance::new(
                "alice",
                Some(blake3::hash(&text).to_hex().to_string()),
                None,
            )
            .unwrap();
            refused(
                admit(&bot, &named, |_| true, |_| Some(text.clone())),
                AppearanceError::AvatarNotImage,
            );
        }
    }
}