Open the sign-in's folders through core's directory helper
Browse filesauth.rs and setup.rs each opened a folder by hand with O_DIRECTORY and O_NOFOLLOW. gitv_core::open_dir now does it, with the same flags as every other handle-based open in the codebase. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 22:33
- Commit
- 85bbb02f65f1
- Parent
- d57b798
3 files changed +24−12
| @@ -421,6 +421,15 @@ pub fn open_root() -> Option<OwnedFd> { | ||
| open(&repos_root(), OFlag::O_DIRECTORY | OFlag::O_CLOEXEC, Mode::empty()).ok() | ||
| } | ||
| /// The directory at `path`, refused if its last part is a symlink or not a directory, to work | ||
| /// inside by handle. | ||
| /// | ||
| /// # Errors | ||
| /// The open's failure: `ELOOP` for a symlink, `ENOTDIR` for anything else that isn't a folder. | ||
| pub fn open_dir(path: &Path) -> std::io::Result<OwnedFd> { | ||
| Ok(open(path, DIR_FLAGS, Mode::empty())?) | ||
| } | ||
| /// The directory `name` inside `dir`; refused if it is a symlink or not a directory. | ||
| pub fn open_dir_at(dir: &impl AsFd, name: &OsStr) -> nix::Result<OwnedFd> { | ||
| openat(dir, name, DIR_FLAGS, Mode::empty()) | ||
| @@ -681,6 +690,18 @@ mod tests { | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
| #[test] | ||
| fn a_directory_opened_by_path_is_never_a_symlink() { | ||
| let dir = scratch("open-dir"); | ||
| std::fs::create_dir(dir.join("real")).unwrap(); | ||
| std::fs::write(dir.join("file"), "").unwrap(); | ||
| std::os::unix::fs::symlink(dir.join("real"), dir.join("link")).unwrap(); | ||
| assert!(open_dir(&dir.join("real")).is_ok()); | ||
| assert!(open_dir(&dir.join("link")).is_err(), "a symlinked folder is refused"); | ||
| assert!(open_dir(&dir.join("file")).is_err(), "a file is refused"); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
| #[test] | ||
| fn removing_a_tree_never_follows_symlinks() { | ||
| let dir = scratch("remove"); | ||
| @@ -12,7 +12,6 @@ | ||
| //! [RFC 6238]: https://www.rfc-editor.org/rfc/rfc6238 | ||
| use std::fs; | ||
| use std::os::unix::fs::OpenOptionsExt; | ||
| use std::path::{Path, PathBuf}; | ||
| use argon2::Argon2; | ||
| @@ -140,11 +139,7 @@ pub fn lock_for_writing(path: &Path, owner: Owner) -> Result<WriteLock, AuthErro | ||
| let lock_path = sibling(path, "lock"); | ||
| let write_error = |source| AuthError::Write { path: lock_path.clone(), source }; | ||
| let parent = path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")); | ||
| let directory = fs::File::options() | ||
| .read(true) | ||
| .custom_flags(nix::libc::O_DIRECTORY | nix::libc::O_NOFOLLOW) | ||
| .open(parent) | ||
| .map_err(write_error)?; | ||
| let directory = gitv_core::open_dir(parent).map_err(write_error)?; | ||
| let name = lock_path.file_name().expect("sibling has a file name"); | ||
| let flags = OFlag::O_NOFOLLOW | OFlag::O_CLOEXEC | OFlag::O_NONBLOCK | OFlag::O_WRONLY; | ||
| let private = Mode::S_IRUSR | Mode::S_IWUSR; | ||
| @@ -2,7 +2,7 @@ | ||
| //! `gitv-web password` changes its password. Neither touches the running server, which | ||
| //! re-reads the account file whenever it changes. | ||
| use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; | ||
| use std::os::unix::fs::DirBuilderExt; | ||
| use std::os::unix::process::CommandExt; | ||
| use std::path::Path; | ||
| @@ -90,11 +90,7 @@ fn prepare_directory(path: &Path, owner: gitv_web::auth::Owner) -> Result<(), Au | ||
| } | ||
| if let Some((uid, gid)) = owner { | ||
| if created || directory == Path::new("/var/lib/gitv-web") { | ||
| let dir = std::fs::File::options() | ||
| .read(true) | ||
| .custom_flags(nix::libc::O_DIRECTORY | nix::libc::O_NOFOLLOW) | ||
| .open(directory) | ||
| .map_err(write_error)?; | ||
| let dir = gitv_core::open_dir(directory).map_err(write_error)?; | ||
| nix::unistd::fchown(&dir, Some(uid), Some(gid)) | ||
| .map_err(|source| AuthError::Ownership { path: directory.to_owned(), source })?; | ||
| } | ||