@@ -2,6 +2,7 @@ package main
22
33import (
44 "fmt"
5+ "net/url"
56 "testing"
67)
78
@@ -43,13 +44,50 @@ func TestScrub(t *testing.T) {
4344
4445func TestFindGit (t * testing.T ) {
4546 t .Run ("finds the second git" , func (t * testing.T ) {
46- if v := findGit ("test/bin1:test/bin2:test/bin3" ); v != "test/bin2/git" {
47+ if v := FindGit ("test/bin1:test/bin2:test/bin3" ); v != "test/bin2/git" {
4748 t .Errorf (v )
4849 }
4950 })
5051 t .Run ("finds nothing" , func (t * testing.T ) {
51- if v := findGit ("test/bin1" ); v != "" {
52+ if v := FindGit ("test/bin1" ); v != "" {
5253 t .Errorf (v )
5354 }
5455 })
5556}
57+
58+ func FuzzScrub (f * testing.F ) {
59+ testcases := []string {
60+ "https://github.com/org/repo" ,
61+ "ssh://[email protected] /org/repo" ,
62+ 63+ "git://github.com/repo" ,
64+ "HEAD~1" ,
65+ "+/refs/HEAD" ,
66+ }
67+ for _ , tc := range testcases {
68+ f .Add (tc )
69+ }
70+ f .Fuzz (func (t * testing.T , orig string ) {
71+ result := Scrub (orig )
72+ if result != orig {
73+ if extractHost (orig , true ) != extractHost (result , false ) {
74+ // transformed a nonURL into a URL or changed what the URL was, which could be an attack
75+ t .Errorf ("Before: %q (%q), after: %q (%q)" , orig , extractHost (orig , true ), result , extractHost (result , false ))
76+ }
77+ }
78+ })
79+ }
80+
81+ func extractHost (input string , orig bool ) string {
82+ u , err := url .ParseRequestURI (input )
83+ if err == nil {
84+ return u .Hostname ()
85+ }
86+ if ! orig {
87+ return ""
88+ }
89+ if scpUrl .MatchString (input ) {
90+ return scpUrl .FindStringSubmatch (input )[2 ]
91+ }
92+ return ""
93+ }
0 commit comments