summaryrefslogtreecommitdiff
path: root/internal/ap/resolve.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2024-01-22 15:38:45 +0100
committerLibravatar GitHub <noreply@github.com>2024-01-22 14:38:45 +0000
commitd9729e7d28bd64a707443a8a7a6b0e4383b14caf (patch)
treea7c9ab3fe8fe3b62b84b025dfc1fbf2f3b4af2b9 /internal/ap/resolve.go
parent[chore]: Bump codeberg.org/gruf/go-mutexes from 1.3.1 to 1.4.0 (#2562) (diff)
downloadgotosocial-d9729e7d28bd64a707443a8a7a6b0e4383b14caf.tar.xz
[bugfix] Don't return Internal Server Error when searching for URIs that don't return AP JSON (#2550)
* [bugfix] Don't return Internal Server Error when searching for URIs that don't return AP JSON * don't pass map pointer
Diffstat (limited to 'internal/ap/resolve.go')
-rw-r--r--internal/ap/resolve.go54
1 files changed, 38 insertions, 16 deletions
diff --git a/internal/ap/resolve.go b/internal/ap/resolve.go
index 4ff4f87fc..20a858900 100644
--- a/internal/ap/resolve.go
+++ b/internal/ap/resolve.go
@@ -27,6 +27,7 @@ import (
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams"
+ "github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
)
@@ -56,6 +57,35 @@ func putMap(m map[string]any) {
mapPool.Put(m)
}
+// bytesToType tries to parse the given bytes slice
+// as a JSON ActivityPub type, failing if the input
+// bytes are not parseable as JSON, or do not parse
+// to an ActivityPub that we can understand.
+//
+// The given map pointer will also be populated with
+// the parsed JSON, to allow further processing.
+func bytesToType(
+ ctx context.Context,
+ b []byte,
+ raw map[string]any,
+) (vocab.Type, error) {
+ // Unmarshal the raw JSON bytes into a "raw" map.
+ // This will fail if the input is not parseable
+ // as JSON; eg., a remote has returned HTML as a
+ // fallback response to an ActivityPub JSON request.
+ if err := json.Unmarshal(b, &raw); err != nil {
+ return nil, gtserror.NewfAt(3, "error unmarshalling bytes into json: %w", err)
+ }
+
+ // Resolve an ActivityStreams type.
+ t, err := streams.ToType(ctx, raw)
+ if err != nil {
+ return nil, gtserror.NewfAt(3, "error resolving json into ap vocab type: %w", err)
+ }
+
+ return t, nil
+}
+
// ResolveActivity is a util function for pulling a pub.Activity type out of an incoming request body,
// returning the resolved activity type, error and whether to accept activity (false = transient i.e. ignore).
func ResolveIncomingActivity(r *http.Request) (pub.Activity, bool, gtserror.WithCode) {
@@ -121,15 +151,11 @@ func ResolveStatusable(ctx context.Context, b []byte) (Statusable, error) {
// destination.
raw := getMap()
- // Unmarshal the raw JSON data in a "raw" JSON map.
- if err := json.Unmarshal(b, &raw); err != nil {
- return nil, gtserror.Newf("error unmarshalling bytes into json: %w", err)
- }
-
- // Resolve an ActivityStreams type from JSON.
- t, err := streams.ToType(ctx, raw)
+ // Convert raw bytes to an AP type.
+ // This will also populate the map.
+ t, err := bytesToType(ctx, b, raw)
if err != nil {
- return nil, gtserror.Newf("error resolving json into ap vocab type: %w", err)
+ return nil, gtserror.SetWrongType(err)
}
// Attempt to cast as Statusable.
@@ -166,15 +192,11 @@ func ResolveAccountable(ctx context.Context, b []byte) (Accountable, error) {
// destination.
raw := getMap()
- // Unmarshal the raw JSON data in a "raw" JSON map.
- if err := json.Unmarshal(b, &raw); err != nil {
- return nil, gtserror.Newf("error unmarshalling bytes into json: %w", err)
- }
-
- // Resolve an ActivityStreams type from JSON.
- t, err := streams.ToType(ctx, raw)
+ // Convert raw bytes to an AP type.
+ // This will also populate the map.
+ t, err := bytesToType(ctx, b, raw)
if err != nil {
- return nil, gtserror.Newf("error resolving json into ap vocab type: %w", err)
+ return nil, gtserror.SetWrongType(err)
}
// Attempt to cast as Statusable.