Skip to content

Commit a67c06c

Browse files
lunnyzeripath
authored andcommitted
Sanitize credentials in mirror form (#9975)
1 parent 5b17bb8 commit a67c06c

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

models/repo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ type Repository struct {
197197
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
198198
}
199199

200+
// SanitizedOriginalURL returns a sanitized OriginalURL
201+
func (repo *Repository) SanitizedOriginalURL() string {
202+
if repo.OriginalURL == "" {
203+
return ""
204+
}
205+
return util.SanitizeURLCredentials(repo.OriginalURL, false)
206+
}
207+
200208
// ColorFormat returns a colored string to represent this repo
201209
func (repo *Repository) ColorFormat(s fmt.State) {
202210
var ownerName interface{}

modules/util/sanitize.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package util
77
import (
88
"net/url"
99
"strings"
10+
11+
"code.gitea.io/gitea/modules/log"
1012
)
1113

1214
// urlSafeError wraps an error whose message may contain a sensitive URL
@@ -36,6 +38,7 @@ func SanitizeMessage(message, unsanitizedURL string) string {
3638
func SanitizeURLCredentials(unsanitizedURL string, usePlaceholder bool) string {
3739
u, err := url.Parse(unsanitizedURL)
3840
if err != nil {
41+
log.Error("parse url %s failed: %v", unsanitizedURL, err)
3942
// don't log the error, since it might contain unsanitized URL.
4043
return "(unparsable url)"
4144
}

modules/util/sanitize_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestSanitizeURLCredentials(t *testing.T) {
14+
var kases = map[string]string{
15+
"https://github.com/go-gitea/test_repo.git": "https://github.com/go-gitea/test_repo.git",
16+
"https://[email protected]/go-gitea/test_repo.git": "https://github.com/go-gitea/test_repo.git",
17+
"http://github.com/go-gitea/test_repo.git": "http://github.com/go-gitea/test_repo.git",
18+
"/test/repos/repo1": "/test/repos/repo1",
19+
"[email protected]:go-gitea/test_repo.git": "(unparsable url)",
20+
}
21+
22+
for source, value := range kases {
23+
assert.EqualValues(t, value, SanitizeURLCredentials(source, false))
24+
}
25+
}

templates/repo/header.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{{if and .RelAvatarLink .IsPrivate}}<i class="mega-octicon octicon-lock"></i>{{end}}
1515
{{if .IsTemplate}}<i class="icon fa-copy"></i>{{end}}
1616
{{if .IsArchived}}<i class="archive icon archived-icon"></i>{{end}}
17-
{{if .IsMirror}}<div class="fork-flag">{{$.i18n.Tr "repo.mirror_from"}} <a target="_blank" rel="noopener noreferrer" href="{{MirrorAddress $.Mirror}}">{{MirrorAddress $.Mirror}}</a></div>{{end}}
17+
{{if .IsMirror}}<div class="fork-flag">{{$.i18n.Tr "repo.mirror_from"}} <a target="_blank" rel="noopener noreferrer" href="{{if .SanitizedOriginalURL}}{{.SanitizedOriginalURL}}{{else}}{{MirrorAddress $.Mirror}}{{end}}">{{if .SanitizedOriginalURL}}{{.SanitizedOriginalURL}}{{else}}{{MirrorAddress $.Mirror}}{{end}}</a></div>{{end}}
1818
{{if .IsFork}}<div class="fork-flag">{{$.i18n.Tr "repo.forked_from"}} <a href="{{.BaseRepo.Link}}">{{SubStr .BaseRepo.RelLink 1 -1}}</a></div>{{end}}
1919
{{if .IsGenerated}}<div class="fork-flag">{{$.i18n.Tr "repo.generated_from"}} <a href="{{.TemplateRepo.Link}}">{{SubStr .TemplateRepo.RelLink 1 -1}}</a></div>{{end}}
2020
</div>

0 commit comments

Comments
 (0)