2021-06-15 01:20:43 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2020-01-25 18:57:43 +08:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-06-15 01:20:43 +08:00
|
|
|
"errors"
|
2020-01-25 18:57:43 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-31 10:25:40 +08:00
|
|
|
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())
|
2021-06-15 01:20:43 +08:00
|
|
|
}
|
|
|
|
|
2022-03-31 10:25:40 +08:00
|
|
|
func TestSanitizeCredentialURLs(t *testing.T) {
|
2022-01-21 01:46:10 +08:00
|
|
|
cases := []struct {
|
2022-03-31 10:25:40 +08:00
|
|
|
input string
|
|
|
|
expected string
|
2021-06-15 01:20:43 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"https://github.com/go-gitea/test_repo.git",
|
|
|
|
"https://github.com/go-gitea/test_repo.git",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"https://mytoken@github.com/go-gitea/test_repo.git",
|
|
|
|
"https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"https://user:password@github.com/go-gitea/test_repo.git",
|
|
|
|
"https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
|
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"ftp://x@",
|
|
|
|
"ftp://" + userPlaceholder + "@",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"ftp://x/@",
|
|
|
|
"ftp://x/@",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"ftp://u@x/@", // test multiple @ chars
|
|
|
|
"ftp://" + userPlaceholder + "@x/@",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"😊ftp://u@x😊", // test unicode
|
|
|
|
"😊ftp://" + userPlaceholder + "@x😊",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"://@",
|
|
|
|
"://@",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"//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",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"s://u@h", // the minimal pattern to be sanitized
|
|
|
|
"s://" + userPlaceholder + "@h",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 10:25:40 +08:00
|
|
|
"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",
|
2021-06-15 01:20:43 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, c := range cases {
|
2022-03-31 10:25:40 +08:00
|
|
|
result := SanitizeCredentialURLs(c.input)
|
|
|
|
assert.Equal(t, c.expected, result, "case %d: error should match", n)
|
2020-01-25 18:57:43 +08:00
|
|
|
}
|
|
|
|
}
|