Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(activitypub): bind keys to owner host
Gabe Kangase26d7fad79b4
diff --git a/services/activitypub/inbox/key_binding_poc_test.go b/services/activitypub/inbox/key_binding_poc_test.gonew file mode 100644index 0000000000..968187bb54--- /dev/null+++ b/services/activitypub/inbox/key_binding_poc_test.go@@ -0,0 +1,110 @@+package inbox++import (+ "bytes"+ "crypto/rand"+ "crypto/rsa"+ "crypto/x509"+ "encoding/json"+ "encoding/pem"+ "net/http"+ "net/http/httptest"+ "testing"+ "time"++ "github.com/go-fed/httpsig"++ "github.com/owncast/owncast/persistence/configrepository"+ "github.com/owncast/owncast/services/activitypub/apmodels"+ apcrypto "github.com/owncast/owncast/services/activitypub/crypto"+ "github.com/owncast/owncast/services/activitypub/persistence/followersrepository"+ "github.com/owncast/owncast/services/activitypub/resolvers"+ "github.com/owncast/owncast/services/datastore"+)++func TestVerifyRejectsCrossHostKeyOwner(t *testing.T) {+ attackerKey, err := rsa.GenerateKey(rand.Reader, 2048)+ if err != nil {+ t.Fatal(err)+ }+ publicDER, err := x509.MarshalPKIXPublicKey(&attackerKey.PublicKey)+ if err != nil {+ t.Fatal(err)+ }+ publicPEM := string(pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: publicDER}))++ const victim = "https://victim.example/users/alice"+ var keyID string+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ w.Header().Set("Content-Type", "application/activity+json")+ _ = json.NewEncoder(w).Encode(map[string]any{+ "@context": []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},+ "id": victim,+ "type": "Person",+ "preferredUsername": "alice",+ "inbox": victim + "/inbox",+ "publicKey": map[string]any{+ "id": keyID,+ "owner": victim,+ "publicKeyPem": publicPEM,+ },+ })+ }))+ defer server.Close()+ keyID = server.URL + "/key"++ ds, err := datastore.SetupPersistence(":memory:", t.TempDir())+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = ds.DB.Close() })+ cfg := configrepository.New(ds)+ localPrivatePEM, localPublicPEM, err := apcrypto.GenerateKeys()+ if err != nil {+ t.Fatal(err)+ }+ for _, set := range []func() error{+ func() error { return cfg.SetServerURL("https://local.example") },+ func() error { return cfg.SetFederationUsername("streamer") },+ func() error { return cfg.SetPrivateKey(string(localPrivatePEM)) },+ func() error { return cfg.SetPublicKey(string(localPublicPEM)) },+ } {+ if err := set(); err != nil {+ t.Fatal(err)+ }+ }+ localSigner := apcrypto.New(apcrypto.Deps{ConfigRepository: cfg})+ builder := apmodels.New(apmodels.Deps{ConfigRepository: cfg, Signer: localSigner})+ resolver := resolvers.New(resolvers.Deps{ConfigRepository: cfg, Builder: builder, Signer: localSigner})+ service := New(Deps{+ ConfigRepository: cfg,+ Followers: followersrepository.New(ds),+ Resolver: resolver,+ })++ body := []byte(`{"type":"Like","actor":"` + victim + `"}`)+ request, err := http.NewRequest(http.MethodPost, "https://local.example/inbox", bytes.NewReader(body))+ if err != nil {+ t.Fatal(err)+ }+ request.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))+ request.Header.Set("Host", request.URL.Host)+ signer, _, err := httpsig.NewSigner(+ []httpsig.Algorithm{httpsig.RSA_SHA256},+ httpsig.DigestSha256,+ []string{httpsig.RequestTarget, "host", "date", "digest"},+ httpsig.Signature,+ 0,+ )+ if err != nil {+ t.Fatal(err)+ }+ if err := signer.SignRequest(attackerKey, keyID, request, body); err != nil {+ t.Fatal(err)+ }++ owner, err := service.Verify(request, body)+ if err == nil {+ t.Fatalf("expected cross-host key ownership to be rejected, got owner %v", owner)+ }+}diff --git a/services/activitypub/inbox/worker.go b/services/activitypub/inbox/worker.goindex 233548007e..7854ead80e 100644--- a/services/activitypub/inbox/worker.go+++ b/services/activitypub/inbox/worker.go@@ -266,7 +266,11 @@ func (s *Service) Verify(request *http.Request, body []byte) (*url.URL, error) {}if publicKeyActorIRI == nil {- return nil, errors.New("public key owner IRI is empty")+ return nil, errors.New("public key has no owner")+ }++ if !strings.EqualFold(pubKeyID.Hostname(), publicKeyActorIRI.Hostname()) {+ return nil, fmt.Errorf("public key host does not match key owner host")}// Test to see if the actor is in the list of blocked federated domains.diff --git a/services/activitypub/inbox/worker_test.go b/services/activitypub/inbox/worker_test.goindex d7f2982b56..bd615e6fe7 100644--- a/services/activitypub/inbox/worker_test.go+++ b/services/activitypub/inbox/worker_test.go@@ -71,6 +71,9 @@ func TestMain(m *testing.M) {if err := os.Setenv("OWNCAST_ALLOW_INTERNAL_FEDERATION", "true"); err != nil {panic(err)}+ if err := os.Setenv("OWNCAST_INSECURE_SKIP_VERIFY", "true"); err != nil {+ panic(err)+ }ds, err := datastore.SetupPersistence(":memory:", os.TempDir())if err != nil {panic(err)