Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

feat(ap): harden inbound ActivityPub handling

Gabe Kangasf161b2b5b1eb
diff --git a/services/activitypub/inbox/accept.go b/services/activitypub/inbox/accept.go
index f055d2eb83..f603bb2d35 100644
--- a/services/activitypub/inbox/accept.go
+++ b/services/activitypub/inbox/accept.go
@@ -83,11 +83,14 @@ func (s *Service) markFederatedServerAccepted(actorIRI string) {
if actorData, err := s.resolver.GetResolvedActorFromIRI(actorIRI); err == nil {
var logoURL string
if actorData.Image != nil {
- logoURL = actorData.Image.String()
+ logoURL = truncateMetadata(actorData.Image.String(), maxMetadataURLLen)
}
+ // Clamp the attacker-controlled remote actor fields before storing.
// Summary falls back to the display name; Owncast actors don't
// expose a separate summary field on the Person object.
- if err := repo.UpdateServerMetadata(serverURL, actorData.Username, actorData.Name, actorData.Name, logoURL); err != nil {
+ name := truncateMetadata(actorData.Username, maxServerNameLen)
+ displayName := truncateMetadata(actorData.Name, maxServerNameLen)
+ if err := repo.UpdateServerMetadata(serverURL, name, displayName, displayName, logoURL); err != nil {
log.Errorf("Failed to update server metadata for %s: %v", serverURL, err)
}
}
diff --git a/services/activitypub/inbox/binding_test.go b/services/activitypub/inbox/binding_test.go
new file mode 100644
index 0000000000..59835a8038
--- /dev/null
+++ b/services/activitypub/inbox/binding_test.go
@@ -0,0 +1,108 @@
+package inbox
+
+import (
+ "net/url"
+ "testing"
+)
+
+func TestActorIRIFromActivity(t *testing.T) {
+ tests := []struct {
+ name string
+ body string
+ want string
+ wantErr bool
+ }{
+ {
+ name: "string actor",
+ body: `{"type":"Offer","actor":"https://remote.example/federation/user/streamer"}`,
+ want: "https://remote.example/federation/user/streamer",
+ },
+ {
+ name: "object actor with id",
+ body: `{"type":"Follow","actor":{"id":"https://remote.example/federation/user/streamer","type":"Service"}}`,
+ want: "https://remote.example/federation/user/streamer",
+ },
+ {
+ name: "array actor",
+ body: `{"type":"Leave","actor":["https://remote.example/federation/user/streamer"]}`,
+ want: "https://remote.example/federation/user/streamer",
+ },
+ {
+ name: "missing actor",
+ body: `{"type":"Offer"}`,
+ wantErr: true,
+ },
+ {
+ name: "empty actor",
+ body: `{"type":"Offer","actor":""}`,
+ wantErr: true,
+ },
+ {
+ name: "invalid json",
+ body: `{not json`,
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := actorIRIFromActivity([]byte(tt.body))
+ if (err != nil) != tt.wantErr {
+ t.Fatalf("actorIRIFromActivity() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ if !tt.wantErr && got != tt.want {
+ t.Errorf("actorIRIFromActivity() = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestSameActorOrigin(t *testing.T) {
+ keyOwner, _ := url.Parse("https://remote.example/federation/user/streamer")
+
+ tests := []struct {
+ name string
+ actorIRI string
+ want bool
+ }{
+ {
+ name: "same host exact actor",
+ actorIRI: "https://remote.example/federation/user/streamer",
+ want: true,
+ },
+ {
+ name: "same host different path",
+ actorIRI: "https://remote.example/federation/user/someoneelse",
+ want: true,
+ },
+ {
+ name: "same host case-insensitive",
+ actorIRI: "https://REMOTE.example/federation/user/streamer",
+ want: true,
+ },
+ {
+ // The core spoofing case: a different host claiming to be us.
+ name: "different host is rejected",
+ actorIRI: "https://attacker.evil/federation/user/streamer",
+ want: false,
+ },
+ {
+ name: "empty actor is rejected",
+ actorIRI: "",
+ want: false,
+ },
+ {
+ name: "non-http scheme different host is rejected",
+ actorIRI: "javascript:alert(1)",
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := sameActorOrigin(tt.actorIRI, keyOwner); got != tt.want {
+ t.Errorf("sameActorOrigin(%q) = %v, want %v", tt.actorIRI, got, tt.want)
+ }
+ })
+ }
+}
diff --git a/services/activitypub/inbox/metadata_clamp_test.go b/services/activitypub/inbox/metadata_clamp_test.go
new file mode 100644
index 0000000000..af76a9950f
--- /dev/null
+++ b/services/activitypub/inbox/metadata_clamp_test.go
@@ -0,0 +1,64 @@
+package inbox
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/owncast/owncast/services/activitypub/apmodels"
+)
+
+func TestTruncateMetadata(t *testing.T) {
+ if got := truncateMetadata("hello", 100); got != "hello" {
+ t.Errorf("short string should be unchanged, got %q", got)
+ }
+ if got := truncateMetadata("hello world", 5); got != "hello" {
+ t.Errorf("expected %q, got %q", "hello", got)
+ }
+ // UTF-8 safety: truncation must not split a multi-byte rune.
+ if got := truncateMetadata("héllo", 2); got != "hé" {
+ t.Errorf("expected %q, got %q", "hé", got)
+ }
+ if got := truncateMetadata("abc", 0); got != "" {
+ t.Errorf("max 0 should yield empty, got %q", got)
+ }
+}
+
+func TestClampTags(t *testing.T) {
+ many := make([]string, maxTags+10)
+ for i := range many {
+ many[i] = "tag"
+ }
+ if got := clampTags(many); len(got) != maxTags {
+ t.Errorf("expected tag count clamped to %d, got %d", maxTags, len(got))
+ }
+
+ long := []string{strings.Repeat("x", maxTagLen+50)}
+ clamped := clampTags(long)
+ if len([]rune(clamped[0])) != maxTagLen {
+ t.Errorf("expected each tag clamped to %d runes, got %d", maxTagLen, len([]rune(clamped[0])))
+ }
+}
+
+func TestBuildStreamUpdateClampsHostileMetadata(t *testing.T) {
+ hostile := &apmodels.OwncastMetadata{
+ StreamTitle: strings.Repeat("T", maxStreamTitleLen+500),
+ StreamDescription: strings.Repeat("D", maxStreamDescriptionLen+500),
+ ThumbnailURL: "https://example.com/" + strings.Repeat("a", maxMetadataURLLen+500),
+ Tags: make([]string, maxTags+5),
+ }
+
+ update := buildStreamUpdateFromMetadata(hostile)
+
+ if update.Title == nil || len([]rune(*update.Title)) != maxStreamTitleLen {
+ t.Errorf("stream title not clamped to %d", maxStreamTitleLen)
+ }
+ if update.Description == nil || len([]rune(*update.Description)) != maxStreamDescriptionLen {
+ t.Errorf("stream description not clamped to %d", maxStreamDescriptionLen)
+ }
+ if update.ThumbnailURL == nil || len([]rune(*update.ThumbnailURL)) != maxMetadataURLLen {
+ t.Errorf("thumbnail URL not clamped to %d", maxMetadataURLLen)
+ }
+ if len(update.Tags) != maxTags {
+ t.Errorf("tags not clamped to %d, got %d", maxTags, len(update.Tags))
+ }
+}
diff --git a/services/activitypub/inbox/offer.go b/services/activitypub/inbox/offer.go
index 6ddc286541..625bf3fc32 100644
--- a/services/activitypub/inbox/offer.go
+++ b/services/activitypub/inbox/offer.go
@@ -107,21 +107,60 @@ func shouldProcessOfferFromServer(repo federatedserversrepository.FederatedServe
return true
}
+// Bounds on attacker-controlled remote metadata. A hostile peer can send
+// anything in these fields, so clamp them before storing to avoid DB bloat and
+// UI breakage.
+const (
+ maxStreamTitleLen = 300
+ maxStreamDescriptionLen = 2000
+ maxServerNameLen = 200
+ maxMetadataURLLen = 2048
+ maxTags = 20
+ maxTagLen = 100
+)
+
+// truncateMetadata returns s limited to max runes (UTF-8 safe).
+func truncateMetadata(s string, max int) string {
+ if max <= 0 {
+ return ""
+ }
+ r := []rune(s)
+ if len(r) <= max {
+ return s
+ }
+ return string(r[:max])
+}
+
+// clampTags limits both the number of tags and the length of each.
+func clampTags(tags []string) []string {
+ if len(tags) > maxTags {
+ tags = tags[:maxTags]
+ }
+ clamped := make([]string, 0, len(tags))
+ for _, t := range tags {
+ clamped = append(clamped, truncateMetadata(t, maxTagLen))
+ }
+ return clamped
+}
+
func buildStreamUpdateFromMetadata(metadata *apmodels.OwncastMetadata) *models.FederatedStreamUpdate {
var streamTitle, streamDescription, thumbnailURL *string
var tags []string
if metadata.StreamTitle != "" {
- streamTitle = &metadata.StreamTitle
+ t := truncateMetadata(metadata.StreamTitle, maxStreamTitleLen)
+ streamTitle = &t
}
if metadata.StreamDescription != "" {
- streamDescription = &metadata.StreamDescription
+ d := truncateMetadata(metadata.StreamDescription, maxStreamDescriptionLen)
+ streamDescription = &d
}
if metadata.ThumbnailURL != "" {
- thumbnailURL = &metadata.ThumbnailURL
+ u := truncateMetadata(metadata.ThumbnailURL, maxMetadataURLLen)
+ thumbnailURL = &u
}
if len(metadata.Tags) > 0 {
- tags = metadata.Tags
+ tags = clampTags(metadata.Tags)
}
return &models.FederatedStreamUpdate{
diff --git a/services/activitypub/inbox/worker.go b/services/activitypub/inbox/worker.go
index ab149d4f0b..f0e874c944 100644
--- a/services/activitypub/inbox/worker.go
+++ b/services/activitypub/inbox/worker.go
@@ -3,11 +3,13 @@ package inbox
import (
"context"
"crypto/x509"
+ "encoding/json"
"encoding/pem"
"fmt"
"net/http"
"net/url"
"strings"
+ "time"
"github.com/go-fed/httpsig"
"github.com/pkg/errors"
@@ -17,11 +19,30 @@ import (
)
func (s *Service) handle(request apmodels.InboxRequest) {
- if verified, err := s.Verify(request.Request); err != nil {
+ keyOwner, err := s.Verify(request.Request)
+ if err != nil {
log.Debugln("Error in attempting to verify request", err)
return
- } else if !verified {
- log.Debugln("Request failed verification", err)
+ }
+ if keyOwner == nil {
+ log.Debugln("Request failed verification")
+ return
+ }
+
+ // Bind the activity to its signer. A validly signed request may only carry
+ // activities authored by the verified key's owner; otherwise a server could
+ // sign with its own key while claiming, in the body, to be a different actor
+ // (e.g. forging a featured-stream Offer/Leave/Accept as another server). We
+ // compare origins (hostname), the standard fediverse binding, which is
+ // robust to id/fragment formatting differences. Fail closed: if no actor
+ // IRI can be determined the activity is rejected.
+ actorIRI, err := actorIRIFromActivity(request.Body)
+ if err != nil {
+ log.Warnln("rejecting inbound activity: unable to bind actor to signing key:", err)
+ return
+ }
+ if !sameActorOrigin(actorIRI, keyOwner) {
+ log.Warnf("rejecting inbound activity: actor %q does not match signing key owner %q", actorIRI, keyOwner.String())
return
}
@@ -30,27 +51,120 @@ func (s *Service) handle(request apmodels.InboxRequest) {
}
}
-// Verify will Verify the http signature of an inbound request as well as
-// check it against the list of blocked domains.
+// actorIRIFromActivity extracts the top-level "actor" IRI from a raw inbound
+// ActivityPub activity. The actor may be a string IRI, an object with an "id",
+// or an array of either; the first usable IRI is returned.
+func actorIRIFromActivity(body []byte) (string, error) {
+ var envelope struct {
+ Actor json.RawMessage `json:"actor"`
+ }
+ if err := json.Unmarshal(body, &envelope); err != nil {
+ return "", errors.Wrap(err, "unable to parse activity body")
+ }
+ return iriFromRawActor(envelope.Actor)
+}
+
+func iriFromRawActor(raw json.RawMessage) (string, error) {
+ if len(raw) == 0 {
+ return "", errors.New("activity has no actor")
+ }
+
+ var asString string
+ if err := json.Unmarshal(raw, &asString); err == nil {
+ if asString == "" {
+ return "", errors.New("actor IRI is empty")
+ }
+ return asString, nil
+ }
+
+ var asObject struct {
+ ID string `json:"id"`
+ }
+ if err := json.Unmarshal(raw, &asObject); err == nil && asObject.ID != "" {
+ return asObject.ID, nil
+ }
+
+ var asArray []json.RawMessage
+ if err := json.Unmarshal(raw, &asArray); err == nil && len(asArray) > 0 {
+ return iriFromRawActor(asArray[0])
+ }
+
+ return "", errors.New("unable to determine actor IRI from activity")
+}
+
+// sameActorOrigin reports whether the activity actor IRI and the verified key
+// owner share the same host. Cross-host mismatches are rejected, which is what
+// prevents one server from forging activities as another.
+func sameActorOrigin(actorIRI string, keyOwner *url.URL) bool {
+ actor, err := url.Parse(actorIRI)
+ if err != nil {
+ return false
+ }
+ return actor.Hostname() != "" && strings.EqualFold(actor.Hostname(), keyOwner.Hostname())
+}
+
+// maxRequestDateAge / maxRequestDateSkew bound how far an inbound request's
+// signed Date may be from now. The window is generous to tolerate clock skew
+// and delivery/queue delay while still limiting how long a captured,
+// validly-signed request can be replayed.
+const (
+ maxRequestDateAge = 1 * time.Hour
+ maxRequestDateSkew = 1 * time.Hour
+)
+
+// requestDateWithinTolerance checks that a present, parseable Date header is
+// recent. It is intentionally lenient: a missing or unparseable Date is not an
+// error (to avoid rejecting otherwise-valid federation from senders that omit
+// or format it unusually). Senders that sign a standard Date — Owncast,
+// Mastodon, etc. — get replay bounding because the signed Date can't be altered
+// without invalidating the signature.
+func requestDateWithinTolerance(request *http.Request) error {
+ dateHeader := request.Header.Get("Date")
+ if dateHeader == "" {
+ return nil
+ }
+
+ sent, err := http.ParseTime(dateHeader)
+ if err != nil {
+ log.Debugln("inbound request has an unparseable Date header, skipping freshness check:", dateHeader)
+ return nil
+ }
+
+ now := time.Now()
+ if now.Sub(sent) > maxRequestDateAge {
+ return fmt.Errorf("request Date is too old (possible replay): %s", dateHeader)
+ }
+ if sent.Sub(now) > maxRequestDateSkew {
+ return fmt.Errorf("request Date is too far in the future: %s", dateHeader)
+ }
+
+ return nil
+}
+
+// Verify validates the HTTP signature of an inbound request against the
+// signing actor's public key and checks it against blocked domains/actors. On
+// success it returns the verified key owner's actor IRI (used by the caller to
+// bind the activity's actor to its signer); on failure it returns a nil IRI
+// and an error.
// nolint: cyclop
-func (s *Service) Verify(request *http.Request) (bool, error) {
+func (s *Service) Verify(request *http.Request) (*url.URL, error) {
verifier, err := httpsig.NewVerifier(request)
if err != nil {
- return false, errors.Wrap(err, "failed to create key verifier for request")
+ return nil, errors.Wrap(err, "failed to create key verifier for request")
}
pubKeyID, err := url.Parse(verifier.KeyId())
if err != nil {
- return false, errors.Wrap(err, "failed to parse key to get key ID")
+ return nil, errors.Wrap(err, "failed to parse key to get key ID")
}
// Force federation only via servers using https.
if pubKeyID.Scheme != "https" {
- return false, errors.New("federated servers must use https: " + pubKeyID.String())
+ return nil, errors.New("federated servers must use https: " + pubKeyID.String())
}
signature := request.Header.Get("signature")
if signature == "" {
- return false, errors.New("http signature header not found in request")
+ return nil, errors.New("http signature header not found in request")
}
var algorithmString string
@@ -65,12 +179,12 @@ func (s *Service) Verify(request *http.Request) (bool, error) {
algorithmString = strings.Trim(algorithmString, "\"")
if algorithmString == "" {
- return false, errors.New("Unable to determine algorithm to verify request")
+ return nil, errors.New("Unable to determine algorithm to verify request")
}
publicKey, err := s.resolver.GetResolvedPublicKeyFromIRI(pubKeyID.String())
if err != nil {
- return false, errors.Wrap(err, "failed to resolve actor from IRI to fetch key")
+ return nil, errors.Wrap(err, "failed to resolve actor from IRI to fetch key")
}
var publicKeyActorIRI *url.URL
@@ -79,33 +193,33 @@ func (s *Service) Verify(request *http.Request) (bool, error) {
}
if publicKeyActorIRI == nil {
- return false, errors.New("public key owner IRI is empty")
+ return nil, errors.New("public key owner IRI is empty")
}
// Test to see if the actor is in the list of blocked federated domains.
if s.isBlockedDomain(publicKeyActorIRI.Hostname()) {
- return false, errors.New("domain is blocked")
+ return nil, errors.New("domain is blocked")
}
// If actor is specifically blocked, then fail validation.
if blocked, err := s.isBlockedActor(publicKeyActorIRI); err != nil || blocked {
- return false, err
+ return nil, err
}
key, err := apmodels.GetPublicKeyPem(publicKey)
if err != nil {
- return false, errors.Wrap(err, "failed to get public key PEM")
+ return nil, errors.Wrap(err, "failed to get public key PEM")
}
block, _ := pem.Decode([]byte(key))
if block == nil {
log.Errorln("failed to parse PEM block containing the public key")
- return false, errors.New("failed to parse PEM block containing the public key")
+ return nil, errors.New("failed to parse PEM block containing the public key")
}
parsedKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
log.Errorln("failed to parse DER encoded public key: " + err.Error())
- return false, errors.Wrap(err, "failed to parse DER encoded public key")
+ return nil, errors.Wrap(err, "failed to parse DER encoded public key")
}
algos := []httpsig.Algorithm{
@@ -120,13 +234,19 @@ func (s *Service) Verify(request *http.Request) (bool, error) {
if _, tried := triedAlgos[algorithm]; !tried {
err := verifier.Verify(parsedKey, algorithm)
if err == nil {
- return true, nil
+ // Bound replay of captured, validly-signed requests: a replayed
+ // capture carries its original (signed) Date, so reject ones
+ // outside a tolerance window.
+ if dateErr := requestDateWithinTolerance(request); dateErr != nil {
+ return nil, dateErr
+ }
+ return publicKeyActorIRI, nil
}
triedAlgos[algorithm] = err
}
}
- return false, fmt.Errorf("http signature verification error(s) for: %s: %+v", pubKeyID.String(), triedAlgos)
+ return nil, fmt.Errorf("http signature verification error(s) for: %s: %+v", pubKeyID.String(), triedAlgos)
}
func (s *Service) isBlockedDomain(domain string) bool {
diff --git a/services/activitypub/resolvers/resolve.go b/services/activitypub/resolvers/resolve.go
index 4a797e355e..37372cee09 100644
--- a/services/activitypub/resolvers/resolve.go
+++ b/services/activitypub/resolvers/resolve.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
+ "net/url"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
@@ -50,7 +51,23 @@ func (r *Resolver) Resolve(c context.Context, data []byte, callbacks ...interfac
func (r *Resolver) ResolveIRI(c context.Context, iri string, callbacks ...interface{}) error {
log.Debugln("Resolving", iri)
- req, _ := http.NewRequest(http.MethodGet, iri, nil)
+ // Guard against SSRF: never resolve an IRI that points at an internal or
+ // loopback address. This covers all resolution, including the signing key
+ // (keyId) fetched while verifying unauthenticated inbound requests, so a
+ // crafted keyId/actor cannot make us reach internal services. The
+ // retryable client additionally re-validates each redirect hop.
+ parsedIRI, err := url.Parse(iri)
+ if err != nil {
+ return errors.Wrap(err, "unable to parse IRI for resolution")
+ }
+ if utils.IsHostnameInternal(parsedIRI.Hostname()) {
+ return errors.Errorf("refusing to resolve internal address: %s", iri)
+ }
+
+ req, err := http.NewRequestWithContext(c, http.MethodGet, iri, nil)
+ if err != nil {
+ return errors.Wrap(err, "unable to build resolution request")
+ }
req.Header.Set("Accept", "application/activity+json, application/ld+json")
actor := r.builder.MakeLocalIRIForAccount(r.configRepository.GetDefaultFederationUsername())
@@ -70,7 +87,9 @@ func (r *Resolver) ResolveIRI(c context.Context, iri string, callbacks ...interf
return errors.New("request failed with status: " + response.Status)
}
- data, err := io.ReadAll(response.Body)
+ // Cap the response size to avoid memory exhaustion from a hostile peer.
+ const maxResolveResponseBytes = 1 << 20 // 1 MiB
+ data, err := io.ReadAll(io.LimitReader(response.Body, maxResolveResponseBytes))
if err != nil {
return err
}
diff --git a/utils/tlsconfig.go b/utils/tlsconfig.go
index d6e41c047e..14f0c138ff 100644
--- a/utils/tlsconfig.go
+++ b/utils/tlsconfig.go
@@ -2,6 +2,7 @@ package utils
import (
"crypto/tls"
+ "fmt"
"net/http"
"os"
"sync"
@@ -67,6 +68,19 @@ func GetRetryableHTTPClient() *http.Client {
})
retryClient.HTTPClient.Transport = transport
+ // Re-validate every redirect hop so a public URL can't 302 to an internal
+ // address (SSRF). Honors OWNCAST_ALLOW_INTERNAL_FEDERATION via
+ // IsHostnameInternal, so local-instance test federation still works.
+ retryClient.HTTPClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
+ if len(via) >= 10 {
+ return fmt.Errorf("stopped after 10 redirects")
+ }
+ if IsHostnameInternal(req.URL.Hostname()) {
+ return fmt.Errorf("refusing to follow redirect to internal host: %s", req.URL.Hostname())
+ }
+ return nil
+ }
+
client := retryClient.StandardClient()
client.Timeout = 8 * time.Second // Short timeout - legitimate servers respond quickly
return client