ma-core 0.10.28

DIDComm service library: inboxes, outboxes, DID document publishing, and transport abstraction
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Capability-based access control for ma identities.
//!
//! An [`AclMap`] maps principal strings to [`CapabilityEntry`] values.
//! Deny always wins over allow; a wildcard deny closes access to everyone.
//!
//! # Capabilities
//!
//! Capabilities are plain strings. Built-in system capabilities:
//!
//! | Capability | Meaning |
//! |------------|---------|
//! | `"rpc"`    | Send RPC messages via `/ma/rpc/0.0.1` |
//! | `"ipfs"`   | Publish DID documents via `/ma/ipfs/0.0.1` |
//! | `"read"`   | Read entities, config, and namespace contents |
//! | `"create"` | Create new namespaces or entities |
//! | `"update"` | Update existing namespaces or entities |
//! | `"delete"` | Delete namespaces or entities |
//! | `"*"`      | Wildcard — grants **all** capabilities at this level |
//!
//! Entity and namespace ACLs may also use arbitrary capability strings that
//! correspond to verb names or sub-namespace names.
//!
//! # Key forms in an [`AclMap`]
//!
//! Keys are **principal strings** — exactly one of:
//!
//! | Form | Meaning |
//! |------|---------|
//! | `"*"` | Wildcard — matches any caller |
//! | `"did:ma:<identity>"` | Bare DID (no fragment) |
//! | `"#<local>"` | Local entity identifier |
//! | `"+<handle>.<path>"` | Named group of principals (unlimited depth) |
//!
//! # YAML format
//!
//! ```yaml
//! acl:
//!   "*": [rpc, create]          # everyone: RPC + create
//!   "did:ma:alice": ["*"]        # alice: all capabilities
//!   "did:ma:bob": [rpc, read]   # bob: restricted
//!   "did:ma:eve":               # null / absent → explicit deny
//!   "+carlotta.friends": [rpc]         # group: all members get rpc
//!   "+alice.project4.admins": ["*"]  # deep path: project4 admins get all caps
//!   "+alice.enemies":                # group: all members denied
//! ```
//!
//! # Example
//!
//! ```rust
//! # use ma_core::{AclMap, CapabilityEntry, check_cap, CAP_RPC};
//! let mut acl = AclMap::new();
//! acl.insert("*".to_string(), CapabilityEntry::from_caps(["rpc"]));
//! acl.insert("did:ma:Qmevil".to_string(), CapabilityEntry::Deny);
//! assert!(check_cap(&acl, "did:ma:Qmgood", CAP_RPC).is_ok());
//! assert!(check_cap(&acl, "did:ma:Qmevil", CAP_RPC).is_err());
//! ```

use std::collections::{BTreeSet, HashMap};

use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "acl")]
use crate::{Error, Result};

// ── Capability constants ───────────────────────────────────────────────────────

/// Deliver messages to an endpoint's inbox (`/ma/inbox/0.0.1`).
pub const CAP_INBOX: &str = "inbox";
/// Send RPC messages via `/ma/rpc/0.0.1`.
pub const CAP_RPC: &str = "rpc";
/// Publish DID documents via `/ma/ipfs/0.0.1`.
pub const CAP_IPFS: &str = "ipfs";
/// Access the structured CRUD service via `/ma/crud/0.0.1`.
pub const CAP_CRUD: &str = "crud";
/// Read entities, config, and namespace contents.
pub const CAP_READ: &str = "read";
/// Create new namespaces or entities.
pub const CAP_CREATE: &str = "create";
/// Update existing namespaces or entities.
pub const CAP_UPDATE: &str = "update";
/// Delete namespaces or entities.
pub const CAP_DELETE: &str = "delete";
/// Read or modify ACL documents.
///
/// Separates ACL-administration rights from general CRUD access.
/// A principal with `acl` capability can read and update ACL documents
/// without necessarily having access to the resources those ACLs protect.
pub const CAP_ACL: &str = "acl";

