diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2023-03-24 08:46:13 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2023-03-24 08:46:13 +0200 |
commit | 8b21be5e14f44c4f9b0d995ed73709bdde30028f (patch) | |
tree | e3f99c2f7f4f1d060a2cea293ea816a33131dece /src | |
parent | a62fa84b0bb5abe3a2c52a047f47b6403ffcd54c (diff) |
Added checking for a valid path, but the test is failing for the wrong reason
Diffstat (limited to 'src')
-rw-r--r-- | src/git.rs | 38 | ||||
-rw-r--r-- | src/lib.rs | 2 |
2 files changed, 34 insertions, 6 deletions
@@ -3,19 +3,42 @@ use crate::{ ShackleError, }; use git2::{Repository, RepositoryInitOptions}; -use std::{path::PathBuf, process::Command}; +use std::{ + path::{Path, PathBuf}, + process::Command, +}; use user_info::get_username; pub struct GitInitResult { pub path: PathBuf, } -pub fn init(repo_name: &str) -> Result<GitInitResult, ShackleError> { +fn personal_git_dir() -> Result<PathBuf, ShackleError> { let username = get_username().ok_or(ShackleError::UserReadError)?; - let mut path = PathBuf::from("git"); - path.push(username); - path.push(repo_name); - path.set_extension("git"); + Ok(Path::new("git").join(username)) +} + +fn is_valid_personal_git_repo(path: &PathBuf) -> Result<bool, ShackleError> { + let valid_dir = personal_git_dir()?.canonicalize()?; + let canonical_path = path.canonicalize()?; + let relative_path = match canonical_path.strip_prefix(&valid_dir) { + Ok(relative_path) => relative_path, + Err(_) => { + return Ok(false); + } + }; + + if relative_path.parent() != Some(Path::new("")) + || relative_path.extension().map(|ext| ext == "git") != Some(true) + { + return Ok(false); + } + + Ok(true) +} + +pub fn init(repo_name: &str) -> Result<GitInitResult, ShackleError> { + let path = personal_git_dir()?.join(repo_name).with_extension("git"); Repository::init_opts( &path, @@ -29,6 +52,9 @@ pub fn init(repo_name: &str) -> Result<GitInitResult, ShackleError> { pub fn upload_pack(upload_pack_args: &GitUploadPackArgs) -> Result<(), ShackleError> { let mut command = Command::new("git-upload-pack"); + if !is_valid_personal_git_repo(&upload_pack_args.directory)? { + return Err(ShackleError::InvalidDirectory); + } if upload_pack_args.strict { command.arg("strict"); @@ -39,4 +39,6 @@ pub enum ShackleError { ReadlineError(#[from] ReadlineError), #[error("Could not get the current user name")] UserReadError, + #[error("Path is not accessible")] + InvalidDirectory, } |