use forge_git::GitService; #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum ParseError { #[error("unsupported command")] UnsupportedCommand, #[error("malformed repository path")] MalformedPath, } const PREFIXES: &[(&str, GitService)] = &[ ("git-upload-pack ", GitService::UploadPack), ("git-receive-pack ", GitService::ReceivePack), ("git upload-pack ", GitService::UploadPack), ("git receive-pack ", GitService::ReceivePack), ]; #[derive(Debug, Clone, PartialEq, Eq)] pub struct RepoPath { pub owner: String, pub name: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum ExecCommand { Git(GitService, RepoPath), LfsAuthenticate(RepoPath, LfsOperation), } pub(crate) fn parse_command(cmd: &str) -> Result { if cmd.starts_with(LFS_AUTHENTICATE_PREFIX) { let (path, operation) = parse_lfs_authenticate(cmd)?; return Ok(ExecCommand::LfsAuthenticate(path, operation)); } let (service, path) = parse_exec(cmd)?; Ok(ExecCommand::Git(service, path)) } fn parse_exec(cmd: &str) -> Result<(GitService, RepoPath), ParseError> { let (service, rest) = split_service(cmd)?; let raw = unquote_argument(rest)?; let path = normalize_path(raw)?; Ok((service, path)) } const LFS_AUTHENTICATE_PREFIX: &str = "git-lfs-authenticate "; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum LfsOperation { Download, Upload, } fn parse_lfs_authenticate(cmd: &str) -> Result<(RepoPath, LfsOperation), ParseError> { let rest = cmd .strip_prefix(LFS_AUTHENTICATE_PREFIX) .ok_or(ParseError::UnsupportedCommand)?; let (arg, op) = rest.rsplit_once(' ').ok_or(ParseError::MalformedPath)?; let operation = match op { "download" => LfsOperation::Download, "upload" => LfsOperation::Upload, _ => return Err(ParseError::MalformedPath), }; let path = normalize_path(unquote_argument(arg)?)?; Ok((path, operation)) } fn split_service(cmd: &str) -> Result<(GitService, &str), ParseError> { PREFIXES .iter() .find_map(|(prefix, service)| cmd.strip_prefix(prefix).map(|rest| (*service, rest))) .ok_or(ParseError::UnsupportedCommand) } fn unquote_argument(rest: &str) -> Result<&str, ParseError> { if let Some(quoted) = rest.strip_prefix('\'') { let inner = quoted.strip_suffix('\'').ok_or(ParseError::MalformedPath)?; if inner .chars() .any(|c| matches!(c, '\'' | '\\') || c.is_control()) { return Err(ParseError::MalformedPath); } return Ok(inner); } if rest.is_empty() || rest .chars() .any(|c| c.is_whitespace() || matches!(c, '\'' | '"' | '\\') || c.is_control()) { return Err(ParseError::MalformedPath); } Ok(rest) } fn normalize_path(raw: &str) -> Result { let path = raw.strip_prefix('/').unwrap_or(raw); let path = path.strip_prefix("~/").unwrap_or(path); let segments: Vec<&str> = path.split('/').collect(); let [owner, repo] = segments.as_slice() else { return Err(ParseError::MalformedPath); }; for segment in [owner, repo] { if segment.is_empty() || *segment == ".." || segment.starts_with('-') || segment.starts_with('.') || !segment .bytes() .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')) { return Err(ParseError::MalformedPath); } } let repo = repo.strip_suffix(".git").unwrap_or(repo); if repo.is_empty() { return Err(ParseError::MalformedPath); } Ok(RepoPath { owner: owner.to_string(), name: repo.to_string(), }) } #[cfg(test)] mod tests { use super::*; fn ok(cmd: &str) -> (GitService, String) { match parse_exec(cmd) { Ok((service, path)) => (service, format!("{}/{}", path.owner, path.name)), Err(e) => panic!("expected {cmd:?} to parse, got {e:?}"), } } fn err(cmd: &str) -> ParseError { match parse_exec(cmd) { Ok(v) => panic!("expected {cmd:?} to be rejected, got {v:?}"), Err(e) => e, } } #[test] fn accepts_valid_exec_commands() { let cases: &[(&str, GitService, &str)] = &[ ( "git-upload-pack '/tahinli/proj.git'", GitService::UploadPack, "tahinli/proj", ), ( "git-receive-pack '/tahinli/proj.git'", GitService::ReceivePack, "tahinli/proj", ), ( "git-receive-pack '/tahinli/proj.wiki.git'", GitService::ReceivePack, "tahinli/proj.wiki", ), ( "git-upload-pack 'tahinli/proj.wiki'", GitService::UploadPack, "tahinli/proj.wiki", ), ( "git upload-pack '/tahinli/proj'", GitService::UploadPack, "tahinli/proj", ), ( "git receive-pack 'tahinli/proj.git'", GitService::ReceivePack, "tahinli/proj", ), ( "git-upload-pack '~/tahinli/proj'", GitService::UploadPack, "tahinli/proj", ), ( "git-upload-pack '/~/tahinli/proj'", GitService::UploadPack, "tahinli/proj", ), ( "git-upload-pack 'tahinli/proj'", GitService::UploadPack, "tahinli/proj", ), ( "git-upload-pack /tahinli/proj.git", GitService::UploadPack, "tahinli/proj", ), ( "git-receive-pack tahinli/proj", GitService::ReceivePack, "tahinli/proj", ), ]; for (cmd, service, path) in cases { assert_eq!(ok(cmd), (*service, path.to_string()), "case {cmd}"); } } #[test] fn rejects_malformed_exec_paths() { let cases: &[&str] = &[ "git-upload-pack '//tahinli/proj'", "git-upload-pack a/b c", "git-upload-pack a/b'", "git-upload-pack a\"b/c", "git-upload-pack a\\b/c", "git-upload-pack '--help'", "git-upload-pack '--upload-pack=touch /x'", "git-upload-pack '-x/y'", "git-upload-pack 'a/-b'", "git-upload-pack 'a/b'; rm -rf /", "git-upload-pack 'a/b' 'c/d'", "git-upload-pack 'a/b$(id)'", "git-upload-pack 'a/b;c'", "git-upload-pack 'a/b|c'", "git-upload-pack 'a/b&c'", "git-upload-pack 'a/b%20c'", "git-upload-pack 'a/é'", "git-upload-pack 'a/b`id`'", "git-upload-pack 'a/b' && id", "git-upload-pack 'a/b\\c'", "git-upload-pack 'a/b\nc'", "git-upload-pack 'a/b\rc'", "git-upload-pack 'a/b\0c'", "git-upload-pack 'a/b\x07c'", "git-upload-pack 'a/b\x7fc'", "git-upload-pack 'a/b'c'", "git-upload-pack '../x/y'", "git-upload-pack 'a/..'", "git-upload-pack '/a/../b'", "git-upload-pack '.hidden/repo'", "git-upload-pack 'owner/.git'", "git-upload-pack 'a/b/c'", "git-upload-pack 'a'", "git-upload-pack 'a/'", "git-upload-pack '/a/b/'", "git-upload-pack '//'", "git-upload-pack ''", "git-upload-pack '/'", "git-upload-pack '~/'", "git-upload-pack '.git'", "git-upload-pack ", "git-upload-pack '/a/b'", "git-upload-pack '/a/b' ", "git-upload-pack '/a/b", ]; for cmd in cases { assert_eq!(err(cmd), ParseError::MalformedPath, "case {cmd:?}"); } } #[test] fn rejects_unknown_commands() { let cases: &[&str] = &[ "git-upload-archive '/a/b'", "git upload-archive '/a/b'", "ls -la", "", "git-upload-packx '/a/b'", "git-upload-pack'/a/b'", "git-upload-pack", "GIT-UPLOAD-PACK '/a/b'", " git-upload-pack '/a/b'", ]; for cmd in cases { assert_eq!(err(cmd), ParseError::UnsupportedCommand, "case {cmd:?}"); } } fn lfs_ok(cmd: &str) -> (String, LfsOperation) { match parse_lfs_authenticate(cmd) { Ok((path, operation)) => (format!("{}/{}", path.owner, path.name), operation), Err(e) => panic!("expected {cmd:?} to parse, got {e:?}"), } } fn lfs_err(cmd: &str) -> ParseError { match parse_lfs_authenticate(cmd) { Ok(v) => panic!("expected {cmd:?} to be rejected, got {v:?}"), Err(e) => e, } } #[test] fn lfs_accepts_quoted_and_bare_paths() { let cases: &[(&str, &str, LfsOperation)] = &[ ( "git-lfs-authenticate '/tahinli/proj.git' download", "tahinli/proj", LfsOperation::Download, ), ( "git-lfs-authenticate 'tahinli/proj' upload", "tahinli/proj", LfsOperation::Upload, ), ( "git-lfs-authenticate /tahinli/proj.git upload", "tahinli/proj", LfsOperation::Upload, ), ]; for (cmd, path, op) in cases { assert_eq!(lfs_ok(cmd), (path.to_string(), *op), "case {cmd}"); } } #[test] fn lfs_rejects_malformed_commands() { let cases: &[&str] = &[ "git-lfs-authenticate 'a/b' push", "git-lfs-authenticate 'a/b' ", "git-lfs-authenticate 'a/b' download extra", "git-lfs-authenticate 'a/b'download", "git-lfs-authenticate 'a/b' Download", "git-lfs-authenticate '--help' download", "git-lfs-authenticate '../x/y' upload", "git-lfs-authenticate 'a/b'; id download", "git-lfs-authenticate 'a/b' 'c' download", "git-lfs-authenticate 'a/b download", "git-lfs-authenticate a/b c download", "git-lfs-authenticate 'a/b/c' download", "git-lfs-authenticate ''", ]; for cmd in cases { assert_eq!(lfs_err(cmd), ParseError::MalformedPath, "case {cmd:?}"); } } #[test] fn lfs_rejects_other_commands() { assert_eq!( lfs_err("git-upload-pack 'a/b' download"), ParseError::UnsupportedCommand ); assert_eq!( lfs_err("git-lfs-authenticate-x 'a/b' download"), ParseError::UnsupportedCommand ); } }