// ── Group-principal prefix ─────────────────────────────────────────────────────

/// Sigil that marks a group principal in an [`AclMap`] key.
///
/// A group key has the form `+<handle>.<path>` where `<handle>` is the owning
/// identity's handle and `<path>` is a dot-separated path of arbitrary depth
/// into that handle's group tree (e.g. `+alice.project4.admins`).
pub const GROUP_PREFIX: &str = "+";

/// Local-entity wildcard principal.
///
/// The bare `"#"` key in an [`AclMap`] matches **any** caller whose principal
/// starts with `"#"` — i.e. any entity running on the same runtime instance.
///
/// It sits between the specific `"#<name>"` form and the global `"*"` wildcard
/// in lookup priority:
///
/// ```text
/// // specific entity  > local wildcard  > global wildcard
/// //   "#fortune"         "#"                 "*"
/// ```
///
/// The runtime pre-normalises intra-runtime callers from their full DID-URL form
/// (`did:ma:<our_did>#fortune`) to the bare fragment form (`#fortune`) before
/// the ACL lookup, so that these keys resolve correctly.
///
/// ## YAML example
///
/// ```yaml
/// acl:
///   "#": [handle_cast]        # any local entity on this runtime may call
///   "did:ma:alice": ["*"]     # remote alice: all caps
///   "*":                      # everyone else: deny
/// ```
pub const LOCAL_ENTITY_WILDCARD: &str = "#";

// ── CapabilityEntry ────────────────────────────────────────────────────────────

/// Capability set for a principal in an [`AclMap`].
///
/// Serialises as:
/// - `null` → [`Deny`](CapabilityEntry::Deny)
/// - YAML sequence → [`Allow`](CapabilityEntry::Allow)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CapabilityEntry {
    /// Explicit deny. Wins over any wildcard allow for the same principal.
    Deny,
    /// Allow the listed capabilities. `["*"]` grants all capabilities.
    Allow(BTreeSet<String>),
}

impl CapabilityEntry {
    /// Construct an `Allow` entry from an iterator of capability name strings.
    pub fn from_caps<I, S>(caps: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self::Allow(caps.into_iter().map(Into::into).collect())
    }

    /// Return `true` if this entry grants `cap`.
    /// `"*"` in the capability set grants any capability.
    pub fn has(&self, cap: &str) -> bool {
        match self {
            Self::Deny => false,
            Self::Allow(caps) => caps.contains(cap) || caps.contains("*"),
        }
    }

    /// Return `true` if this is an explicit deny.
    pub fn is_deny(&self) -> bool {
        matches!(self, Self::Deny)
    }
}

impl Serialize for CapabilityEntry {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        match self {
            Self::Deny => serializer.serialize_none(),
            Self::Allow(caps) => {
                use serde::ser::SerializeSeq;
                let mut seq = serializer.serialize_seq(Some(caps.len()))?;
                for cap in caps {
                    seq.serialize_element(cap)?;
                }
                seq.end()
            }
        }
    }
}

impl<'de> Deserialize<'de> for CapabilityEntry {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum Raw {
            #[allow(dead_code)]
            Str(String),
            Seq(Vec<String>),
        }
        let opt: Option<Raw> = Option::deserialize(deserializer)?;
        match opt {
            None => Ok(Self::Deny),
            Some(Raw::Seq(v)) if v.is_empty() => Ok(Self::Deny),
            Some(Raw::Seq(v)) => Ok(Self::Allow(v.into_iter().collect())),
            Some(Raw::Str(_)) => Err(serde::de::Error::custom(
                "invalid ACL entry: use a YAML sequence for Allow or null for Deny",
            )),
        }
    }
}

// ── AclMap ─────────────────────────────────────────────────────────────────────

