Currently `DeriveKey` is called every time that a secret must be encoded/decoded. Since this function is deterministic, its result can be cached to allow a 250x speedup (the original took less than half a microsecond, so this more of a micro-optimization...). ``` go test -bench=. goos: linux goarch: amd64 pkg: forgejo.org/modules/keying cpu: Intel(R) Core(TM) Ultra 5 125H BenchmarkExpandPRK-18 2071627 564.2 ns/op BenchmarkExpandPRKOnce-18 541438192 2.206 ns/op PASS ok forgejo.org/modules/keying 2.369s ``` ## Other changes - Since the keys can be constructed once, it simplifies a bit the callsites (`keying.TOTP.Encrypt(...)` instead of `keying.DeriveKey(keying.ContextTOTP).Encrypt(...)`) - All `Encrypt`/`Decrypt` calls will panic forever if called before `Init` has been called (current it panics as long as `Init` has not been called) - Calling `Init` twice with different keys will trigger a panic (currently racy) - Calling `Decrypt` with a short ciphertext does not panic anymore (like when calling with long-enough garbage) Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10114 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: oliverpool <git@olivier.pfad.fr> Co-committed-by: oliverpool <git@olivier.pfad.fr>
115 lines
4.0 KiB
Go
115 lines
4.0 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package forgejo_migrations
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
|
|
admin_model "forgejo.org/models/admin"
|
|
"forgejo.org/models/db"
|
|
"forgejo.org/modules/json"
|
|
"forgejo.org/modules/keying"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/migration"
|
|
"forgejo.org/modules/secret"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/modules/structs"
|
|
|
|
"xorm.io/builder"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func init() {
|
|
registerMigration(&Migration{
|
|
Description: "migrate columns of `task` table to store keying material",
|
|
Upgrade: migrateTaskSecrets,
|
|
})
|
|
}
|
|
|
|
func migrateTaskSecrets(x *xorm.Engine) error {
|
|
return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
|
sess := db.GetEngine(ctx)
|
|
|
|
key := keying.MigrateTask
|
|
|
|
oldEncryptionKey := setting.SecretKey
|
|
messages := make([]string, 0, 100)
|
|
ids := make([]int64, 0, 100)
|
|
|
|
err := db.Iterate(ctx, builder.Eq{"type": structs.TaskTypeMigrateRepo}, func(ctx context.Context, bean *admin_model.Task) error {
|
|
var opts migration.MigrateOptions
|
|
err := json.Unmarshal([]byte(bean.PayloadContent), &opts)
|
|
if err != nil {
|
|
messages = append(messages, fmt.Sprintf("task.id=%d, task.doer_id=%d, task.repo_id=%d, task.owner_id=%d: json.Unmarshal(): %v", bean.ID, bean.DoerID, bean.RepoID, bean.OwnerID, err))
|
|
ids = append(ids, bean.ID)
|
|
return nil
|
|
}
|
|
|
|
decryptionError := false
|
|
if opts.CloneAddrEncrypted != "" {
|
|
if opts.CloneAddr, err = secret.DecryptSecret(oldEncryptionKey, opts.CloneAddrEncrypted); err != nil {
|
|
messages = append(messages, fmt.Sprintf("task.id=%d, task.doer_id=%d, task.repo_id=%d, task.owner_id=%d: secret.DecryptSecret(CloneAddrEncrypted): %v", bean.ID, bean.DoerID, bean.RepoID, bean.OwnerID, err))
|
|
ids = append(ids, bean.ID)
|
|
decryptionError = true
|
|
}
|
|
}
|
|
|
|
if opts.AuthPasswordEncrypted != "" {
|
|
if opts.AuthPassword, err = secret.DecryptSecret(oldEncryptionKey, opts.AuthPasswordEncrypted); err != nil {
|
|
messages = append(messages, fmt.Sprintf("task.id=%d, task.doer_id=%d, task.repo_id=%d, task.owner_id=%d: secret.DecryptSecret(AuthPasswordEncrypted): %v", bean.ID, bean.DoerID, bean.RepoID, bean.OwnerID, err))
|
|
ids = append(ids, bean.ID)
|
|
decryptionError = true
|
|
}
|
|
}
|
|
|
|
if opts.AuthTokenEncrypted != "" {
|
|
if opts.AuthToken, err = secret.DecryptSecret(oldEncryptionKey, opts.AuthTokenEncrypted); err != nil {
|
|
messages = append(messages, fmt.Sprintf("task.id=%d, task.doer_id=%d, task.repo_id=%d, task.owner_id=%d: secret.DecryptSecret(AuthTokenEncrypted): %v", bean.ID, bean.DoerID, bean.RepoID, bean.OwnerID, err))
|
|
ids = append(ids, bean.ID)
|
|
decryptionError = true
|
|
}
|
|
}
|
|
|
|
// Don't migrate a task that has a decryption error.
|
|
if decryptionError {
|
|
return nil
|
|
}
|
|
|
|
if opts.CloneAddrEncrypted != "" {
|
|
opts.CloneAddrEncrypted = base64.RawStdEncoding.EncodeToString(key.Encrypt([]byte(opts.CloneAddr), keying.ColumnAndJSONSelectorAndID("payload_content", "clone_addr_encrypted", bean.ID)))
|
|
}
|
|
|
|
if opts.AuthPasswordEncrypted != "" {
|
|
opts.AuthPasswordEncrypted = base64.RawStdEncoding.EncodeToString(key.Encrypt([]byte(opts.AuthPassword), keying.ColumnAndJSONSelectorAndID("payload_content", "auth_password_encrypted", bean.ID)))
|
|
}
|
|
|
|
if opts.AuthTokenEncrypted != "" {
|
|
opts.AuthTokenEncrypted = base64.RawStdEncoding.EncodeToString(key.Encrypt([]byte(opts.AuthToken), keying.ColumnAndJSONSelectorAndID("payload_content", "auth_token_encrypted", bean.ID)))
|
|
}
|
|
|
|
bs, err := json.Marshal(&opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
bean.PayloadContent = string(bs)
|
|
|
|
return bean.UpdateCols(ctx, "payload_content")
|
|
})
|
|
|
|
if err == nil {
|
|
if len(ids) > 0 {
|
|
log.Error("v14a_migrate_task_secrets: The following tasks were found to be corrupted and removed from the database.")
|
|
for _, message := range messages {
|
|
log.Error("v14a_migrate_task_secrets: %s", message)
|
|
}
|
|
|
|
_, err = sess.In("id", ids).NoAutoCondition().NoAutoTime().Delete(&admin_model.Task{})
|
|
}
|
|
}
|
|
return err
|
|
})
|
|
}
|