-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56b05a9
commit ea2873b
Showing
2 changed files
with
56 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package main | |
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
|
@@ -43,13 +44,50 @@ func TestScrub(t *testing.T) { | |
|
||
func TestFindGit(t *testing.T) { | ||
t.Run("finds the second git", func(t *testing.T) { | ||
if v := findGit("test/bin1:test/bin2:test/bin3"); v != "test/bin2/git" { | ||
if v := FindGit("test/bin1:test/bin2:test/bin3"); v != "test/bin2/git" { | ||
t.Errorf(v) | ||
} | ||
}) | ||
t.Run("finds nothing", func(t *testing.T) { | ||
if v := findGit("test/bin1"); v != "" { | ||
if v := FindGit("test/bin1"); v != "" { | ||
t.Errorf(v) | ||
} | ||
}) | ||
} | ||
|
||
func FuzzScrub(f *testing.F) { | ||
testcases := []string{ | ||
"https://github.com/org/repo", | ||
"ssh://[email protected]/org/repo", | ||
"[email protected]:org/repo", | ||
"git://github.com/repo", | ||
"HEAD~1", | ||
"+/refs/HEAD", | ||
} | ||
for _, tc := range testcases { | ||
f.Add(tc) | ||
} | ||
f.Fuzz(func(t *testing.T, orig string) { | ||
result := Scrub(orig) | ||
if result != orig { | ||
if extractHost(orig, true) != extractHost(result, false) { | ||
// transformed a nonURL into a URL or changed what the URL was, which could be an attack | ||
t.Errorf("Before: %q (%q), after: %q (%q)", orig, extractHost(orig, true), result, extractHost(result, false)) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func extractHost(input string, orig bool) string { | ||
u, err := url.ParseRequestURI(input) | ||
if err == nil { | ||
return u.Hostname() | ||
} | ||
if !orig { | ||
return "" | ||
} | ||
if scpUrl.MatchString(input) { | ||
return scpUrl.FindStringSubmatch(input)[2] | ||
} | ||
return "" | ||
} |