/// Capability-based access control map.
///
/// Keys are principal strings — exactly one of:
/// - `"*"` — wildcard
/// - `"did:ma:<identity>"` — bare DID, no fragment
/// - `"#<local>"` — local entity identifier
/// - `"+<handle>.<path>"` — named group of principals (unlimited depth)
///
/// DID-URLs with fragments (`did:ma:foo#bar`) are **not** valid keys;
/// use [`is_valid_acl_key`] to validate before inserting.
pub type AclMap = HashMap<String, CapabilityEntry>;

// ── check_cap ──────────────────────────────────────────────────────────────────

/// Check whether `caller` has capability `cap` in `acl`.
///
/// 1. Normalise `caller` (strip fragment from DID-URLs).
/// 2. Look up the normalised caller directly — if a principal entry, apply and stop.
/// 3. Fall back to the `"*"` wildcard principal entry.
/// 4. Explicit deny → `Err`; capability absent → `Err`; no entry → `Err`.
///
/// Group principals (`+<handle>.<path>`) are **not** resolved here;
/// they are expanded by the runtime's async `check_full`.
///
/// A `"*"` item inside an `Allow` set grants **all** capabilities.
#[cfg(feature = "acl")]
pub fn check_cap(acl: &AclMap, caller: &str, cap: &str) -> Result<()> {
    let normalized = normalize_principal(caller);

    // 1. Direct principal match (highest priority).
    if let Some(direct) = acl.get(normalized) {
        return match direct {
            CapabilityEntry::Deny => Err(Error::Acl(format!("operation denied for {caller}"))),
            CapabilityEntry::Allow(caps) if caps.contains(cap) || caps.contains("*") => Ok(()),
            CapabilityEntry::Allow(_) => Err(Error::Acl(format!(
                "capability '{cap}' denied for {caller}"
            ))),
        };
    }

    // 2. Local-entity wildcard "#" — matches any `#`-prefixed caller.
    if normalized.starts_with('#') {
        if let Some(local_wild) = acl.get(LOCAL_ENTITY_WILDCARD) {
            return match local_wild {
                CapabilityEntry::Deny => Err(Error::Acl(format!("operation denied for {caller}"))),
                CapabilityEntry::Allow(caps) if caps.contains(cap) || caps.contains("*") => Ok(()),
                CapabilityEntry::Allow(_) => Err(Error::Acl(format!(
                    "capability '{cap}' denied for {caller}"
                ))),
            };
        }
    }

    // 3. Global wildcard "*" (lowest priority).
    match acl.get("*") {
        None => Err(Error::Acl(format!("no ACL entry for {caller}"))),
        Some(CapabilityEntry::Deny) => Err(Error::Acl(format!("operation denied for {caller}"))),
        Some(CapabilityEntry::Allow(caps)) if caps.contains(cap) || caps.contains("*") => Ok(()),
        Some(CapabilityEntry::Allow(_)) => Err(Error::Acl(format!(
            "capability '{cap}' denied for {caller}"
        ))),
    }
}

/// Return `true` if `key` is a valid [`AclMap`] key.
///
/// Valid keys: `"*"`, `"did:ma:<id>"`, `"#<local>"`, `"+<handle>.<path>"`
/// (where `<path>` is dot-separated with unlimited depth),
/// and arbitrary capability/verb name strings (non-empty).
pub fn is_valid_acl_key(key: &str) -> bool {
    !key.is_empty()
}

/// Return `true` if `key` is a principal key (identifies *who*).
///
/// Valid principal key forms:
/// - `"*"` — global wildcard
/// - `"did:ma:<id>"` — bare DID (no fragment)
/// - `"#"` — local-entity wildcard (any entity on this runtime)
/// - `"#<name>"` — specific local entity by fragment name
/// - `"+<handle>.<path>"` — group principal
pub fn is_principal_key(key: &str) -> bool {
    key == "*"
        || (key.starts_with("did:") && !key.contains('#'))
        || key.starts_with('#')   // covers both "#" (local wildcard) and "#name" (specific)
        || is_valid_group_key(key)
}

