zond-engine 0.14.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # Detections from somebody else, safely
//!
//! The [compute](super::compute) sandbox hands a module `Speak`, `Resolve` and
//! `Now` and nothing else, metered by byte and by connection, and a
//! [flow](super::flow) carries no code at all. A detection's power is exactly what
//! it was handed, which is a thing nmap cannot retrofit: an NSE script gets
//! unrestricted Lua with sockets, `io` and `os`, so nobody can safely run a
//! community script they have not read line by line, and in practice nobody runs
//! community scripts at all.
//!
//! Nobody benefits from a sandbox they cannot put a stranger's detection into.
//! This is the other half: a set of detections, published as one signed
//! [`Bundle`], that a caller loads by naming the key they trust.
//!
//! ## What is signed, and why it is the manifest
//!
//! One document, listing every detection in the bundle by name, tier and the
//! SHA-256 of its source. The signature covers that document; each source is then
//! held to the hash the document records.
//!
//! Signing each source separately would leave the set itself unsigned, and the
//! set is where the interesting attack is. A publisher who signs ten detections
//! individually has signed nothing about which ten, so an attacker who can serve
//! files may drop the one that would have found their foothold, or replay an
//! older, weaker version of it, and every signature still checks out. Signing the
//! manifest binds the membership, the versions and the bytes together.
//!
//! ## The load is the guarantee, not a setting
//!
//! There is no "require signatures" flag to leave unset. A source a caller wrote
//! reaches the corpus through
//! [`DetectionsBuilder::flow`](super::DetectionsBuilder::flow) and its two
//! siblings, which is their own deliberate act on bytes they hold. A bundle from
//! anybody else reaches it through
//! [`DetectionsBuilder::bundle`](super::DetectionsBuilder::bundle), which takes a
//! [`Bundle`], and the only way to obtain one is
//! [`Bundle::verified`], which takes the key. A caller who never names a key
//! never loads a stranger's detection, and no configuration can undo that.
//!
//! ## What this does not do
//!
//! Fetch anything. There is no client here, no trust store, no update schedule
//! and no revocation, and each of those is its own argument with its own
//! failure modes. What a bundle needs is bytes and a key, and where a caller got
//! them is theirs to decide, which is the same position
//! [`signature`](crate::signature) takes about keys and for the same reason.
//!
//! ```no_run
//! use std::collections::BTreeMap;
//! use zond_engine::detect::{Detections, bundle::Bundle};
//! use zond_engine::signature::Signature;
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let trusted_key: Vec<u8> = Vec::new();
//! let manifest = std::fs::read_to_string("detections.toml")?;
//! let signature = Signature::read(&mut std::io::BufReader::new(
//!     std::fs::File::open("detections.toml.sig")?,
//! ))?;
//!
//! let mut sources = BTreeMap::new();
//! sources.insert(
//!     "redis-unauth.toml".to_string(),
//!     std::fs::read_to_string("redis-unauth.toml")?,
//! );
//!
//! let bundle = Bundle::verified(&manifest, &signature, &trusted_key, sources)?;
//! let detections = Detections::builder().bundle(bundle)?.build();
//! # let _ = detections;
//! # Ok(())
//! # }
//! ```

use std::collections::BTreeMap;
use std::fmt;

use serde::Deserialize;

use crate::signature::{Domain, Signature, SignatureError};

use super::corpus::DetectionError;

/// How many detections one bundle may carry.
///
/// A bound on what a manifest can ask this process to compile, since the
/// manifest arrives before it is trusted and compiling is the expensive part. Set
/// far above any corpus anybody would hand-author and far below anything that
/// costs a machine its memory.
pub const MAX_DETECTIONS: usize = 4_096;

/// A verified set of detections, ready to add to a corpus.
///
/// The only way to build one is [`verified`](Self::verified), so holding a
/// `Bundle` is itself the evidence that a signature was checked against a key the
/// caller named. Nothing here re-checks it, and nothing needs to.
#[derive(Debug, Clone)]
pub struct Bundle {
    name: String,
    version: String,
    entries: Vec<Entry>,
}

