Skip to content

Commit 544ef7d

Browse files
authored
Encrypt migration credentials at rest (#15895) (#16187)
Backport #15895 Storing these credentials is a liability. * Encrypt credentials with SECRET_KEY before persisting to task queue table (they need to be persisted due to the nature of the task queue) - security in depth: helps when attacker has access to DB only, but not app.ini * Delete all credentials (even encrypted) from the task table, once the migration is done, for safety - security in depth: minimizes leaked data if attacker gains access to snapshot of both DB and app.ini
1 parent 5ff807a commit 544ef7d

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

models/task.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"fmt"
99

1010
migration "code.gitea.io/gitea/modules/migrations/base"
11+
"code.gitea.io/gitea/modules/secret"
12+
"code.gitea.io/gitea/modules/setting"
1113
"code.gitea.io/gitea/modules/structs"
1214
"code.gitea.io/gitea/modules/timeutil"
15+
"code.gitea.io/gitea/modules/util"
1316
jsoniter "github.com/json-iterator/go"
1417

1518
"xorm.io/builder"
@@ -110,6 +113,24 @@ func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
110113
if err != nil {
111114
return nil, err
112115
}
116+
117+
// decrypt credentials
118+
if opts.CloneAddrEncrypted != "" {
119+
if opts.CloneAddr, err = secret.DecryptSecret(setting.SecretKey, opts.CloneAddrEncrypted); err != nil {
120+
return nil, err
121+
}
122+
}
123+
if opts.AuthPasswordEncrypted != "" {
124+
if opts.AuthPassword, err = secret.DecryptSecret(setting.SecretKey, opts.AuthPasswordEncrypted); err != nil {
125+
return nil, err
126+
}
127+
}
128+
if opts.AuthTokenEncrypted != "" {
129+
if opts.AuthToken, err = secret.DecryptSecret(setting.SecretKey, opts.AuthTokenEncrypted); err != nil {
130+
return nil, err
131+
}
132+
}
133+
113134
return &opts, nil
114135
}
115136
return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name())
@@ -205,12 +226,31 @@ func createTask(e Engine, task *Task) error {
205226
func FinishMigrateTask(task *Task) error {
206227
task.Status = structs.TaskStatusFinished
207228
task.EndTime = timeutil.TimeStampNow()
229+
230+
// delete credentials when we're done, they're a liability.
231+
conf, err := task.MigrateConfig()
232+
if err != nil {
233+
return err
234+
}
235+
conf.AuthPassword = ""
236+
conf.AuthToken = ""
237+
conf.CloneAddr = util.SanitizeURLCredentials(conf.CloneAddr, true)
238+
conf.AuthPasswordEncrypted = ""
239+
conf.AuthTokenEncrypted = ""
240+
conf.CloneAddrEncrypted = ""
241+
json := jsoniter.ConfigCompatibleWithStandardLibrary
242+
confBytes, err := json.Marshal(conf)
243+
if err != nil {
244+
return err
245+
}
246+
task.PayloadContent = string(confBytes)
247+
208248
sess := x.NewSession()
209249
defer sess.Close()
210250
if err := sess.Begin(); err != nil {
211251
return err
212252
}
213-
if _, err := sess.ID(task.ID).Cols("status", "end_time").Update(task); err != nil {
253+
if _, err := sess.ID(task.ID).Cols("status", "end_time", "payload_content").Update(task); err != nil {
214254
return err
215255
}
216256

modules/migrations/base/options.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import "code.gitea.io/gitea/modules/structs"
1111
// this is for internal usage by migrations module and func who interact with it
1212
type MigrateOptions struct {
1313
// required: true
14-
CloneAddr string `json:"clone_addr" binding:"Required"`
15-
AuthUsername string `json:"auth_username"`
16-
AuthPassword string `json:"auth_password"`
17-
AuthToken string `json:"auth_token"`
14+
CloneAddr string `json:"clone_addr" binding:"Required"`
15+
CloneAddrEncrypted string `json:"clone_addr_encrypted,omitempty"`
16+
AuthUsername string `json:"auth_username"`
17+
AuthPassword string `json:"auth_password,omitempty"`
18+
AuthPasswordEncrypted string `json:"auth_password_encrypted,omitempty"`
19+
AuthToken string `json:"auth_token,omitempty"`
20+
AuthTokenEncrypted string `json:"auth_token_encrypted,omitempty"`
1821
// required: true
1922
UID int `json:"uid" binding:"Required"`
2023
// required: true

modules/task/task.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313
"code.gitea.io/gitea/modules/migrations/base"
1414
"code.gitea.io/gitea/modules/queue"
1515
repo_module "code.gitea.io/gitea/modules/repository"
16+
"code.gitea.io/gitea/modules/secret"
17+
"code.gitea.io/gitea/modules/setting"
1618
"code.gitea.io/gitea/modules/structs"
1719
"code.gitea.io/gitea/modules/timeutil"
20+
"code.gitea.io/gitea/modules/util"
1821
jsoniter "github.com/json-iterator/go"
1922
)
2023

@@ -65,6 +68,24 @@ func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error {
6568

6669
// CreateMigrateTask creates a migrate task
6770
func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.Task, error) {
71+
// encrypt credentials for persistence
72+
var err error
73+
opts.CloneAddrEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.CloneAddr)
74+
if err != nil {
75+
return nil, err
76+
}
77+
opts.CloneAddr = util.SanitizeURLCredentials(opts.CloneAddr, true)
78+
opts.AuthPasswordEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthPassword)
79+
if err != nil {
80+
return nil, err
81+
}
82+
opts.AuthPassword = ""
83+
opts.AuthTokenEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthToken)
84+
if err != nil {
85+
return nil, err
86+
}
87+
opts.AuthToken = ""
88+
6889
json := jsoniter.ConfigCompatibleWithStandardLibrary
6990
bs, err := json.Marshal(&opts)
7091
if err != nil {

0 commit comments

Comments
 (0)