/// Return `true` if `key` is a valid group principal.
///
/// Form: `+<handle>.<path>` where `<handle>` is non-empty and `<path>` is a
/// non-empty dot-separated string of arbitrary depth
/// (e.g. `+alice.admins`, `+alice.project4.admins`).
fn is_valid_group_key(key: &str) -> bool {
    if let Some(rest) = key.strip_prefix(GROUP_PREFIX) {
        if let Some(dot) = rest.find('.') {
            let handle = &rest[..dot];
            let path = &rest[dot + 1..];
            return !handle.is_empty() && !path.is_empty();
        }
    }
    false
}

/// Validate all keys in an [`AclMap`], returning a descriptive error for the
/// first invalid key found.
///
/// Call this immediately after loading an ACL from YAML or any external source.
#[cfg(feature = "acl")]
pub fn validate_acl_map(acl: &AclMap) -> Result<()> {
    for key in acl.keys() {
        if !is_valid_acl_key(key) {
            return Err(Error::Acl(format!(
                "invalid ACL key {key:?}: key must be non-empty"
            )));
        }
    }
    Ok(())
}

/// Normalise a caller identity for [`AclMap`] lookup.
///
/// - `did:ma:foo#bar` → `did:ma:foo` (strips fragment from **remote** DID-URLs)
/// - `#local` → `#local` (already-normalised local entity, passed through)
/// - `#` → `#` (local-entity wildcard, passed through)
/// - `*` → `*` (global wildcard, passed through)
///
/// **Note:** Callers from the same runtime arrive as `did:ma:<our_did>#fragment`.
/// The runtime is responsible for converting those to `#fragment` *before*
/// calling this function (or [`check_cap`]) so that `"#<name>"` and `"#"`
/// ACL keys resolve correctly.  This function only strips fragments from
/// foreign DID-URLs; it does not know which DID belongs to the local runtime.
pub fn normalize_principal(did: &str) -> &str {
    if did.starts_with("did:") {
        if let Some(pos) = did.find('#') {
            return &did[..pos];
        }
    }
    did
}