/// One detection in a verified bundle: which tier runs it, its source, and the
/// hash that was signed.
#[derive(Debug, Clone)]
pub struct Entry {
    name: String,
    tier: Tier,
    source: String,
    sha256: String,
}

impl Entry {
    /// The name the manifest gave it, which is the key its source arrived under.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Which tier runs it.
    pub fn tier(&self) -> Tier {
        self.tier
    }

    /// Its source, as the bundle carried it.
    pub fn source(&self) -> &str {
        &self.source
    }

    /// The SHA-256 of that source, hex-encoded.
    ///
    /// The value the manifest recorded, which the source was checked against and
    /// the signature covered. It is what the corpus stamps on every finding this
    /// detection produces, so a finding names bytes somebody signed rather than
    /// bytes that happened to be on disk.
    pub fn sha256(&self) -> &str {
        &self.sha256
    }
}

/// Which tier runs a detection.
///
/// The manifest names it rather than the loader guessing from the source,
/// because guessing is a thing an attacker gets to influence: a compute module
/// that reads as a flow would be handed a flow's power and run as one, and the
/// difference between the two tiers is the whole of what the sandbox is about.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Tier {
    /// A declarative [flow](super::flow): steps and matches, carrying no code.
    Flow,
    /// A [compute module](super::compute): code, run in the capability sandbox.
    Compute,
    /// A host-level correlation.
    Host,
}

/// Why a bundle could not be verified.
///
/// Every variant is a refusal to load, never a warning. A bundle that fails any
/// of these is one nothing should compile, and a caller who wanted to load it
/// anyway has the sources and can add them one at a time through the builder,
/// which is a different and visible act.
#[non_exhaustive]
#[derive(Debug)]
pub enum BundleError {
    /// The signature did not check out against the key the caller named.
    Signature(SignatureError),
    /// The manifest did not parse.
    Manifest(String),
    /// The manifest names a detection whose source the caller did not supply.
    Missing {
        /// The name the manifest gave it.
        name: String,
    },
    /// A source does not hash to what the manifest recorded, so it is not the
    /// source that was signed.
    Altered {
        /// The name the manifest gave it.
        name: String,
    },
    /// The caller supplied a source the manifest does not name.
    ///
    /// Refused rather than ignored. An unnamed source is one the signature says
    /// nothing about, and a bundle that quietly dropped it would leave a caller
    /// believing they had loaded a file that never ran.
    Unnamed {
        /// The name it arrived under.
        name: String,
    },
    /// The manifest names the same detection twice, so which source the hash
    /// covers is not decidable.
    Duplicate {
        /// The name given twice.
        name: String,
    },
    /// The manifest names more detections than [`MAX_DETECTIONS`].
    TooMany {
        /// How many it named.
        named: usize,
    },
}

impl fmt::Display for BundleError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BundleError::Signature(error) => {
                write!(f, "the bundle's signature did not verify: {error}")
            }
            BundleError::Manifest(reason) => {
                write!(f, "the bundle manifest is malformed: {reason}")
            }
            BundleError::Missing { name } => {
                write!(
                    f,
                    "the manifest names '{name}' and no source was supplied for it"
                )
            }
            BundleError::Altered { name } => write!(
                f,
                "'{name}' does not hash to what the manifest recorded, so it is not what was signed"
            ),
            BundleError::Unnamed { name } => write!(
                f,
                "a source was supplied for '{name}', which the manifest does not name and the \
                 signature says nothing about"
            ),
            BundleError::Duplicate { name } => {
                write!(f, "the manifest names '{name}' more than once")
            }
            BundleError::TooMany { named } => write!(
                f,
                "the manifest names {named} detections, above the {MAX_DETECTIONS} a bundle may \
                 carry"
            ),
        }
    }
}

impl std::error::Error for BundleError {}

