summaryrefslogtreecommitdiff
path: root/internal/api/s2s
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-09-24 17:56:48 +0200
committerLibravatar GitHub <noreply@github.com>2021-09-24 17:56:48 +0200
commitc7cfbe2702913a6d6e7c962992546c96aa512068 (patch)
tree4b0bb21aab9be586d5db26d172f548e12c07f96e /internal/api/s2s
parentGoreleaser (#241) (diff)
downloadgotosocial-c7cfbe2702913a6d6e7c962992546c96aa512068.tar.xz
Unblock fix (#247)
* start tests for inbox posts * go mod tidy * rename transferContext * test block/unblock * improve logging * improve logging * fix comment typo
Diffstat (limited to 'internal/api/s2s')
-rw-r--r--internal/api/s2s/user/common.go4
-rw-r--r--internal/api/s2s/user/followers.go2
-rw-r--r--internal/api/s2s/user/following.go2
-rw-r--r--internal/api/s2s/user/inboxpost.go6
-rw-r--r--internal/api/s2s/user/inboxpost_test.go228
-rw-r--r--internal/api/s2s/user/publickeyget.go20
-rw-r--r--internal/api/s2s/user/repliesget.go20
-rw-r--r--internal/api/s2s/user/repliesget_test.go18
-rw-r--r--internal/api/s2s/user/statusget.go20
-rw-r--r--internal/api/s2s/user/user_test.go18
-rw-r--r--internal/api/s2s/user/userget.go2
-rw-r--r--internal/api/s2s/user/userget_test.go18
12 files changed, 347 insertions, 11 deletions
diff --git a/internal/api/s2s/user/common.go b/internal/api/s2s/user/common.go
index 8a3846901..e96082c3e 100644
--- a/internal/api/s2s/user/common.go
+++ b/internal/api/s2s/user/common.go
@@ -33,8 +33,8 @@ var ActivityPubAcceptHeaders = []string{
`application/ld+json; profile="https://www.w3.org/ns/activitystreams"`,
}
-// populateContext transfers the signature verifier and signature from the gin context to the request context
-func populateContext(c *gin.Context) context.Context {
+// transferContext transfers the signature verifier and signature from the gin context to the request context
+func transferContext(c *gin.Context) context.Context {
ctx := c.Request.Context()
verifier, signed := c.Get(string(util.APRequestingPublicKeyVerifier))
diff --git a/internal/api/s2s/user/followers.go b/internal/api/s2s/user/followers.go
index c85c24c95..63d43db91 100644
--- a/internal/api/s2s/user/followers.go
+++ b/internal/api/s2s/user/followers.go
@@ -47,7 +47,7 @@ func (m *Module) FollowersGETHandler(c *gin.Context) {
}
l.Tracef("negotiated format: %s", format)
- ctx := populateContext(c)
+ ctx := transferContext(c)
followers, errWithCode := m.processor.GetFediFollowers(ctx, requestedUsername, c.Request.URL)
if errWithCode != nil {
diff --git a/internal/api/s2s/user/following.go b/internal/api/s2s/user/following.go
index 7c5b1bbf7..3a3e1146e 100644
--- a/internal/api/s2s/user/following.go
+++ b/internal/api/s2s/user/following.go
@@ -47,7 +47,7 @@ func (m *Module) FollowingGETHandler(c *gin.Context) {
}
l.Tracef("negotiated format: %s", format)
- ctx := populateContext(c)
+ ctx := transferContext(c)
following, errWithCode := m.processor.GetFediFollowing(ctx, requestedUsername, c.Request.URL)
if errWithCode != nil {
diff --git a/internal/api/s2s/user/inboxpost.go b/internal/api/s2s/user/inboxpost.go
index 8e44454d7..d59555e15 100644
--- a/internal/api/s2s/user/inboxpost.go
+++ b/internal/api/s2s/user/inboxpost.go
@@ -40,12 +40,12 @@ func (m *Module) InboxPOSTHandler(c *gin.Context) {
return
}
- ctx := populateContext(c)
+ ctx := transferContext(c)
posted, err := m.processor.InboxPost(ctx, c.Writer, c.Request)
if err != nil {
if withCode, ok := err.(gtserror.WithCode); ok {
- l.Debug(withCode.Error())
+ l.Debugf("InboxPOSTHandler: %s", withCode.Error())
c.JSON(withCode.Code(), withCode.Safe())
return
}
@@ -55,7 +55,7 @@ func (m *Module) InboxPOSTHandler(c *gin.Context) {
}
if !posted {
- l.Debugf("request could not be handled as an AP request; headers were: %+v", c.Request.Header)
+ l.Debugf("InboxPOSTHandler: request could not be handled as an AP request; headers were: %+v", c.Request.Header)
c.JSON(http.StatusBadRequest, gin.H{"error": "unable to process request"})
}
}
diff --git a/internal/api/s2s/user/inboxpost_test.go b/internal/api/s2s/user/inboxpost_test.go
new file mode 100644
index 000000000..47f5295f0
--- /dev/null
+++ b/internal/api/s2s/user/inboxpost_test.go
@@ -0,0 +1,228 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user_test
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/go-fed/activity/streams"
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/api/s2s/user"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/id"
+ "github.com/superseriousbusiness/gotosocial/testrig"
+)
+
+type InboxPostTestSuite struct {
+ UserStandardTestSuite
+}
+
+func (suite *InboxPostTestSuite) TestPostBlock() {
+ blockingAccount := suite.testAccounts["remote_account_1"]
+ blockedAccount := suite.testAccounts["local_account_1"]
+ blockURI := testrig.URLMustParse("http://fossbros-anonymous.io/users/foss_satan/blocks/01FG9C441MCTW3R2W117V2PQK3")
+
+ block := streams.NewActivityStreamsBlock()
+
+ // set the actor property to the block-ing account's URI
+ actorProp := streams.NewActivityStreamsActorProperty()
+ actorIRI := testrig.URLMustParse(blockingAccount.URI)
+ actorProp.AppendIRI(actorIRI)
+ block.SetActivityStreamsActor(actorProp)
+
+ // set the ID property to the blocks's URI
+ idProp := streams.NewJSONLDIdProperty()
+ idProp.Set(blockURI)
+ block.SetJSONLDId(idProp)
+
+ // set the object property to the target account's URI
+ objectProp := streams.NewActivityStreamsObjectProperty()
+ targetIRI := testrig.URLMustParse(blockedAccount.URI)
+ objectProp.AppendIRI(targetIRI)
+ block.SetActivityStreamsObject(objectProp)
+
+ // set the TO property to the target account's IRI
+ toProp := streams.NewActivityStreamsToProperty()
+ toIRI := testrig.URLMustParse(blockedAccount.URI)
+ toProp.AppendIRI(toIRI)
+ block.SetActivityStreamsTo(toProp)
+
+ targetURI := testrig.URLMustParse(blockedAccount.InboxURI)
+
+ signature, digestHeader, dateHeader := testrig.GetSignatureForActivity(block, blockingAccount.PublicKeyURI, blockingAccount.PrivateKey, targetURI)
+ bodyI, err := streams.Serialize(block)
+ suite.NoError(err)
+
+ bodyJson, err := json.Marshal(bodyI)
+ suite.NoError(err)
+ body := bytes.NewReader(bodyJson)
+
+ tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db)
+ federator := testrig.NewTestFederator(suite.db, tc, suite.storage)
+ processor := testrig.NewTestProcessor(suite.db, suite.storage, federator)
+ userModule := user.New(suite.config, processor, suite.log).(*user.Module)
+
+ // setup request
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Request = httptest.NewRequest(http.MethodPost, targetURI.String(), body) // the endpoint we're hitting
+ ctx.Request.Header.Set("Signature", signature)
+ ctx.Request.Header.Set("Date", dateHeader)
+ ctx.Request.Header.Set("Digest", digestHeader)
+ ctx.Request.Header.Set("Content-Type", "application/activity+json")
+
+ // we need to pass the context through signature check first to set appropriate values on it
+ suite.securityModule.SignatureCheck(ctx)
+
+ // normally the router would populate these params from the path values,
+ // but because we're calling the function directly, we need to set them manually.
+ ctx.Params = gin.Params{
+ gin.Param{
+ Key: user.UsernameKey,
+ Value: blockedAccount.Username,
+ },
+ }
+
+ // trigger the function being tested
+ userModule.InboxPOSTHandler(ctx)
+
+ result := recorder.Result()
+ defer result.Body.Close()
+ b, err := ioutil.ReadAll(result.Body)
+ suite.NoError(err)
+ suite.Empty(b)
+
+ // there should be a block in the database now between the accounts
+ dbBlock, err := suite.db.GetBlock(context.Background(), blockingAccount.ID, blockedAccount.ID)
+ suite.NoError(err)
+ suite.NotNil(dbBlock)
+ suite.WithinDuration(time.Now(), dbBlock.CreatedAt, 30*time.Second)
+ suite.WithinDuration(time.Now(), dbBlock.UpdatedAt, 30*time.Second)
+ suite.Equal("http://fossbros-anonymous.io/users/foss_satan/blocks/01FG9C441MCTW3R2W117V2PQK3", dbBlock.URI)
+}
+
+// TestPostUnblock verifies that a remote account with a block targeting one of our instance users should be able to undo that block.
+func (suite *InboxPostTestSuite) TestPostUnblock() {
+ blockingAccount := suite.testAccounts["remote_account_1"]
+ blockedAccount := suite.testAccounts["local_account_1"]
+
+ // first put a block in the database so we have something to undo
+ blockURI := "http://fossbros-anonymous.io/users/foss_satan/blocks/01FG9C441MCTW3R2W117V2PQK3"
+ dbBlockID, err := id.NewRandomULID()
+ suite.NoError(err)
+
+ dbBlock := &gtsmodel.Block{
+ ID: dbBlockID,
+ CreatedAt: time.Now(),
+ UpdatedAt: time.Now(),
+ URI: blockURI,
+ AccountID: blockingAccount.ID,
+ TargetAccountID: blockedAccount.ID,
+ }
+
+ err = suite.db.Put(context.Background(), dbBlock)
+ suite.NoError(err)
+
+ asBlock, err := suite.tc.BlockToAS(context.Background(), dbBlock)
+ suite.NoError(err)
+
+ targetAccountURI := testrig.URLMustParse(blockedAccount.URI)
+
+ // create an Undo and set the appropriate actor on it
+ undo := streams.NewActivityStreamsUndo()
+ undo.SetActivityStreamsActor(asBlock.GetActivityStreamsActor())
+
+ // Set the block as the 'object' property.
+ undoObject := streams.NewActivityStreamsObjectProperty()
+ undoObject.AppendActivityStreamsBlock(asBlock)
+ undo.SetActivityStreamsObject(undoObject)
+
+ // Set the To of the undo as the target of the block
+ undoTo := streams.NewActivityStreamsToProperty()
+ undoTo.AppendIRI(targetAccountURI)
+ undo.SetActivityStreamsTo(undoTo)
+
+ undoID := streams.NewJSONLDIdProperty()
+ undoID.SetIRI(testrig.URLMustParse("http://fossbros-anonymous.io/72cc96a3-f742-4daf-b9f5-3407667260c5"))
+ undo.SetJSONLDId(undoID)
+
+ targetURI := testrig.URLMustParse(blockedAccount.InboxURI)
+
+ signature, digestHeader, dateHeader := testrig.GetSignatureForActivity(undo, blockingAccount.PublicKeyURI, blockingAccount.PrivateKey, targetURI)
+ bodyI, err := streams.Serialize(undo)
+ suite.NoError(err)
+
+ bodyJson, err := json.Marshal(bodyI)
+ suite.NoError(err)
+ body := bytes.NewReader(bodyJson)
+
+ tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db)
+ federator := testrig.NewTestFederator(suite.db, tc, suite.storage)
+ processor := testrig.NewTestProcessor(suite.db, suite.storage, federator)
+ userModule := user.New(suite.config, processor, suite.log).(*user.Module)
+
+ // setup request
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Request = httptest.NewRequest(http.MethodPost, targetURI.String(), body) // the endpoint we're hitting
+ ctx.Request.Header.Set("Signature", signature)
+ ctx.Request.Header.Set("Date", dateHeader)
+ ctx.Request.Header.Set("Digest", digestHeader)
+ ctx.Request.Header.Set("Content-Type", "application/activity+json")
+
+ // we need to pass the context through signature check first to set appropriate values on it
+ suite.securityModule.SignatureCheck(ctx)
+
+ // normally the router would populate these params from the path values,
+ // but because we're calling the function directly, we need to set them manually.
+ ctx.Params = gin.Params{
+ gin.Param{
+ Key: user.UsernameKey,
+ Value: blockedAccount.Username,
+ },
+ }
+
+ // trigger the function being tested
+ userModule.InboxPOSTHandler(ctx)
+
+ result := recorder.Result()
+ defer result.Body.Close()
+ b, err := ioutil.ReadAll(result.Body)
+ suite.NoError(err)
+ suite.Empty(b)
+ suite.Equal(http.StatusOK, result.StatusCode)
+
+ // the block should be undone
+ block, err := suite.db.GetBlock(context.Background(), blockingAccount.ID, blockedAccount.ID)
+ suite.ErrorIs(err, db.ErrNoEntries)
+ suite.Nil(block)
+}
+
+func TestInboxPostTestSuite(t *testing.T) {
+ suite.Run(t, &InboxPostTestSuite{})
+}
diff --git a/internal/api/s2s/user/publickeyget.go b/internal/api/s2s/user/publickeyget.go
index f5fe79441..8b9d3aa02 100644
--- a/internal/api/s2s/user/publickeyget.go
+++ b/internal/api/s2s/user/publickeyget.go
@@ -1,3 +1,21 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user
import (
@@ -33,7 +51,7 @@ func (m *Module) PublicKeyGETHandler(c *gin.Context) {
}
l.Tracef("negotiated format: %s", format)
- ctx := populateContext(c)
+ ctx := transferContext(c)
user, errWithCode := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL)
if errWithCode != nil {
diff --git a/internal/api/s2s/user/repliesget.go b/internal/api/s2s/user/repliesget.go
index 9031b7841..13b9061ed 100644
--- a/internal/api/s2s/user/repliesget.go
+++ b/internal/api/s2s/user/repliesget.go
@@ -1,3 +1,21 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user
import (
@@ -121,7 +139,7 @@ func (m *Module) StatusRepliesGETHandler(c *gin.Context) {
}
l.Tracef("negotiated format: %s", format)
- ctx := populateContext(c)
+ ctx := transferContext(c)
replies, errWithCode := m.processor.GetFediStatusReplies(ctx, requestedUsername, requestedStatusID, page, onlyOtherAccounts, minID, c.Request.URL)
if errWithCode != nil {
diff --git a/internal/api/s2s/user/repliesget_test.go b/internal/api/s2s/user/repliesget_test.go
index ccd2eb4f6..f8e833741 100644
--- a/internal/api/s2s/user/repliesget_test.go
+++ b/internal/api/s2s/user/repliesget_test.go
@@ -1,3 +1,21 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user_test
import (
diff --git a/internal/api/s2s/user/statusget.go b/internal/api/s2s/user/statusget.go
index 20b9064eb..74615424d 100644
--- a/internal/api/s2s/user/statusget.go
+++ b/internal/api/s2s/user/statusget.go
@@ -1,3 +1,21 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user
import (
@@ -35,7 +53,7 @@ func (m *Module) StatusGETHandler(c *gin.Context) {
}
l.Tracef("negotiated format: %s", format)
- ctx := populateContext(c)
+ ctx := transferContext(c)
status, errWithCode := m.processor.GetFediStatus(ctx, requestedUsername, requestedStatusID, c.Request.URL)
if errWithCode != nil {
diff --git a/internal/api/s2s/user/user_test.go b/internal/api/s2s/user/user_test.go
index 768dd941b..eba0f61f0 100644
--- a/internal/api/s2s/user/user_test.go
+++ b/internal/api/s2s/user/user_test.go
@@ -1,3 +1,21 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user_test
import (
diff --git a/internal/api/s2s/user/userget.go b/internal/api/s2s/user/userget.go
index abd52da22..105ffd7bc 100644
--- a/internal/api/s2s/user/userget.go
+++ b/internal/api/s2s/user/userget.go
@@ -55,7 +55,7 @@ func (m *Module) UsersGETHandler(c *gin.Context) {
}
l.Tracef("negotiated format: %s", format)
- ctx := populateContext(c)
+ ctx := transferContext(c)
user, errWithCode := m.processor.GetFediUser(ctx, requestedUsername, c.Request.URL) // GetFediUser handles auth as well
if errWithCode != nil {
diff --git a/internal/api/s2s/user/userget_test.go b/internal/api/s2s/user/userget_test.go
index bcf53daa9..f560125ca 100644
--- a/internal/api/s2s/user/userget_test.go
+++ b/internal/api/s2s/user/userget_test.go
@@ -1,3 +1,21 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 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 user_test
import (