diff --git a/crates/forge-ssh/src/parse.rs b/crates/forge-ssh/src/parse.rs index f7e0c54..80042eb 100644 --- a/crates/forge-ssh/src/parse.rs +++ b/crates/forge-ssh/src/parse.rs @@ -144,184 +144,151 @@ mod tests { } #[test] - fn accepts_upload_and_receive_pack() { - assert_eq!( - ok("git-upload-pack '/tahinli/proj.git'"), - (GitService::UploadPack, "tahinli/proj".into()) - ); - assert_eq!( - ok("git-receive-pack '/tahinli/proj.git'"), - (GitService::ReceivePack, "tahinli/proj".into()) - ); - } - - #[test] - fn wiki_paths_keep_the_wiki_suffix() { - assert_eq!( - ok("git-receive-pack '/tahinli/proj.wiki.git'"), - (GitService::ReceivePack, "tahinli/proj.wiki".to_owned()) - ); - assert_eq!( - ok("git-upload-pack 'tahinli/proj.wiki'"), - (GitService::UploadPack, "tahinli/proj.wiki".to_owned()) - ); - } - - #[test] - fn accepts_spaced_form() { - assert_eq!( - ok("git upload-pack '/tahinli/proj'"), - (GitService::UploadPack, "tahinli/proj".into()) - ); - assert_eq!( - ok("git receive-pack 'tahinli/proj.git'"), - (GitService::ReceivePack, "tahinli/proj".into()) - ); - } - - #[test] - fn strips_leading_slash_and_tilde() { - assert_eq!(ok("git-upload-pack '~/tahinli/proj'").1, "tahinli/proj"); - assert_eq!(ok("git-upload-pack '/~/tahinli/proj'").1, "tahinli/proj"); - assert_eq!(ok("git-upload-pack 'tahinli/proj'").1, "tahinli/proj"); - // Only one leading slash is stripped. - assert_eq!( - err("git-upload-pack '//tahinli/proj'"), - ParseError::MalformedPath - ); - } - - #[test] - fn unquoted_path_without_spaces_is_accepted() { - assert_eq!( - ok("git-upload-pack /tahinli/proj.git"), - (GitService::UploadPack, "tahinli/proj".into()) - ); - assert_eq!( - ok("git-receive-pack tahinli/proj"), - (GitService::ReceivePack, "tahinli/proj".into()) - ); - } - - #[test] - fn unquoted_path_with_quote_or_space_is_rejected() { - assert_eq!(err("git-upload-pack a/b c"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack a/b'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack a\"b/c"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack a\\b/c"), ParseError::MalformedPath); - } - - #[test] - fn rejects_option_injection() { - assert_eq!(err("git-upload-pack '--help'"), ParseError::MalformedPath); - assert_eq!( - err("git-upload-pack '--upload-pack=touch /x'"), - ParseError::MalformedPath - ); - assert_eq!(err("git-upload-pack '-x/y'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/-b'"), ParseError::MalformedPath); - } - - #[test] - fn rejects_shell_injection() { - assert_eq!( - err("git-upload-pack 'a/b'; rm -rf /"), - ParseError::MalformedPath - ); - assert_eq!( - err("git-upload-pack 'a/b' 'c/d'"), - ParseError::MalformedPath - ); - assert_eq!(err("git-upload-pack 'a/b$(id)'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b;c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b|c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b&c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b%20c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/é'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b`id`'"), ParseError::MalformedPath); - assert_eq!( - err("git-upload-pack 'a/b' && id"), - ParseError::MalformedPath - ); - } - - #[test] - fn rejects_quote_backslash_newline_nul_and_control_chars_inside_quotes() { - assert_eq!(err("git-upload-pack 'a/b\\c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b\nc'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b\rc'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b\0c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b\x07c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b\x7fc'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/b'c'"), ParseError::MalformedPath); - } - - #[test] - fn rejects_traversal() { - assert_eq!(err("git-upload-pack '../x/y'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/..'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '/a/../b'"), ParseError::MalformedPath); - assert_eq!( - err("git-upload-pack '.hidden/repo'"), - ParseError::MalformedPath - ); - assert_eq!( - err("git-upload-pack 'owner/.git'"), - ParseError::MalformedPath - ); - } - - #[test] - fn rejects_wrong_segment_count() { - assert_eq!(err("git-upload-pack 'a/b/c'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack 'a/'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '/a/b/'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '//'"), ParseError::MalformedPath); + fn accepts_valid_exec_commands() { + // Covers both services, wiki suffix, spaced form, leading slash and + // tilde stripping, and unquoted paths. + 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_empty_values() { - assert_eq!(err("git-upload-pack ''"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '/'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '~/'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '.git'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack "), ParseError::MalformedPath); + fn rejects_malformed_exec_paths() { + // Option, shell and traversal injection, bad quoting, control + // characters, wrong segment counts, empty values and stray spaces. + let cases: &[&str] = &[ + // Only one leading slash is stripped. + "git-upload-pack '//tahinli/proj'", + // Unquoted paths with a quote, backslash or space. + "git-upload-pack a/b c", + "git-upload-pack a/b'", + "git-upload-pack a\"b/c", + "git-upload-pack a\\b/c", + // Option injection. + "git-upload-pack '--help'", + "git-upload-pack '--upload-pack=touch /x'", + "git-upload-pack '-x/y'", + "git-upload-pack 'a/-b'", + // Shell injection. + "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", + // Quote, backslash, newline, NUL and control characters inside quotes. + "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'", + // Traversal and hidden segments. + "git-upload-pack '../x/y'", + "git-upload-pack 'a/..'", + "git-upload-pack '/a/../b'", + "git-upload-pack '.hidden/repo'", + "git-upload-pack 'owner/.git'", + // Wrong segment count. + "git-upload-pack 'a/b/c'", + "git-upload-pack 'a'", + "git-upload-pack 'a/'", + "git-upload-pack '/a/b/'", + "git-upload-pack '//'", + // Empty values. + "git-upload-pack ''", + "git-upload-pack '/'", + "git-upload-pack '~/'", + "git-upload-pack '.git'", + "git-upload-pack ", + // Extra spaces and trailing garbage. + "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() { - assert_eq!( - err("git-upload-archive '/a/b'"), - ParseError::UnsupportedCommand - ); - assert_eq!( - err("git upload-archive '/a/b'"), - ParseError::UnsupportedCommand - ); - assert_eq!(err("ls -la"), ParseError::UnsupportedCommand); - assert_eq!(err(""), ParseError::UnsupportedCommand); - assert_eq!( - err("git-upload-packx '/a/b'"), - ParseError::UnsupportedCommand - ); - assert_eq!(err("git-upload-pack'/a/b'"), ParseError::UnsupportedCommand); - assert_eq!(err("git-upload-pack"), ParseError::UnsupportedCommand); - assert_eq!( - err("GIT-UPLOAD-PACK '/a/b'"), - ParseError::UnsupportedCommand - ); - assert_eq!( - err(" git-upload-pack '/a/b'"), - ParseError::UnsupportedCommand - ); - } - - #[test] - fn rejects_extra_spaces_and_trailing_garbage() { - assert_eq!(err("git-upload-pack '/a/b'"), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '/a/b' "), ParseError::MalformedPath); - assert_eq!(err("git-upload-pack '/a/b"), ParseError::MalformedPath); + 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) { @@ -340,78 +307,50 @@ mod tests { #[test] fn lfs_accepts_quoted_and_bare_paths() { - assert_eq!( - lfs_ok("git-lfs-authenticate '/tahinli/proj.git' download"), - ("tahinli/proj".into(), LfsOperation::Download) - ); - assert_eq!( - lfs_ok("git-lfs-authenticate 'tahinli/proj' upload"), - ("tahinli/proj".into(), LfsOperation::Upload) - ); - assert_eq!( - lfs_ok("git-lfs-authenticate /tahinli/proj.git upload"), - ("tahinli/proj".into(), LfsOperation::Upload) - ); - } - - #[test] - fn lfs_rejects_bad_operation() { - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b' push"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b' "), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b' download extra"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b'download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b' Download"), - ParseError::MalformedPath - ); + 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_bad_paths() { - assert_eq!( - lfs_err("git-lfs-authenticate '--help' download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate '../x/y' upload"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b'; id download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b' 'c' download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate a/b c download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate 'a/b/c' download"), - ParseError::MalformedPath - ); - assert_eq!( - lfs_err("git-lfs-authenticate ''"), - ParseError::MalformedPath - ); + fn lfs_rejects_malformed_commands() { + let cases: &[&str] = &[ + // Bad operation. + "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", + // Bad paths. + "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]