| 1 | use crate::support::*; |
| 2 | use publication::{Error, Limits, Publication, Request, Stop, Usage}; |
| 3 | use std::{ |
| 4 | ffi::{CString, OsStr, OsString}, |
| 5 | fs::{self, File}, |
| 6 | os::unix::{ |
| 7 | ffi::{OsStrExt, OsStringExt}, |
| 8 | io::AsRawFd, |
| 9 | fs::{FileTypeExt, MetadataExt, symlink}, |
| 10 | net::UnixListener, |
| 11 | }, |
| 12 | path::{Path, PathBuf}, |
| 13 | }; |
| 14 | |
| 15 | fn invalid(parent: &Path, leaf: &OsStr) { |
| 16 | let (task, _) = Publication::new(); |
| 17 | let (error, usage, closed) = not_committed(task.publish( |
| 18 | Request { |
| 19 | parent, |
| 20 | leaf, |
| 21 | bytes: b"abc", |
| 22 | limits: Limits::default(), |
| 23 | }, |
| 24 | |_| -> Result<(), Stop<()>> { panic!("invalid path callback") }, |
| 25 | )); |
| 26 | assert!(matches!(error, Error::InvalidPath)); |
| 27 | assert_eq!(usage, Usage::default()); |
| 28 | cleanup(&closed, 0); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn invalid_parent_forms_rejected_before_callback() { |
| 33 | for value in [ |
| 34 | "", |
| 35 | "relative", |
| 36 | "/owned/./x", |
| 37 | "/owned/../x", |
| 38 | "/owned//x", |
| 39 | "/owned/", |
| 40 | "/owned/\0x", |
| 41 | ] { |
| 42 | invalid(Path::new(value), OsStr::new("output")); |
| 43 | } |
| 44 | invalid( |
| 45 | &PathBuf::from(format!("/{}", "x".repeat(4095))), |
| 46 | OsStr::new("output"), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn invalid_leaf_forms_rejected_before_callback() { |
| 52 | for value in ["", ".", "..", "a/b", "/absolute", "nul\0byte"] { |
| 53 | invalid(Path::new("/owned"), OsStr::new(value)); |
| 54 | } |
| 55 | invalid(Path::new("/owned"), OsStr::new(&"x".repeat(256))); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn root_parent_is_lexically_valid_without_opening_root() { |
| 60 | let (task, _) = Publication::new(); |
| 61 | let (error, usage, closed) = not_committed(task.publish( |
| 62 | Request { |
| 63 | parent: Path::new("/"), |
| 64 | leaf: OsStr::new("unused"), |
| 65 | bytes: &[], |
| 66 | limits: Limits::default(), |
| 67 | }, |
| 68 | |_| Err(Stop::<()>::Cancelled), |
| 69 | )); |
| 70 | assert!(matches!(error, Error::Cancelled)); |
| 71 | assert_eq!(usage, Usage::default()); |
| 72 | cleanup(&closed, 0); |
| 73 | } |
| 74 | |
| 75 | fn open_error(parent: &Path, expected: i32) { |
| 76 | let (error, usage, closed) = not_committed(publish( |
| 77 | parent, |
| 78 | OsStr::new("output"), |
| 79 | b"abc", |
| 80 | Limits::default(), |
| 81 | )); |
| 82 | match error { |
| 83 | Error::Io { operation, source } => { |
| 84 | assert_eq!(operation, "openat2"); |
| 85 | assert_eq!(source.raw_os_error(), Some(expected)); |
| 86 | } |
| 87 | other => panic!("unexpected: {other:?}"), |
| 88 | } |
| 89 | assert_eq!(usage.open_calls, 1); |
| 90 | assert_eq!( |
| 91 | usage.metadata_calls + usage.link_calls + usage.write_calls + usage.read_calls, |
| 92 | 0 |
| 93 | ); |
| 94 | cleanup(&closed, 0); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn missing_and_regular_parent_preserve_errno() { |
| 99 | let sandbox = Sandbox::new(); |
| 100 | open_error(&sandbox.path("missing"), libc::ENOENT); |
| 101 | let regular = sandbox.file("regular", b"untouched"); |
| 102 | open_error(®ular, libc::ENOTDIR); |
| 103 | assert_eq!(fs::read(regular).unwrap(), b"untouched"); |
| 104 | assert!(names(&sandbox.parent()).is_empty()); |
| 105 | sandbox.finish(); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn parent_and_ancestor_symlinks_rejected() { |
| 110 | let sandbox = Sandbox::new(); |
| 111 | let link = sandbox.path("alias"); |
| 112 | symlink(sandbox.parent(), &link).unwrap(); |
| 113 | open_error(&link, libc::ELOOP); |
| 114 | let ancestor = sandbox.path("ancestor"); |
| 115 | symlink(&sandbox.root, &ancestor).unwrap(); |
| 116 | open_error(&ancestor.join("parent"), libc::ELOOP); |
| 117 | assert!(names(&sandbox.parent()).is_empty()); |
| 118 | sandbox.finish(); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn non_utf8_leaf_and_parent_are_supported() { |
| 123 | let sandbox = Sandbox::new(); |
| 124 | let parent_name = OsString::from_vec(vec![b'p', 0xfe]); |
| 125 | let parent = sandbox.dir(Path::new(&parent_name)); |
| 126 | let leaf = OsString::from_vec(vec![b'o', 0xff]); |
| 127 | committed( |
| 128 | publish(&parent, &leaf, b"abc", Limits::default()), |
| 129 | &parent, |
| 130 | &leaf, |
| 131 | b"abc", |
| 132 | ); |
| 133 | sandbox.finish(); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn maximum_leaf_length_is_supported() { |
| 138 | let sandbox = Sandbox::new(); |
| 139 | let leaf = OsString::from("x".repeat(255)); |
| 140 | let parent = sandbox.parent(); |
| 141 | committed( |
| 142 | publish(&parent, &leaf, b"abc", Limits::default()), |
| 143 | &parent, |
| 144 | &leaf, |
| 145 | b"abc", |
| 146 | ); |
| 147 | sandbox.finish(); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn linux_backslash_and_colon_are_leaf_bytes() { |
| 152 | let sandbox = Sandbox::new(); |
| 153 | let parent = sandbox.parent(); |
| 154 | let leaf = OsStr::new("a\\b:c"); |
| 155 | committed( |
| 156 | publish(&parent, leaf, b"abc", Limits::default()), |
| 157 | &parent, |
| 158 | leaf, |
| 159 | b"abc", |
| 160 | ); |
| 161 | sandbox.finish(); |
| 162 | } |
| 163 | |
| 164 | #[derive(Clone, Copy)] |
| 165 | enum Existing { |
| 166 | Regular, |
| 167 | Directory, |
| 168 | Symlink, |
| 169 | Dangling, |
| 170 | HardLink, |
| 171 | Fifo, |
| 172 | Socket, |
| 173 | } |
| 174 | |
| 175 | fn bind_socket(output: &Path) -> UnixListener { |
| 176 | // 디렉터리 핸들로 긴 절대 경로를 소켓 주소에서 제외함. |
| 177 | let parent = File::open(output.parent().unwrap()).unwrap(); |
| 178 | let address = format!( |
| 179 | "/proc/self/fd/{}/{}", |
| 180 | parent.as_raw_fd(), |
| 181 | output.file_name().unwrap().to_string_lossy() |
| 182 | ); |
| 183 | UnixListener::bind(address).unwrap() |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn socket_conflict_preserves_output_beyond_absolute_address_limit() { |
| 188 | let sandbox = Sandbox::new(); |
| 189 | let padding = 108usize |
| 190 | .saturating_sub(sandbox.root.as_os_str().as_bytes().len() + "/output".len()) |
| 191 | .max(1); |
| 192 | let parent = sandbox.dir("p".repeat(padding)); |
| 193 | let output = parent.join("output"); |
| 194 | assert!(output.as_os_str().as_bytes().len() >= 108); |
| 195 | let listener = bind_socket(&output); |
| 196 | let before = fs::symlink_metadata(&output).unwrap(); |
| 197 | assert!(before.file_type().is_socket()); |
| 198 | conflict( |
| 199 | publish( |
| 200 | &parent, |
| 201 | OsStr::new("output"), |
| 202 | b"replacement", |
| 203 | Limits::default(), |
| 204 | ), |
| 205 | 11, |
| 206 | ); |
| 207 | let after = fs::symlink_metadata(&output).unwrap(); |
| 208 | assert!(after.file_type().is_socket()); |
| 209 | assert_eq!((before.dev(), before.ino()), (after.dev(), after.ino())); |
| 210 | assert_eq!(names(&parent), vec![OsString::from("output")]); |
| 211 | drop(listener); |
| 212 | sandbox.finish(); |
| 213 | } |
| 214 | |
| 215 | fn existing(kind: Existing) { |
| 216 | let sandbox = Sandbox::new(); |
| 217 | let parent = sandbox.parent(); |
| 218 | let leaf = OsStr::new("output"); |
| 219 | let output = parent.join(leaf); |
| 220 | let sentinel = sandbox.file("sentinel", b"untouched"); |
| 221 | let mut listener = None; |
| 222 | match kind { |
| 223 | Existing::Regular => { |
| 224 | sandbox.file("parent/output", b"existing"); |
| 225 | } |
| 226 | Existing::Directory => { |
| 227 | sandbox.dir("parent/output"); |
| 228 | sandbox.file("parent/output/child", b"child"); |
| 229 | } |
| 230 | Existing::Symlink => symlink(&sentinel, &output).unwrap(), |
| 231 | Existing::Dangling => symlink(sandbox.path("missing"), &output).unwrap(), |
| 232 | Existing::HardLink => fs::hard_link(&sentinel, &output).unwrap(), |
| 233 | Existing::Fifo => { |
| 234 | let raw = CString::new(output.as_os_str().as_bytes()).unwrap(); |
| 235 | // 작업 전용 이름의 합성 FIFO만 생성함. |
| 236 | assert_eq!(unsafe { libc::mkfifo(raw.as_ptr(), 0o600) }, 0); |
| 237 | } |
| 238 | Existing::Socket => { |
| 239 | listener = Some(bind_socket(&output)); |
| 240 | } |
| 241 | } |
| 242 | let before = fs::symlink_metadata(&output).unwrap(); |
| 243 | conflict( |
| 244 | publish(&parent, leaf, b"replacement", Limits::default()), |
| 245 | 11, |
| 246 | ); |
| 247 | let after = fs::symlink_metadata(&output).unwrap(); |
| 248 | assert_eq!( |
| 249 | ( |
| 250 | before.dev(), |
| 251 | before.ino(), |
| 252 | before.mode(), |
| 253 | before.nlink(), |
| 254 | before.len() |
| 255 | ), |
| 256 | ( |
| 257 | after.dev(), |
| 258 | after.ino(), |
| 259 | after.mode(), |
| 260 | after.nlink(), |
| 261 | after.len() |
| 262 | ) |
| 263 | ); |
| 264 | assert_eq!(fs::read(&sentinel).unwrap(), b"untouched"); |
| 265 | match kind { |
| 266 | Existing::Regular => assert_eq!(fs::read(&output).unwrap(), b"existing"), |
| 267 | Existing::Directory => { |
| 268 | assert_eq!(names(&output), vec![OsString::from("child")]); |
| 269 | assert_eq!(fs::read(output.join("child")).unwrap(), b"child"); |
| 270 | } |
| 271 | Existing::Symlink => assert_eq!(fs::read_link(&output).unwrap(), sentinel), |
| 272 | Existing::Dangling => assert_eq!(fs::read_link(&output).unwrap(), sandbox.path("missing")), |
| 273 | Existing::HardLink => assert_eq!(fs::read(&output).unwrap(), b"untouched"), |
| 274 | Existing::Fifo | Existing::Socket => {} |
| 275 | } |
| 276 | assert_eq!(names(&parent), vec![leaf.to_os_string()]); |
| 277 | drop(listener); |
| 278 | sandbox.finish(); |
| 279 | } |
| 280 | |
| 281 | #[test] |
| 282 | fn existing_regular_file_preserved() { |
| 283 | existing(Existing::Regular); |
| 284 | } |
| 285 | #[test] |
| 286 | fn existing_directory_and_child_preserved() { |
| 287 | existing(Existing::Directory); |
| 288 | } |
| 289 | #[test] |
| 290 | fn existing_symlink_and_target_preserved() { |
| 291 | existing(Existing::Symlink); |
| 292 | } |
| 293 | #[test] |
| 294 | fn dangling_symlink_preserved() { |
| 295 | existing(Existing::Dangling); |
| 296 | } |
| 297 | #[test] |
| 298 | fn existing_hard_links_preserved() { |
| 299 | existing(Existing::HardLink); |
| 300 | } |
| 301 | #[test] |
| 302 | fn existing_fifo_preserved_without_opening_it() { |
| 303 | existing(Existing::Fifo); |
| 304 | } |
| 305 | #[test] |
| 306 | fn existing_socket_preserved_without_accepting_connections() { |
| 307 | existing(Existing::Socket); |
| 308 | } |