Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(plugins): stop any Authorization header bypassing the viewer auth-gate
Gabe Kangasace302b541dd
diff --git a/pluginhost/authgate.go b/pluginhost/authgate.goindex c22371069c..1f272cea58 100644--- a/pluginhost/authgate.go+++ b/pluginhost/authgate.go@@ -60,9 +60,13 @@ var gateExemptions = []gateExemption{{"admin", exemptAdminRoutes},{"static-assets", exemptStaticAssets},{"active-gate-plugin", exemptActiveGatePluginRoutes},- {"self-credentialed", exemptSelfCredentialed},+ {"external-api", exemptExternalAPIRoutes},}+// externalAPIPrefix is the namespace for third-party API routes. Each handler+// there validates its own access token via RequireExternalAPIAccessToken.+const externalAPIPrefix = "/api/integrations/"+// webFS is the embedded web build (rooted at web/), resolved once. We test// request paths against it to decide what's a static asset.var webFS = static.GetWeb()@@ -113,11 +117,21 @@ func exemptActiveGatePluginRoutes(r *http.Request, gateSlug string) bool {return r.URL.Path == base || strings.HasPrefix(r.URL.Path, base+"/")}-// exemptSelfCredentialed: requests carrying their own credential (admin Basic-// auth, external-API Bearer tokens) are left for the downstream handler's own-// auth to evaluate, rather than bounced to the viewer login screen.-func exemptSelfCredentialed(r *http.Request, _ string) bool {- return r.Header.Get("Authorization") != ""+// exemptExternalAPIRoutes: the external-API (third-party token) routes under+// /api/integrations/ carry their own Bearer token, validated per-route by+// RequireExternalAPIAccessToken. A token client carries no session cookie, so+// gating these would bounce a valid API call into the HTML login screen; a+// token-less call still hits the route's own 401. (Admin Basic-auth /+// admin-session routes are exempted separately by exemptAdminRoutes.)+//+// Scoped to that namespace on purpose. An earlier version exempted ANY request+// carrying an Authorization header, on any path. But the viewer page (/) and+// the HLS handlers never inspect Authorization, so "Authorization: anything"+// let an anonymous visitor walk straight through the gate to the page and the+// live video. Gating is opt-out: a credential a route never checks must never+// be a bypass on that route.+func exemptExternalAPIRoutes(r *http.Request, _ string) bool {+ return strings.HasPrefix(r.URL.Path, externalAPIPrefix)}// gateExemptionFor returns the name of the first exemption that applies, or ""@@ -214,6 +228,7 @@ func (h *Host) AuthGateMiddleware() func(http.Handler) http.Handler {// Redirect navigations; 401 anything else so XHR/API callers// get a status they can act on instead of an HTML body.if r.Method == http.MethodGet || r.Method == http.MethodHead {+ //nolint:gosec // G710: loginURL is a same-origin /plugins/<slug>/ path with a query-escaped return_to, not attacker-controlled.http.Redirect(w, r, loginURL, http.StatusFound)} else {http.Error(w, "authentication required", http.StatusUnauthorized)diff --git a/pluginhost/authgate_test.go b/pluginhost/authgate_test.goindex 84dd09b8a7..53fbfb0fae 100644--- a/pluginhost/authgate_test.go+++ b/pluginhost/authgate_test.go@@ -34,7 +34,11 @@ func TestDecideGate(t *testing.T) {{"no session redirects to login", newReq("GET", "/", ""), "github-auth", true, true, false, gateLogin},{"valid session allowed", newReq("GET", "/hls/0.ts", ""), "github-auth", true, true, true, gateAllow},{"valid session survives a gate-plugin outage", newReq("GET", "/", ""), "github-auth", false, true, true, gateAllow},- {"authorization header allowed (self-credentialed)", newReq("GET", "/api/integrations/foo", "Bearer xyz"), "github-auth", true, true, false, gateAllow},+ {"external-api route allowed", newReq("GET", "/api/integrations/status", "Bearer xyz"), "github-auth", true, true, false, gateAllow},+ // An Authorization header must NOT be a bypass on routes that never+ // check it — these are the gate-defeat vectors the exemption scoping fixes.+ {"auth header on viewer page is gated", newReq("GET", "/", "Bearer xyz"), "github-auth", true, true, false, gateLogin},+ {"auth header on HLS segment is gated", newReq("GET", "/hls/0.ts", "anything"), "github-auth", true, true, false, gateLogin},}for _, tc := range cases {@@ -72,7 +76,12 @@ func TestGateExemptions(t *testing.T) {{"active gate plugin root", req("/plugins/g", ""), "g", "active-gate-plugin"},{"active gate plugin subpath", req("/plugins/g/callback", ""), "g", "active-gate-plugin"},{"a different plugin is NOT exempt", req("/plugins/other/x", ""), "g", ""},- {"self-credentialed", req("/api/x", "Bearer t"), "g", "self-credentialed"},+ {"external-api route exempt", req("/api/integrations/status", "Bearer t"), "g", "external-api"},+ {"external-api route exempt even without a header (route 401s itself)", req("/api/integrations/status", ""), "g", "external-api"},+ // An Authorization header is NOT a bypass off the external-API namespace.+ {"auth header on viewer page is NOT a bypass", req("/", "Bearer t"), "g", ""},+ {"auth header on HLS is NOT a bypass", req("/hls/0.ts", "Bearer t"), "g", ""},+ {"auth header on public api/config is NOT a bypass", req("/api/config", "Bearer t"), "g", ""},// Real files in the embedded web build are exempt (resolved by fs.Stat).{"admin stylesheet exempt", req("/styles/admin/chat.css", ""), "g", "static-assets"},{"service worker exempt", req("/sw.js", ""), "g", "static-assets"},diff --git a/pluginhost/pluginhost.go b/pluginhost/pluginhost.goindex fe3c35440c..2881cca55b 100644--- a/pluginhost/pluginhost.go+++ b/pluginhost/pluginhost.go@@ -1530,7 +1530,7 @@ func wireUserHostFns(env *plugins.HostEnv, deps Deps) {// pluginAuthSigningSecretKey is where the gate session signing secret is// persisted. Generated once on first use; rotating it (clearing the row)// invalidates every outstanding session.-const pluginAuthSigningSecretKey = "plugin_auth_signing_secret"+const pluginAuthSigningSecretKey = "plugin_auth_signing_secret" //nolint:gosec // G101: datastore key name, not a credential.// ensureSigningSecret returns the persisted gate session signing secret,// generating and storing one on first use.