2021-06-14 17:20:43 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-25 10:57:43 +00:00
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-06-14 17:20:43 +00:00
|
|
|
"errors"
|
2020-01-25 10:57:43 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-31 02:25:40 +00: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-14 17:20:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 02:25:40 +00:00
|
|
|
func TestSanitizeCredentialURLs(t *testing.T) {
|
2022-01-20 17:46:10 +00:00
|
|
|
cases := []struct {
|
2022-03-31 02:25:40 +00:00
|
|
|
input string
|
|
|
|
expected string
|
2021-06-14 17:20:43 +00: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 02:25:40 +00:00
|
|
|
"ftp://x@",
|
|
|
|
"ftp://" + userPlaceholder + "@",
|
2021-06-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00:00
|
|
|
"ftp://x/@",
|
|
|
|
"ftp://x/@",
|
2021-06-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00:00
|
|
|
"ftp://u@x/@", // test multiple @ chars
|
|
|
|
"ftp://" + userPlaceholder + "@x/@",
|
2021-06-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00:00
|
|
|
"😊ftp://u@x😊", // test unicode
|
|
|
|
"😊ftp://" + userPlaceholder + "@x😊",
|
2021-06-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00:00
|
|
|
"://@",
|
|
|
|
"://@",
|
2021-06-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00: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-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00:00
|
|
|
"s://u@h", // the minimal pattern to be sanitized
|
|
|
|
"s://" + userPlaceholder + "@h",
|
2021-06-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-31 02:25:40 +00: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-14 17:20:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, c := range cases {
|
2022-03-31 02:25:40 +00:00
|
|
|
result := SanitizeCredentialURLs(c.input)
|
|
|
|
assert.Equal(t, c.expected, result, "case %d: error should match", n)
|
2020-01-25 10:57:43 +00:00
|
|
|
}
|
|
|
|
}
|