forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.3 MiB·e910f7e·Tahinli·1 hour ago·361 commits
| 1 | use forge_git::GitService; |
| 2 | |
| 3 | #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] |
| 4 | pub enum ParseError { |
| 5 | #[error("unsupported command")] |
| 6 | UnsupportedCommand, |
| 7 | #[error("malformed repository path")] |
| 8 | MalformedPath, |
| 9 | } |
| 10 | |
| 11 | const PREFIXES: &[(&str, GitService)] = &[ |
| 12 | ("git-upload-pack ", GitService::UploadPack), |
| 13 | ("git-receive-pack ", GitService::ReceivePack), |
| 14 | ("git upload-pack ", GitService::UploadPack), |
| 15 | ("git receive-pack ", GitService::ReceivePack), |
| 16 | ]; |
| 17 | |
| 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | pub struct RepoPath { |
| 20 | pub owner: String, |
| 21 | pub name: String, |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | pub(crate) enum ExecCommand { |
| 26 | Git(GitService, RepoPath), |
| 27 | LfsAuthenticate(RepoPath, LfsOperation), |
| 28 | } |
| 29 | |
| 30 | pub(crate) fn parse_command(cmd: &str) -> Result<ExecCommand, ParseError> { |
| 31 | if cmd.starts_with(LFS_AUTHENTICATE_PREFIX) { |
| 32 | let (path, operation) = parse_lfs_authenticate(cmd)?; |
| 33 | return Ok(ExecCommand::LfsAuthenticate(path, operation)); |
| 34 | } |
| 35 | let (service, path) = parse_exec(cmd)?; |
| 36 | Ok(ExecCommand::Git(service, path)) |
| 37 | } |
| 38 | |
| 39 | fn parse_exec(cmd: &str) -> Result<(GitService, RepoPath), ParseError> { |
| 40 | let (service, rest) = split_service(cmd)?; |
| 41 | let raw = unquote_argument(rest)?; |
| 42 | let path = normalize_path(raw)?; |
| 43 | Ok((service, path)) |
| 44 | } |
| 45 | |
| 46 | const LFS_AUTHENTICATE_PREFIX: &str = "git-lfs-authenticate "; |
| 47 | |
| 48 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 49 | pub enum LfsOperation { |
| 50 | Download, |
| 51 | Upload, |
| 52 | } |
| 53 | |
| 54 | fn parse_lfs_authenticate(cmd: &str) -> Result<(RepoPath, LfsOperation), ParseError> { |
| 55 | let rest = cmd |
| 56 | .strip_prefix(LFS_AUTHENTICATE_PREFIX) |
| 57 | .ok_or(ParseError::UnsupportedCommand)?; |
| 58 | let (arg, op) = rest.rsplit_once(' ').ok_or(ParseError::MalformedPath)?; |
| 59 | let operation = match op { |
| 60 | "download" => LfsOperation::Download, |
| 61 | "upload" => LfsOperation::Upload, |
| 62 | _ => return Err(ParseError::MalformedPath), |
| 63 | }; |
| 64 | let path = normalize_path(unquote_argument(arg)?)?; |
| 65 | Ok((path, operation)) |
| 66 | } |
| 67 | |
| 68 | fn split_service(cmd: &str) -> Result<(GitService, &str), ParseError> { |
| 69 | PREFIXES |
| 70 | .iter() |
| 71 | .find_map(|(prefix, service)| cmd.strip_prefix(prefix).map(|rest| (*service, rest))) |
| 72 | .ok_or(ParseError::UnsupportedCommand) |
| 73 | } |
| 74 | |
| 75 | fn unquote_argument(rest: &str) -> Result<&str, ParseError> { |
| 76 | if let Some(quoted) = rest.strip_prefix('\'') { |
| 77 | let inner = quoted.strip_suffix('\'').ok_or(ParseError::MalformedPath)?; |
| 78 | if inner |
| 79 | .chars() |
| 80 | .any(|c| matches!(c, '\'' | '\\') || c.is_control()) |
| 81 | { |
| 82 | return Err(ParseError::MalformedPath); |
| 83 | } |
| 84 | return Ok(inner); |
| 85 | } |
| 86 | if rest.is_empty() |
| 87 | || rest |
| 88 | .chars() |
| 89 | .any(|c| c.is_whitespace() || matches!(c, '\'' | '"' | '\\') || c.is_control()) |
| 90 | { |
| 91 | return Err(ParseError::MalformedPath); |
| 92 | } |
| 93 | Ok(rest) |
| 94 | } |
| 95 | |
| 96 | fn normalize_path(raw: &str) -> Result<RepoPath, ParseError> { |
| 97 | let path = raw.strip_prefix('/').unwrap_or(raw); |
| 98 | let path = path.strip_prefix("~/").unwrap_or(path); |
| 99 | |
| 100 | let segments: Vec<&str> = path.split('/').collect(); |
| 101 | let [owner, repo] = segments.as_slice() else { |
| 102 | return Err(ParseError::MalformedPath); |
| 103 | }; |
| 104 | for segment in [owner, repo] { |
| 105 | if segment.is_empty() |
| 106 | || *segment == ".." |
| 107 | || segment.starts_with('-') |
| 108 | || segment.starts_with('.') |
| 109 | || !segment |
| 110 | .bytes() |
| 111 | .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')) |
| 112 | { |
| 113 | return Err(ParseError::MalformedPath); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | let repo = repo.strip_suffix(".git").unwrap_or(repo); |
| 118 | if repo.is_empty() { |
| 119 | return Err(ParseError::MalformedPath); |
| 120 | } |
| 121 | Ok(RepoPath { |
| 122 | owner: owner.to_string(), |
| 123 | name: repo.to_string(), |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | #[cfg(test)] |
| 128 | mod tests { |
| 129 | use super::*; |
| 130 | |
| 131 | fn ok(cmd: &str) -> (GitService, String) { |
| 132 | match parse_exec(cmd) { |
| 133 | Ok((service, path)) => (service, format!("{}/{}", path.owner, path.name)), |
| 134 | Err(e) => panic!("expected {cmd:?} to parse, got {e:?}"), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn err(cmd: &str) -> ParseError { |
| 139 | match parse_exec(cmd) { |
| 140 | Ok(v) => panic!("expected {cmd:?} to be rejected, got {v:?}"), |
| 141 | Err(e) => e, |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn accepts_valid_exec_commands() { |
| 147 | let cases: &[(&str, GitService, &str)] = &[ |
| 148 | ( |
| 149 | "git-upload-pack '/tahinli/proj.git'", |
| 150 | GitService::UploadPack, |
| 151 | "tahinli/proj", |
| 152 | ), |
| 153 | ( |
| 154 | "git-receive-pack '/tahinli/proj.git'", |
| 155 | GitService::ReceivePack, |
| 156 | "tahinli/proj", |
| 157 | ), |
| 158 | ( |
| 159 | "git upload-pack '/tahinli/proj'", |
| 160 | GitService::UploadPack, |
| 161 | "tahinli/proj", |
| 162 | ), |
| 163 | ( |
| 164 | "git receive-pack 'tahinli/proj.git'", |
| 165 | GitService::ReceivePack, |
| 166 | "tahinli/proj", |
| 167 | ), |
| 168 | ( |
| 169 | "git-upload-pack '~/tahinli/proj'", |
| 170 | GitService::UploadPack, |
| 171 | "tahinli/proj", |
| 172 | ), |
| 173 | ( |
| 174 | "git-upload-pack '/~/tahinli/proj'", |
| 175 | GitService::UploadPack, |
| 176 | "tahinli/proj", |
| 177 | ), |
| 178 | ( |
| 179 | "git-upload-pack 'tahinli/proj'", |
| 180 | GitService::UploadPack, |
| 181 | "tahinli/proj", |
| 182 | ), |
| 183 | ( |
| 184 | "git-upload-pack /tahinli/proj.git", |
| 185 | GitService::UploadPack, |
| 186 | "tahinli/proj", |
| 187 | ), |
| 188 | ( |
| 189 | "git-receive-pack tahinli/proj", |
| 190 | GitService::ReceivePack, |
| 191 | "tahinli/proj", |
| 192 | ), |
| 193 | ]; |
| 194 | for (cmd, service, path) in cases { |
| 195 | assert_eq!(ok(cmd), (*service, path.to_string()), "case {cmd}"); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn rejects_malformed_exec_paths() { |
| 201 | let cases: &[&str] = &[ |
| 202 | "git-upload-pack '//tahinli/proj'", |
| 203 | "git-upload-pack a/b c", |
| 204 | "git-upload-pack a/b'", |
| 205 | "git-upload-pack a\"b/c", |
| 206 | "git-upload-pack a\\b/c", |
| 207 | "git-upload-pack '--help'", |
| 208 | "git-upload-pack '--upload-pack=touch /x'", |
| 209 | "git-upload-pack '-x/y'", |
| 210 | "git-upload-pack 'a/-b'", |
| 211 | "git-upload-pack 'a/b'; rm -rf /", |
| 212 | "git-upload-pack 'a/b' 'c/d'", |
| 213 | "git-upload-pack 'a/b$(id)'", |
| 214 | "git-upload-pack 'a/b;c'", |
| 215 | "git-upload-pack 'a/b|c'", |
| 216 | "git-upload-pack 'a/b&c'", |
| 217 | "git-upload-pack 'a/b%20c'", |
| 218 | "git-upload-pack 'a/é'", |
| 219 | "git-upload-pack 'a/b`id`'", |
| 220 | "git-upload-pack 'a/b' && id", |
| 221 | "git-upload-pack 'a/b\\c'", |
| 222 | "git-upload-pack 'a/b\nc'", |
| 223 | "git-upload-pack 'a/b\rc'", |
| 224 | "git-upload-pack 'a/b\0c'", |
| 225 | "git-upload-pack 'a/b\x07c'", |
| 226 | "git-upload-pack 'a/b\x7fc'", |
| 227 | "git-upload-pack 'a/b'c'", |
| 228 | "git-upload-pack '../x/y'", |
| 229 | "git-upload-pack 'a/..'", |
| 230 | "git-upload-pack '/a/../b'", |
| 231 | "git-upload-pack '.hidden/repo'", |
| 232 | "git-upload-pack 'owner/.git'", |
| 233 | "git-upload-pack 'a/b/c'", |
| 234 | "git-upload-pack 'a'", |
| 235 | "git-upload-pack 'a/'", |
| 236 | "git-upload-pack '/a/b/'", |
| 237 | "git-upload-pack '//'", |
| 238 | "git-upload-pack ''", |
| 239 | "git-upload-pack '/'", |
| 240 | "git-upload-pack '~/'", |
| 241 | "git-upload-pack '.git'", |
| 242 | "git-upload-pack ", |
| 243 | "git-upload-pack '/a/b'", |
| 244 | "git-upload-pack '/a/b' ", |
| 245 | "git-upload-pack '/a/b", |
| 246 | ]; |
| 247 | for cmd in cases { |
| 248 | assert_eq!(err(cmd), ParseError::MalformedPath, "case {cmd:?}"); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #[test] |
| 253 | fn rejects_unknown_commands() { |
| 254 | let cases: &[&str] = &[ |
| 255 | "git-upload-archive '/a/b'", |
| 256 | "git upload-archive '/a/b'", |
| 257 | "ls -la", |
| 258 | "", |
| 259 | "git-upload-packx '/a/b'", |
| 260 | "git-upload-pack'/a/b'", |
| 261 | "git-upload-pack", |
| 262 | "GIT-UPLOAD-PACK '/a/b'", |
| 263 | " git-upload-pack '/a/b'", |
| 264 | ]; |
| 265 | for cmd in cases { |
| 266 | assert_eq!(err(cmd), ParseError::UnsupportedCommand, "case {cmd:?}"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | fn lfs_ok(cmd: &str) -> (String, LfsOperation) { |
| 271 | match parse_lfs_authenticate(cmd) { |
| 272 | Ok((path, operation)) => (format!("{}/{}", path.owner, path.name), operation), |
| 273 | Err(e) => panic!("expected {cmd:?} to parse, got {e:?}"), |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | fn lfs_err(cmd: &str) -> ParseError { |
| 278 | match parse_lfs_authenticate(cmd) { |
| 279 | Ok(v) => panic!("expected {cmd:?} to be rejected, got {v:?}"), |
| 280 | Err(e) => e, |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn lfs_accepts_quoted_and_bare_paths() { |
| 286 | let cases: &[(&str, &str, LfsOperation)] = &[ |
| 287 | ( |
| 288 | "git-lfs-authenticate '/tahinli/proj.git' download", |
| 289 | "tahinli/proj", |
| 290 | LfsOperation::Download, |
| 291 | ), |
| 292 | ( |
| 293 | "git-lfs-authenticate 'tahinli/proj' upload", |
| 294 | "tahinli/proj", |
| 295 | LfsOperation::Upload, |
| 296 | ), |
| 297 | ( |
| 298 | "git-lfs-authenticate /tahinli/proj.git upload", |
| 299 | "tahinli/proj", |
| 300 | LfsOperation::Upload, |
| 301 | ), |
| 302 | ]; |
| 303 | for (cmd, path, op) in cases { |
| 304 | assert_eq!(lfs_ok(cmd), (path.to_string(), *op), "case {cmd}"); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn lfs_rejects_malformed_commands() { |
| 310 | let cases: &[&str] = &[ |
| 311 | "git-lfs-authenticate 'a/b' push", |
| 312 | "git-lfs-authenticate 'a/b' ", |
| 313 | "git-lfs-authenticate 'a/b' download extra", |
| 314 | "git-lfs-authenticate 'a/b'download", |
| 315 | "git-lfs-authenticate 'a/b' Download", |
| 316 | "git-lfs-authenticate '--help' download", |
| 317 | "git-lfs-authenticate '../x/y' upload", |
| 318 | "git-lfs-authenticate 'a/b'; id download", |
| 319 | "git-lfs-authenticate 'a/b' 'c' download", |
| 320 | "git-lfs-authenticate 'a/b download", |
| 321 | "git-lfs-authenticate a/b c download", |
| 322 | "git-lfs-authenticate 'a/b/c' download", |
| 323 | "git-lfs-authenticate ''", |
| 324 | ]; |
| 325 | for cmd in cases { |
| 326 | assert_eq!(lfs_err(cmd), ParseError::MalformedPath, "case {cmd:?}"); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #[test] |
| 331 | fn lfs_rejects_other_commands() { |
| 332 | assert_eq!( |
| 333 | lfs_err("git-upload-pack 'a/b' download"), |
| 334 | ParseError::UnsupportedCommand |
| 335 | ); |
| 336 | assert_eq!( |
| 337 | lfs_err("git-lfs-authenticate-x 'a/b' download"), |
| 338 | ParseError::UnsupportedCommand |
| 339 | ); |
| 340 | } |
| 341 | } |