Add Cross-Origin-Resource-Policy baseline header #341

Merged
timothy merged 1 commits from codex/330-corp into main 2026-07-13 21:33:15 +02:00
3 changed files with 34 additions and 1 deletions
@@ -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");
}
}
@@ -11,7 +11,8 @@ namespace ErsatzTV.Middleware;
/// <see cref="InvokeAsync" />. 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.
/// </summary>
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
+17
View File
@@ -1771,3 +1771,20 @@ prod-copy migration smoke before live recreation. Tagging and promotion remain s
tag build's immutable `:<version>` 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.