diff --git a/ErsatzTV.Tests/Middleware/SecurityHeadersMiddlewareTests.cs b/ErsatzTV.Tests/Middleware/SecurityHeadersMiddlewareTests.cs index 540ba8c12..b3cfeede4 100644 --- a/ErsatzTV.Tests/Middleware/SecurityHeadersMiddlewareTests.cs +++ b/ErsatzTV.Tests/Middleware/SecurityHeadersMiddlewareTests.cs @@ -38,6 +38,19 @@ public class SecurityHeadersMiddlewareTests context.Response.Headers["X-Frame-Options"].ToString().ShouldBe("DENY"); context.Response.Headers["Referrer-Policy"].ToString().ShouldBe("strict-origin-when-cross-origin"); context.Response.Headers["Permissions-Policy"].ToString().ShouldNotBeEmpty(); + context.Response.Headers["Cross-Origin-Resource-Policy"].ToString().ShouldBe("same-origin"); + } + + [TestCase("/api/v1/channels")] + [TestCase("/artwork/x.jpg")] + [TestCase("/iptv/channels.m3u")] + [TestCase("/docs")] + [TestCase("/openapi/v1.json")] + public async Task Should_Set_Cross_Origin_Resource_Policy_On_Every_Response(string path) + { + HttpContext context = await Invoke(path); + + context.Response.Headers["Cross-Origin-Resource-Policy"].ToString().ShouldBe("same-origin"); } [Test] @@ -74,6 +87,7 @@ public class SecurityHeadersMiddlewareTests context.Response.Headers.ContainsKey("Content-Security-Policy").ShouldBeFalse($"CSP must not be set on {path}"); // The cheap baseline headers still apply everywhere. context.Response.Headers["X-Content-Type-Options"].ToString().ShouldBe("nosniff"); + context.Response.Headers["Cross-Origin-Resource-Policy"].ToString().ShouldBe("same-origin"); } } diff --git a/ErsatzTV/Middleware/SecurityHeadersMiddleware.cs b/ErsatzTV/Middleware/SecurityHeadersMiddleware.cs index 278af31d0..4cd73cf22 100644 --- a/ErsatzTV/Middleware/SecurityHeadersMiddleware.cs +++ b/ErsatzTV/Middleware/SecurityHeadersMiddleware.cs @@ -11,7 +11,8 @@ namespace ErsatzTV.Middleware; /// . HSTS is intentionally left out: it is a deployment/TLS decision /// that belongs with the go-live reverse-proxy posture, not this host middleware. /// Baseline headers landed for #197; the CSP + Permissions-Policy were added for #319 (ZAP -/// baseline hardening). +/// baseline hardening). Cross-Origin-Resource-Policy was added for #330 to prevent browsers +/// from embedding responses through cross-origin no-cors requests. /// public class SecurityHeadersMiddleware(RequestDelegate next) { @@ -63,6 +64,7 @@ public class SecurityHeadersMiddleware(RequestDelegate next) headers["X-Frame-Options"] = "DENY"; headers["Referrer-Policy"] = "strict-origin-when-cross-origin"; headers["Permissions-Policy"] = "camera=(), microphone=(), geolocation=(), payment=(), usb=()"; + headers["Cross-Origin-Resource-Policy"] = "same-origin"; // The Scalar API-reference UI (/docs) and the OpenAPI document (/openapi) rely on inline // bootstrap scripts/styles that a strict policy would break; hardening that admin surface is diff --git a/docs/decisions.md b/docs/decisions.md index fbc8c9211..d1aa94948 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -1771,3 +1771,20 @@ prod-copy migration smoke before live recreation. Tagging and promotion remain s tag build's immutable `:` image, then deploy manually. Daily auto-update is only a fallback, so cut tags with enough runway before 03:00 to prevent an unscanned promotion. Refs #335 and server-management#585/#589. + +## 2026-07-13 — Cross-origin resource policy: `same-origin` on every response (#330) + +The authenticated #314 ZAP scan found that ErsatzTV's baseline response posture omitted +`Cross-Origin-Resource-Policy`. `SecurityHeadersMiddleware` now sends +`Cross-Origin-Resource-Policy: same-origin` on every response, including `/docs` and `/openapi`. +Those two paths remain exempt only from the strict CSP that would break Scalar's inline bootstrap; +CORP has no equivalent rendering conflict and belongs with the middleware's path-independent baseline +headers. + +`same-origin` requires the browser request and response to share the exact scheme, host, and port. It +blocks cross-origin `no-cors` loads, so direct browser embedding of ErsatzTV artwork or media from an +alternate origin is deliberately unsupported. It does not reject an allowed CORS-mode API fetch, so the +explicit `Api:CorsAllowedOrigins` machine-client path continues to work. It is also not enforced by +server-side HTTP clients, so Jellyfin's `/iptv/*` requests are unaffected; same-origin SPA artwork and +IPTV requests remain allowed. This is defense in depth for browser embedding and does not replace CORS +or authentication. Refs #330 #319 #314.