impl From<SignatureError> for BundleError {
    fn from(error: SignatureError) -> Self {
        BundleError::Signature(error)
    }
}

impl Bundle {
    /// Checks `signature` over `manifest` under `trusted_key`, then holds every
    /// source in `sources` to the hash the manifest recorded for it.
    ///
    /// `sources` is keyed by the names the manifest uses. Where they come from is
    /// the caller's business: a directory, an archive, a database row.
    ///
    /// The order is the whole of the discipline. The signature is checked before
    /// the manifest is parsed, so the parser never runs on bytes nobody vouched
    /// for; the hashes are checked before anything is compiled, so the compiler
    /// never runs on a source nobody signed. Each stage refuses rather than
    /// warning.
    ///
    /// # Errors
    ///
    /// [`BundleError::Signature`] where the signature does not verify under the
    /// named key, and one of the others where the manifest and the sources do not
    /// agree. Every one of them means nothing was loaded.
    pub fn verified(
        manifest: &str,
        signature: &Signature,
        trusted_key: &[u8],
        mut sources: BTreeMap<String, String>,
    ) -> Result<Self, BundleError> {
        // First, and on the bytes as they arrived. Parsing before checking would
        // run a parser on a stranger's input to no purpose, and every stage after
        // this one is reasoning about a document somebody vouched for.
        signature.verify(manifest.as_bytes(), trusted_key, Domain::DETECTIONS)?;

        let document: ManifestDocument =
            toml::from_str(manifest).map_err(|error| BundleError::Manifest(error.to_string()))?;

        if document.detection.len() > MAX_DETECTIONS {
            return Err(BundleError::TooMany {
                named: document.detection.len(),
            });
        }

        let mut entries = Vec::with_capacity(document.detection.len());
        let mut seen: BTreeMap<&str, ()> = BTreeMap::new();

        for named in &document.detection {
            if seen.insert(named.name.as_str(), ()).is_some() {
                return Err(BundleError::Duplicate {
                    name: named.name.clone(),
                });
            }

            // Taken rather than borrowed, so what is left in `sources` at the end
            // is exactly what the manifest did not name.
            let source = sources
                .remove(&named.name)
                .ok_or_else(|| BundleError::Missing {
                    name: named.name.clone(),
                })?;

            if !hash_matches(&source, &named.sha256) {
                return Err(BundleError::Altered {
                    name: named.name.clone(),
                });
            }

            entries.push(Entry {
                name: named.name.clone(),
                tier: named.tier,
                source,
                sha256: named.sha256.clone(),
            });
        }

        if let Some((name, _)) = sources.into_iter().next() {
            return Err(BundleError::Unnamed { name });
        }

        Ok(Self {
            name: document.bundle.name,
            version: document.bundle.version,
            entries,
        })
    }

    /// The name the bundle gives itself.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The version it gives itself.
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Every detection it carries, in the order the manifest listed them.
    pub fn entries(&self) -> &[Entry] {
        &self.entries
    }

    /// The documents a set of loose detection files should be published as.
    ///
    /// A bundle carries detections, not the files an author kept them in: a
    /// module whose code sits in a sibling `.rhai` is one detection, and the
    /// signature has to cover the code as part of the document that runs it. This
    /// resolves each such reference, so what comes back is a set of
    /// self-contained documents keyed by name, ready for
    /// [`manifest`](Self::manifest) and to be written out as the bundle.
    ///
    /// Write these documents rather than the originals. A recipient hashes the
    /// files they were given, before anything is parsed, so the bundle directory
    /// has to hold what was signed.
    ///
    /// The tier comes from each document, which is safe here and nowhere else:
    /// this reads a publisher's own files on their own machine, where
    /// [`verified`](Self::verified) reads a stranger's and takes every tier from
    /// the manifest instead.
    ///
    /// # Errors
    ///
    /// [`DetectionError::InSource`] naming the document that would not resolve,
    /// or [`DetectionError::UnusedBody`] for a body no document references.
    pub fn publishable(
        sources: &BTreeMap<String, String>,
    ) -> Result<BTreeMap<String, (Tier, String)>, DetectionError> {
        let prepared = super::source::prepare(sources).map_err(DetectionError::from)?;
        Ok(prepared
            .into_iter()
            .map(|detection| (detection.name, (detection.tier, detection.document)))
            .collect())
    }

