Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(plugins): batch of low-severity host hardening
Gabe Kangas10756a4ce7e5
diff --git a/services/plugins/dispatcher_test.go b/services/plugins/dispatcher_test.goindex dab3867a53..8f73a0290d 100644--- a/services/plugins/dispatcher_test.go+++ b/services/plugins/dispatcher_test.go@@ -8,6 +8,28 @@ import ("time")+// TestReservedEventTypes_CoversCoreEvents is the B4 guard: the set the emit+// host function rejects must include every built-in event type, so a plugin+// can't forge a core event onto other plugins. Catches a new Event* constant+// added without updating the reserved set.+func TestReservedEventTypes_CoversCoreEvents(t *testing.T) {+ for _, e := range []string{+ EventChatMessageReceived, EventChatUserJoined, EventChatUserParted,+ EventChatUserRenamed, EventChatMessageModerated, EventStreamStarted,+ EventStreamStopped, EventStreamTitleChanged, EventSSEConnect,+ EventSSEDisconnect, EventTick, EventTimerFire, EventFediverseFollow,+ EventFediverseLike, EventFediverseRepost, EventFediverseMention,+ EventFediverseReply,+ } {+ if !reservedEventTypes[e] {+ t.Errorf("core event %q must be reserved (plugins must not be able to emit it)", e)+ }+ }+ if reservedEventTypes["my.plugin.custom"] {+ t.Error("a plugin's custom event type must not be treated as reserved")+ }+}+// TestFilterTimeoutErrorShape verifies the error-string the dispatcher// produces when CallWithContext returns a deadline-exceeded error. The// real Wazero-level cancellation is verified separately by integrationdiff --git a/services/plugins/events.go b/services/plugins/events.goindex c842b14528..716901014e 100644--- a/services/plugins/events.go+++ b/services/plugins/events.go@@ -43,3 +43,27 @@ const (EventFediverseMention = "fediverse.mention"EventFediverseReply = "fediverse.reply")++// reservedEventTypes are the built-in event types the host originates. A+// plugin's owncast.emit must not forge them: emitting e.g.+// "chat.message.received" would deliver a fake core event to every other+// plugin's on_event handler. Plugin-emitted custom events use any other string.+var reservedEventTypes = map[string]bool{+ EventChatMessageReceived: true,+ EventChatUserJoined: true,+ EventChatUserParted: true,+ EventChatUserRenamed: true,+ EventChatMessageModerated: true,+ EventStreamStarted: true,+ EventStreamStopped: true,+ EventStreamTitleChanged: true,+ EventSSEConnect: true,+ EventSSEDisconnect: true,+ EventTick: true,+ EventTimerFire: true,+ EventFediverseFollow: true,+ EventFediverseLike: true,+ EventFediverseRepost: true,+ EventFediverseMention: true,+ EventFediverseReply: true,+}diff --git a/services/plugins/hostfns.go b/services/plugins/hostfns.goindex 6e40f28ce0..be976e05e7 100644--- a/services/plugins/hostfns.go+++ b/services/plugins/hostfns.go@@ -836,17 +836,24 @@ func hostAuthGrantSession(env *HostEnv) extism.HostFunction {stack[0] = writeJSON(p, authResult{Error: "auth.gate is not available on this host"})return}+ // A session is only meaningful when there's an in-flight response to+ // attach the cookie to, i.e. inside on_http_request (which seeds the+ // sink). Reject other contexts BEFORE minting a token, so a plugin+ // can't silently mint DB-backed access tokens from a timer or event+ // handler where no cookie is ever set.+ sink := authSinkFrom(ctx)+ if sink == nil {+ stack[0] = writeJSON(p, authResult{Error: "grantSession is only available inside an on_http_request handler"})+ return+ }token, err := env.GrantSession(ident.slug, req.UserID, req.TTL)if err != nil {stack[0] = writeJSON(p, authResult{Error: err.Error()})return}// Hand the freshly-minted token to the request-scoped sink so the- // HTTP layer can set the cookie on the response after the wasm- // returns. Absent in non-HTTP contexts (e.g. the test harness).- if sink := authSinkFrom(ctx); sink != nil {- sink.grant(token, ClampSessionTTL(req.TTL))- }+ // HTTP layer can set the cookie on the response after the wasm returns.+ sink.grant(token, ClampSessionTTL(req.TTL))stack[0] = writeJSON(p, authResult{})},[]extism.ValueType{extism.ValueTypePTR},@@ -1591,6 +1598,10 @@ func hostEmitEvent(env *HostEnv) extism.HostFunction {if err != nil {return}+ if reservedEventTypes[eventType] {+ fmt.Fprintf(os.Stderr, "owncast_emit_event from %s: %q is a reserved host event type and cannot be emitted by a plugin\n", pluginName, eventType)+ return+ }payloadBytes, err := p.ReadBytes(stack[1])if err != nil {returndiff --git a/services/plugins/manager.go b/services/plugins/manager.goindex d4ea40d4e0..97963cbd0f 100644--- a/services/plugins/manager.go+++ b/services/plugins/manager.go@@ -183,6 +183,12 @@ func (l *Loaded) recordFilterSuccess() {// explicit wildcard). Used by Server to require authentication on// admin-only routes.func (l *Loaded) IsAdminPath(path string) bool {+ // Match case-insensitively: the gate must fire even if a request uses a+ // different case than the manifest's declared path (e.g. "/Admin/x" vs a+ // declared "/admin/*"), since a plugin that routes case-insensitively would+ // otherwise serve admin content to an unauthenticated caller. The glob+ // patterns and prefixes are lowered at load to match (see loadFromBytes).+ path = strings.ToLower(path)for i, g := range l.adminGlobs {if g.Match(path) {return true@@ -1438,13 +1444,16 @@ func loadFromBytes(ctx context.Context, env *HostEnv, manifestBytes, artifactBytvar adminGlobs []glob.Globvar adminPaths []stringfor _, page := range manifest.Admin.Pages {- g, err := glob.Compile(page.Path)+ // Lower the pattern so admin-path matching is case-insensitive (see+ // IsAdminPath); the request path is lowered there to match.+ lowered := strings.ToLower(page.Path)+ g, err := glob.Compile(lowered)if err != nil {_ = p.Close(ctx)return nil, fmt.Errorf("manifest.admin.pages: invalid path glob %q: %w", page.Path, err)}adminGlobs = append(adminGlobs, g)- adminPaths = append(adminPaths, page.Path)+ adminPaths = append(adminPaths, lowered)}loaded = truediff --git a/services/plugins/server_test.go b/services/plugins/server_test.goindex 59b0d4607d..0b44d3dbed 100644--- a/services/plugins/server_test.go+++ b/services/plugins/server_test.go@@ -318,6 +318,25 @@ func TestWritePluginHTTPResponse_StripsCoreSessionCookie(t *testing.T) {}}+// TestIsAdminPath_CaseInsensitive is the A5 regression: the admin gate must+// fire regardless of the request path's case, so a case-insensitively-routing+// plugin can't be tricked into serving admin content via "/Admin/...".+func TestIsAdminPath_CaseInsensitive(t *testing.T) {+ // adminGlobs/adminPaths are stored lowered by loadFromBytes; mirror that.+ l := &Loaded{+ adminGlobs: []glob.Glob{glob.MustCompile("/admin/*")},+ adminPaths: []string{"/admin/*"},+ }+ for _, p := range []string{"/admin/secret", "/Admin/secret", "/ADMIN/secret"} {+ if !l.IsAdminPath(p) {+ t.Errorf("%q should be gated as an admin path", p)+ }+ }+ if l.IsAdminPath("/public/x") {+ t.Error("/public/x should not be gated")+ }+}+func TestServer_NonGetFallsThroughStatic(t *testing.T) {// POST to a static asset path shouldn't serve the file. (Static is// read-only; non-GET/HEAD requests fall through to the dynamic handler.)diff --git a/services/plugins/sse.go b/services/plugins/sse.goindex ab55c4842e..f0cfbb5bcd 100644--- a/services/plugins/sse.go+++ b/services/plugins/sse.go@@ -159,6 +159,11 @@ func (h *SSEHub) Publish(pluginName, channel, event string, data []byte) int {// per newline) and terminated by a blank line.func frameSSE(event string, data []byte) []byte {var b strings.Builder+ // Strip CR/LF from the plugin-supplied event name so it can't inject extra+ // SSE fields or frames into the host-written stream (the data lines below+ // are already split per newline, so only the event name needs sanitizing).+ event = strings.ReplaceAll(event, "\r", "")+ event = strings.ReplaceAll(event, "\n", "")if event != "" {b.WriteString("event: ")b.WriteString(event)diff --git a/services/plugins/sse_test.go b/services/plugins/sse_test.goindex ec685b9415..abf40c54af 100644--- a/services/plugins/sse_test.go+++ b/services/plugins/sse_test.go@@ -42,6 +42,28 @@ func TestSSEHub_CloseForPlugin(t *testing.T) {}}+// TestFrameSSE_SanitizesEventName is the B5 regression: a plugin-supplied event+// name containing newlines must not inject extra SSE fields/frames. After+// sanitizing, the frame has exactly one event line and one data line.+func TestFrameSSE_SanitizesEventName(t *testing.T) {+ frame := string(frameSSE("evil\ndata: injected\nevent: spoof", []byte("real")))+ var eventLines, dataLines int+ for _, ln := range strings.Split(strings.TrimRight(frame, "\n"), "\n") {+ if strings.HasPrefix(ln, "event: ") {+ eventLines+++ }+ if strings.HasPrefix(ln, "data: ") {+ dataLines+++ }+ }+ if eventLines != 1 {+ t.Fatalf("expected exactly 1 event line, got %d:\n%q", eventLines, frame)+ }+ if dataLines != 1 {+ t.Fatalf("event-name newline injected extra data lines (%d):\n%q", dataLines, frame)+ }+}+func TestSSEHubPublishFansOutToSubscribers(t *testing.T) {h := NewSSEHub()