mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 13:32:25 -05:00
[bugfix] Fix wrong URI used in dereferenceLocal (#4097)
# Description > If this is a code change, please include a summary of what you've coded, and link to the issue(s) it closes/implements. > > If this is a documentation change, please briefly describe what you've changed and why. Previously we were passing a *followers* URI into the federatingDB `Followers` function, which expects an *account* URI. This led to a nil body when doing internal dereferences (ie., while sending out messages). I think I probably wrote this bug when I refactored stuff a while ago. This PR fixes that up by making the dereferenceLocal function more consistent, and falling back to an http dereference if nothing found. Clarifies behavior of the federatingdb.Get function as well, by commenting the shit out of it. Also adds tests, precious tests. Closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4066 ## Checklist Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]` If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want). - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [x] I/we have performed a self-review of added code. - [x] I/we have written code that is legible and maintainable by others. - [x] I/we have commented the added code, particularly in hard-to-understand areas. - [x] I/we have made any necessary changes to documentation. - [x] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [x] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4097 Co-authored-by: tobi <tobi.smethurst@protonmail.com> Co-committed-by: tobi <tobi.smethurst@protonmail.com>
This commit is contained in:
parent
90f5004db0
commit
1bad36b637
5 changed files with 385 additions and 122 deletions
|
|
@ -23,18 +23,20 @@ import (
|
|||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"code.superseriousbusiness.org/activity/pub"
|
||||
"code.superseriousbusiness.org/activity/streams/vocab"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/ap"
|
||||
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/config"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/db"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/federation/federatingdb"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/state"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/util"
|
||||
"codeberg.org/gruf/go-byteutil"
|
||||
"codeberg.org/gruf/go-cache/v3"
|
||||
)
|
||||
|
|
@ -140,23 +142,37 @@ func (c *controller) NewTransportForUsername(ctx context.Context, username strin
|
|||
return transport, nil
|
||||
}
|
||||
|
||||
// dereferenceLocalFollowers is a shortcut to dereference followers of an
|
||||
// account on this instance, without making any external api/http calls.
|
||||
// dereferenceLocal is a shortcut to try dereferencing
|
||||
// something on this instance without making any http calls.
|
||||
//
|
||||
// It is passed to new transports, and should only be invoked when the iri.Host == this host.
|
||||
func (c *controller) dereferenceLocalFollowers(ctx context.Context, iri *url.URL) (*http.Response, error) {
|
||||
followers, err := c.fedDB.Followers(ctx, iri)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
// Will return an error if nothing could be found, indicating that
|
||||
// the calling transport should continue with an http call anyway.
|
||||
//
|
||||
// It should only be invoked when the iri.Host == this host.
|
||||
func (c *controller) dereferenceLocal(
|
||||
ctx context.Context,
|
||||
uri *url.URL,
|
||||
) (*http.Response, error) {
|
||||
var (
|
||||
t vocab.Type
|
||||
err error
|
||||
)
|
||||
|
||||
t, err = c.fedDB.Get(ctx, uri)
|
||||
if err != nil {
|
||||
// Don't check especially for
|
||||
// db.ErrNoEntries, as we *want*
|
||||
// to pass this back to the caller
|
||||
// if we didn't get anything.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if followers == nil {
|
||||
// Return a generic 404 not found response.
|
||||
rsp := craftResponse(iri, http.StatusNotFound)
|
||||
return rsp, nil
|
||||
if util.IsNil(t) {
|
||||
// This should never happen.
|
||||
panic("nil vocab.Type after successful c.fedDB.Get call")
|
||||
}
|
||||
|
||||
i, err := ap.Serialize(followers)
|
||||
i, err := ap.Serialize(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -165,86 +181,24 @@ func (c *controller) dereferenceLocalFollowers(ctx context.Context, iri *url.URL
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contentLength := len(b)
|
||||
|
||||
// Return a response with AS data as body.
|
||||
rsp := craftResponse(iri, http.StatusOK)
|
||||
rsp.Body = io.NopCloser(bytes.NewReader(b))
|
||||
rsp := &http.Response{
|
||||
Request: &http.Request{URL: uri},
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader(b)),
|
||||
ContentLength: int64(contentLength),
|
||||
Header: map[string][]string{
|
||||
"Content-Type": {apiutil.AppActivityLDJSON},
|
||||
"Content-Length": {strconv.Itoa(contentLength)},
|
||||
},
|
||||
}
|
||||
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
// dereferenceLocalUser is a shortcut to dereference followers an account on
|
||||
// this instance, without making any external api/http calls.
|
||||
//
|
||||
// It is passed to new transports, and should only be invoked when the iri.Host == this host.
|
||||
func (c *controller) dereferenceLocalUser(ctx context.Context, iri *url.URL) (*http.Response, error) {
|
||||
user, err := c.fedDB.Get(ctx, iri)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
// Return a generic 404 not found response.
|
||||
rsp := craftResponse(iri, http.StatusNotFound)
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
i, err := ap.Serialize(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := json.Marshal(i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return a response with AS data as body.
|
||||
rsp := craftResponse(iri, http.StatusOK)
|
||||
rsp.Body = io.NopCloser(bytes.NewReader(b))
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
// dereferenceLocalAccept is a shortcut to dereference an accept created
|
||||
// by an account on this instance, without making any external api/http calls.
|
||||
//
|
||||
// It is passed to new transports, and should only be invoked when the iri.Host == this host.
|
||||
func (c *controller) dereferenceLocalAccept(ctx context.Context, iri *url.URL) (*http.Response, error) {
|
||||
accept, err := c.fedDB.GetAccept(ctx, iri)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if accept == nil {
|
||||
// Return a generic 404 not found response.
|
||||
rsp := craftResponse(iri, http.StatusNotFound)
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
i, err := ap.Serialize(accept)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := json.Marshal(i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return a response with AS data as body.
|
||||
rsp := craftResponse(iri, http.StatusOK)
|
||||
rsp.Body = io.NopCloser(bytes.NewReader(b))
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
func craftResponse(url *url.URL, code int) *http.Response {
|
||||
rsp := new(http.Response)
|
||||
rsp.Request = new(http.Request)
|
||||
rsp.Request.URL = url
|
||||
rsp.Status = http.StatusText(code)
|
||||
rsp.StatusCode = code
|
||||
return rsp
|
||||
}
|
||||
|
||||
// privkeyToPublicStr will create a string representation of RSA public key from private.
|
||||
func privkeyToPublicStr(privkey *rsa.PrivateKey) string {
|
||||
b := x509.MarshalPKCS1PublicKey(&privkey.PublicKey)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue