Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
Prevent encoded HTML in chat display names (#5050)
Gabe Kangas07bb1a8f049e
diff --git a/services/chat/messageRendering_test.go b/services/chat/messageRendering_test.goindex 9c3083570d..1d62a55c4d 100644--- a/services/chat/messageRendering_test.go+++ b/services/chat/messageRendering_test.go@@ -1,9 +1,13 @@package chatimport (+ "encoding/json"+ "fmt""testing"+ "github.com/owncast/owncast/config""github.com/owncast/owncast/services/chat/events"+ "github.com/owncast/owncast/utils")// Test a bunch of arbitrary markup and markdown to make sure we get sanitized@@ -60,3 +64,46 @@ func TestAllowHTML(t *testing.T) {t.Errorf("message rendering does not match expected. Got\n%s, \n\n want:\n%s", result, expected)}}++func TestSendSystemActionRendersTrustedHTML(t *testing.T) {+ client := &Client{send: make(chan []byte, 1)}+ service := &Service{clients: map[uint]*Client{1: client}}++ if err := service.SendSystemAction(`<span class="trusted" style="color:red">Status</span>`, true); err != nil {+ t.Fatal(err)+ }++ var payload struct {+ Body string `json:"body"`+ }+ if err := json.Unmarshal(<-client.send, &payload); err != nil {+ t.Fatal(err)+ }++ const want = "<p><span class=\"trusted\" style=\"color:red\">Status</span></p>\n"+ if payload.Body != want {+ t.Fatalf("action body = %q, want %q", payload.Body, want)+ }+}++func TestSendSystemActionDoesNotReactivateSanitizedDisplayName(t *testing.T) {+ client := &Client{send: make(chan []byte, 1)}+ service := &Service{clients: map[uint]*Client{1: client}}+ displayName := utils.MakeSafeStringOfLength(`<img src=x onerror=alert(1)>Alice`, config.MaxChatDisplayNameLength)++ if err := service.SendSystemAction(fmt.Sprintf("**%s** has been removed from chat.", displayName), true); err != nil {+ t.Fatal(err)+ }++ var payload struct {+ Body string `json:"body"`+ }+ if err := json.Unmarshal(<-client.send, &payload); err != nil {+ t.Fatal(err)+ }++ const want = "<p><strong>Alice</strong> has been removed from chat.</p>\n"+ if payload.Body != want {+ t.Fatalf("action body = %q, want %q", payload.Body, want)+ }+}diff --git a/utils/strings.go b/utils/strings.goindex 82062b35ee..004e877b01 100644--- a/utils/strings.go+++ b/utils/strings.go@@ -1,10 +1,10 @@package utilsimport (+ "html""strings""github.com/microcosm-cc/bluemonday"- "golang.org/x/net/html")// StripHTML will strip HTML tags from a string.@@ -16,9 +16,13 @@ func StripHTML(s string) string {// MakeSafeStringOfLength will take a string and strip HTML tags,// trim whitespace, and limit the length.func MakeSafeStringOfLength(s string, length int) string {- newString := s+ newString, fullyUnescaped := unescapeHTML(s)newString = StripHTML(newString)- newString = htmlUnescape(newString)+ // Only decode display entities after the input is stable. If the pass limit+ // was reached, preserving the encoding prevents latent markup from activating.+ if fullyUnescaped {+ newString = html.UnescapeString(newString)+ }// Convert utf-8 string into Unicode code points.codePoints := []rune(newString)@@ -34,18 +38,14 @@ func MakeSafeStringOfLength(s string, length int) string {return newString}-func htmlUnescape(input string) string {- token := html.NewTokenizer(strings.NewReader(input))- var output strings.Builder-- for {- tt := token.Next()- switch tt {- case html.ErrorToken:- return output.String()- case html.TextToken:- text := string(token.Text())- output.WriteString(text)+func unescapeHTML(s string) (string, bool) {+ const maxPasses = 8+ for range maxPasses {+ unescaped := html.UnescapeString(s)+ if unescaped == s {+ return s, true}+ s = unescaped}+ return s, false}diff --git a/utils/strings_test.go b/utils/strings_test.goindex 70fa5adf18..1912fbfeba 100644--- a/utils/strings_test.go+++ b/utils/strings_test.go@@ -2,6 +2,8 @@ package utilsimport ("fmt"+ "html"+ "strings""testing")@@ -31,6 +33,33 @@ func TestSafeString(t *testing.T) {}}+func TestMakeSafeStringDecodesEntitiesBeforeStrippingHTML(t *testing.T) {+ tests := []struct {+ input string+ want string+ }{+ {input: `<img src=x onerror=alert(1)>Alice`, want: "Alice"},+ {input: `<b>Alice</b>`, want: "Alice"},+ {input: `Alice & Bob`, want: "Alice & Bob"},+ }++ for _, test := range tests {+ t.Run(test.input, func(t *testing.T) {+ if got := MakeSafeStringOfLength(test.input, 100); got != test.want {+ t.Fatalf("MakeSafeStringOfLength(%q) = %q, want %q", test.input, got, test.want)+ }+ })+ }++ nested := `<img src=x onerror=alert(1)>`+ for range 10 {+ nested = html.EscapeString(nested)+ }+ if got := MakeSafeStringOfLength(nested, 100); strings.Contains(got, "<img") {+ t.Fatalf("deeply encoded HTML became active markup: %q", got)+ }+}+// TestSafeStringWhitespace tests the MakeSafeStringOfLength function with whitespace-only input.func TestSafeStringWhitespace(t *testing.T) {testCases := []struct {diff --git a/webserver/handlers/admin/chat.go b/webserver/handlers/admin/chat.goindex bd7f177b42..173fdd2ee5 100644--- a/webserver/handlers/admin/chat.go+++ b/webserver/handlers/admin/chat.go@@ -11,6 +11,7 @@ import ("github.com/pkg/errors"log "github.com/sirupsen/logrus"+ "github.com/owncast/owncast/config""github.com/owncast/owncast/models""github.com/owncast/owncast/services/chat/events""github.com/owncast/owncast/utils"@@ -173,7 +174,8 @@ func (a *Admin) handleUserDisabling(userID string) error {a.chat.DisconnectClients(clients)disconnectedUser := a.userRepository.GetUserByID(userID)- _ = a.chat.SendSystemAction(fmt.Sprintf("**%s** has been removed from chat.", disconnectedUser.DisplayName), true)+ displayName := utils.MakeSafeStringOfLength(disconnectedUser.DisplayName, config.MaxChatDisplayNameLength)+ _ = a.chat.SendSystemAction(fmt.Sprintf("**%s** has been removed from chat.", displayName), true)localIP4Address := "127.0.0.1"localIP6Address := "::1"@@ -350,9 +352,6 @@ func (a *Admin) SendChatAction(integration models.ExternalAPIUser, w http.Responreturn}- message.SetDefaults()- message.RenderBody()-if err := a.chat.SendSystemAction(message.Body, false); err != nil {webutils.BadRequestHandler(w, err)returndiff --git a/webserver/handlers/auth/fediverse/fediverse.go b/webserver/handlers/auth/fediverse/fediverse.goindex 7fcd2b2b4a..cde4629e62 100644--- a/webserver/handlers/auth/fediverse/fediverse.go+++ b/webserver/handlers/auth/fediverse/fediverse.go@@ -8,11 +8,13 @@ import (log "github.com/sirupsen/logrus"fediverseauth "github.com/owncast/owncast/auth/fediverse"+ "github.com/owncast/owncast/config""github.com/owncast/owncast/models""github.com/owncast/owncast/persistence/configrepository""github.com/owncast/owncast/persistence/userrepository""github.com/owncast/owncast/services/activitypub""github.com/owncast/owncast/services/chat"+ "github.com/owncast/owncast/utils""github.com/owncast/owncast/webserver/handlers/generated"webutils "github.com/owncast/owncast/webserver/utils")@@ -120,8 +122,10 @@ func (h *Handler) VerifyFediverseOTPRequest(w http.ResponseWriter, r *http.Requereturn}- if authRegistration.UserDisplayName != u.DisplayName {- loginMessage := fmt.Sprintf("**%s** is now authenticated as **%s**", authRegistration.UserDisplayName, u.DisplayName)+ displayName := utils.MakeSafeStringOfLength(authRegistration.UserDisplayName, config.MaxChatDisplayNameLength)+ authenticatedDisplayName := utils.MakeSafeStringOfLength(u.DisplayName, config.MaxChatDisplayNameLength)+ if displayName != authenticatedDisplayName {+ loginMessage := fmt.Sprintf("**%s** is now authenticated as **%s**", displayName, authenticatedDisplayName)if err := h.chat.SendSystemAction(loginMessage, true); err != nil {log.Errorln(err)}diff --git a/webserver/handlers/auth/indieauth/client.go b/webserver/handlers/auth/indieauth/client.goindex d2bbc1352a..cd3d8d8de4 100644--- a/webserver/handlers/auth/indieauth/client.go+++ b/webserver/handlers/auth/indieauth/client.go@@ -9,9 +9,11 @@ import (log "github.com/sirupsen/logrus"ia "github.com/owncast/owncast/auth/indieauth"+ "github.com/owncast/owncast/config""github.com/owncast/owncast/models""github.com/owncast/owncast/persistence/userrepository""github.com/owncast/owncast/services/chat"+ "github.com/owncast/owncast/utils""github.com/owncast/owncast/webserver/router/middleware"webutils "github.com/owncast/owncast/webserver/utils")@@ -104,8 +106,10 @@ func (h *Handler) HandleRedirect(w http.ResponseWriter, r *http.Request) {return}- if request.DisplayName != u.DisplayName {- loginMessage := fmt.Sprintf("**%s** is now authenticated as **%s**", request.DisplayName, u.DisplayName)+ displayName := utils.MakeSafeStringOfLength(request.DisplayName, config.MaxChatDisplayNameLength)+ authenticatedDisplayName := utils.MakeSafeStringOfLength(u.DisplayName, config.MaxChatDisplayNameLength)+ if displayName != authenticatedDisplayName {+ loginMessage := fmt.Sprintf("**%s** is now authenticated as **%s**", displayName, authenticatedDisplayName)if err := h.chat.SendSystemAction(loginMessage, true); err != nil {log.Errorln(err)}