exarch-core 0.9.0

Memory-safe archive extraction library with security validation
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
//! Parent-directory creation that never walks through a link it did not vet.
//!
//! The walk starts at the destination's pinned directory handle and descends
//! one component at a time with a handle-relative, no-follow open (`openat`
//! with `O_NOFOLLOW` on Unix, `NtCreateFile` on Windows), creating missing
//! components with `mkdirat` or its Windows equivalent. Every step operates on
//! the handle the previous step returned, so a link swapped in for an already
//! vetted component cannot redirect what follows, and nothing is remembered
//! between entries.
//! The returned [`VettedDir`] is the handle the caller writes through.
//!
//! The walk holds at most two handles: the current directory and the one being
//! opened.
//!
//! # Failure handling
//!
//! A failed open is classified by `fstatat(NOFOLLOW)`, not by `errno`, because
//! platforms disagree on what a directory open of a symlink reports. A missing
//! component is created and opened once more; a second failure, or any
//! failure whose probe finds nothing to blame, returns the original I/O error
//! so an attacker who removes and re-creates entries cannot make the walk
//! retry forever.

use std::path::Path;
use std::path::PathBuf;

use crate::ArchiveError;
use crate::Result;
use crate::SecurityConfig;
use crate::Violation;
#[cfg(any(unix, target_os = "wasi"))]
use crate::formats::handle_resolution::LinkCursor;
use crate::formats::pinned_dir::Leaf;
use crate::formats::pinned_dir::PinnedRoot;
use crate::formats::pinned_dir::VettedDir;
#[cfg(not(any(unix, target_os = "wasi")))]
use crate::security::fs_view::HostFs;
#[cfg(not(any(unix, target_os = "wasi")))]
use crate::security::fs_view::LinkClass;
use crate::security::fs_view::Occupant;
use crate::security::fs_view::ParentBlocker;
use crate::security::fs_view::PathModel;
#[cfg(not(any(unix, target_os = "wasi")))]
use crate::security::fs_view::classify_link;
use crate::security::fs_view::joined;
use crate::security::fs_view::name_too_long;
use crate::security::fs_view::projected_landing_within_limits;
use crate::types::SafePath;
use crate::types::safe_path::ParentCheck;

/// A directory handle that is either the pinned destination root, borrowed, or
/// a handle the walk opened below it.
///
/// An entry directly in the destination needs no new descriptor: the root
/// handle is already pinned, so lending it saves an `openat` and a `close` per
/// top-level entry.
#[derive(Debug)]
pub enum DirRef<'r> {
    /// The destination's pinned root handle, lent by the [`PinnedRoot`].
    Root(&'r VettedDir),
    /// A handle the walk opened below the root, owned by the caller.
    Opened(VettedDir),
}

impl DirRef<'_> {
    /// Returns an owned handle, reopening the root when this one is borrowed,
    /// for callers that keep the directory beyond the borrow of the root.
    pub(crate) fn into_owned(self) -> Result<VettedDir> {
        match self {
            Self::Root(root) => Ok(root.reopen()?),
            Self::Opened(dir) => Ok(dir),
        }
    }
}

impl std::ops::Deref for DirRef<'_> {
    type Target = VettedDir;

    fn deref(&self) -> &VettedDir {
        match self {
            Self::Root(root) => root,
            Self::Opened(dir) => dir,
        }
    }
}

/// What an existing symlink among the parent components may be.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LinkPolicy {
    /// Any symlink component is refused.
    Refuse,
    /// A symlink component is accepted only if it resolves to a directory
    /// inside the destination.
    ContainedOnly,
}

impl LinkPolicy {
    fn of(config: &SecurityConfig) -> Self {
        if config.allowed.symlinks.is_allowed() {
            Self::ContainedOnly
        } else {
            Self::Refuse
        }
    }
}

/// What a walk does with a component that does not exist.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Missing {
    Create,
    Fail,
}

/// Which symlink rule a walk applies.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Links {
    /// The walker's configured [`LinkPolicy`].
    Policy,
    /// Any symlink is a blocker; used to re-walk a path that was already
    /// resolved to a symlink-free form.
    Strict,
}

/// One opened component, with the symlink-free location it resolved to when
/// the component was a symlink.
struct Hop<'r> {
    dir: DirRef<'r>,
    resolved: Option<PathBuf>,
}

impl Hop<'_> {
    fn plain(dir: VettedDir) -> Self {
        Self {
            dir: DirRef::Opened(dir),
            resolved: None,
        }
    }
}

/// Creates and vets the directories that archive entries are written into.
#[derive(Debug, Clone, Copy)]
pub struct DirWalker {
    links: LinkPolicy,
}

