Skip to content

Commit 3c1b1ca

Browse files
ethantkoeniglafriks
authored andcommitted
Fix error message sanitiziation (#3082)
1 parent 5dc37b1 commit 3c1b1ca

File tree

4 files changed

+64
-31
lines changed

4 files changed

+64
-31
lines changed

models/repo_mirror.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ package models
66

77
import (
88
"fmt"
9-
"strings"
109
"time"
1110

12-
"github.com/Unknwon/com"
13-
"github.com/go-xorm/xorm"
14-
"gopkg.in/ini.v1"
15-
1611
"code.gitea.io/git"
1712
"code.gitea.io/gitea/modules/log"
1813
"code.gitea.io/gitea/modules/process"
1914
"code.gitea.io/gitea/modules/setting"
2015
"code.gitea.io/gitea/modules/sync"
16+
"code.gitea.io/gitea/modules/util"
17+
18+
"github.com/Unknwon/com"
19+
"github.com/go-xorm/xorm"
20+
"gopkg.in/ini.v1"
2121
)
2222

2323
// MirrorQueue holds an UniqueQueue object of the mirror
@@ -95,24 +95,6 @@ func (m *Mirror) readAddress() {
9595
}
9696
}
9797

98-
// HandleCloneUserCredentials replaces user credentials from HTTP/HTTPS URL
99-
// with placeholder <credentials>.
100-
// It will fail for any other forms of clone addresses.
101-
func HandleCloneUserCredentials(url string, mosaics bool) string {
102-
i := strings.Index(url, "@")
103-
if i == -1 {
104-
return url
105-
}
106-
start := strings.Index(url, "://")
107-
if start == -1 {
108-
return url
109-
}
110-
if mosaics {
111-
return url[:start+3] + "<credentials>" + url[i:]
112-
}
113-
return url[:start+3] + url[i+1:]
114-
}
115-
11698
// sanitizeOutput sanitizes output of a command, replacing occurrences of the
11799
// repository's remote address with a sanitized version.
118100
func sanitizeOutput(output, repoPath string) (string, error) {
@@ -122,14 +104,13 @@ func sanitizeOutput(output, repoPath string) (string, error) {
122104
// sanitize.
123105
return "", err
124106
}
125-
sanitized := HandleCloneUserCredentials(remoteAddr, true)
126-
return strings.Replace(output, remoteAddr, sanitized, -1), nil
107+
return util.SanitizeMessage(output, remoteAddr), nil
127108
}
128109

129110
// Address returns mirror address from Git repository config without credentials.
130111
func (m *Mirror) Address() string {
131112
m.readAddress()
132-
return HandleCloneUserCredentials(m.address, false)
113+
return util.SanitizeURLCredentials(m.address, false)
133114
}
134115

135116
// FullAddress returns mirror address from Git repository config.

modules/util/sanitize.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2017 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+
"net/url"
9+
"strings"
10+
)
11+
12+
// urlSafeError wraps an error whose message may contain a sensitive URL
13+
type urlSafeError struct {
14+
err error
15+
unsanitizedURL string
16+
}
17+
18+
func (err urlSafeError) Error() string {
19+
return SanitizeMessage(err.err.Error(), err.unsanitizedURL)
20+
}
21+
22+
// URLSanitizedError returns the sanitized version an error whose message may
23+
// contain a sensitive URL
24+
func URLSanitizedError(err error, unsanitizedURL string) error {
25+
return urlSafeError{err: err, unsanitizedURL: unsanitizedURL}
26+
}
27+
28+
// SanitizeMessage sanitizes a message which may contains a sensitive URL
29+
func SanitizeMessage(message, unsanitizedURL string) string {
30+
sanitizedURL := SanitizeURLCredentials(unsanitizedURL, true)
31+
return strings.Replace(message, unsanitizedURL, sanitizedURL, -1)
32+
}
33+
34+
// SanitizeURLCredentials sanitizes a url, either removing user credentials
35+
// or replacing them with a placeholder.
36+
func SanitizeURLCredentials(unsanitizedURL string, usePlaceholder bool) string {
37+
u, err := url.Parse(unsanitizedURL)
38+
if err != nil {
39+
// don't log the error, since it might contain unsanitized URL.
40+
return "(unparsable url)"
41+
}
42+
if u.User != nil && usePlaceholder {
43+
u.User = url.User("<credentials>")
44+
} else {
45+
u.User = nil
46+
}
47+
return u.String()
48+
}

routers/api/v1/repo/repo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import (
99
"net/http"
1010
"strings"
1111

12-
api "code.gitea.io/sdk/gitea"
13-
1412
"code.gitea.io/gitea/models"
1513
"code.gitea.io/gitea/modules/auth"
1614
"code.gitea.io/gitea/modules/context"
1715
"code.gitea.io/gitea/modules/log"
1816
"code.gitea.io/gitea/modules/setting"
1917
"code.gitea.io/gitea/modules/util"
2018
"code.gitea.io/gitea/routers/api/v1/convert"
19+
api "code.gitea.io/sdk/gitea"
2120
)
2221

2322
// Search repositories via options
@@ -327,12 +326,13 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
327326
RemoteAddr: remoteAddr,
328327
})
329328
if err != nil {
329+
err = util.URLSanitizedError(err, remoteAddr)
330330
if repo != nil {
331331
if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
332332
log.Error(4, "DeleteRepository: %v", errDelete)
333333
}
334334
}
335-
ctx.Error(500, "MigrateRepository", models.HandleCloneUserCredentials(err.Error(), true))
335+
ctx.Error(500, "MigrateRepository", err)
336336
return
337337
}
338338

routers/repo/repo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/modules/context"
2121
"code.gitea.io/gitea/modules/log"
2222
"code.gitea.io/gitea/modules/setting"
23+
"code.gitea.io/gitea/modules/util"
2324
)
2425

2526
const (
@@ -232,6 +233,9 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
232233
return
233234
}
234235

236+
// remoteAddr may contain credentials, so we sanitize it
237+
err = util.URLSanitizedError(err, remoteAddr)
238+
235239
if repo != nil {
236240
if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
237241
log.Error(4, "DeleteRepository: %v", errDelete)
@@ -241,11 +245,11 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
241245
if strings.Contains(err.Error(), "Authentication failed") ||
242246
strings.Contains(err.Error(), "could not read Username") {
243247
ctx.Data["Err_Auth"] = true
244-
ctx.RenderWithErr(ctx.Tr("form.auth_failed", models.HandleCloneUserCredentials(err.Error(), true)), tplMigrate, &form)
248+
ctx.RenderWithErr(ctx.Tr("form.auth_failed", err.Error()), tplMigrate, &form)
245249
return
246250
} else if strings.Contains(err.Error(), "fatal:") {
247251
ctx.Data["Err_CloneAddr"] = true
248-
ctx.RenderWithErr(ctx.Tr("repo.migrate.failed", models.HandleCloneUserCredentials(err.Error(), true)), tplMigrate, &form)
252+
ctx.RenderWithErr(ctx.Tr("repo.migrate.failed", err.Error()), tplMigrate, &form)
249253
return
250254
}
251255

0 commit comments

Comments
 (0)