// ── Tests ──────────────────────────────────────────────────────────────────────

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

    fn allow(caps: &[&str]) -> CapabilityEntry {
        CapabilityEntry::from_caps(caps.iter().copied())
    }

    fn m(entries: &[(&str, CapabilityEntry)]) -> AclMap {
        entries
            .iter()
            .map(|(k, v)| (k.to_string(), v.clone()))
            .collect()
    }

    #[test]
    fn wildcard_rpc_allows_rpc() {
        let acl = m(&[("*", allow(&[CAP_RPC]))]);
        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
    }

    #[test]
    fn wildcard_rpc_denies_ipfs() {
        let acl = m(&[("*", allow(&[CAP_RPC]))]);
        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_err());
    }

    #[test]
    fn explicit_deny_wins_over_wildcard_allow() {
        let acl = m(&[
            ("*", allow(&[CAP_RPC, CAP_IPFS])),
            ("did:ma:bandit", CapabilityEntry::Deny),
        ]);
        assert!(check_cap(&acl, "did:ma:bandit", CAP_RPC).is_err());
    }

    #[test]
    fn exact_match_restricts_below_wildcard() {
        let acl = m(&[
            ("*", allow(&[CAP_RPC, CAP_IPFS])),
            ("did:ma:bob", allow(&[CAP_RPC])),
        ]);
        assert!(check_cap(&acl, "did:ma:bob", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "did:ma:bob", CAP_IPFS).is_err());
    }

    #[test]
    fn did_url_caller_is_normalized() {
        let acl = m(&[("did:ma:alice", allow(&[CAP_RPC, CAP_IPFS]))]);
        assert!(check_cap(&acl, "did:ma:alice#sign", CAP_RPC).is_ok());
    }

    #[test]
    fn no_entry_default_deny() {
        assert!(check_cap(&AclMap::new(), "did:ma:anyone", CAP_RPC).is_err());
    }

    #[test]
    fn wildcard_deny_blocks_all() {
        let acl = m(&[("*", CapabilityEntry::Deny)]);
        assert!(check_cap(&acl, "did:ma:anyone", CAP_RPC).is_err());
    }

    #[test]
    fn local_entity_key_allowed() {
        let acl = m(&[("#agent", allow(&[CAP_RPC]))]);
        assert!(check_cap(&acl, "#agent", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "#other", CAP_RPC).is_err());
    }

    #[test]
    fn arbitrary_capability_works() {
        let acl = m(&[("did:ma:alice", allow(&["emote", "reply"]))]);
        assert!(check_cap(&acl, "did:ma:alice", "emote").is_ok());
        assert!(check_cap(&acl, "did:ma:alice", "reply").is_ok());
        assert!(check_cap(&acl, "did:ma:alice", "admin").is_err());
    }

    #[test]
    fn wildcard_cap_grants_all_capabilities() {
        let acl = m(&[("did:ma:alice", allow(&["*"]))]);
        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
        assert!(check_cap(&acl, "did:ma:alice", "emote").is_ok());
        assert!(check_cap(&acl, "did:ma:alice", "admin").is_ok());
    }

    #[test]
    fn owner_capability_is_just_a_string() {
        let acl = m(&[("did:ma:alice", allow(&["owner"]))]);
        assert!(check_cap(&acl, "did:ma:alice", "owner").is_ok());
        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_err());
    }

    #[test]
    fn normalize_strips_fragment() {
        assert_eq!(normalize_principal("did:ma:foo#bar"), "did:ma:foo");
        assert_eq!(normalize_principal("did:ma:foo"), "did:ma:foo");
        assert_eq!(normalize_principal("#local"), "#local");
        assert_eq!(normalize_principal("#"), "#");
        assert_eq!(normalize_principal("*"), "*");
    }

    // ── Local-entity wildcard ("#") tests ─────────────────────────────────────

    #[test]
    fn local_wildcard_allows_any_hash_prefixed_caller() {
        // "#" matches any #-prefixed caller when there is no specific entry.
        let acl = m(&[("#", allow(&[CAP_RPC]))]);
        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "#scheduler", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "#any_entity", CAP_RPC).is_ok());
    }

    #[test]
    fn local_wildcard_does_not_match_remote_callers() {
        // Remote DIDs do not start with '#', so "#" should not grant them.
        let acl = m(&[("#", allow(&[CAP_RPC]))]);
        assert!(check_cap(&acl, "did:ma:remote", CAP_RPC).is_err());
    }

    #[test]
    fn specific_local_entity_wins_over_local_wildcard() {
        // "#fortune" is more specific: it restricts below the "#" wildcard.
        let acl = m(&[
            ("#", allow(&[CAP_RPC, CAP_IPFS])),
            ("#fortune", allow(&[CAP_RPC])), // only rpc, not ipfs
        ]);
        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "#fortune", CAP_IPFS).is_err());
        // Other local entities still get rpc+ipfs from the wildcard.
        assert!(check_cap(&acl, "#other", CAP_IPFS).is_ok());
    }

    #[test]
    fn local_wildcard_deny_blocks_all_local_entities() {
        let acl = m(&[
            ("#", CapabilityEntry::Deny),
            ("*", allow(&[CAP_RPC])), // global wildcard would allow, but # deny wins
        ]);
        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_err());
        assert!(check_cap(&acl, "#any", CAP_RPC).is_err());
        // Remote callers are unaffected by the "#" deny.
        assert!(check_cap(&acl, "did:ma:remote", CAP_RPC).is_ok());
    }

    #[test]
    fn specific_local_entity_allow_overrides_local_wildcard_deny() {
        // "#fortune" explicit allow wins over "#" deny for that entity.
        let acl = m(&[
            ("#", CapabilityEntry::Deny),
            ("#fortune", allow(&[CAP_RPC])),
        ]);
        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "#other", CAP_RPC).is_err());
    }

    #[test]
    fn global_wildcard_not_triggered_for_hash_caller_when_local_wildcard_present() {
        // When "#" deny is set, "*" allow must NOT override it for local callers.
        let acl = m(&[("#", CapabilityEntry::Deny), ("*", allow(&[CAP_RPC]))]);
        // Local entity is denied by "#" — must not fall through to "*".
        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_err());
    }

    #[test]
    fn local_wildcard_is_key_form_valid() {
        assert!(is_principal_key("#"));
        assert!(is_principal_key("#fortune"));
        assert!(is_principal_key("*"));
        assert!(is_principal_key("did:ma:alice"));
    }

    #[test]
    fn explicit_deny_without_wildcard() {
        // A bare Deny entry with no wildcard still denies.
        let acl = m(&[("did:ma:bandit", CapabilityEntry::Deny)]);
        assert!(check_cap(&acl, "did:ma:bandit", CAP_RPC).is_err());
        // Others get default deny too (no wildcard).
        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_err());
    }

    #[test]
    fn multiple_caps_in_single_entry() {
        let acl = m(&[("did:ma:alice", allow(&[CAP_RPC, CAP_IPFS, CAP_READ]))]);
        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
        assert!(check_cap(&acl, "did:ma:alice", CAP_READ).is_ok());
        assert!(check_cap(&acl, "did:ma:alice", CAP_CREATE).is_err());
        assert!(check_cap(&acl, "did:ma:alice", CAP_DELETE).is_err());
    }

    #[test]
    fn direct_entry_restricts_even_when_wildcard_is_broader() {
        // Wildcard gives everyone rpc+ipfs, but bob only gets rpc.
        // Direct entry wins and caps don't accumulate from wildcard.
        let acl = m(&[
            ("*", allow(&[CAP_RPC, CAP_IPFS])),
            ("did:ma:bob", allow(&[CAP_RPC])),
        ]);
        assert!(check_cap(&acl, "did:ma:bob", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "did:ma:bob", CAP_IPFS).is_err());
        // Alice (no direct entry) still gets both from wildcard.
        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
    }

    #[test]
    fn group_principal_allowed() {
        // "+group" keys are not resolved by check_cap; they pass through.
        // Resolution happens in the runtime's async check_full.
        let acl = m(&[("*", allow(&[CAP_RPC]))]);
        assert!(check_cap(&acl, "did:ma:anyone", CAP_RPC).is_ok());
    }

    #[test]
    fn valid_acl_keys() {
        assert!(is_valid_acl_key("*"));
        assert!(is_valid_acl_key("did:ma:Qmfoo"));
        assert!(is_valid_acl_key("#agent"));
        assert!(is_valid_acl_key("+alice.venner"));
        assert!(is_valid_acl_key("+runtime.admins"));
        assert!(is_valid_acl_key("fortune"));
        assert!(is_valid_acl_key("admin"));
        assert!(is_valid_acl_key("emote"));
        assert!(!is_valid_acl_key(""));
    }

    #[cfg(feature = "acl")]
    #[test]
    fn capability_serde_roundtrip() {
        let acl: AclMap = [
            (
                "*".to_string(),
                CapabilityEntry::from_caps(["rpc", "create"]),
            ),
            ("did:ma:bandit".to_string(), CapabilityEntry::Deny),
        ]
        .into_iter()
        .collect();
        let yaml = serde_yaml::to_string(&acl).unwrap();
        let roundtrip: AclMap = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(acl, roundtrip);
    }

    #[cfg(feature = "acl")]
    #[test]
    fn yaml_null_deserializes_to_deny() {
        let yaml = "'did:ma:x': ~\n'*':\n- rpc\n- create\n";
        let acl: AclMap = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(acl.get("did:ma:x"), Some(&CapabilityEntry::Deny));
        assert_eq!(
            acl.get("*"),
            Some(&CapabilityEntry::from_caps(["rpc", "create"]))
        );
    }
}