diff options
Diffstat (limited to 'modules/util/sanitize_test.go')
-rw-r--r-- | modules/util/sanitize_test.go | 139 |
1 files changed, 25 insertions, 114 deletions
diff --git a/modules/util/sanitize_test.go b/modules/util/sanitize_test.go index c141f5e947..78166cfdff 100644 --- a/modules/util/sanitize_test.go +++ b/modules/util/sanitize_test.go @@ -11,154 +11,65 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewSanitizedError(t *testing.T) { - err := errors.New("error while secret on test") - err2 := NewSanitizedError(err) - assert.Equal(t, err.Error(), err2.Error()) - - cases := []struct { - input error - oldnew []string - expected string - }{ - // case 0 - { - errors.New("error while secret on test"), - []string{"secret", "replaced"}, - "error while replaced on test", - }, - // case 1 - { - errors.New("error while sec-ret on test"), - []string{"secret", "replaced"}, - "error while sec-ret on test", - }, - } - - for n, c := range cases { - err := NewSanitizedError(c.input, c.oldnew...) - - assert.Equal(t, c.expected, err.Error(), "case %d: error should match", n) - } +func TestSanitizeErrorCredentialURLs(t *testing.T) { + err := errors.New("error with https://a@b.com") + se := SanitizeErrorCredentialURLs(err) + assert.Equal(t, "error with https://"+userPlaceholder+"@b.com", se.Error()) } -func TestNewStringURLSanitizer(t *testing.T) { +func TestSanitizeCredentialURLs(t *testing.T) { cases := []struct { - input string - placeholder bool - expected string + input string + expected string }{ - // case 0 { "https://github.com/go-gitea/test_repo.git", - true, "https://github.com/go-gitea/test_repo.git", }, - // case 1 - { - "https://github.com/go-gitea/test_repo.git", - false, - "https://github.com/go-gitea/test_repo.git", - }, - // case 2 { "https://mytoken@github.com/go-gitea/test_repo.git", - true, "https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git", }, - // case 3 - { - "https://mytoken@github.com/go-gitea/test_repo.git", - false, - "https://github.com/go-gitea/test_repo.git", - }, - // case 4 { "https://user:password@github.com/go-gitea/test_repo.git", - true, "https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git", }, - // case 5 { - "https://user:password@github.com/go-gitea/test_repo.git", - false, - "https://github.com/go-gitea/test_repo.git", + "ftp://x@", + "ftp://" + userPlaceholder + "@", }, - // case 6 { - "https://gi\nthub.com/go-gitea/test_repo.git", - false, - unparsableURL, + "ftp://x/@", + "ftp://x/@", }, - } - - for n, c := range cases { - // uses NewURLSanitizer internally - result := NewStringURLSanitizer(c.input, c.placeholder).Replace(c.input) - - assert.Equal(t, c.expected, result, "case %d: error should match", n) - } -} - -func TestNewStringURLSanitizedError(t *testing.T) { - cases := []struct { - input string - placeholder bool - expected string - }{ - // case 0 { - "https://github.com/go-gitea/test_repo.git", - true, - "https://github.com/go-gitea/test_repo.git", - }, - // case 1 - { - "https://github.com/go-gitea/test_repo.git", - false, - "https://github.com/go-gitea/test_repo.git", + "ftp://u@x/@", // test multiple @ chars + "ftp://" + userPlaceholder + "@x/@", }, - // case 2 { - "https://mytoken@github.com/go-gitea/test_repo.git", - true, - "https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git", + "😊ftp://u@x😊", // test unicode + "😊ftp://" + userPlaceholder + "@x😊", }, - // case 3 { - "https://mytoken@github.com/go-gitea/test_repo.git", - false, - "https://github.com/go-gitea/test_repo.git", + "://@", + "://@", }, - // case 4 { - "https://user:password@github.com/go-gitea/test_repo.git", - true, - "https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git", + "//u:p@h", // do not process URLs without explicit scheme, they are not treated as "valid" URLs because there is no scheme context in string + "//u:p@h", }, - // case 5 { - "https://user:password@github.com/go-gitea/test_repo.git", - false, - "https://github.com/go-gitea/test_repo.git", + "s://u@h", // the minimal pattern to be sanitized + "s://" + userPlaceholder + "@h", }, - // case 6 { - "https://gi\nthub.com/go-gitea/test_repo.git", - false, - unparsableURL, + "URLs in log https://u:b@h and https://u:b@h:80/, with https://h.com and u@h.com", + "URLs in log https://" + userPlaceholder + "@h and https://" + userPlaceholder + "@h:80/, with https://h.com and u@h.com", }, } - encloseText := func(input string) string { - return "test " + input + " test" - } - for n, c := range cases { - err := errors.New(encloseText(c.input)) - - result := NewStringURLSanitizedError(err, c.input, c.placeholder) - - assert.Equal(t, encloseText(c.expected), result.Error(), "case %d: error should match", n) + result := SanitizeCredentialURLs(c.input) + assert.Equal(t, c.expected, result, "case %d: error should match", n) } } |