Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(plugins): allow-list the scopes a plugin can grant via users.register
Gabe Kangas9d189becc4d9
diff --git a/pluginhost/pluginhost.go b/pluginhost/pluginhost.goindex 512a3d75e6..bcba63b908 100644--- a/pluginhost/pluginhost.go+++ b/pluginhost/pluginhost.go@@ -1612,6 +1612,29 @@ func ensureSigningSecret(ds *datastore.Datastore) ([]byte, error) {return secret, nil}+// pluginGrantableScopes are the user scopes a viewer-auth plugin may assign via+// owncast.users.register. MODERATOR and the chat-send scopes map cleanly from+// an external provider's roles; HAS_ADMIN_ACCESS — the scope that unlocks the+// external admin API — is intentionally excluded so a login plugin can't mint+// administrative access.+var pluginGrantableScopes = map[string]bool{+ models.ModeratorScopeKey: true,+ models.ScopeCanSendChatMessages: true,+ models.ScopeCanSendSystemMessages: true,+}++// validatePluginGrantedScopes rejects any scope a plugin is not allowed to+// assign through users.register, so the whole registration fails up front+// rather than silently dropping (or, worse, applying) an unintended scope.+func validatePluginGrantedScopes(scopes []string) error {+ for _, s := range scopes {+ if !pluginGrantableScopes[s] {+ return fmt.Errorf("plugin may not grant scope %q", s)+ }+ }+ return nil+}+// wireAuthHostFns wires the viewer-authentication host functions: register an// authenticated user for an external (plugin-provided) identity, and mint a// signed gate session for them.@@ -1627,6 +1650,12 @@ func wireAuthHostFns(env *plugins.HostEnv, deps Deps) {}env.RegisterUser = func(pluginName, authID, displayName string, scopes []string) (string, error) {+ // Validate requested scopes against the plugin-grantable allow-list+ // BEFORE creating any user, so a disallowed scope (e.g. HAS_ADMIN_ACCESS)+ // fails cleanly without leaving an orphan account.+ if err := validatePluginGrantedScopes(scopes); err != nil {+ return "", err+ }// Namespace the external identity by plugin slug so two auth plugins// can't collide on, or impersonate, each other's users.namespaced := pluginName + ":" + authID@@ -1646,16 +1675,12 @@ func wireAuthHostFns(env *plugins.HostEnv, deps Deps) {}userID = user.ID}- // Map the provider's roles onto Owncast scopes (e.g. MODERATOR). Applied- // on every login so a role change upstream takes effect. Invalid scopes- // are rejected rather than silently stored.+ // Map the provider's roles onto Owncast scopes (e.g. MODERATOR), applied+ // on every login so an upstream role change takes effect. The scopes+ // were allow-listed above.if len(scopes) > 0 {- if users.HasValidScopes(scopes) {- if err := users.SetUserScopes(userID, scopes); err != nil {- log.Errorln("plugin", pluginName, "set user scopes:", err)- }- } else {- log.Errorln("plugin", pluginName, "ignoring invalid scopes:", scopes)+ if err := users.SetUserScopes(userID, scopes); err != nil {+ log.Errorln("plugin", pluginName, "set user scopes:", err)}}return userID, nildiff --git a/pluginhost/scopes_test.go b/pluginhost/scopes_test.gonew file mode 100644index 0000000000..2a81d51032--- /dev/null+++ b/pluginhost/scopes_test.go@@ -0,0 +1,38 @@+package pluginhost++import (+ "testing"++ "github.com/owncast/owncast/models"+)++// F1 regression: a viewer-auth plugin may grant MODERATOR (the documented+// scope) and the chat-send scopes via users.register, but never+// HAS_ADMIN_ACCESS — and an unknown scope is rejected rather than silently+// dropped or applied.+func TestValidatePluginGrantedScopes(t *testing.T) {+ cases := []struct {+ name string+ scopes []string+ wantErr bool+ }{+ {"none", nil, false},+ {"moderator", []string{models.ModeratorScopeKey}, false},+ {"chat-send scopes", []string{models.ScopeCanSendChatMessages, models.ScopeCanSendSystemMessages}, false},+ {"moderator + chat", []string{models.ModeratorScopeKey, models.ScopeCanSendChatMessages}, false},+ {"admin access rejected", []string{models.ScopeHasAdminAccess}, true},+ {"admin mixed in is rejected", []string{models.ModeratorScopeKey, models.ScopeHasAdminAccess}, true},+ {"unknown scope rejected", []string{"BOGUS"}, true},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ err := validatePluginGrantedScopes(tc.scopes)+ if tc.wantErr && err == nil {+ t.Fatalf("scopes %v should be rejected", tc.scopes)+ }+ if !tc.wantErr && err != nil {+ t.Fatalf("scopes %v should be allowed, got: %v", tc.scopes, err)+ }+ })+ }+}