    /// The manifest document for a set of sources, for a publisher building a
    /// bundle.
    ///
    /// Hashes each source and writes the document that
    /// [`verified`](Self::verified) will check them against. What comes back is
    /// signed with [`Signing`](crate::signature::Signing) under
    /// [`Domain::DETECTIONS`], and the signature published beside it.
    ///
    /// Here rather than left to a publisher's own script because the two ends
    /// have to agree exactly about which bytes are hashed and how the document is
    /// shaped, and a crate that shipped only the checking half would be asking
    /// every publisher to reimplement the writing half from prose.
    #[must_use]
    pub fn manifest(
        name: &str,
        version: &str,
        sources: &BTreeMap<String, (Tier, String)>,
    ) -> String {
        let mut document = format!(
            "# A zond detection bundle. The signature beside this file covers these\n\
             # bytes; each source is held to the hash recorded here.\n\
             [bundle]\n\
             name = \"{name}\"\n\
             version = \"{version}\"\n"
        );

        // `BTreeMap` iterates by name, so one set of sources produces one
        // document whatever order a publisher assembled them in.
        for (source_name, (tier, source)) in sources {
            document.push_str(&format!(
                "\n[[detection]]\n\
                 name = \"{source_name}\"\n\
                 tier = \"{}\"\n\
                 sha256 = \"{}\"\n",
                tier.name(),
                sha256_hex(source),
            ));
        }

        document
    }
}

impl Tier {
    /// The name the manifest spells this tier with.
    pub const fn name(self) -> &'static str {
        match self {
            Tier::Flow => "flow",
            Tier::Compute => "compute",
            Tier::Host => "host",
        }
    }
}

/// The manifest as TOML holds it.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ManifestDocument {
    bundle: BundleHeader,
    /// Absent for a bundle carrying nothing, which is a bundle rather than an
    /// error: a publisher who withdrew every detection has said so, and refusing
    /// it would leave them no way to say it.
    #[serde(default)]
    detection: Vec<NamedDetection>,
}

/// `[bundle]`: what the set calls itself.
///
/// Neither field decides anything. They are what a report and an operator use to
/// say which bundle a finding came from, and they are inside the signature so a
/// publisher's name cannot be put on somebody else's detections.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct BundleHeader {
    /// The publisher's name for the set.
    name: String,
    /// The publisher's version for it.
    version: String,
}

/// One `[[detection]]`: which source, run by which tier, and the bytes it must
/// be.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct NamedDetection {
    /// The name the source is supplied under.
    name: String,
    /// Which tier runs it, decided here rather than read off the source.
    tier: Tier,
    /// The SHA-256 the source is held to, lowercase hex.
    sha256: String,
}

/// Whether `source` hashes to `expected`, which is lowercase hex.
///
/// Compared as bytes rather than as text, so a manifest recording an
/// upper-case digest or one with stray whitespace is a mismatch rather than a
/// near-match nobody notices. A publisher writes what this crate wrote.
fn hash_matches(source: &str, expected: &str) -> bool {
    sha256_hex(source) == expected
}

/// The SHA-256 of a detection source, lowercase hex, as a manifest records it.
///
/// The provenance a finding carries: the corpus stamps this on every finding a
/// detection produces, so a reader who doubts one can hash the source themselves
/// and see whether it is the source that ran. A publisher gets it through
/// [`Bundle::manifest`], which hashes a whole set at once; this is the same value
/// for one source, for a caller adding a detection through
/// [`flow`](super::corpus::DetectionsBuilder::flow) and its siblings, which take
/// the hash rather than computing one.
#[must_use]
pub fn content_hash(source: &str) -> String {
    sha256_hex(source)
}

