2023-03-12 16:00:57 +01:00
|
|
|
// 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/>.
|
2021-05-08 14:25:55 +02:00
|
|
|
|
|
|
|
|
package transport
|
|
|
|
|
|
|
|
|
|
import (
|
2024-02-23 15:24:40 +00:00
|
|
|
"bytes"
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2022-05-15 10:16:43 +01:00
|
|
|
"crypto/rsa"
|
|
|
|
|
"crypto/x509"
|
2022-03-15 15:01:19 +01:00
|
|
|
"encoding/json"
|
2025-05-15 09:40:48 +00:00
|
|
|
"errors"
|
2021-05-08 14:25:55 +02:00
|
|
|
"fmt"
|
2024-02-23 15:24:40 +00:00
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2022-03-15 15:01:19 +01:00
|
|
|
"net/url"
|
[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>
2025-04-30 18:19:01 +00:00
|
|
|
"strconv"
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2025-04-25 15:15:36 +02:00
|
|
|
"code.superseriousbusiness.org/activity/pub"
|
2025-04-26 15:34:10 +02:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/ap"
|
[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>
2025-04-30 18:19:01 +00:00
|
|
|
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
|
2025-04-26 15:34:10 +02:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/config"
|
2025-05-15 09:40:48 +00:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/db"
|
2025-04-26 15:34:10 +02:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/federation/federatingdb"
|
2025-05-15 09:40:48 +00:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
2025-04-26 15:34:10 +02:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/state"
|
[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>
2025-04-30 18:19:01 +00:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/util"
|
2022-05-15 10:16:43 +01:00
|
|
|
"codeberg.org/gruf/go-byteutil"
|
2022-11-15 18:45:15 +00:00
|
|
|
"codeberg.org/gruf/go-cache/v3"
|
2021-05-08 14:25:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Controller generates transports for use in making federation requests to other servers.
|
|
|
|
|
type Controller interface {
|
2022-05-15 10:16:43 +01:00
|
|
|
// NewTransport returns an http signature transport with the given public key ID (URL location of pubkey), and the given private key.
|
|
|
|
|
NewTransport(pubKeyID string, privkey *rsa.PrivateKey) (Transport, error)
|
|
|
|
|
|
|
|
|
|
// NewTransportForUsername searches for account with username, and returns result of .NewTransport().
|
2021-08-25 15:34:33 +02:00
|
|
|
NewTransportForUsername(ctx context.Context, username string) (Transport, error)
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type controller struct {
|
2023-03-08 13:57:41 +01:00
|
|
|
state *state.State
|
2025-05-15 09:40:48 +00:00
|
|
|
fedDB *federatingdb.DB
|
2024-04-02 12:12:26 +01:00
|
|
|
client pub.HttpClient
|
2023-08-03 10:34:35 +01:00
|
|
|
trspCache cache.TTLCache[string, *transport]
|
2022-05-15 10:16:43 +01:00
|
|
|
userAgent string
|
2022-03-15 15:01:19 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
// NewController returns an implementation of the Controller interface for creating new transports
|
2025-05-15 09:40:48 +00:00
|
|
|
func NewController(state *state.State, federatingDB *federatingdb.DB, client pub.HttpClient) Controller {
|
2023-05-08 19:03:38 +02:00
|
|
|
var (
|
2024-04-11 10:45:35 +01:00
|
|
|
host = config.GetHost()
|
|
|
|
|
proto = config.GetProtocol()
|
|
|
|
|
version = config.GetSoftwareVersion()
|
2023-05-08 19:03:38 +02:00
|
|
|
)
|
|
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
c := &controller{
|
2023-03-08 13:57:41 +01:00
|
|
|
state: state,
|
2022-05-15 10:16:43 +01:00
|
|
|
fedDB: federatingDB,
|
|
|
|
|
client: client,
|
2023-08-03 10:34:35 +01:00
|
|
|
trspCache: cache.NewTTL[string, *transport](0, 100, 0),
|
2024-02-17 09:54:10 +01:00
|
|
|
userAgent: fmt.Sprintf("gotosocial/%s (+%s://%s)", version, proto, host),
|
2022-03-15 15:01:19 +01:00
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
return c
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
func (c *controller) NewTransport(pubKeyID string, privkey *rsa.PrivateKey) (Transport, error) {
|
|
|
|
|
// Generate public key string for cache key
|
|
|
|
|
//
|
|
|
|
|
// NOTE: it is safe to use the public key as the cache
|
|
|
|
|
// key here as we are generating it ourselves from the
|
|
|
|
|
// private key. If we were simply using a public key
|
|
|
|
|
// provided as argument that would absolutely NOT be safe.
|
|
|
|
|
pubStr := privkeyToPublicStr(privkey)
|
|
|
|
|
|
|
|
|
|
// First check for cached transport
|
2022-10-08 12:50:16 +01:00
|
|
|
transp, ok := c.trspCache.Get(pubStr)
|
2022-05-15 10:16:43 +01:00
|
|
|
if ok {
|
|
|
|
|
return transp, nil
|
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
// Create the transport
|
|
|
|
|
transp = &transport{
|
|
|
|
|
controller: c,
|
|
|
|
|
pubKeyID: pubKeyID,
|
|
|
|
|
privkey: privkey,
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
// Cache this transport under pubkey
|
2022-11-15 18:45:15 +00:00
|
|
|
if !c.trspCache.Add(pubStr, transp) {
|
2022-05-15 10:16:43 +01:00
|
|
|
var cached *transport
|
|
|
|
|
|
2022-10-08 12:50:16 +01:00
|
|
|
cached, ok = c.trspCache.Get(pubStr)
|
2022-05-15 10:16:43 +01:00
|
|
|
if !ok {
|
|
|
|
|
// Some ridiculous race cond.
|
2022-10-08 12:50:16 +01:00
|
|
|
c.trspCache.Set(pubStr, transp)
|
2022-05-15 10:16:43 +01:00
|
|
|
} else {
|
|
|
|
|
// Use already cached
|
|
|
|
|
transp = cached
|
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
return transp, nil
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
2021-08-10 13:32:39 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *controller) NewTransportForUsername(ctx context.Context, username string) (Transport, error) {
|
2021-08-10 13:32:39 +02:00
|
|
|
// We need an account to use to create a transport for dereferecing something.
|
|
|
|
|
// If a username has been given, we can fetch the account with that username and use it.
|
|
|
|
|
// Otherwise, we can take the instance account and use those credentials to make the request.
|
|
|
|
|
var u string
|
|
|
|
|
if username == "" {
|
2022-05-30 13:41:24 +01:00
|
|
|
u = config.GetHost()
|
2021-08-10 13:32:39 +02:00
|
|
|
} else {
|
|
|
|
|
u = username
|
|
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2023-03-08 13:57:41 +01:00
|
|
|
ourAccount, err := c.state.DB.GetAccountByUsernameDomain(ctx, u, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return nil, fmt.Errorf("error getting account %s from db: %s", username, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transport, err := c.NewTransport(ourAccount.PublicKeyURI, ourAccount.PrivateKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error creating transport for user %s: %s", username, err)
|
|
|
|
|
}
|
2022-09-02 10:56:33 +01:00
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
return transport, nil
|
|
|
|
|
}
|
2022-05-15 10:16:43 +01: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>
2025-04-30 18:19:01 +00:00
|
|
|
// dereferenceLocal is a shortcut to try dereferencing
|
|
|
|
|
// something on this instance without making any http calls.
|
2022-05-15 10:16:43 +01: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>
2025-04-30 18:19:01 +00:00
|
|
|
// 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) {
|
2022-05-15 10:16:43 +01:00
|
|
|
|
2025-05-15 09:40:48 +00:00
|
|
|
// Try fetch via federating DB.
|
|
|
|
|
t, err := c.fedDB.Get(ctx, uri)
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
// No problem.
|
|
|
|
|
case err == nil:
|
|
|
|
|
|
|
|
|
|
// Catch and handle objects not found.
|
|
|
|
|
case errors.Is(err, db.ErrNoEntries):
|
|
|
|
|
return &http.Response{
|
|
|
|
|
Request: &http.Request{URL: uri},
|
|
|
|
|
Status: http.StatusText(http.StatusNotFound),
|
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
|
Header: map[string][]string{
|
|
|
|
|
"Content-Type": {apiutil.AppActivityLDJSON},
|
|
|
|
|
"Content-Length": {"0"},
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
|
|
// Any other.
|
|
|
|
|
default:
|
|
|
|
|
return nil, gtserror.Newf("error getting: %w", err)
|
2024-02-23 15:24:40 +00: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>
2025-04-30 18:19:01 +00:00
|
|
|
if util.IsNil(t) {
|
2025-05-15 09:40:48 +00:00
|
|
|
// Assert this should never happen.
|
|
|
|
|
panic(gtserror.New("nil vocab.Type"))
|
2024-02-23 15:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 09:40:48 +00:00
|
|
|
// Serialize type to JSON map.
|
|
|
|
|
m, err := ap.Serialize(t)
|
2022-05-15 10:16:43 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 09:40:48 +00:00
|
|
|
// Marshal JSON to bytes.
|
|
|
|
|
b, err := json.Marshal(m)
|
2024-02-23 15:24:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 09:40:48 +00:00
|
|
|
// Return a response
|
|
|
|
|
// with AS data as body.
|
|
|
|
|
contentLength := len(b)
|
[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>
2025-04-30 18:19:01 +00:00
|
|
|
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)},
|
|
|
|
|
},
|
2024-07-26 12:04:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rsp, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 10:16:43 +01:00
|
|
|
// privkeyToPublicStr will create a string representation of RSA public key from private.
|
|
|
|
|
func privkeyToPublicStr(privkey *rsa.PrivateKey) string {
|
|
|
|
|
b := x509.MarshalPKCS1PublicKey(&privkey.PublicKey)
|
|
|
|
|
return byteutil.B2S(b)
|
|
|
|
|
}
|