mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-22 11:46:40 +00:00
[bugfix] Prevent URL + URI for same account being used as alias target (#2545)
* [bugfix] Ensure URL and URI for same account can't both be provided as alias * test whoopsie from previous PR
This commit is contained in:
parent
33dbd3ab7a
commit
b2cacd6b01
|
@ -96,26 +96,21 @@ type uri struct {
|
||||||
newAKAs[i].str = newAKAURI.String()
|
newAKAs[i].str = newAKAURI.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dedupe the URI/string pairs.
|
|
||||||
newAKAs = util.DeduplicateFunc(
|
|
||||||
newAKAs,
|
|
||||||
func(v uri) string {
|
|
||||||
return v.str
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// For each deduped entry, get and
|
// For each deduped entry, get and
|
||||||
// check the target account, and set.
|
// check the target account, and set.
|
||||||
for _, newAKA := range newAKAs {
|
for _, newAKA := range newAKAs {
|
||||||
// Don't let account do anything
|
// Don't let account do anything
|
||||||
// daft by aliasing to itself.
|
// daft by aliasing to itself.
|
||||||
if newAKA.str == account.URI {
|
if newAKA.str == account.URI ||
|
||||||
|
newAKA.str == account.URL {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we have a valid, up-to-date
|
// Ensure we have account dereferenced.
|
||||||
// representation of the target account.
|
targetAccount, _, err := p.federator.GetAccountByURI(ctx,
|
||||||
targetAccount, _, err := p.federator.GetAccountByURI(ctx, account.Username, newAKA.uri)
|
account.Username,
|
||||||
|
newAKA.uri,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf(
|
err := fmt.Errorf(
|
||||||
"error dereferencing also_known_as_uri (%s) account: %w",
|
"error dereferencing also_known_as_uri (%s) account: %w",
|
||||||
|
@ -124,7 +119,7 @@ func(v uri) string {
|
||||||
return nil, gtserror.NewErrorUnprocessableEntity(err, err.Error())
|
return nil, gtserror.NewErrorUnprocessableEntity(err, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alias target must not be suspended.
|
// Target must not be suspended.
|
||||||
if !targetAccount.SuspendedAt.IsZero() {
|
if !targetAccount.SuspendedAt.IsZero() {
|
||||||
err := fmt.Errorf(
|
err := fmt.Errorf(
|
||||||
"target account %s is suspended from this instance; "+
|
"target account %s is suspended from this instance; "+
|
||||||
|
@ -135,10 +130,21 @@ func(v uri) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alrighty-roo, looks good, add this one.
|
// Alrighty-roo, looks good, add this one.
|
||||||
account.AlsoKnownAsURIs = append(account.AlsoKnownAsURIs, newAKA.str)
|
account.AlsoKnownAsURIs = append(account.AlsoKnownAsURIs, targetAccount.URI)
|
||||||
account.AlsoKnownAs = append(account.AlsoKnownAs, targetAccount)
|
account.AlsoKnownAs = append(account.AlsoKnownAs, targetAccount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dedupe URIs + accounts, in case someone
|
||||||
|
// provided both an account URL and an
|
||||||
|
// account URI above, for the same account.
|
||||||
|
account.AlsoKnownAsURIs = util.Deduplicate(account.AlsoKnownAsURIs)
|
||||||
|
account.AlsoKnownAs = util.DeduplicateFunc(
|
||||||
|
account.AlsoKnownAs,
|
||||||
|
func(a *gtsmodel.Account) string {
|
||||||
|
return a.URI
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
err := p.state.DB.UpdateAccount(ctx, account, "also_known_as_uris")
|
err := p.state.DB.UpdateAccount(ctx, account, "also_known_as_uris")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := gtserror.Newf("db error updating also_known_as_uri: %w", err)
|
err := gtserror.Newf("db error updating also_known_as_uri: %w", err)
|
||||||
|
|
|
@ -132,6 +132,17 @@ func (suite *AliasTestSuite) TestAliasAccount() {
|
||||||
"http://localhost:8080/users/admin",
|
"http://localhost:8080/users/admin",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
// Alias zork to turtle using both URI and URL
|
||||||
|
// for turtle. Only URI should end up being used.
|
||||||
|
{
|
||||||
|
newAliases: []string{
|
||||||
|
"http://localhost:8080/users/1happyturtle",
|
||||||
|
"http://localhost:8080/@1happyturtle",
|
||||||
|
},
|
||||||
|
expectedAliases: []string{
|
||||||
|
"http://localhost:8080/users/1happyturtle",
|
||||||
|
},
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
|
|
|
@ -631,7 +631,7 @@ func (suite *ASToInternalTestSuite) TestParseHonkAccount() {
|
||||||
|
|
||||||
// Clear caches.
|
// Clear caches.
|
||||||
suite.state.Caches.GTS = cache.GTSCaches{}
|
suite.state.Caches.GTS = cache.GTSCaches{}
|
||||||
suite.state.Caches.GTS.Init()
|
suite.state.Caches.Init()
|
||||||
|
|
||||||
dbAcct, err = suite.db.GetAccountByID(ctx, acct.ID)
|
dbAcct, err = suite.db.GetAccountByID(ctx, acct.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue