@@ -6,18 +6,18 @@ package models
6
6
7
7
import (
8
8
"fmt"
9
- "strings"
10
9
"time"
11
10
12
- "github.com/Unknwon/com"
13
- "github.com/go-xorm/xorm"
14
- "gopkg.in/ini.v1"
15
-
16
11
"code.gitea.io/git"
17
12
"code.gitea.io/gitea/modules/log"
18
13
"code.gitea.io/gitea/modules/process"
19
14
"code.gitea.io/gitea/modules/setting"
20
15
"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"
21
21
)
22
22
23
23
// MirrorQueue holds an UniqueQueue object of the mirror
@@ -76,41 +76,41 @@ func (m *Mirror) ScheduleNextUpdate() {
76
76
m .NextUpdate = time .Now ().Add (m .Interval )
77
77
}
78
78
79
+ func remoteAddress (repoPath string ) (string , error ) {
80
+ cfg , err := ini .Load (GitConfigPath (repoPath ))
81
+ if err != nil {
82
+ return "" , err
83
+ }
84
+ return cfg .Section ("remote \" origin\" " ).Key ("url" ).Value (), nil
85
+ }
86
+
79
87
func (m * Mirror ) readAddress () {
80
88
if len (m .address ) > 0 {
81
89
return
82
90
}
83
-
84
- cfg , err := ini . Load (m .Repo .GitConfigPath ())
91
+ var err error
92
+ m . address , err = remoteAddress (m .Repo .RepoPath ())
85
93
if err != nil {
86
- log .Error (4 , "Load: %v" , err )
87
- return
94
+ log .Error (4 , "remoteAddress: %v" , err )
88
95
}
89
- m .address = cfg .Section ("remote \" origin\" " ).Key ("url" ).Value ()
90
96
}
91
97
92
- // HandleCloneUserCredentials replaces user credentials from HTTP/HTTPS URL
93
- // with placeholder <credentials>.
94
- // It will fail for any other forms of clone addresses.
95
- func HandleCloneUserCredentials (url string , mosaics bool ) string {
96
- i := strings .Index (url , "@" )
97
- if i == - 1 {
98
- return url
99
- }
100
- start := strings .Index (url , "://" )
101
- if start == - 1 {
102
- return url
103
- }
104
- if mosaics {
105
- return url [:start + 3 ] + "<credentials>" + url [i :]
98
+ // sanitizeOutput sanitizes output of a command, replacing occurrences of the
99
+ // repository's remote address with a sanitized version.
100
+ func sanitizeOutput (output , repoPath string ) (string , error ) {
101
+ remoteAddr , err := remoteAddress (repoPath )
102
+ if err != nil {
103
+ // if we're unable to load the remote address, then we're unable to
104
+ // sanitize.
105
+ return "" , err
106
106
}
107
- return url [: start + 3 ] + url [ i + 1 :]
107
+ return util . SanitizeMessage ( output , remoteAddr ), nil
108
108
}
109
109
110
110
// Address returns mirror address from Git repository config without credentials.
111
111
func (m * Mirror ) Address () string {
112
112
m .readAddress ()
113
- return HandleCloneUserCredentials (m .address , false )
113
+ return util . SanitizeURLCredentials (m .address , false )
114
114
}
115
115
116
116
// FullAddress returns mirror address from Git repository config.
@@ -145,7 +145,14 @@ func (m *Mirror) runSync() bool {
145
145
if _ , stderr , err := process .GetManager ().ExecDir (
146
146
timeout , repoPath , fmt .Sprintf ("Mirror.runSync: %s" , repoPath ),
147
147
"git" , gitArgs ... ); err != nil {
148
- desc := fmt .Sprintf ("Failed to update mirror repository '%s': %s" , repoPath , stderr )
148
+ // sanitize the output, since it may contain the remote address, which may
149
+ // contain a password
150
+ message , err := sanitizeOutput (stderr , repoPath )
151
+ if err != nil {
152
+ log .Error (4 , "sanitizeOutput: %v" , err )
153
+ return false
154
+ }
155
+ desc := fmt .Sprintf ("Failed to update mirror repository '%s': %s" , repoPath , message )
149
156
log .Error (4 , desc )
150
157
if err = CreateRepositoryNotice (desc ); err != nil {
151
158
log .Error (4 , "CreateRepositoryNotice: %v" , err )
@@ -170,7 +177,14 @@ func (m *Mirror) runSync() bool {
170
177
if _ , stderr , err := process .GetManager ().ExecDir (
171
178
timeout , wikiPath , fmt .Sprintf ("Mirror.runSync: %s" , wikiPath ),
172
179
"git" , "remote" , "update" , "--prune" ); err != nil {
173
- desc := fmt .Sprintf ("Failed to update mirror wiki repository '%s': %s" , wikiPath , stderr )
180
+ // sanitize the output, since it may contain the remote address, which may
181
+ // contain a password
182
+ message , err := sanitizeOutput (stderr , wikiPath )
183
+ if err != nil {
184
+ log .Error (4 , "sanitizeOutput: %v" , err )
185
+ return false
186
+ }
187
+ desc := fmt .Sprintf ("Failed to update mirror wiki repository '%s': %s" , wikiPath , message )
174
188
log .Error (4 , desc )
175
189
if err = CreateRepositoryNotice (desc ); err != nil {
176
190
log .Error (4 , "CreateRepositoryNotice: %v" , err )
0 commit comments