|
| 1 | +// Copyright 2022 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 v1_19 //nolint |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/webhook" |
| 11 | + "code.gitea.io/gitea/modules/json" |
| 12 | + "code.gitea.io/gitea/modules/secret" |
| 13 | + "code.gitea.io/gitea/modules/setting" |
| 14 | + api "code.gitea.io/gitea/modules/structs" |
| 15 | + |
| 16 | + "xorm.io/builder" |
| 17 | + "xorm.io/xorm" |
| 18 | +) |
| 19 | + |
| 20 | +func batchProcess[T any](x *xorm.Engine, buf []T, query func(limit, start int) *xorm.Session, process func(*xorm.Session, T) error) error { |
| 21 | + size := cap(buf) |
| 22 | + start := 0 |
| 23 | + for { |
| 24 | + err := query(size, start).Find(&buf) |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + if len(buf) == 0 { |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + err = func() error { |
| 33 | + sess := x.NewSession() |
| 34 | + defer sess.Close() |
| 35 | + if err := sess.Begin(); err != nil { |
| 36 | + return fmt.Errorf("unable to allow start session. Error: %w", err) |
| 37 | + } |
| 38 | + for _, record := range buf { |
| 39 | + if err := process(sess, record); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + } |
| 43 | + return sess.Commit() |
| 44 | + }() |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + if len(buf) < size { |
| 50 | + return nil |
| 51 | + } |
| 52 | + start += size |
| 53 | + buf = buf[:0] |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func AddHeaderAuthorizationEncryptedColWebhook(x *xorm.Engine) error { |
| 58 | + // Add the column to the table |
| 59 | + type Webhook struct { |
| 60 | + ID int64 `xorm:"pk autoincr"` |
| 61 | + Type webhook.HookType `xorm:"VARCHAR(16) 'type'"` |
| 62 | + Meta string `xorm:"TEXT"` // store hook-specific attributes |
| 63 | + |
| 64 | + // HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization() |
| 65 | + HeaderAuthorizationEncrypted string `xorm:"TEXT"` |
| 66 | + } |
| 67 | + err := x.Sync(new(Webhook)) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + // Migrate the matrix webhooks |
| 73 | + |
| 74 | + type MatrixMeta struct { |
| 75 | + HomeserverURL string `json:"homeserver_url"` |
| 76 | + Room string `json:"room_id"` |
| 77 | + MessageType int `json:"message_type"` |
| 78 | + } |
| 79 | + type MatrixMetaWithAccessToken struct { |
| 80 | + MatrixMeta |
| 81 | + AccessToken string `json:"access_token"` |
| 82 | + } |
| 83 | + |
| 84 | + err = batchProcess(x, |
| 85 | + make([]*Webhook, 0, 50), |
| 86 | + func(limit, start int) *xorm.Session { |
| 87 | + return x.Where("type=?", "matrix").OrderBy("id").Limit(limit, start) |
| 88 | + }, |
| 89 | + func(sess *xorm.Session, hook *Webhook) error { |
| 90 | + // retrieve token from meta |
| 91 | + var withToken MatrixMetaWithAccessToken |
| 92 | + err := json.Unmarshal([]byte(hook.Meta), &withToken) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("unable to unmarshal matrix meta for webhook[id=%d]: %w", hook.ID, err) |
| 95 | + } |
| 96 | + if withToken.AccessToken == "" { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + // encrypt token |
| 101 | + authorization := "Bearer " + withToken.AccessToken |
| 102 | + hook.HeaderAuthorizationEncrypted, err = secret.EncryptSecret(setting.SecretKey, authorization) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("unable to encrypt access token for webhook[id=%d]: %w", hook.ID, err) |
| 105 | + } |
| 106 | + |
| 107 | + // remove token from meta |
| 108 | + withoutToken, err := json.Marshal(withToken.MatrixMeta) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("unable to marshal matrix meta for webhook[id=%d]: %w", hook.ID, err) |
| 111 | + } |
| 112 | + hook.Meta = string(withoutToken) |
| 113 | + |
| 114 | + // save in database |
| 115 | + count, err := sess.ID(hook.ID).Cols("meta", "header_authorization_encrypted").Update(hook) |
| 116 | + if count != 1 || err != nil { |
| 117 | + return fmt.Errorf("unable to update header_authorization_encrypted for webhook[id=%d]: %d,%w", hook.ID, count, err) |
| 118 | + } |
| 119 | + return nil |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + // Remove access_token from HookTask |
| 126 | + |
| 127 | + type HookTask struct { |
| 128 | + ID int64 `xorm:"pk autoincr"` |
| 129 | + HookID int64 |
| 130 | + PayloadContent string `xorm:"LONGTEXT"` |
| 131 | + } |
| 132 | + |
| 133 | + type MatrixPayloadSafe struct { |
| 134 | + Body string `json:"body"` |
| 135 | + MsgType string `json:"msgtype"` |
| 136 | + Format string `json:"format"` |
| 137 | + FormattedBody string `json:"formatted_body"` |
| 138 | + Commits []*api.PayloadCommit `json:"io.gitea.commits,omitempty"` |
| 139 | + } |
| 140 | + type MatrixPayloadUnsafe struct { |
| 141 | + MatrixPayloadSafe |
| 142 | + AccessToken string `json:"access_token"` |
| 143 | + } |
| 144 | + |
| 145 | + err = batchProcess(x, |
| 146 | + make([]*HookTask, 0, 50), |
| 147 | + func(limit, start int) *xorm.Session { |
| 148 | + return x.Where(builder.And( |
| 149 | + builder.In("hook_id", builder.Select("id").From("webhook").Where(builder.Eq{"type": "matrix"})), |
| 150 | + builder.Like{"payload_content", "access_token"}, |
| 151 | + )).OrderBy("id").Limit(limit, 0) // ignore the provided "start", since other payload were already converted and don't contain 'payload_content' anymore |
| 152 | + }, |
| 153 | + func(sess *xorm.Session, hookTask *HookTask) error { |
| 154 | + // retrieve token from payload_content |
| 155 | + var withToken MatrixPayloadUnsafe |
| 156 | + err := json.Unmarshal([]byte(hookTask.PayloadContent), &withToken) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("unable to unmarshal payload_content for hook_task[id=%d]: %w", hookTask.ID, err) |
| 159 | + } |
| 160 | + if withToken.AccessToken == "" { |
| 161 | + return nil |
| 162 | + } |
| 163 | + |
| 164 | + // remove token from payload_content |
| 165 | + withoutToken, err := json.Marshal(withToken.MatrixPayloadSafe) |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("unable to marshal payload_content for hook_task[id=%d]: %w", hookTask.ID, err) |
| 168 | + } |
| 169 | + hookTask.PayloadContent = string(withoutToken) |
| 170 | + |
| 171 | + // save in database |
| 172 | + count, err := sess.ID(hookTask.ID).Cols("payload_content").Update(hookTask) |
| 173 | + if count != 1 || err != nil { |
| 174 | + return fmt.Errorf("unable to update payload_content for hook_task[id=%d]: %d,%w", hookTask.ID, count, err) |
| 175 | + } |
| 176 | + return nil |
| 177 | + }) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
0 commit comments