diff options
author | 2022-03-29 11:54:56 +0200 | |
---|---|---|
committer | 2022-03-29 11:54:56 +0200 | |
commit | 37d310f981f3e26bc643aeabac134b591c84455a (patch) | |
tree | 041f5db16254b5257471bcac0730ac9d93ce13d5 /internal/processing | |
parent | [feature/security] Add systemd sandboxing options to harden security (#440) (diff) | |
download | gotosocial-37d310f981f3e26bc643aeabac134b591c84455a.tar.xz |
[feature] Dereference remote mentions when the account is not already known (#442)v0.2.2
* remove mention util function from db
* add ParseMentionFunc to gtsmodel
* add parseMentionFunc to processor
* refactor search to simplify it a bit
* add parseMentionFunc to account
* add parseMentionFunc to status
* some renaming for clarity
* test dereference of unknown mentioned account
Diffstat (limited to 'internal/processing')
-rw-r--r-- | internal/processing/account/account.go | 4 | ||||
-rw-r--r-- | internal/processing/account/account_test.go | 3 | ||||
-rw-r--r-- | internal/processing/account/update.go | 12 | ||||
-rw-r--r-- | internal/processing/processor.go | 6 | ||||
-rw-r--r-- | internal/processing/search.go | 11 | ||||
-rw-r--r-- | internal/processing/status/status.go | 4 | ||||
-rw-r--r-- | internal/processing/status/status_test.go | 21 | ||||
-rw-r--r-- | internal/processing/status/util.go | 33 | ||||
-rw-r--r-- | internal/processing/util.go | 151 |
9 files changed, 210 insertions, 35 deletions
diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go index c670ee24a..1ef92cf85 100644 --- a/internal/processing/account/account.go +++ b/internal/processing/account/account.go @@ -87,10 +87,11 @@ type processor struct { formatter text.Formatter db db.DB federator federation.Federator + parseMention gtsmodel.ParseMentionFunc } // New returns a new account processor. -func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator) Processor { +func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator, parseMention gtsmodel.ParseMentionFunc) Processor { return &processor{ tc: tc, mediaManager: mediaManager, @@ -100,5 +101,6 @@ func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauth formatter: text.NewFormatter(db), db: db, federator: federator, + parseMention: parseMention, } } diff --git a/internal/processing/account/account_test.go b/internal/processing/account/account_test.go index 5a9382ed6..aff4f02aa 100644 --- a/internal/processing/account/account_test.go +++ b/internal/processing/account/account_test.go @@ -29,6 +29,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/oauth" + "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/processing/account" "github.com/superseriousbusiness/gotosocial/internal/transport" "github.com/superseriousbusiness/gotosocial/internal/typeutils" @@ -88,7 +89,7 @@ func (suite *AccountStandardTestSuite) SetupTest() { suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage, suite.mediaManager) suite.sentEmails = make(map[string]string) suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails) - suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaManager, suite.oauthServer, suite.fromClientAPIChan, suite.federator) + suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaManager, suite.oauthServer, suite.fromClientAPIChan, suite.federator, processing.GetParseMentionFunc(suite.db, suite.federator)) testrig.StandardDBSetup(suite.db, nil) testrig.StandardStorageSetup(suite.storage, "../../../testrig/media") } diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go index a96b44bef..a0056563b 100644 --- a/internal/processing/account/update.go +++ b/internal/processing/account/update.go @@ -199,10 +199,14 @@ func (p *processor) processNote(ctx context.Context, note string, accountID stri return "", err } - mentionStrings := util.DeriveMentionsFromText(note) - mentions, err := p.db.MentionStringsToMentions(ctx, mentionStrings, accountID, "") - if err != nil { - return "", err + mentionStrings := util.DeriveMentionNamesFromText(note) + mentions := []*gtsmodel.Mention{} + for _, mentionString := range mentionStrings { + mention, err := p.parseMention(ctx, mentionString, accountID, "") + if err != nil { + continue + } + mentions = append(mentions, mention) } // TODO: support emojis in account notes diff --git a/internal/processing/processor.go b/internal/processing/processor.go index 50f4af492..f5d9eab28 100644 --- a/internal/processing/processor.go +++ b/internal/processing/processor.go @@ -267,12 +267,14 @@ func NewProcessor( storage *kv.KVStore, db db.DB, emailSender email.Sender) Processor { + fromClientAPI := make(chan messages.FromClientAPI, 1000) fromFederator := make(chan messages.FromFederator, 1000) + parseMentionFunc := GetParseMentionFunc(db, federator) - statusProcessor := status.New(db, tc, fromClientAPI) + statusProcessor := status.New(db, tc, fromClientAPI, parseMentionFunc) streamingProcessor := streaming.New(db, oauthServer) - accountProcessor := account.New(db, tc, mediaManager, oauthServer, fromClientAPI, federator) + accountProcessor := account.New(db, tc, mediaManager, oauthServer, fromClientAPI, federator, parseMentionFunc) adminProcessor := admin.New(db, tc, mediaManager, fromClientAPI) mediaProcessor := mediaProcessor.New(db, tc, mediaManager, federator.TransportController(), storage) userProcessor := user.New(db, emailSender) diff --git a/internal/processing/search.go b/internal/processing/search.go index c8c302857..6c3d7da39 100644 --- a/internal/processing/search.go +++ b/internal/processing/search.go @@ -194,20 +194,15 @@ func (p *processor) searchAccountByMention(ctx context.Context, authed *oauth.Au // we got a db.ErrNoEntries, so we just don't have the account locally stored -- check if we can dereference it if resolve { // we're allowed to resolve it so let's try - // first we need to webfinger the remote account to convert the username and domain into the activitypub URI for the account acctURI, err := p.federator.FingerRemoteAccount(ctx, authed.Account.Username, username, domain) if err != nil { // something went wrong doing the webfinger lookup so we can't process the request - return nil, fmt.Errorf("searchAccountByMention: error fingering remote account with username %s and domain %s: %s", username, domain, err) + return nil, fmt.Errorf("error fingering remote account with username %s and domain %s: %s", username, domain, err) } - // we don't have it locally so try and dereference it - account, err := p.federator.GetRemoteAccount(ctx, authed.Account.Username, acctURI, true, true) - if err != nil { - return nil, fmt.Errorf("searchAccountByMention: error dereferencing account with uri %s: %s", acctURI.String(), err) - } - return account, nil + // return the attempt to get the remove account + return p.federator.GetRemoteAccount(ctx, authed.Account.Username, acctURI, true, true) } return nil, nil diff --git a/internal/processing/status/status.go b/internal/processing/status/status.go index 3118b23ad..421ab5bbe 100644 --- a/internal/processing/status/status.go +++ b/internal/processing/status/status.go @@ -74,15 +74,17 @@ type processor struct { filter visibility.Filter formatter text.Formatter fromClientAPI chan messages.FromClientAPI + parseMention gtsmodel.ParseMentionFunc } // New returns a new status processor. -func New(db db.DB, tc typeutils.TypeConverter, fromClientAPI chan messages.FromClientAPI) Processor { +func New(db db.DB, tc typeutils.TypeConverter, fromClientAPI chan messages.FromClientAPI, parseMention gtsmodel.ParseMentionFunc) Processor { return &processor{ tc: tc, db: db, filter: visibility.NewFilter(db), formatter: text.NewFormatter(db), fromClientAPI: fromClientAPI, + parseMention: parseMention, } } diff --git a/internal/processing/status/status_test.go b/internal/processing/status/status_test.go index 803e7a0a5..25794070e 100644 --- a/internal/processing/status/status_test.go +++ b/internal/processing/status/status_test.go @@ -19,11 +19,16 @@ package status_test import ( + "codeberg.org/gruf/go-store/kv" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/federation" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/messages" + "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/processing/status" + "github.com/superseriousbusiness/gotosocial/internal/transport" "github.com/superseriousbusiness/gotosocial/internal/typeutils" "github.com/superseriousbusiness/gotosocial/testrig" ) @@ -32,6 +37,10 @@ type StatusStandardTestSuite struct { suite.Suite db db.DB typeConverter typeutils.TypeConverter + tc transport.Controller + storage *kv.KVStore + mediaManager media.Manager + federator federation.Federator fromClientAPIChan chan messages.FromClientAPI // standard suite models @@ -62,17 +71,23 @@ func (suite *StatusStandardTestSuite) SetupSuite() { } func (suite *StatusStandardTestSuite) SetupTest() { - testrig.InitTestLog() testrig.InitTestConfig() + testrig.InitTestLog() suite.db = testrig.NewTestDB() suite.typeConverter = testrig.NewTestTypeConverter(suite.db) suite.fromClientAPIChan = make(chan messages.FromClientAPI, 100) - suite.status = status.New(suite.db, suite.typeConverter, suite.fromClientAPIChan) + suite.tc = testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db) + suite.storage = testrig.NewTestStorage() + suite.mediaManager = testrig.NewTestMediaManager(suite.db, suite.storage) + suite.federator = testrig.NewTestFederator(suite.db, suite.tc, suite.storage, suite.mediaManager) + suite.status = status.New(suite.db, suite.typeConverter, suite.fromClientAPIChan, processing.GetParseMentionFunc(suite.db, suite.federator)) - testrig.StandardDBSetup(suite.db, nil) + testrig.StandardDBSetup(suite.db, suite.testAccounts) + testrig.StandardStorageSetup(suite.storage, "../../../testrig/media") } func (suite *StatusStandardTestSuite) TearDownTest() { testrig.StandardDBTeardown(suite.db) + testrig.StandardStorageTeardown(suite.storage) } diff --git a/internal/processing/status/util.go b/internal/processing/status/util.go index f2640929d..5de66af8a 100644 --- a/internal/processing/status/util.go +++ b/internal/processing/status/util.go @@ -23,10 +23,10 @@ import ( "errors" "fmt" + "github.com/sirupsen/logrus" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -192,27 +192,30 @@ func (p *processor) ProcessLanguage(ctx context.Context, form *apimodel.Advanced } func (p *processor) ProcessMentions(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error { - menchies := []string{} - gtsMenchies, err := p.db.MentionStringsToMentions(ctx, util.DeriveMentionsFromText(form.Status), accountID, status.ID) - if err != nil { - return fmt.Errorf("error generating mentions from status: %s", err) - } - for _, menchie := range gtsMenchies { - menchieID, err := id.NewRandomULID() + mentionedAccountNames := util.DeriveMentionNamesFromText(form.Status) + mentions := []*gtsmodel.Mention{} + mentionIDs := []string{} + + for _, mentionedAccountName := range mentionedAccountNames { + gtsMention, err := p.parseMention(ctx, mentionedAccountName, accountID, status.ID) if err != nil { - return err + logrus.Errorf("ProcessMentions: error parsing mention %s from status: %s", mentionedAccountName, err) + continue } - menchie.ID = menchieID - if err := p.db.Put(ctx, menchie); err != nil { - return fmt.Errorf("error putting mentions in db: %s", err) + if err := p.db.Put(ctx, gtsMention); err != nil { + logrus.Errorf("ProcessMentions: error putting mention in db: %s", err) } - menchies = append(menchies, menchie.ID) + + mentions = append(mentions, gtsMention) + mentionIDs = append(mentionIDs, gtsMention.ID) } + // add full populated gts menchies to the status for passing them around conveniently - status.Mentions = gtsMenchies + status.Mentions = mentions // add just the ids of the mentioned accounts to the status for putting in the db - status.MentionIDs = menchies + status.MentionIDs = mentionIDs + return nil } diff --git a/internal/processing/util.go b/internal/processing/util.go new file mode 100644 index 000000000..7ae7b8604 --- /dev/null +++ b/internal/processing/util.go @@ -0,0 +1,151 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + 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 processing + +import ( + "context" + "fmt" + "strings" + + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/federation" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/id" +) + +func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.ParseMentionFunc { + return func(ctx context.Context, targetAccount string, originAccountID string, statusID string) (*gtsmodel.Mention, error) { + // get the origin account first since we'll need it to create the mention + originAccount, err := dbConn.GetAccountByID(ctx, originAccountID) + if err != nil { + return nil, fmt.Errorf("couldn't get mention origin account with id %s", originAccountID) + } + + // A mentioned account looks like "@test@example.org" or just "@test" for a local account + // -- we can guarantee this from the regex that targetAccounts should have been derived from. + // But we still need to do a bit of fiddling to get what we need here -- the username and domain (if given). + + // 1. trim off the first @ + trimmed := strings.TrimPrefix(targetAccount, "@") + + // 2. split the username and domain + split := strings.Split(trimmed, "@") + + // 3. if it's length 1 it's a local account, length 2 means remote, anything else means something is wrong + + var local bool + switch len(split) { + case 1: + local = true + case 2: + local = false + default: + return nil, fmt.Errorf("mentioned account format '%s' was not valid", targetAccount) + } + + var username, domain string + username = split[0] + if !local { + domain = split[1] + } + + // 4. check we now have a proper username and domain + if username == "" || (!local && domain == "") { + return nil, fmt.Errorf("username or domain for '%s' was nil", targetAccount) + } + + var mentionedAccount *gtsmodel.Account + + if local { + localAccount, err := dbConn.GetLocalAccountByUsername(ctx, username) + if err != nil { + return nil, err + } + mentionedAccount = localAccount + } else { + remoteAccount := >smodel.Account{} + + where := []db.Where{ + { + Key: "username", + Value: username, + CaseInsensitive: true, + }, + { + Key: "domain", + Value: domain, + CaseInsensitive: true, + }, + } + + err := dbConn.GetWhere(ctx, where, remoteAccount) + if err == nil { + // the account was already in the database + mentionedAccount = remoteAccount + } else { + // we couldn't get it from the database + if err != db.ErrNoEntries { + // a serious error has happened so bail + return nil, fmt.Errorf("error getting account with username '%s' and domain '%s': %s", username, domain, err) + } + + // We just don't have the account, so try webfingering it. + // + // If the mention originates from our instance we should use the username of the origin account to do the dereferencing, + // otherwise we should just use our instance account (that is, provide an empty string), since obviously we can't use + // a remote account to do remote dereferencing! + var fingeringUsername string + if originAccount.Domain == "" { + fingeringUsername = originAccount.Username + } + + acctURI, err := federator.FingerRemoteAccount(ctx, fingeringUsername, username, domain) + if err != nil { + // something went wrong doing the webfinger lookup so we can't process the request + return nil, fmt.Errorf("error fingering remote account with username %s and domain %s: %s", username, domain, err) + } + + resolvedAccount, err := federator.GetRemoteAccount(ctx, fingeringUsername, acctURI, true, true) + if err != nil { + return nil, fmt.Errorf("error dereferencing account with uri %s: %s", acctURI.String(), err) + } + + // we were able to resolve it! + mentionedAccount = resolvedAccount + } + } + + mentionID, err := id.NewRandomULID() + if err != nil { + return nil, err + } + + return >smodel.Mention{ + ID: mentionID, + StatusID: statusID, + OriginAccountID: originAccount.ID, + OriginAccountURI: originAccount.URI, + TargetAccountID: mentionedAccount.ID, + NameString: targetAccount, + TargetAccountURI: mentionedAccount.URI, + TargetAccountURL: mentionedAccount.URL, + OriginAccount: mentionedAccount, + }, nil + } +} |