Skip to content

Commit 92f4cd9

Browse files
GiteaBotwolfogre
andauthored
Avoid losing token when updating mirror settings (#30429) (#30464)
Backport #30429 by @wolfogre Fix #30416. Before (it shows as "Unset" while there's a token): <img width="980" alt="image" src="https://github.com/go-gitea/gitea/assets/9418365/d7148e3e-62c9-4d2e-942d-3d795b79515a"> After: <img width="977" alt="image" src="https://github.com/go-gitea/gitea/assets/9418365/24aaa1db-5baa-4204-9081-470b15ea72b5"> The username shows as "oauth2" because of https://github.com/go-gitea/gitea/blob/f9fdac9809335729b2ac3227b2a5f71a62fc64ad/services/migrations/dump.go#L99 I have checked that all usage of `MirrorRemoteAddress` has been updated. <img width="1806" alt="image" src="https://github.com/go-gitea/gitea/assets/9418365/2f042501-2824-4511-9203-c84a6731a02d"> However, it needs to be checked again when backporting. Co-authored-by: Jason Song <i@wolfogre.com>
1 parent 846888f commit 92f4cd9

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

modules/templates/util_misc.go

+21-17
Original file line numberDiff line numberDiff line change
@@ -142,35 +142,39 @@ type remoteAddress struct {
142142
Password string
143143
}
144144

145-
func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteName string, ignoreOriginalURL bool) remoteAddress {
146-
a := remoteAddress{}
147-
148-
remoteURL := m.OriginalURL
149-
if ignoreOriginalURL || remoteURL == "" {
150-
var err error
151-
remoteURL, err = git.GetRemoteAddress(ctx, m.RepoPath(), remoteName)
152-
if err != nil {
153-
log.Error("GetRemoteURL %v", err)
154-
return a
155-
}
145+
func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteName string) remoteAddress {
146+
ret := remoteAddress{}
147+
remoteURL, err := git.GetRemoteAddress(ctx, m.RepoPath(), remoteName)
148+
if err != nil {
149+
log.Error("GetRemoteURL %v", err)
150+
return ret
156151
}
157152

158153
u, err := giturl.Parse(remoteURL)
159154
if err != nil {
160155
log.Error("giturl.Parse %v", err)
161-
return a
156+
return ret
162157
}
163158

164159
if u.Scheme != "ssh" && u.Scheme != "file" {
165160
if u.User != nil {
166-
a.Username = u.User.Username()
167-
a.Password, _ = u.User.Password()
161+
ret.Username = u.User.Username()
162+
ret.Password, _ = u.User.Password()
168163
}
169-
u.User = nil
170164
}
171-
a.Address = u.String()
172165

173-
return a
166+
// The URL stored in the git repo could contain authentication,
167+
// erase it, or it will be shown in the UI.
168+
u.User = nil
169+
ret.Address = u.String()
170+
// Why not use m.OriginalURL to set ret.Address?
171+
// It should be OK to use it, since m.OriginalURL should be the same as the authentication-erased URL from the Git repository.
172+
// However, the old code has already stored authentication in m.OriginalURL when updating mirror settings.
173+
// That means we need to use "giturl.Parse" for m.OriginalURL again to ensure authentication is erased.
174+
// Instead of doing this, why not directly use the authentication-erased URL from the Git repository?
175+
// It should be the same as long as there are no bugs.
176+
177+
return ret
174178
}
175179

176180
func FilenameIsImage(filename string) bool {

services/mirror/mirror_pull.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
system_model "code.gitea.io/gitea/models/system"
1414
"code.gitea.io/gitea/modules/cache"
1515
"code.gitea.io/gitea/modules/git"
16+
giturl "code.gitea.io/gitea/modules/git/url"
1617
"code.gitea.io/gitea/modules/gitrepo"
1718
"code.gitea.io/gitea/modules/lfs"
1819
"code.gitea.io/gitea/modules/log"
@@ -30,10 +31,15 @@ const gitShortEmptySha = "0000000"
3031

3132
// UpdateAddress writes new address to Git repository and database
3233
func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error {
34+
u, err := giturl.Parse(addr)
35+
if err != nil {
36+
return fmt.Errorf("invalid addr: %v", err)
37+
}
38+
3339
remoteName := m.GetRemoteName()
3440
repoPath := m.GetRepository(ctx).RepoPath()
3541
// Remove old remote
36-
_, _, err := git.NewCommand(ctx, "remote", "rm").AddDynamicArguments(remoteName).RunStdString(&git.RunOpts{Dir: repoPath})
42+
_, _, err = git.NewCommand(ctx, "remote", "rm").AddDynamicArguments(remoteName).RunStdString(&git.RunOpts{Dir: repoPath})
3743
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
3844
return err
3945
}
@@ -70,7 +76,9 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error
7076
}
7177
}
7278

73-
m.Repo.OriginalURL = addr
79+
// erase authentication before storing in database
80+
u.User = nil
81+
m.Repo.OriginalURL = u.String()
7482
return repo_model.UpdateRepositoryCols(ctx, m.Repo, "original_url")
7583
}
7684

templates/repo/settings/options.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
<label for="interval">{{ctx.Locale.Tr "repo.mirror_interval" .MinimumMirrorInterval}}</label>
157157
<input id="interval" name="interval" value="{{.PullMirror.Interval}}">
158158
</div>
159-
{{$address := MirrorRemoteAddress $.Context .Repository .PullMirror.GetRemoteName false}}
159+
{{$address := MirrorRemoteAddress $.Context .Repository .PullMirror.GetRemoteName}}
160160
<div class="field {{if .Err_MirrorAddress}}error{{end}}">
161161
<label for="mirror_address">{{ctx.Locale.Tr "repo.mirror_address"}}</label>
162162
<input id="mirror_address" name="mirror_address" value="{{$address.Address}}" required>

0 commit comments

Comments
 (0)