Skip to content

Commit 0386b80

Browse files
committed
Add migration to sanitize repository original_url
During a large code move in go-gitea#6200 the OriginalURL field was accidentially changed to be populated with the CloneAddr field which will contain the username and/or password provided during a migration. This behavior was fixed in previous PR go-gitea#9097 and this migration will remove any authentication details that were stored in the database between those two.
1 parent 559fb6c commit 0386b80

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ var migrations = []Migration{
282282
NewMigration("remove release attachments which repository deleted", removeAttachmentMissedRepo),
283283
// v113 -> v114
284284
NewMigration("new feature: change target branch of pull requests", featureChangeTargetBranch),
285+
// v113 -> v114
286+
NewMigration("Remove authentication credentials from stored URL", sanitizeOriginalURL),
285287
}
286288

287289
// Migrate database to current version

models/migrations/v114.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2019 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 migrations
6+
7+
import (
8+
"net/url"
9+
"strings"
10+
11+
"xorm.io/xorm"
12+
)
13+
14+
func sanitizeOriginalURL(x *xorm.Engine) error {
15+
16+
type Repository struct {
17+
ID int64
18+
OriginalURL string `xorm:"VARCHAR(2048)"`
19+
}
20+
21+
sess := x.NewSession()
22+
defer sess.Close()
23+
var last int
24+
const batchSize = 50
25+
for {
26+
var results = make([]Repository, 0, batchSize)
27+
err := x.Where("original_url <> '' AND original_url IS NOT NULL").
28+
And("original_service_type = 0 OR original_service_type IS NULL").
29+
OrderBy("id").
30+
Limit(batchSize, last).
31+
Find(&results)
32+
if err != nil {
33+
return err
34+
}
35+
if len(results) == 0 {
36+
break
37+
}
38+
last += len(results)
39+
40+
for _, res := range results {
41+
u, err := url.Parse(res.OriginalURL)
42+
if err != nil {
43+
// it is ok to continue here, we only care about fixing URLs that we can read
44+
continue
45+
}
46+
47+
if len(u.User.Username()) > 0 {
48+
pass, _ := u.User.Password()
49+
userAuth := u.User.Username() + ":" + pass + "@"
50+
OriginalURL := strings.Replace(res.OriginalURL, userAuth, "", -1)
51+
52+
_, err = x.Exec("UPDATE repository SET original_url = ? WHERE id = ?", OriginalURL, res.ID)
53+
if err != nil {
54+
return err
55+
}
56+
}
57+
}
58+
}
59+
return nil
60+
}

0 commit comments

Comments
 (0)