impl DirWalker {
    /// Builds a walker that applies the symlink policy of `config`.
    pub fn new(config: &SecurityConfig) -> Self {
        Self {
            links: LinkPolicy::of(config),
        }
    }

    /// Creates `dest/rel` and every missing ancestor, vetting each component,
    /// and returns a handle on the directory.
    ///
    /// # Errors
    ///
    /// Returns `SecurityViolation` for a symlink component the policy refuses
    /// or one that dangles or does not resolve to a directory, `SymlinkEscape`
    /// for one that leaves `dest`, `PathTraversal` for a non-relative `rel`,
    /// and an I/O error when a component is a file or cannot be created. A
    /// blocking component is reported against `rel` with the same text and
    /// category as `verify`.
    pub(crate) fn ensure_dir<'r, P: ParentCheck>(
        self,
        root: &'r PinnedRoot,
        rel: &SafePath<P>,
    ) -> Result<DirRef<'r>> {
        let rel = rel.as_path();
        self.descend(root, rel, rel, Missing::Create, Links::Policy)
    }

    /// Creates the parent directory of `dest/rel` like [`Self::ensure_dir`]
    /// and returns it with the name of the entry inside it.
    ///
    /// # Errors
    ///
    /// Same as [`Self::ensure_dir`], plus `PathTraversal` when `rel` does not
    /// end in a normal name.
    pub(crate) fn ensure_parent<'r, 'p, P: ParentCheck>(
        self,
        root: &'r PinnedRoot,
        rel: &'p SafePath<P>,
    ) -> Result<(DirRef<'r>, Leaf<'p>)> {
        let rel = rel.as_path();
        let (parent, leaf) = Leaf::split(rel)?;
        let dir = self.descend(root, parent, rel, Missing::Create, Links::Policy)?;
        Ok((dir, leaf))
    }

    /// Opens the parent directory of `dest/rel` without creating anything.
    ///
    /// # Errors
    ///
    /// Same as [`Self::ensure_parent`]; a missing component is an I/O error of
    /// kind `NotFound`.
    pub(crate) fn open_parent<'r, 'p>(
        self,
        root: &'r PinnedRoot,
        rel: &'p Path,
    ) -> Result<(DirRef<'r>, Leaf<'p>)> {
        let (parent, leaf) = Leaf::split(rel)?;
        let dir = self.descend(root, parent, rel, Missing::Fail, Links::Policy)?;
        Ok((dir, leaf))
    }

    /// Like [`Self::open_parent`], but any symlink on the way is refused. For
    /// a path that was resolved through symlinks and is expected to be free of
    /// them.
    ///
    /// # Errors
    ///
    /// Same as [`Self::open_parent`].
    pub(crate) fn open_resolved_parent<'r, 'p>(
        self,
        root: &'r PinnedRoot,
        rel: &'p Path,
    ) -> Result<(DirRef<'r>, Leaf<'p>)> {
        let (parent, leaf) = Leaf::split(rel)?;
        let dir = self.descend(root, parent, rel, Missing::Fail, Links::Strict)?;
        Ok((dir, leaf))
    }

    fn descend<'r>(
        self,
        root: &'r PinnedRoot,
        dir: &Path,
        entry: &Path,
        missing: Missing,
        links: Links,
    ) -> Result<DirRef<'r>> {
        let (dir_ref, landing) = self.walk(root, dir, entry, missing, links)?;
        if let Some(landing) = landing {
            let tail = entry.strip_prefix(dir).unwrap_or_else(|_| Path::new(""));
            projected_landing_within_limits(PathModel::Host, &landing, tail)?;
        }
        Ok(dir_ref)
    }

    /// Walks `dir` and also returns where the walk ended, relative to the
    /// destination, when a symlink on the way redirected it.
    fn walk<'r>(
        self,
        root: &'r PinnedRoot,
        dir: &Path,
        entry: &Path,
        missing: Missing,
        links: Links,
    ) -> Result<(DirRef<'r>, Option<PathBuf>)> {
        let mut walked = PathBuf::new();
        let mut landing: Option<PathBuf> = None;
        let mut current: Option<DirRef<'r>> = None;
        let tail = entry.strip_prefix(dir).unwrap_or_else(|_| Path::new(""));
        let mut components = dir.components();
        while let Some(component) = components.next() {
            let Some(leaf) = Leaf::from_component(component) else {
                if matches!(component, std::path::Component::CurDir) {
                    continue;
                }
                return Err(ArchiveError::PathTraversal {
                    path: dir.to_path_buf(),
                });
            };
            walked.push(leaf.as_os_str());
            let parent = current.as_deref().unwrap_or_else(|| root.dir());
            let here = landing
                .as_deref()
                .unwrap_or_else(|| walked.parent().unwrap_or_else(|| Path::new("")));
            let site = LinkSite { parent, leaf, here };
            let hop = self.step(root, &site, &walked, entry, missing, links)?;
            let redirected = hop.resolved.is_some();
            match (hop.resolved, landing.as_mut()) {
                (Some(resolved), _) => landing = Some(resolved),
                (None, Some(landing)) => landing.push(leaf.as_os_str()),
                (None, None) => {}
            }
            if let (true, Some(landing)) = (redirected, landing.as_deref()) {
                Self::ensure_fits(&hop.dir, landing, components.as_path(), tail)?;
            }
            current = Some(hop.dir);
        }
        Ok((current.unwrap_or_else(|| DirRef::Root(root.dir())), landing))
    }

    /// Refuses, before anything is created below a symlink target, a landing
    /// that no later component can bring under the host path limit.
    ///
    /// The rest of the path may still cross another symlink, which resets the
    /// landing; only when it provably ends here is the projected length final.
    fn ensure_fits(at: &VettedDir, landing: &Path, rest: &Path, tail: &Path) -> Result<()> {
        let projected = joined(landing, rest);
        if projected_landing_within_limits(PathModel::Host, &projected, tail).is_ok()
            || !Self::ends_here(at, rest)
        {
            return Ok(());
        }
        Err(ArchiveError::Io(name_too_long()))
    }

    /// Whether `rest` below `at` holds no symlink, file or unreadable entry, so
    /// no later component can change where the walk lands.
    fn ends_here(at: &VettedDir, rest: &Path) -> bool {
        let mut opened: Option<VettedDir> = None;
        for component in rest.components() {
            let Some(leaf) = Leaf::from_component(component) else {
                continue;
            };
            let parent = opened.as_ref().unwrap_or(at);
            match parent.probe(leaf) {
                Ok(None) => return true,
                Ok(Some(Occupant::Directory)) => match parent.open_dir(leaf) {
                    Ok(next) => opened = Some(next),
                    Err(_) => return false,
                },
                Ok(Some(_)) | Err(_) => return false,
            }
        }
        true
    }

    fn step<'r>(
        self,
        root: &'r PinnedRoot,
        site: &LinkSite<'_>,
        walked: &Path,
        entry: &Path,
        missing: Missing,
        links: Links,
    ) -> Result<Hop<'r>> {
        let LinkSite { parent, leaf, .. } = *site;
        let mut err = match parent.open_dir(leaf) {
            Ok(dir) => return Ok(Hop::plain(dir)),
            Err(e) => e,
        };
        if err.kind() == std::io::ErrorKind::NotFound && missing == Missing::Create {
            match parent.make_dir(leaf) {
                Ok(()) => {}
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
                Err(e) => return Err(e.into()),
            }
            err = match parent.open_dir(leaf) {
                Ok(dir) => return Ok(Hop::plain(dir)),
                Err(e) => e,
            };
        }
        match parent.probe(leaf) {
            Ok(Some(Occupant::Symlink)) => self.follow(root, site, walked, entry, links),
            Ok(Some(Occupant::File)) => Err(ParentBlocker::File.error(entry, walked)),
            Ok(Some(Occupant::Directory) | None) | Err(_) => Err(err.into()),
        }
    }

    /// Resolves the symlink component `walked` under the active rule.
    ///
    /// The link is resolved even under `Refuse`, to tell a link no policy
    /// admits from one `--allow-symlinks` would; resolving can trigger an
    /// automount lookup.
    fn follow<'r>(
        self,
        root: &'r PinnedRoot,
        site: &LinkSite<'_>,
        walked: &Path,
        entry: &Path,
        links: Links,
    ) -> Result<Hop<'r>> {
        if links == Links::Strict {
            return Err(ParentBlocker::Symlink.error(entry, walked));
        }
        match (Followed::of(root, site, walked), self.links) {
            (Followed::Blocked(blocker), _) => Err(blocker.error(entry, walked)),
            (Followed::Failed(error), _) => Err(error),
            (Followed::Inside { .. }, LinkPolicy::Refuse) => {
                Err(ArchiveError::from(Violation::SymlinkComponent {
                    walked: walked.display().to_string().into(),
                }))
            }
            (Followed::Inside { position, dir }, LinkPolicy::ContainedOnly) => {
                let dir = match dir {
                    Some(dir) => DirRef::Opened(dir),
                    None => {
                        self.walk(root, &position, entry, Missing::Fail, Links::Strict)?
                            .0
                    }
                };
                Ok(Hop {
                    dir,
                    resolved: Some(position),
                })
            }
        }
    }
}

