1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package account
import (
"context"
"errors"
"fmt"
"net/url"
"slices"
"time"
"github.com/superseriousbusiness/gotosocial/internal/ap"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/messages"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/uris"
"golang.org/x/crypto/bcrypt"
)
func (p *Processor) MoveSelf(
ctx context.Context,
authed *oauth.Auth,
form *apimodel.AccountMoveRequest,
) gtserror.WithCode {
// Ensure valid MovedToURI.
if form.MovedToURI == "" {
err := errors.New("no moved_to_uri provided in account Move request")
return gtserror.NewErrorBadRequest(err, err.Error())
}
targetAcctURIStr := form.MovedToURI
targetAcctURI, err := url.Parse(form.MovedToURI)
if err != nil {
err := fmt.Errorf("invalid moved_to_uri provided in account Move request: %w", err)
return gtserror.NewErrorBadRequest(err, err.Error())
}
if targetAcctURI.Scheme != "https" && targetAcctURI.Scheme != "http" {
err := errors.New("invalid moved_to_uri provided in account Move request: uri scheme must be http or https")
return gtserror.NewErrorBadRequest(err, err.Error())
}
// Self account Move requires password to ensure it's for real.
if form.Password == "" {
err := errors.New("no password provided in account Move request")
return gtserror.NewErrorBadRequest(err, err.Error())
}
if err := bcrypt.CompareHashAndPassword(
[]byte(authed.User.EncryptedPassword),
[]byte(form.Password),
); err != nil {
err := errors.New("invalid password provided in account Move request")
return gtserror.NewErrorBadRequest(err, err.Error())
}
// We can't/won't validate Move activities
// to domains we have blocked, so check this.
targetDomainBlocked, err := p.state.DB.IsDomainBlocked(ctx, targetAcctURI.Host)
if err != nil {
err := fmt.Errorf(
"db error checking if target domain %s blocked: %w",
targetAcctURI.Host, err,
)
return gtserror.NewErrorInternalError(err)
}
if targetDomainBlocked {
err := fmt.Errorf(
"domain of %s is blocked from this instance; "+
"you will not be able to Move to that account",
targetAcctURIStr,
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
var (
// Current account from which
// the move is taking place.
originAcct = authed.Account
// Target account to which
// the move is taking place.
targetAcct *gtsmodel.Account
// AP representation of target.
targetAcctable ap.Accountable
)
// Next steps involve checking + setting
// state that might get messed up if a
// client triggers this function twice
// in quick succession, so get a lock on
// this account.
lockKey := originAcct.URI
unlock := p.state.ProcessingLocks.Lock(lockKey)
defer unlock()
// Ensure we have a valid, up-to-date representation of the target account.
targetAcct, targetAcctable, err = p.federator.GetAccountByURI(
ctx,
originAcct.Username,
targetAcctURI,
)
if err != nil {
err := fmt.Errorf("error dereferencing moved_to_uri account: %w", err)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
if !targetAcct.SuspendedAt.IsZero() {
err := fmt.Errorf(
"target account %s is suspended from this instance; "+
"you will not be able to Move to that account",
targetAcct.URI,
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
if targetAcct.IsRemote() {
// Force refresh Move target account
// to ensure we have up-to-date version.
targetAcct, _, err = p.federator.RefreshAccount(ctx,
originAcct.Username,
targetAcct,
targetAcctable,
dereferencing.Freshest,
)
if err != nil {
err := fmt.Errorf(
"error refreshing target account %s: %w",
targetAcctURIStr, err,
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
}
// If originAcct has already moved, ensure
// this move reattempt is to the same account.
if originAcct.IsMoving() &&
originAcct.MovedToURI != targetAcct.URI {
err := fmt.Errorf(
"your account is already Moving or has Moved to %s; you cannot also Move to %s",
originAcct.MovedToURI, targetAcct.URI,
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
// Target account MUST be aliased to this
// account for this to be a valid Move.
if !slices.Contains(targetAcct.AlsoKnownAsURIs, originAcct.URI) {
err := fmt.Errorf(
"target account %s is not aliased to this account via alsoKnownAs; "+
"if you just changed it, please wait a few minutes and try the Move again",
targetAcct.URI,
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
// Target account cannot itself have
// already Moved somewhere else.
if targetAcct.MovedToURI != "" {
err := fmt.Errorf(
"target account %s has already Moved somewhere else (%s); "+
"you will not be able to Move to that account",
targetAcct.URI, targetAcct.MovedToURI,
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
// If a Move has been *attempted* within last 5m,
// that involved the origin and target in any way,
// then we shouldn't try to reprocess immediately.
latestMoveAttempt, err := p.state.DB.GetLatestMoveAttemptInvolvingURIs(
ctx, originAcct.URI, targetAcct.URI,
)
if err != nil {
err := fmt.Errorf(
"error checking latest Move attempt involving origin %s and target %s: %w",
originAcct.URI, targetAcct.URI, err,
)
return gtserror.NewErrorInternalError(err)
}
if !latestMoveAttempt.IsZero() &&
time.Since(latestMoveAttempt) < 5*time.Minute {
err := fmt.Errorf(
"your account or target account have been involved in a Move attempt within "+
"the last 5 minutes, will not process Move; please try again after %s",
latestMoveAttempt.Add(5*time.Minute),
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
// If a Move has *succeeded* within the last week
// that involved the origin and target in any way,
// then we shouldn't process again for a while.
latestMoveSuccess, err := p.state.DB.GetLatestMoveSuccessInvolvingURIs(
ctx, originAcct.URI, targetAcct.URI,
)
if err != nil {
err := fmt.Errorf(
"error checking latest Move success involving origin %s and target %s: %w",
originAcct.URI, targetAcct.URI, err,
)
return gtserror.NewErrorInternalError(err)
}
if !latestMoveSuccess.IsZero() &&
time.Since(latestMoveSuccess) < 168*time.Hour {
err := fmt.Errorf(
"your account or target account have been involved in a successful Move within "+
"the last 7 days, will not process Move; please try again after %s",
latestMoveSuccess.Add(168*time.Hour),
)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
// See if we have a Move stored already
// or if we need to create a new one.
var move *gtsmodel.Move
if originAcct.MoveID != "" {
// Move already stored, ensure it's
// to the target and nothing weird is
// happening with race conditions etc.
move = originAcct.Move
if move == nil {
// This shouldn't happen...
err := fmt.Errorf("nil move for id %s", originAcct.MoveID)
return gtserror.NewErrorInternalError(err)
}
if move.OriginURI != originAcct.URI ||
move.TargetURI != targetAcct.URI {
// This is also weird...
err := errors.New("a Move is already stored for your account but contains invalid fields")
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
if originAcct.MovedToURI != move.TargetURI {
// Huh... I'll be damned.
err := errors.New("stored Move target URI does not equal your moved_to_uri value")
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
} else {
// Move not stored yet, create it.
moveID := id.NewULID()
moveURIStr := uris.GenerateURIForMove(originAcct.Username, moveID)
// We might have selected the target
// using the URL and not the URI.
// Ensure we continue with the URI!
if targetAcctURIStr != targetAcct.URI {
targetAcctURIStr = targetAcct.URI
targetAcctURI, err = url.Parse(targetAcctURIStr)
if err != nil {
return gtserror.NewErrorInternalError(err)
}
}
// Parse origin URI.
originAcctURI, err := url.Parse(originAcct.URI)
if err != nil {
return gtserror.NewErrorInternalError(err)
}
// Store the Move.
move = >smodel.Move{
ID: moveID,
AttemptedAt: time.Now(),
OriginURI: originAcct.URI,
Origin: originAcctURI,
TargetURI: targetAcctURIStr,
Target: targetAcctURI,
URI: moveURIStr,
}
if err := p.state.DB.PutMove(ctx, move); err != nil {
err := fmt.Errorf("db error storing move %s: %w", moveURIStr, err)
return gtserror.NewErrorInternalError(err)
}
// Update account with the new
// Move, and set moved_to_uri.
originAcct.MoveID = move.ID
originAcct.Move = move
originAcct.MovedToURI = targetAcct.URI
originAcct.MovedTo = targetAcct
if err := p.state.DB.UpdateAccount(
ctx,
originAcct,
"move_id",
"moved_to_uri",
); err != nil {
err := fmt.Errorf("db error updating account: %w", err)
return gtserror.NewErrorInternalError(err)
}
}
// Everything seems OK, process Move side effects async.
p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
APObjectType: ap.ActorPerson,
APActivityType: ap.ActivityMove,
GTSModel: move,
Origin: originAcct,
Target: targetAcct,
})
return nil
}
|