forge
Explore
Log in

Don't you dare go Hollow

Ssh command parser tests merge into tables

Browse files at this commit

36ee1fa1f3040fe60b6a1c7ea0fdc513bccca0c9Ahmet Kaan GümüşParent: 7a15066
  • test
    • ssh command parser tests merge into tables
Show full message
✅ test: ssh command parser tests merge into tables

Test count in forge-ssh src went from 20 to 8.
The 18 parse.rs tests are now 6 table-driven tests. Every accept and
reject case is kept and labelled in its assert message.
Server tests are unchanged.
crates/forge-ssh/src/parse.rsmodified+179 -240
@@ -144,184 +144,151 @@ mod tests {
144144 }
145145
146146 #[test]
147- fn accepts_upload_and_receive_pack() {
148- assert_eq!(
149- ok("git-upload-pack '/tahinli/proj.git'"),
150- (GitService::UploadPack, "tahinli/proj".into())
151- );
152- assert_eq!(
153- ok("git-receive-pack '/tahinli/proj.git'"),
154- (GitService::ReceivePack, "tahinli/proj".into())
155- );
156- }
157-
158- #[test]
159- fn wiki_paths_keep_the_wiki_suffix() {
160- assert_eq!(
161- ok("git-receive-pack '/tahinli/proj.wiki.git'"),
162- (GitService::ReceivePack, "tahinli/proj.wiki".to_owned())
163- );
164- assert_eq!(
165- ok("git-upload-pack 'tahinli/proj.wiki'"),
166- (GitService::UploadPack, "tahinli/proj.wiki".to_owned())
167- );
168- }
169-
170- #[test]
171- fn accepts_spaced_form() {
172- assert_eq!(
173- ok("git upload-pack '/tahinli/proj'"),
174- (GitService::UploadPack, "tahinli/proj".into())
175- );
176- assert_eq!(
177- ok("git receive-pack 'tahinli/proj.git'"),
178- (GitService::ReceivePack, "tahinli/proj".into())
179- );
180- }
181-
182- #[test]
183- fn strips_leading_slash_and_tilde() {
184- assert_eq!(ok("git-upload-pack '~/tahinli/proj'").1, "tahinli/proj");
185- assert_eq!(ok("git-upload-pack '/~/tahinli/proj'").1, "tahinli/proj");
186- assert_eq!(ok("git-upload-pack 'tahinli/proj'").1, "tahinli/proj");
187- // Only one leading slash is stripped.
188- assert_eq!(
189- err("git-upload-pack '//tahinli/proj'"),
190- ParseError::MalformedPath
191- );
192- }
193-
194- #[test]
195- fn unquoted_path_without_spaces_is_accepted() {
196- assert_eq!(
197- ok("git-upload-pack /tahinli/proj.git"),
198- (GitService::UploadPack, "tahinli/proj".into())
199- );
200- assert_eq!(
201- ok("git-receive-pack tahinli/proj"),
202- (GitService::ReceivePack, "tahinli/proj".into())
203- );
204- }
205-
206- #[test]
207- fn unquoted_path_with_quote_or_space_is_rejected() {
208- assert_eq!(err("git-upload-pack a/b c"), ParseError::MalformedPath);
209- assert_eq!(err("git-upload-pack a/b'"), ParseError::MalformedPath);
210- assert_eq!(err("git-upload-pack a\"b/c"), ParseError::MalformedPath);
211- assert_eq!(err("git-upload-pack a\\b/c"), ParseError::MalformedPath);
212- }
213-
214- #[test]
215- fn rejects_option_injection() {
216- assert_eq!(err("git-upload-pack '--help'"), ParseError::MalformedPath);
217- assert_eq!(
218- err("git-upload-pack '--upload-pack=touch /x'"),
219- ParseError::MalformedPath
220- );
221- assert_eq!(err("git-upload-pack '-x/y'"), ParseError::MalformedPath);
222- assert_eq!(err("git-upload-pack 'a/-b'"), ParseError::MalformedPath);
223- }
224-
225- #[test]
226- fn rejects_shell_injection() {
227- assert_eq!(
228- err("git-upload-pack 'a/b'; rm -rf /"),
229- ParseError::MalformedPath
230- );
231- assert_eq!(
232- err("git-upload-pack 'a/b' 'c/d'"),
233- ParseError::MalformedPath
234- );
235- assert_eq!(err("git-upload-pack 'a/b$(id)'"), ParseError::MalformedPath);
236- assert_eq!(err("git-upload-pack 'a/b;c'"), ParseError::MalformedPath);
237- assert_eq!(err("git-upload-pack 'a/b|c'"), ParseError::MalformedPath);
238- assert_eq!(err("git-upload-pack 'a/b&c'"), ParseError::MalformedPath);
239- assert_eq!(err("git-upload-pack 'a/b%20c'"), ParseError::MalformedPath);
240- assert_eq!(err("git-upload-pack 'a/é'"), ParseError::MalformedPath);
241- assert_eq!(err("git-upload-pack 'a/b`id`'"), ParseError::MalformedPath);
242- assert_eq!(
243- err("git-upload-pack 'a/b' && id"),
244- ParseError::MalformedPath
245- );
246- }
247-
248- #[test]
249- fn rejects_quote_backslash_newline_nul_and_control_chars_inside_quotes() {
250- assert_eq!(err("git-upload-pack 'a/b\\c'"), ParseError::MalformedPath);
251- assert_eq!(err("git-upload-pack 'a/b\nc'"), ParseError::MalformedPath);
252- assert_eq!(err("git-upload-pack 'a/b\rc'"), ParseError::MalformedPath);
253- assert_eq!(err("git-upload-pack 'a/b\0c'"), ParseError::MalformedPath);
254- assert_eq!(err("git-upload-pack 'a/b\x07c'"), ParseError::MalformedPath);
255- assert_eq!(err("git-upload-pack 'a/b\x7fc'"), ParseError::MalformedPath);
256- assert_eq!(err("git-upload-pack 'a/b'c'"), ParseError::MalformedPath);
257- }
258-
259- #[test]
260- fn rejects_traversal() {
261- assert_eq!(err("git-upload-pack '../x/y'"), ParseError::MalformedPath);
262- assert_eq!(err("git-upload-pack 'a/..'"), ParseError::MalformedPath);
263- assert_eq!(err("git-upload-pack '/a/../b'"), ParseError::MalformedPath);
264- assert_eq!(
265- err("git-upload-pack '.hidden/repo'"),
266- ParseError::MalformedPath
267- );
268- assert_eq!(
269- err("git-upload-pack 'owner/.git'"),
270- ParseError::MalformedPath
271- );
272- }
273-
274- #[test]
275- fn rejects_wrong_segment_count() {
276- assert_eq!(err("git-upload-pack 'a/b/c'"), ParseError::MalformedPath);
277- assert_eq!(err("git-upload-pack 'a'"), ParseError::MalformedPath);
278- assert_eq!(err("git-upload-pack 'a/'"), ParseError::MalformedPath);
279- assert_eq!(err("git-upload-pack '/a/b/'"), ParseError::MalformedPath);
280- assert_eq!(err("git-upload-pack '//'"), ParseError::MalformedPath);
147+ fn accepts_valid_exec_commands() {
148+ // Covers both services, wiki suffix, spaced form, leading slash and
149+ // tilde stripping, and unquoted paths.
150+ let cases: &[(&str, GitService, &str)] = &[
151+ (
152+ "git-upload-pack '/tahinli/proj.git'",
153+ GitService::UploadPack,
154+ "tahinli/proj",
155+ ),
156+ (
157+ "git-receive-pack '/tahinli/proj.git'",
158+ GitService::ReceivePack,
159+ "tahinli/proj",
160+ ),
161+ (
162+ "git-receive-pack '/tahinli/proj.wiki.git'",
163+ GitService::ReceivePack,
164+ "tahinli/proj.wiki",
165+ ),
166+ (
167+ "git-upload-pack 'tahinli/proj.wiki'",
168+ GitService::UploadPack,
169+ "tahinli/proj.wiki",
170+ ),
171+ (
172+ "git upload-pack '/tahinli/proj'",
173+ GitService::UploadPack,
174+ "tahinli/proj",
175+ ),
176+ (
177+ "git receive-pack 'tahinli/proj.git'",
178+ GitService::ReceivePack,
179+ "tahinli/proj",
180+ ),
181+ (
182+ "git-upload-pack '~/tahinli/proj'",
183+ GitService::UploadPack,
184+ "tahinli/proj",
185+ ),
186+ (
187+ "git-upload-pack '/~/tahinli/proj'",
188+ GitService::UploadPack,
189+ "tahinli/proj",
190+ ),
191+ (
192+ "git-upload-pack 'tahinli/proj'",
193+ GitService::UploadPack,
194+ "tahinli/proj",
195+ ),
196+ (
197+ "git-upload-pack /tahinli/proj.git",
198+ GitService::UploadPack,
199+ "tahinli/proj",
200+ ),
201+ (
202+ "git-receive-pack tahinli/proj",
203+ GitService::ReceivePack,
204+ "tahinli/proj",
205+ ),
206+ ];
207+ for (cmd, service, path) in cases {
208+ assert_eq!(ok(cmd), (*service, path.to_string()), "case {cmd}");
209+ }
281210 }
282211
283212 #[test]
284- fn rejects_empty_values() {
285- assert_eq!(err("git-upload-pack ''"), ParseError::MalformedPath);
286- assert_eq!(err("git-upload-pack '/'"), ParseError::MalformedPath);
287- assert_eq!(err("git-upload-pack '~/'"), ParseError::MalformedPath);
288- assert_eq!(err("git-upload-pack '.git'"), ParseError::MalformedPath);
289- assert_eq!(err("git-upload-pack "), ParseError::MalformedPath);
213+ fn rejects_malformed_exec_paths() {
214+ // Option, shell and traversal injection, bad quoting, control
215+ // characters, wrong segment counts, empty values and stray spaces.
216+ let cases: &[&str] = &[
217+ // Only one leading slash is stripped.
218+ "git-upload-pack '//tahinli/proj'",
219+ // Unquoted paths with a quote, backslash or space.
220+ "git-upload-pack a/b c",
221+ "git-upload-pack a/b'",
222+ "git-upload-pack a\"b/c",
223+ "git-upload-pack a\\b/c",
224+ // Option injection.
225+ "git-upload-pack '--help'",
226+ "git-upload-pack '--upload-pack=touch /x'",
227+ "git-upload-pack '-x/y'",
228+ "git-upload-pack 'a/-b'",
229+ // Shell injection.
230+ "git-upload-pack 'a/b'; rm -rf /",
231+ "git-upload-pack 'a/b' 'c/d'",
232+ "git-upload-pack 'a/b$(id)'",
233+ "git-upload-pack 'a/b;c'",
234+ "git-upload-pack 'a/b|c'",
235+ "git-upload-pack 'a/b&c'",
236+ "git-upload-pack 'a/b%20c'",
237+ "git-upload-pack 'a/é'",
238+ "git-upload-pack 'a/b`id`'",
239+ "git-upload-pack 'a/b' && id",
240+ // Quote, backslash, newline, NUL and control characters inside quotes.
241+ "git-upload-pack 'a/b\\c'",
242+ "git-upload-pack 'a/b\nc'",
243+ "git-upload-pack 'a/b\rc'",
244+ "git-upload-pack 'a/b\0c'",
245+ "git-upload-pack 'a/b\x07c'",
246+ "git-upload-pack 'a/b\x7fc'",
247+ "git-upload-pack 'a/b'c'",
248+ // Traversal and hidden segments.
249+ "git-upload-pack '../x/y'",
250+ "git-upload-pack 'a/..'",
251+ "git-upload-pack '/a/../b'",
252+ "git-upload-pack '.hidden/repo'",
253+ "git-upload-pack 'owner/.git'",
254+ // Wrong segment count.
255+ "git-upload-pack 'a/b/c'",
256+ "git-upload-pack 'a'",
257+ "git-upload-pack 'a/'",
258+ "git-upload-pack '/a/b/'",
259+ "git-upload-pack '//'",
260+ // Empty values.
261+ "git-upload-pack ''",
262+ "git-upload-pack '/'",
263+ "git-upload-pack '~/'",
264+ "git-upload-pack '.git'",
265+ "git-upload-pack ",
266+ // Extra spaces and trailing garbage.
267+ "git-upload-pack '/a/b'",
268+ "git-upload-pack '/a/b' ",
269+ "git-upload-pack '/a/b",
270+ ];
271+ for cmd in cases {
272+ assert_eq!(err(cmd), ParseError::MalformedPath, "case {cmd:?}");
273+ }
290274 }
291275
292276 #[test]
293277 fn rejects_unknown_commands() {
294- assert_eq!(
295- err("git-upload-archive '/a/b'"),
296- ParseError::UnsupportedCommand
297- );
298- assert_eq!(
299- err("git upload-archive '/a/b'"),
300- ParseError::UnsupportedCommand
301- );
302- assert_eq!(err("ls -la"), ParseError::UnsupportedCommand);
303- assert_eq!(err(""), ParseError::UnsupportedCommand);
304- assert_eq!(
305- err("git-upload-packx '/a/b'"),
306- ParseError::UnsupportedCommand
307- );
308- assert_eq!(err("git-upload-pack'/a/b'"), ParseError::UnsupportedCommand);
309- assert_eq!(err("git-upload-pack"), ParseError::UnsupportedCommand);
310- assert_eq!(
311- err("GIT-UPLOAD-PACK '/a/b'"),
312- ParseError::UnsupportedCommand
313- );
314- assert_eq!(
315- err(" git-upload-pack '/a/b'"),
316- ParseError::UnsupportedCommand
317- );
318- }
319-
320- #[test]
321- fn rejects_extra_spaces_and_trailing_garbage() {
322- assert_eq!(err("git-upload-pack '/a/b'"), ParseError::MalformedPath);
323- assert_eq!(err("git-upload-pack '/a/b' "), ParseError::MalformedPath);
324- assert_eq!(err("git-upload-pack '/a/b"), ParseError::MalformedPath);
278+ let cases: &[&str] = &[
279+ "git-upload-archive '/a/b'",
280+ "git upload-archive '/a/b'",
281+ "ls -la",
282+ "",
283+ "git-upload-packx '/a/b'",
284+ "git-upload-pack'/a/b'",
285+ "git-upload-pack",
286+ "GIT-UPLOAD-PACK '/a/b'",
287+ " git-upload-pack '/a/b'",
288+ ];
289+ for cmd in cases {
290+ assert_eq!(err(cmd), ParseError::UnsupportedCommand, "case {cmd:?}");
291+ }
325292 }
326293
327294 fn lfs_ok(cmd: &str) -> (String, LfsOperation) {
@@ -340,78 +307,50 @@ mod tests {
340307
341308 #[test]
342309 fn lfs_accepts_quoted_and_bare_paths() {
343- assert_eq!(
344- lfs_ok("git-lfs-authenticate '/tahinli/proj.git' download"),
345- ("tahinli/proj".into(), LfsOperation::Download)
346- );
347- assert_eq!(
348- lfs_ok("git-lfs-authenticate 'tahinli/proj' upload"),
349- ("tahinli/proj".into(), LfsOperation::Upload)
350- );
351- assert_eq!(
352- lfs_ok("git-lfs-authenticate /tahinli/proj.git upload"),
353- ("tahinli/proj".into(), LfsOperation::Upload)
354- );
355- }
356-
357- #[test]
358- fn lfs_rejects_bad_operation() {
359- assert_eq!(
360- lfs_err("git-lfs-authenticate 'a/b' push"),
361- ParseError::MalformedPath
362- );
363- assert_eq!(
364- lfs_err("git-lfs-authenticate 'a/b' "),
365- ParseError::MalformedPath
366- );
367- assert_eq!(
368- lfs_err("git-lfs-authenticate 'a/b' download extra"),
369- ParseError::MalformedPath
370- );
371- assert_eq!(
372- lfs_err("git-lfs-authenticate 'a/b'download"),
373- ParseError::MalformedPath
374- );
375- assert_eq!(
376- lfs_err("git-lfs-authenticate 'a/b' Download"),
377- ParseError::MalformedPath
378- );
310+ let cases: &[(&str, &str, LfsOperation)] = &[
311+ (
312+ "git-lfs-authenticate '/tahinli/proj.git' download",
313+ "tahinli/proj",
314+ LfsOperation::Download,
315+ ),
316+ (
317+ "git-lfs-authenticate 'tahinli/proj' upload",
318+ "tahinli/proj",
319+ LfsOperation::Upload,
320+ ),
321+ (
322+ "git-lfs-authenticate /tahinli/proj.git upload",
323+ "tahinli/proj",
324+ LfsOperation::Upload,
325+ ),
326+ ];
327+ for (cmd, path, op) in cases {
328+ assert_eq!(lfs_ok(cmd), (path.to_string(), *op), "case {cmd}");
329+ }
379330 }
380331
381332 #[test]
382- fn lfs_rejects_bad_paths() {
383- assert_eq!(
384- lfs_err("git-lfs-authenticate '--help' download"),
385- ParseError::MalformedPath
386- );
387- assert_eq!(
388- lfs_err("git-lfs-authenticate '../x/y' upload"),
389- ParseError::MalformedPath
390- );
391- assert_eq!(
392- lfs_err("git-lfs-authenticate 'a/b'; id download"),
393- ParseError::MalformedPath
394- );
395- assert_eq!(
396- lfs_err("git-lfs-authenticate 'a/b' 'c' download"),
397- ParseError::MalformedPath
398- );
399- assert_eq!(
400- lfs_err("git-lfs-authenticate 'a/b download"),
401- ParseError::MalformedPath
402- );
403- assert_eq!(
404- lfs_err("git-lfs-authenticate a/b c download"),
405- ParseError::MalformedPath
406- );
407- assert_eq!(
408- lfs_err("git-lfs-authenticate 'a/b/c' download"),
409- ParseError::MalformedPath
410- );
411- assert_eq!(
412- lfs_err("git-lfs-authenticate ''"),
413- ParseError::MalformedPath
414- );
333+ fn lfs_rejects_malformed_commands() {
334+ let cases: &[&str] = &[
335+ // Bad operation.
336+ "git-lfs-authenticate 'a/b' push",
337+ "git-lfs-authenticate 'a/b' ",
338+ "git-lfs-authenticate 'a/b' download extra",
339+ "git-lfs-authenticate 'a/b'download",
340+ "git-lfs-authenticate 'a/b' Download",
341+ // Bad paths.
342+ "git-lfs-authenticate '--help' download",
343+ "git-lfs-authenticate '../x/y' upload",
344+ "git-lfs-authenticate 'a/b'; id download",
345+ "git-lfs-authenticate 'a/b' 'c' download",
346+ "git-lfs-authenticate 'a/b download",
347+ "git-lfs-authenticate a/b c download",
348+ "git-lfs-authenticate 'a/b/c' download",
349+ "git-lfs-authenticate ''",
350+ ];
351+ for cmd in cases {
352+ assert_eq!(lfs_err(cmd), ParseError::MalformedPath, "case {cmd:?}");
353+ }
415354 }
416355
417356 #[test]