/// Where a symlink component sits: the handle of the directory that holds it,
/// its name, and that directory's symlink-free position below the destination.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(
    not(any(unix, target_os = "wasi")),
    expect(
        dead_code,
        reason = "only the handle-relative resolver reads the link site"
    )
)]
struct LinkSite<'a> {
    parent: &'a VettedDir,
    leaf: Leaf<'a>,
    here: &'a Path,
}

/// What a pre-existing symlink among the parent components turns out to be.
#[derive(Debug)]
enum Followed {
    /// A directory inside the destination, at this symlink-free path relative
    /// to it, with the handle the resolution ended on when it kept one.
    Inside {
        position: PathBuf,
        dir: Option<VettedDir>,
    },
    /// Nothing may be written through the link.
    Blocked(ParentBlocker),
    /// The host could not answer; says nothing about the link.
    #[cfg_attr(
        not(any(unix, target_os = "wasi")),
        expect(dead_code, reason = "only the handle-relative resolver fails this way")
    )]
    Failed(ArchiveError),
}

#[cfg(any(unix, target_os = "wasi"))]
impl Followed {
    /// Resolves the link with a [`LinkCursor`] on the handles the walk holds,
    /// so the verdict never depends on how long the destination's own absolute
    /// path is, and the walk continues in the cursor's final handle.
    fn of(root: &PinnedRoot, site: &LinkSite<'_>, _walked: &Path) -> Self {
        match LinkCursor::at_site(root, site.parent, site.here, site.leaf) {
            Ok((position, dir)) => Self::Inside {
                position,
                dir: Some(dir),
            },
            Err(refusal) => match refusal.into_failure() {
                Ok(error) => Self::Failed(error),
                Err(verdict) => Self::Blocked(verdict.blocker()),
            },
        }
    }
}

