| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Checkouts of hostile trees: nothing may be written outside the |
| 5 | //! workspace, and names that collide are refused rather than both written. |
| 6 | |
| 7 | mod common; |
| 8 | |
| 9 | use std::fs; |
| 10 | |
| 11 | use braid_wc::{Error, FileSystem, Updated, Workspace}; |
| 12 | use common::{Fixture, ada, listing}; |
| 13 | |
| 14 | /// The bytes of an absolute path, as a symlink target. |
| 15 | fn target(path: &std::path::Path) -> Vec<u8> { |
| 16 | path.as_os_str().as_encoded_bytes().to_vec() |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn a_symlink_replaced_by_a_directory_is_never_gone_through() { |
| 21 | let f = Fixture::new(); |
| 22 | let outside = tempfile::tempdir().unwrap(); |
| 23 | let first = f.commit( |
| 24 | &[], |
| 25 | vec![ |
| 26 | ("dir", f.symlink(&target(outside.path()))), |
| 27 | ("keep", f.file("k")), |
| 28 | ], |
| 29 | ); |
| 30 | f.set_branch("main", first); |
| 31 | let ws_dir = f.path("ws"); |
| 32 | let (ws, _) = Workspace::create(&f.repo, "ws", &ws_dir, Some(first), "main").unwrap(); |
| 33 | // The symlink is there, as a symlink. |
| 34 | assert_eq!(fs::read_link(ws_dir.join("dir")).unwrap(), outside.path()); |
| 35 | |
| 36 | // A later tree makes `dir` a directory with a file in it. |
| 37 | let second = f.commit( |
| 38 | &[first], |
| 39 | vec![ |
| 40 | ("dir", braid_format::ChangeOp::Delete), |
| 41 | ("dir/x", f.file("pwned")), |
| 42 | ], |
| 43 | ); |
| 44 | f.set_branch("main", second); |
| 45 | assert_eq!( |
| 46 | ws.update(&ada()).unwrap().updated, |
| 47 | Updated::Moved { head: second } |
| 48 | ); |
| 49 | assert!( |
| 50 | listing(outside.path()).is_empty(), |
| 51 | "wrote outside the workspace" |
| 52 | ); |
| 53 | assert!(fs::symlink_metadata(ws_dir.join("dir")).unwrap().is_dir()); |
| 54 | assert_eq!(fs::read(ws_dir.join("dir/x")).unwrap(), b"pwned"); |
| 55 | assert!(ws.status().unwrap().status.is_clean()); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn a_symlink_and_a_case_twin_directory_in_one_tree() { |
| 60 | let f = Fixture::new(); |
| 61 | let outside = tempfile::tempdir().unwrap(); |
| 62 | let hostile = f.commit( |
| 63 | &[], |
| 64 | vec![ |
| 65 | ("dir", f.symlink(&target(outside.path()))), |
| 66 | ("DIR/x", f.file("pwned")), |
| 67 | ], |
| 68 | ); |
| 69 | let ws_dir = f.path("ws"); |
| 70 | fs::create_dir(&ws_dir).unwrap(); |
| 71 | let probe = FileSystem::probe( |
| 72 | &cap_std::fs::Dir::open_ambient_dir(&ws_dir, cap_std::ambient_authority()).unwrap(), |
| 73 | ); |
| 74 | fs::remove_dir(&ws_dir).unwrap(); |
| 75 | |
| 76 | let created = Workspace::create(&f.repo, "ws", &ws_dir, Some(hostile), "main"); |
| 77 | if probe.ignores_case { |
| 78 | // One file on this file system: refused, and nothing written. |
| 79 | assert!( |
| 80 | matches!(created, Err(Error::Collision { .. })), |
| 81 | "{created:?}" |
| 82 | ); |
| 83 | assert!(!ws_dir.exists()); |
| 84 | assert!(Workspace::list(&f.repo).unwrap().is_empty()); |
| 85 | } else { |
| 86 | // Two names here: both written, the directory a real one. |
| 87 | created.unwrap(); |
| 88 | assert!(fs::symlink_metadata(ws_dir.join("DIR")).unwrap().is_dir()); |
| 89 | assert_eq!(fs::read(ws_dir.join("DIR/x")).unwrap(), b"pwned"); |
| 90 | } |
| 91 | assert!( |
| 92 | listing(outside.path()).is_empty(), |
| 93 | "wrote outside the workspace" |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn case_twins_are_refused_where_they_would_collide() { |
| 99 | let f = Fixture::new(); |
| 100 | let twins = f.commit( |
| 101 | &[], |
| 102 | vec![("README", f.file("upper")), ("readme", f.file("lower"))], |
| 103 | ); |
| 104 | let ws_dir = f.path("ws"); |
| 105 | fs::create_dir(&ws_dir).unwrap(); |
| 106 | let probe = FileSystem::probe( |
| 107 | &cap_std::fs::Dir::open_ambient_dir(&ws_dir, cap_std::ambient_authority()).unwrap(), |
| 108 | ); |
| 109 | let created = Workspace::create(&f.repo, "ws", &ws_dir, Some(twins), "main"); |
| 110 | if probe.ignores_case { |
| 111 | let Err(Error::Collision { first, second }) = created else { |
| 112 | panic!("expected a collision, got {created:?}"); |
| 113 | }; |
| 114 | assert_eq!( |
| 115 | (first.as_slice(), second.as_slice()), |
| 116 | (&b"README"[..], &b"readme"[..]) |
| 117 | ); |
| 118 | // The directory was there before and stays, empty. |
| 119 | assert!(listing(&ws_dir).is_empty()); |
| 120 | } else { |
| 121 | created.unwrap(); |
| 122 | assert_eq!(fs::read(ws_dir.join("README")).unwrap(), b"upper"); |
| 123 | assert_eq!(fs::read(ws_dir.join("readme")).unwrap(), b"lower"); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn an_untracked_symlink_in_the_way_is_refused() { |
| 129 | let f = Fixture::new(); |
| 130 | let outside = tempfile::tempdir().unwrap(); |
| 131 | // `dir` is ignored, so a symlink there does not count as a change. |
| 132 | let first = f.commit(&[], vec![(".gitignore", f.file("dir\n"))]); |
| 133 | f.set_branch("main", first); |
| 134 | let ws_dir = f.path("ws"); |
| 135 | let (ws, _) = Workspace::create(&f.repo, "ws", &ws_dir, Some(first), "main").unwrap(); |
| 136 | #[cfg(unix)] |
| 137 | std::os::unix::fs::symlink(outside.path(), ws_dir.join("dir")).unwrap(); |
| 138 | assert!(ws.status().unwrap().status.is_clean()); |
| 139 | |
| 140 | let second = f.commit(&[first], vec![("dir/x", f.file("pwned"))]); |
| 141 | f.set_branch("main", second); |
| 142 | let updated = ws.update(&ada()); |
| 143 | assert!( |
| 144 | matches!(&updated, Err(Error::Unsafe { path, .. }) if path == b"dir"), |
| 145 | "{updated:?}" |
| 146 | ); |
| 147 | assert!( |
| 148 | listing(outside.path()).is_empty(), |
| 149 | "wrote outside the workspace" |
| 150 | ); |
| 151 | // Nothing moved: the workspace is still at the first changeset. |
| 152 | assert_eq!(ws.state().unwrap().base, Some(first)); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn a_symlink_replaced_by_a_file_is_replaced_not_written_through() { |
| 157 | let f = Fixture::new(); |
| 158 | let outside = tempfile::tempdir().unwrap(); |
| 159 | let secret = outside.path().join("secret"); |
| 160 | fs::write(&secret, "keep me").unwrap(); |
| 161 | let first = f.commit(&[], vec![("link", f.symlink(&target(&secret)))]); |
| 162 | f.set_branch("main", first); |
| 163 | let ws_dir = f.path("ws"); |
| 164 | let (ws, _) = Workspace::create(&f.repo, "ws", &ws_dir, Some(first), "main").unwrap(); |
| 165 | let second = f.commit(&[first], vec![("link", f.file("replaced"))]); |
| 166 | f.set_branch("main", second); |
| 167 | ws.update(&ada()).unwrap(); |
| 168 | assert_eq!(fs::read(&secret).unwrap(), b"keep me"); |
| 169 | assert!(fs::symlink_metadata(ws_dir.join("link")).unwrap().is_file()); |
| 170 | assert_eq!(fs::read(ws_dir.join("link")).unwrap(), b"replaced"); |
| 171 | } |
| 172 | |
| 173 | #[cfg(unix)] |
| 174 | #[test] |
| 175 | fn modes_targets_and_gitlinks_are_written_as_data() { |
| 176 | use std::os::unix::fs::PermissionsExt; |
| 177 | let f = Fixture::new(); |
| 178 | let gitlink = braid_format::ChangeOp::Gitlink { |
| 179 | commit: braid_format::GitId::Sha1([7; 20]), |
| 180 | }; |
| 181 | let first = f.commit( |
| 182 | &[], |
| 183 | vec![ |
| 184 | ("plain", f.file("p")), |
| 185 | ("run.sh", f.exec("#!/bin/sh\n")), |
| 186 | ("weird", f.symlink(b"../../../../etc/passwd")), |
| 187 | ("vendor/lib", gitlink), |
| 188 | ], |
| 189 | ); |
| 190 | let ws_dir = f.path("ws"); |
| 191 | let (ws, written) = Workspace::create(&f.repo, "ws", &ws_dir, Some(first), "main").unwrap(); |
| 192 | assert_eq!(written, 4); |
| 193 | let mode = |name: &str| { |
| 194 | fs::metadata(ws_dir.join(name)) |
| 195 | .unwrap() |
| 196 | .permissions() |
| 197 | .mode() |
| 198 | & 0o7777 |
| 199 | }; |
| 200 | assert_eq!(mode("plain"), 0o644); |
| 201 | assert_eq!(mode("run.sh"), 0o755); |
| 202 | assert_eq!( |
| 203 | fs::read_link(ws_dir.join("weird")) |
| 204 | .unwrap() |
| 205 | .as_os_str() |
| 206 | .as_encoded_bytes(), |
| 207 | b"../../../../etc/passwd" |
| 208 | ); |
| 209 | assert!( |
| 210 | fs::symlink_metadata(ws_dir.join("vendor/lib")) |
| 211 | .unwrap() |
| 212 | .is_dir() |
| 213 | ); |
| 214 | assert!(listing(&ws_dir.join("vendor/lib")).is_empty()); |
| 215 | assert!(ws.status().unwrap().status.is_clean()); |
| 216 | } |
| 217 | |
| 218 | #[cfg(target_vendor = "apple")] |
| 219 | #[test] |
| 220 | fn names_this_platform_cannot_hold_are_refused() { |
| 221 | let f = Fixture::new(); |
| 222 | let bad = braid_engine::commit( |
| 223 | f.repo.store(), |
| 224 | Vec::new(), |
| 225 | vec![braid_format::Change { |
| 226 | path: b"bad-\xff".to_vec(), |
| 227 | op: f.file("x"), |
| 228 | }], |
| 229 | common::signature(), |
| 230 | common::signature(), |
| 231 | b"m\n".to_vec(), |
| 232 | Vec::new(), |
| 233 | ) |
| 234 | .unwrap(); |
| 235 | let created = Workspace::create(&f.repo, "ws", &f.path("ws"), Some(bad), "main"); |
| 236 | assert!(matches!(created, Err(Error::Unsafe { .. })), "{created:?}"); |
| 237 | } |