/// The SHA-256 of `source`, lowercase hex.
fn sha256_hex(source: &str) -> String {
    let digest = ring::digest::digest(&ring::digest::SHA256, source.as_bytes());
    digest.as_ref().iter().fold(String::new(), |mut hex, byte| {
        use std::fmt::Write;
        let _ = write!(hex, "{byte:02x}");
        hex
    })
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signature::{Signing, SigningKey};
    use std::io::Write;

    /// A minimal, sound flow, so a bundle built from it is one the corpus would
    /// actually compile.
    const FLOW: &str = r#"
        [detection]
        id      = "bundle-test"
        version = "1.0.0"
        title   = "Bundle test"
        [detection.when]
        service = "http"
        [detection.capabilities]
        class = "active-benign"
        speak = "target"
        [[step]]
        send        = "PING\r\n"
        expect      = "PONG"
        on_no_match = "continue"
        [[step.finding]]
        when     = "matched"
        severity = "low"
        summary  = "the bundle test fired"
    "#;

    fn sources() -> BTreeMap<String, (Tier, String)> {
        let mut sources = BTreeMap::new();
        sources.insert("flow.toml".to_string(), (Tier::Flow, FLOW.to_string()));
        sources
    }

    fn plain(sources: &BTreeMap<String, (Tier, String)>) -> BTreeMap<String, String> {
        sources
            .iter()
            .map(|(name, (_, source))| (name.clone(), source.clone()))
            .collect()
    }

    /// Signs `manifest` and returns the key that did, with the signature.
    fn signed(manifest: &str) -> (SigningKey, Signature) {
        let (_, key) = SigningKey::generate().expect("a key");
        let mut sink = Vec::new();
        let mut writer = Signing::new(&mut sink);
        writer
            .write_all(manifest.as_bytes())
            .expect("the manifest is written");
        let signature = writer.finish(&key, Domain::DETECTIONS);
        (key, signature)
    }

    /// The publisher's half and the loader's half agree, which is the whole
    /// contract: a manifest this crate wrote is one this crate accepts.
    #[test]
    fn a_bundle_this_crate_wrote_is_one_it_verifies() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "2026.09.1", &sources);
        let (key, signature) = signed(&manifest);

        let bundle = Bundle::verified(&manifest, &signature, &key.public_key(), plain(&sources))
            .expect("the bundle verifies");

        assert_eq!(bundle.name(), "acme");
        assert_eq!(bundle.version(), "2026.09.1");
        assert_eq!(bundle.entries().len(), 1);
        assert_eq!(bundle.entries()[0].name(), "flow.toml");
        assert_eq!(bundle.entries()[0].tier(), Tier::Flow);
        assert_eq!(bundle.entries()[0].source(), FLOW);
        assert_eq!(bundle.entries()[0].sha256().len(), 64, "a hex sha256");
    }

    /// The bypass this whole module is shaped to prevent, and it is the same one
    /// [`signature`](crate::signature) is shaped to prevent one layer down: an
    /// attacker who serves a bundle can sign it perfectly well with a key of
    /// their own.
    #[test]
    fn a_bundle_signed_by_another_key_is_refused() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "2026.09.1", &sources);
        let (_, signature) = signed(&manifest);

        let (_, recipient) = SigningKey::generate().expect("a key");

        assert!(matches!(
            Bundle::verified(
                &manifest,
                &signature,
                &recipient.public_key(),
                plain(&sources)
            ),
            Err(BundleError::Signature(_))
        ));
    }

    /// A source swapped for another after the manifest was signed fails its
    /// hash, which is what makes the signature cover the detections rather than
    /// only their names.
    #[test]
    fn a_source_that_is_not_what_was_signed_is_refused() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "2026.09.1", &sources);
        let (key, signature) = signed(&manifest);

        let mut swapped = plain(&sources);
        swapped.insert(
            "flow.toml".to_string(),
            FLOW.replace("active-benign", "exploit"),
        );

        assert!(matches!(
            Bundle::verified(&manifest, &signature, &key.public_key(), swapped),
            Err(BundleError::Altered { name }) if name == "flow.toml"
        ));
    }

    /// The attack signing each source separately would leave open: an attacker
    /// who can serve files drops the detection that would have found them, and
    /// every remaining signature still checks out.
    ///
    /// Signing the manifest binds the membership, so a missing source is a
    /// refusal rather than a quietly smaller corpus.
    #[test]
    fn a_detection_withheld_from_a_signed_set_is_refused() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "2026.09.1", &sources);
        let (key, signature) = signed(&manifest);

        assert!(matches!(
            Bundle::verified(&manifest, &signature, &key.public_key(), BTreeMap::new()),
            Err(BundleError::Missing { name }) if name == "flow.toml"
        ));
    }

    /// And its mirror: a source nobody signed does not ride along beside ones
    /// somebody did.
    #[test]
    fn a_source_the_manifest_does_not_name_is_refused() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "2026.09.1", &sources);
        let (key, signature) = signed(&manifest);

        let mut extra = plain(&sources);
        extra.insert("smuggled.toml".to_string(), FLOW.to_string());

        assert!(matches!(
            Bundle::verified(&manifest, &signature, &key.public_key(), extra),
            Err(BundleError::Unnamed { name }) if name == "smuggled.toml"
        ));
    }

    /// A manifest edited after signing does not verify, whatever it now says.
    #[test]
    fn a_manifest_altered_after_signing_is_refused() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "2026.09.1", &sources);
        let (key, signature) = signed(&manifest);

        let edited = manifest.replace("2026.09.1", "2026.09.2");

        assert!(matches!(
            Bundle::verified(&edited, &signature, &key.public_key(), plain(&sources)),
            Err(BundleError::Signature(_))
        ));
    }

    /// The tier comes from the signed manifest, never from the source, so an
    /// attacker cannot have a compute module run as a flow or the other way
    /// about. This is the manifest's answer surviving to the entry.
    #[test]
    fn the_tier_is_the_manifests_and_not_the_sources() {
        let mut sources = BTreeMap::new();
        // A flow's source, declared as a compute module. Nothing here compiles
        // it; what matters is which tier the loader will be told to use.
        sources.insert("thing.toml".to_string(), (Tier::Compute, FLOW.to_string()));

        let manifest = Bundle::manifest("acme", "1", &sources);
        let (key, signature) = signed(&manifest);

        let bundle = Bundle::verified(&manifest, &signature, &key.public_key(), plain(&sources))
            .expect("the bundle verifies");

        assert_eq!(bundle.entries()[0].tier(), Tier::Compute);
    }

    /// A manifest naming one detection twice makes the hash ambiguous, so it is
    /// refused rather than resolved by a rule nobody would remember.
    #[test]
    fn a_manifest_naming_one_detection_twice_is_refused() {
        let sources = sources();
        let manifest = Bundle::manifest("acme", "1", &sources);
        let doubled = format!(
            "{manifest}\n[[detection]]\nname = \"flow.toml\"\ntier = \"flow\"\nsha256 = \"{}\"\n",
            sha256_hex(FLOW)
        );
        let (key, signature) = signed(&doubled);

        assert!(matches!(
            Bundle::verified(&doubled, &signature, &key.public_key(), plain(&sources)),
            Err(BundleError::Duplicate { name }) if name == "flow.toml"
        ));
    }

    /// A publisher who withdrew everything has said so, and a bundle carrying
    /// nothing is how they say it.
    #[test]
    fn a_bundle_carrying_nothing_verifies_and_carries_nothing() {
        let manifest = Bundle::manifest("acme", "2026.09.2", &BTreeMap::new());
        let (key, signature) = signed(&manifest);

        let bundle = Bundle::verified(&manifest, &signature, &key.public_key(), BTreeMap::new())
            .expect("an empty bundle is a bundle");

        assert!(bundle.entries().is_empty());
    }
}