Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(security): restrict browser push destinations (#5049)
Gabe Kangasea11746363ef
diff --git a/notifications/browser/browser.go b/notifications/browser/browser.goindex 6584d5c81a..6cbba945ec 100644--- a/notifications/browser/browser.go+++ b/notifications/browser/browser.go@@ -7,6 +7,7 @@ import ("github.com/pkg/errors""github.com/owncast/owncast/services/datastore"+ "github.com/owncast/owncast/utils")// Browser is an instance of the Browser service.@@ -35,6 +36,19 @@ func GenerateBrowserPushKeys() (string, string, error) {return privateKey, publicKey, nil}+// ParseAndValidateSubscription decodes a browser push subscription and+// requires a public HTTPS endpoint.+func ParseAndValidateSubscription(subscription string) (*webpush.Subscription, error) {+ s := &webpush.Subscription{}+ if err := json.Unmarshal([]byte(subscription), s); err != nil {+ return nil, errors.Wrap(err, "error decoding destination subscription")+ }+ if err := utils.ValidatePublicHTTPSURL(s.Endpoint); err != nil {+ return nil, errors.Wrap(err, "invalid browser push endpoint")+ }+ return s, nil+}+// Send will send a browser push notification to the given subscription.func (b *Browser) Send(subscription string,@@ -58,14 +72,14 @@ func (b *Browser) Send(return false, errors.Wrap(err, "error marshalling web push message")}- // Decode subscription- s := &webpush.Subscription{}- if err := json.Unmarshal([]byte(subscription), s); err != nil {- return false, errors.Wrap(err, "error decoding destination subscription")+ s, err := ParseAndValidateSubscription(subscription)+ if err != nil {+ return false, err}// Send Notificationresp, err := webpush.SendNotification(d, s, &webpush.Options{+ HTTPClient: utils.GetPublicHTTPClient(),VAPIDPublicKey: b.publicKey,VAPIDPrivateKey: b.privateKey,Topic: "owncast-go-live",diff --git a/notifications/browser/browser_test.go b/notifications/browser/browser_test.gonew file mode 100644index 0000000000..4bbbbab105--- /dev/null+++ b/notifications/browser/browser_test.go@@ -0,0 +1,26 @@+package browser++import "testing"++func TestParseAndValidateSubscription(t *testing.T) {+ valid := `{"endpoint":"https://93.184.216.34/push","keys":{"auth":"auth","p256dh":"key"}}`+ if _, err := ParseAndValidateSubscription(valid); err != nil {+ t.Fatalf("expected public HTTPS endpoint to be accepted: %v", err)+ }++ for _, subscription := range []string{+ `{"endpoint":"http://93.184.216.34/push"}`,+ `{"endpoint":"https://127.0.0.1/push"}`,+ `{"endpoint":"https://169.254.169.254/latest"}`,+ `{"endpoint":"https://100.64.0.1/push"}`,+ `{"endpoint":"https://[2002:a00:1::]/push"}`,+ `{"endpoint":"https://does-not-exist.invalid/push"}`,+ `not json`,+ } {+ t.Run(subscription, func(t *testing.T) {+ if _, err := ParseAndValidateSubscription(subscription); err == nil {+ t.Fatalf("expected subscription %q to be rejected", subscription)+ }+ })+ }+}diff --git a/webserver/handlers/notifications.go b/webserver/handlers/notifications.goindex 706e9160fb..28b16b904c 100644--- a/webserver/handlers/notifications.go+++ b/webserver/handlers/notifications.go@@ -7,6 +7,7 @@ import (log "github.com/sirupsen/logrus""github.com/owncast/owncast/models"+ browsernotifications "github.com/owncast/owncast/notifications/browser"notificationsrepo "github.com/owncast/owncast/persistence/notificationsrepository""github.com/owncast/owncast/utils"webutils "github.com/owncast/owncast/webserver/utils"@@ -43,6 +44,12 @@ func (h *Handlers) RegisterForLiveNotifications(u models.User, w http.ResponseWrreturn}+ if _, err := browsernotifications.ParseAndValidateSubscription(req.Destination); err != nil {+ log.Debugln(err)+ webutils.WriteSimpleResponse(w, false, "invalid browser notification destination")+ return+ }+if err := h.notificationsRepository.AddNotification(req.Channel, req.Destination); err != nil {log.Errorln(err)webutils.WriteSimpleResponse(w, false, "unable to save notification")diff --git a/webserver/handlers/notifications_test.go b/webserver/handlers/notifications_test.gonew file mode 100644index 0000000000..43e8e75bde--- /dev/null+++ b/webserver/handlers/notifications_test.go@@ -0,0 +1,76 @@+package handlers++import (+ "net/http"+ "net/http/httptest"+ "strconv"+ "strings"+ "testing"++ "github.com/owncast/owncast/models"+ notificationsrepo "github.com/owncast/owncast/persistence/notificationsrepository"+)++type notificationRepositoryStub struct {+ addedChannel string+ addedDestination string+}++func (r *notificationRepositoryStub) AddNotification(channel, destination string) error {+ r.addedChannel = channel+ r.addedDestination = destination+ return nil+}++func (*notificationRepositoryStub) RemoveNotificationForChannel(string, string) error {+ return nil+}++func (*notificationRepositoryStub) GetNotificationDestinationsForChannel(string) ([]string, error) {+ return nil, nil+}++func (*notificationRepositoryStub) Setup() {}++func TestRegisterForLiveNotificationsValidatesBrowserEndpoint(t *testing.T) {+ tests := []struct {+ name string+ destination string+ wantStored bool+ }{+ {+ name: "public HTTPS endpoint",+ destination: `{"endpoint":"https://93.184.216.34/push","keys":{"auth":"auth","p256dh":"key"}}`,+ wantStored: true,+ },+ {+ name: "loopback endpoint",+ destination: `{"endpoint":"https://127.0.0.1/push"}`,+ },+ {+ name: "link local endpoint",+ destination: `{"endpoint":"https://169.254.169.254/latest"}`,+ },+ {+ name: "plain HTTP endpoint",+ destination: `{"endpoint":"http://93.184.216.34/push"}`,+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ repo := ¬ificationRepositoryStub{}+ h := &Handlers{notificationsRepository: repo}+ body := `{"channel":"` + notificationsrepo.BrowserPushNotification + `","destination":` + strconv.Quote(test.destination) + `}`+ req := httptest.NewRequest(http.MethodPost, "/api/notifications/register", strings.NewReader(body))+ rec := httptest.NewRecorder()++ h.RegisterForLiveNotifications(models.User{}, rec, req)++ stored := repo.addedDestination != ""+ if stored != test.wantStored {+ t.Fatalf("stored = %v, want %v, response = %s", stored, test.wantStored, rec.Body.String())+ }+ })+ }+}