Skip to content

Commit e57f763

Browse files
mrsdizzielunny
authored andcommitted
Add migration to sanitize repository original_url (#9423)
* Add migration to sanitize repository original_url During a large code move in #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 #9097 and this migration will remove any authentication details that were stored in the database between those two. * use net/url to rebuild URL instead of strings.Replace * Update models/migrations/migrations.go * changes per lunny * make fmt
1 parent 4147cc9 commit e57f763

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

models/migrations/migrations.go

+2
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+
// v114 -> v115
286+
NewMigration("Remove authentication credentials from stored URL", sanitizeOriginalURL),
285287
}
286288

287289
// Migrate database to current version

models/migrations/v114.go

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

0 commit comments

Comments
 (0)