#[cfg(not(any(unix, target_os = "wasi")))]
impl Followed {
    /// Resolves the link by its absolute path: there is no pinned
    /// `readlinkat` on this platform.
    // TODO(#958): resolve handle-relatively on Windows; realpath makes
    // acceptance depend on the destination's absolute length there.
    fn of(root: &PinnedRoot, _site: &LinkSite<'_>, walked: &Path) -> Self {
        let dest = root.dest();
        match classify_link(&HostFs, &dest.as_path().join(walked), dest) {
            LinkClass::InsideDir(resolved) => Self::Inside {
                position: resolved
                    .strip_prefix(dest.as_path())
                    .unwrap_or(&resolved)
                    .to_path_buf(),
                dir: None,
            },
            LinkClass::Blocked(blocker) => Self::Blocked(blocker),
        }
    }
}

#[cfg(all(test, unix))]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::PolicySetting;
    use crate::security::fs_view::MAX_PATH_LEN;
    use crate::types::DestDir;
    use crate::types::safe_path::Resolved;
    use std::assert_matches;
    use std::fs;
    use std::os::unix::fs::symlink;
    use tempfile::TempDir;

    fn walker(allow_symlinks: bool) -> DirWalker {
        let config = SecurityConfig::default().with_allow_symlinks(allow_symlinks);
        DirWalker::new(&config)
    }

    fn sp(path: &str) -> SafePath {
        SafePath::<Resolved>::for_test(path)
    }

    fn dest_with_outside() -> (TempDir, PinnedRoot, PathBuf) {
        let root = TempDir::new().unwrap();
        let dest = root.path().join("dest");
        let outside = root.path().join("outside");
        fs::create_dir(&dest).unwrap();
        fs::create_dir(&outside).unwrap();
        (
            root,
            PinnedRoot::open(DestDir::new(dest).unwrap()).unwrap(),
            outside.canonicalize().unwrap(),
        )
    }

    #[test]
    fn creates_missing_components_and_is_idempotent() {
        let (_root, dest, _) = dest_with_outside();
        let walker = walker(false);

        walker.ensure_dir(&dest, &sp("a/b/c")).unwrap();
        walker.ensure_dir(&dest, &sp("a/b/c")).unwrap();
        walker.ensure_parent(&dest, &sp("a/b/c/f.txt")).unwrap();

        assert!(dest.dest().as_path().join("a/b/c").is_dir());
    }

    #[test]
    fn parent_of_a_top_level_name_is_the_destination() {
        let (_root, dest, _) = dest_with_outside();
        let rel = sp("f.txt");
        let (dir, leaf) = walker(false).ensure_parent(&dest, &rel).unwrap();

        dir.create_new(leaf, None).unwrap();

        assert!(dest.dest().as_path().join("f.txt").is_file());
    }

    #[test]
    fn top_level_parent_borrows_the_root_and_nested_parent_opens_one() {
        let (_root, dest, _) = dest_with_outside();
        let walker = walker(false);

        let (top, _) = walker.ensure_parent(&dest, &sp("f.txt")).unwrap();
        let (nested, _) = walker.ensure_parent(&dest, &sp("d/f.txt")).unwrap();

        assert_matches!(top, DirRef::Root(_));
        assert_matches!(nested, DirRef::Opened(_));
    }

    #[test]
    fn borrowed_root_stays_pinned_when_the_destination_path_is_swapped() {
        let (root, dest, outside) = dest_with_outside();
        let rel = sp("f.txt");
        let (dir, leaf) = walker(false).ensure_parent(&dest, &rel).unwrap();
        assert_matches!(dir, DirRef::Root(_));

        let moved = root.path().join("moved");
        fs::rename(dest.dest().as_path(), &moved).unwrap();
        symlink(&outside, dest.dest().as_path()).unwrap();
        dir.create_new(leaf, None).unwrap();

        assert!(moved.join("f.txt").is_file());
        assert!(!outside.join("f.txt").exists());
    }

    #[test]
    fn planted_symlink_is_refused_with_the_borrowed_root() {
        let (_root, dest, outside) = dest_with_outside();
        symlink(&outside, dest.dest().as_path().join("planted")).unwrap();

        let err = walker(true)
            .ensure_parent(&dest, &sp("planted/f.txt"))
            .map(|_| ())
            .unwrap_err();

        assert_matches!(err, ArchiveError::SymlinkEscape { .. }, "{err}");
        assert!(!outside.join("f.txt").exists());
    }

    #[test]
    fn into_owned_gives_a_usable_handle_for_both_variants() {
        let (_root, dest, _) = dest_with_outside();
        let walker = walker(false);

        let (top_rel, nested_rel) = (sp("top.txt"), sp("d/n.txt"));
        let (top, top_leaf) = walker.ensure_parent(&dest, &top_rel).unwrap();
        let (nested, nested_leaf) = walker.ensure_parent(&dest, &nested_rel).unwrap();
        let top = top.into_owned().unwrap();
        let nested = nested.into_owned().unwrap();
        top.create_new(top_leaf, None).unwrap();
        nested.create_new(nested_leaf, None).unwrap();

        assert!(dest.dest().as_path().join("top.txt").is_file());
        assert!(dest.dest().as_path().join("d/n.txt").is_file());
    }

    #[test]
    fn open_parent_does_not_create_and_reports_not_found() {
        let (_root, dest, _) = dest_with_outside();

        let err = walker(false)
            .open_parent(&dest, sp("missing/f").as_path())
            .unwrap_err();

        assert_matches!(&err, ArchiveError::Io(e) if e.kind() == std::io::ErrorKind::NotFound);
        assert!(!dest.dest().as_path().join("missing").exists());
    }

    #[test]
    fn refuses_a_symlink_component_without_creating_below_it() {
        let (_root, dest, outside) = dest_with_outside();
        symlink(&outside, dest.dest().as_path().join("redirect")).unwrap();

        let err = walker(false)
            .ensure_dir(&dest, &sp("redirect/sub"))
            .unwrap_err();

        assert_matches!(err, ArchiveError::SymlinkEscape { .. }, "{err}");
        assert!(!outside.join("sub").exists());
    }

    #[test]
    fn refuse_policy_offers_the_flag_only_for_a_link_it_would_admit() {
        let (_root, dest, outside) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        fs::create_dir(base.join("real")).unwrap();
        symlink("real", base.join("alias")).unwrap();
        symlink("missing", base.join("dangling")).unwrap();
        fs::write(base.join("f"), b"x").unwrap();
        symlink("f", base.join("tofile")).unwrap();
        symlink(&outside, base.join("away")).unwrap();

        let relaxable = |rel: &str| {
            walker(false)
                .ensure_dir(&dest, &sp(rel))
                .unwrap_err()
                .relaxable_by()
        };

        assert_eq!(relaxable("alias/x"), Some(PolicySetting::Symlinks));
        assert_eq!(relaxable("dangling/x"), None);
        assert_eq!(relaxable("tofile/x"), None);
        assert_eq!(relaxable("away/x"), None);
    }

    #[test]
    fn refuses_a_symlink_to_a_file_component() {
        let (_root, dest, outside) = dest_with_outside();
        fs::write(outside.join("f"), b"x").unwrap();
        symlink(outside.join("f"), dest.dest().as_path().join("link")).unwrap();

        assert!(walker(false).ensure_dir(&dest, &sp("link/x")).is_err());
    }

    #[test]
    fn contained_policy_refuses_an_escaping_symlink() {
        let (_root, dest, outside) = dest_with_outside();
        symlink(&outside, dest.dest().as_path().join("redirect")).unwrap();

        let err = walker(true)
            .ensure_dir(&dest, &sp("redirect/sub"))
            .unwrap_err();

        assert_matches!(err, ArchiveError::SymlinkEscape { .. }, "{err}");
        assert!(!outside.join("sub").exists());
    }

    #[test]
    fn contained_policy_accepts_a_link_to_a_directory_inside_dest() {
        let (_root, dest, _) = dest_with_outside();
        fs::create_dir(dest.dest().as_path().join("real")).unwrap();
        symlink("real", dest.dest().as_path().join("alias")).unwrap();

        walker(true).ensure_dir(&dest, &sp("alias/sub")).unwrap();

        assert!(dest.dest().as_path().join("real/sub").is_dir());
    }

    #[test]
    fn an_absolute_target_through_the_canonical_root_is_accepted() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        fs::create_dir(base.join("real")).unwrap();
        symlink(base.join("real"), base.join("alias")).unwrap();

        walker(true).ensure_dir(&dest, &sp("alias/sub")).unwrap();

        assert!(base.join("real/sub").is_dir());
    }

    // `/var/...` written by the caller versus `/private/var/...` canonical: an
    // absolute target spelled through the written root is accepted.
    #[test]
    fn an_absolute_target_spelled_through_the_written_root_is_accepted() {
        let temp = TempDir::new().unwrap();
        let real_root = temp.path().join("dest");
        fs::create_dir(&real_root).unwrap();
        let written = temp.path().join("written_alias");
        symlink(&real_root, &written).unwrap();
        let dest = PinnedRoot::open(DestDir::new(&written).unwrap()).unwrap();
        assert_ne!(dest.dest().as_path(), dest.dest().written());
        fs::create_dir(real_root.join("real")).unwrap();
        symlink(written.join("real"), real_root.join("alias")).unwrap();

        walker(true).ensure_dir(&dest, &sp("alias/sub")).unwrap();

        assert!(real_root.join("real/sub").is_dir());
    }

    // The absolute spelling is mapped only through the canonical or the written
    // root; another spelling of the same place is refused where `realpath`
    // accepted it.
    #[test]
    fn an_absolute_target_spelled_through_another_alias_of_the_root_is_refused() {
        let (root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        fs::create_dir(base.join("real")).unwrap();
        let alias_of_root = root.path().join("alias_of_dest");
        symlink(&base, &alias_of_root).unwrap();
        symlink(alias_of_root.join("real"), base.join("alias")).unwrap();

        let err = walker(true)
            .ensure_dir(&dest, &sp("alias/sub"))
            .unwrap_err();

        assert_matches!(err, ArchiveError::SymlinkEscape { .. }, "{err}");
        assert!(!base.join("real/sub").exists());
    }

    #[test]
    fn a_target_that_leaves_the_root_and_reenters_it_is_refused() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        fs::create_dir(base.join("real")).unwrap();
        symlink("../dest/real", base.join("up")).unwrap();

        let err = walker(true).ensure_dir(&dest, &sp("up/sub")).unwrap_err();

        assert_matches!(err, ArchiveError::SymlinkEscape { .. }, "{err}");
        assert!(!base.join("real/sub").exists());
    }

    #[test]
    fn a_link_to_the_parent_directory_resolves_to_it() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        fs::create_dir(base.join("x")).unwrap();
        symlink("..", base.join("x/up")).unwrap();

        walker(true).ensure_dir(&dest, &sp("x/up/y")).unwrap();

        assert!(base.join("y").is_dir());
    }

    /// A link at the bottom of a deep directory whose target climbs the whole
    /// way back up, crossed by many entries: each crossing is one handle pop
    /// per `..`, not a re-walk from the root.
    fn deep_climbing_link(depth: usize) -> (TempDir, PinnedRoot, String) {
        let (root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        let deep = vec!["a"; depth].join("/");
        walker(false).ensure_dir(&dest, &sp(&deep)).unwrap();
        fs::create_dir(base.join("x")).unwrap();
        let climb = vec![".."; depth].join("/");
        symlink(format!("{climb}/x"), base.join(&deep).join("l")).unwrap();
        (root, dest, format!("{deep}/l"))
    }

    #[test]
    fn a_link_climbing_out_of_a_deep_directory_resolves_to_the_root_level_target() {
        let (_root, dest, link) = deep_climbing_link(30);

        walker(true)
            .ensure_dir(&dest, &sp(&format!("{link}/sub")))
            .unwrap();

        assert!(dest.dest().as_path().join("x/sub").is_dir());
    }

    #[test]
    fn crossing_a_deep_climbing_link_many_times_keeps_each_hop_cheap() {
        let (_root, dest, link) = deep_climbing_link(30);
        let started = std::time::Instant::now();

        for i in 0..2000 {
            walker(true)
                .ensure_dir(&dest, &sp(&format!("{link}/d{i}")))
                .unwrap();
        }

        assert!(
            started.elapsed() < std::time::Duration::from_secs(60),
            "2000 crossings took {:?}",
            started.elapsed()
        );
    }

    #[test]
    fn name_then_parent_returns_only_through_a_real_directory() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        fs::create_dir_all(base.join("a/b")).unwrap();
        fs::create_dir(base.join("real")).unwrap();
        fs::write(base.join("file"), b"x").unwrap();
        symlink("a/b", base.join("s")).unwrap();
        symlink("real/../real", base.join("plain")).unwrap();
        symlink("s/..", base.join("through_link")).unwrap();
        symlink("file/..", base.join("through_file")).unwrap();
        symlink("missing/..", base.join("through_missing")).unwrap();

        walker(true).ensure_dir(&dest, &sp("plain/x")).unwrap();
        assert!(base.join("real/x").is_dir());
        // `s/..` is the parent of the link's target (`a`), not of the link.
        walker(true)
            .ensure_dir(&dest, &sp("through_link/y"))
            .unwrap();
        assert!(base.join("a/y").is_dir());
        assert!(!base.join("y").exists());
        assert!(
            walker(true)
                .ensure_dir(&dest, &sp("through_file/z"))
                .is_err()
        );
        assert!(
            walker(true)
                .ensure_dir(&dest, &sp("through_missing/z"))
                .is_err()
        );
    }

    // The shape of the perf fixture, scaled down: a chain of links whose
    // targets each go down a nested directory, back up, and on to the previous
    // link.
    #[test]
    fn a_chain_of_down_and_up_links_resolves_and_stays_cheap() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        let nested = vec!["d"; 15].join("/");
        walker(false).ensure_dir(&dest, &sp(&nested)).unwrap();
        fs::create_dir(base.join("end")).unwrap();
        let wiggle = format!("{nested}/{}", vec![".."; 15].join("/"));
        symlink("end", base.join("l0")).unwrap();
        for i in 1..=10 {
            symlink(format!("{wiggle}/l{}", i - 1), base.join(format!("l{i}"))).unwrap();
        }
        let started = std::time::Instant::now();

        for i in 0..300 {
            walker(true)
                .ensure_dir(&dest, &sp(&format!("l10/e{i}")))
                .unwrap();
        }

        assert!(base.join("end/e299").is_dir());
        assert!(
            started.elapsed() < std::time::Duration::from_secs(30),
            "{:?}",
            started.elapsed()
        );
    }

    #[test]
    fn a_loop_of_links_is_refused_and_creates_nothing() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        symlink("b", base.join("a")).unwrap();
        symlink("a", base.join("b")).unwrap();

        assert!(walker(true).ensure_dir(&dest, &sp("a/x")).is_err());
        assert_eq!(fs::read_dir(&base).unwrap().count(), 2);
    }

    #[test]
    fn the_hop_limit_is_exact_and_shared_with_the_virtual_tree() {
        use crate::security::fs_view::MAX_SYMLINK_HOPS;

        for (hops, accepted) in [(MAX_SYMLINK_HOPS, true), (MAX_SYMLINK_HOPS + 1, false)] {
            let (_root, dest, _) = dest_with_outside();
            let base = dest.dest().as_path().to_path_buf();
            fs::create_dir(base.join("real")).unwrap();
            for i in 1..hops {
                symlink(format!("l{}", i + 1), base.join(format!("l{i}"))).unwrap();
            }
            symlink("real", base.join(format!("l{hops}"))).unwrap();

            let result = walker(true).ensure_dir(&dest, &sp("l1/x"));

            assert_eq!(result.is_ok(), accepted, "{hops} hops");
        }
    }

    fn name_of_len(len: usize) -> String {
        let mut name = String::new();
        while name.len() < len {
            if !name.is_empty() {
                name.push('/');
            }
            let room = len - name.len();
            name.push_str(&"n".repeat(room.min(200)));
        }
        assert_eq!(name.len(), len);
        name
    }

    /// Builds `dest/d -> chain` and returns a relative entry `d/<rest>` whose
    /// landing is `landing_len` bytes long. The chain is sized from the
    /// destination so the link itself resolves on every platform.
    fn long_landing(dest: &PinnedRoot, landing_len: usize) -> SafePath {
        let base = dest.dest().as_path();
        let chain_len = MAX_PATH_LEN - base.as_os_str().len() - 64;
        let chain = name_of_len(chain_len);
        walker(false).ensure_dir(dest, &sp(&chain)).unwrap();
        symlink(&chain, base.join("d")).unwrap();
        let rest = name_of_len(landing_len - chain_len - 1);
        sp(&format!("d/{rest}/f"))
    }

    #[test]
    fn contained_policy_accepts_a_landing_exactly_at_the_path_limit() {
        let (_root, dest, _) = dest_with_outside();
        let landing_len = MAX_PATH_LEN - "/f".len();
        let rel = long_landing(&dest, landing_len);

        walker(true).ensure_parent(&dest, &rel).unwrap();
    }

    #[test]
    fn contained_policy_refuses_a_landing_one_byte_over_the_path_limit() {
        let (_root, dest, _) = dest_with_outside();
        let landing_len = MAX_PATH_LEN - "/f".len() + 1;
        let rel = long_landing(&dest, landing_len);

        let err = walker(true)
            .ensure_parent(&dest, &rel)
            .map(|_| ())
            .unwrap_err();

        assert_matches!(&err, ArchiveError::Io(e) if e.kind() == std::io::ErrorKind::InvalidFilename, "{err}");
        assert_eq!(
            fs::read_dir(dest.dest().as_path().join("d"))
                .unwrap()
                .count(),
            0,
            "nothing is created below the link target"
        );
    }

    #[test]
    fn contained_policy_judges_the_final_landing_not_the_first_hop() {
        let (_root, dest, _) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        let chain = name_of_len(MAX_PATH_LEN - base.as_os_str().len() - 64);
        walker(false).ensure_dir(&dest, &sp(&chain)).unwrap();
        fs::create_dir(base.join("short")).unwrap();
        symlink(&chain, base.join("d")).unwrap();
        symlink(base.join("short"), base.join(&chain).join("x")).unwrap();

        walker(true).ensure_parent(&dest, &sp("d/x/f")).unwrap();
    }

    #[test]
    fn contained_policy_refuses_a_dangling_symlink() {
        let (_root, dest, _) = dest_with_outside();
        symlink("missing", dest.dest().as_path().join("dangling")).unwrap();

        assert!(walker(true).ensure_dir(&dest, &sp("dangling/x")).is_err());
    }

    #[test]
    fn refuses_a_regular_file_component() {
        let (_root, dest, _) = dest_with_outside();
        fs::write(dest.dest().as_path().join("f"), b"x").unwrap();

        let err = walker(false).ensure_dir(&dest, &sp("f/x")).unwrap_err();

        assert_matches!(err, ArchiveError::Io(_), "{err}");
    }

    #[test]
    fn refuses_non_relative_paths() {
        let (_root, dest, _) = dest_with_outside();
        for bad in ["../x", "/abs", "a/../../x"] {
            let err = walker(false).ensure_dir(&dest, &sp(bad)).unwrap_err();
            assert_matches!(err, ArchiveError::PathTraversal { .. }, "{bad}: {err}");
        }
    }

    #[test]
    fn a_swap_after_a_successful_walk_is_caught_by_the_next_one() {
        let (_root, dest, outside) = dest_with_outside();
        let walker = walker(false);
        walker.ensure_dir(&dest, &sp("d")).unwrap();

        fs::remove_dir(dest.dest().as_path().join("d")).unwrap();
        symlink(&outside, dest.dest().as_path().join("d")).unwrap();

        assert!(walker.ensure_parent(&dest, &sp("d/f")).is_err());
    }

    #[test]
    fn a_swap_after_a_walk_does_not_redirect_writes_through_the_returned_handle() {
        let (_root, dest, outside) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        let rel = sp("a/b/f");
        let (dir, leaf) = walker(false).ensure_parent(&dest, &rel).unwrap();

        fs::rename(base.join("a"), base.join("a_moved")).unwrap();
        symlink(&outside, base.join("a")).unwrap();
        dir.create_new(leaf, None).unwrap();

        assert!(base.join("a_moved/b/f").is_file());
        assert_eq!(fs::read_dir(&outside).unwrap().count(), 0);
    }

    #[test]
    fn components_swapped_while_walking_never_write_outside() {
        use std::sync::atomic::AtomicBool;
        use std::sync::atomic::Ordering;

        let (_root, dest, outside) = dest_with_outside();
        let base = dest.dest().as_path().to_path_buf();
        let stop = AtomicBool::new(false);
        let walker = walker(false);

        std::thread::scope(|scope| {
            scope.spawn(|| {
                while !stop.load(Ordering::Relaxed) {
                    let moved = base.join("d_moved");
                    if fs::rename(base.join("d"), &moved).is_ok() {
                        let _ = symlink(&outside, base.join("d"));
                        let _ = fs::remove_file(base.join("d"));
                        let _ = fs::rename(&moved, base.join("d"));
                    }
                }
            });
            for i in 0..200 {
                let rel = sp(&format!("d/sub/f{i}"));
                if let Ok((dir, leaf)) = walker.ensure_parent(&dest, &rel) {
                    let _ = dir.create_new(leaf, None);
                }
            }
            stop.store(true, Ordering::Relaxed);
        });

        assert_eq!(fs::read_dir(&outside).unwrap().count(), 0);
    }
}