From 489956b167a79252356ffc399337f7d2a28cc91b Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 16 Jul 2026 18:49:03 +0200 Subject: [PATCH 1/2] fix(api): protect local library path details Fixes #334 Co-Authored-By: OpenAI Codex --- .../Controllers/ApiControllerSecurityTests.cs | 14 ++++++++++++++ .../Controllers/Api/LocalLibrariesController.cs | 2 ++ docs/api-conventions.md | 4 +++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs b/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs index a1510a59b..f68d008dd 100644 --- a/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs +++ b/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs @@ -118,6 +118,20 @@ public class ApiControllerSecurityTests } } + [Test] + public void Local_Library_Detail_Should_Require_Authentication_While_Catalog_List_Remains_Opt_Out() + { + MethodInfo detailAction = typeof(LocalLibrariesController).GetMethod(nameof(LocalLibrariesController.GetById)) + ?? throw new AssertionException($"Missing action {nameof(LocalLibrariesController.GetById)}"); + MethodInfo listAction = typeof(LocalLibrariesController).GetMethod(nameof(LocalLibrariesController.GetAll)) + ?? throw new AssertionException($"Missing action {nameof(LocalLibrariesController.GetAll)}"); + + detailAction.GetCustomAttributes(inherit: true).Any() + .ShouldBeTrue("local-library detail exposes server filesystem paths and must stay authenticated"); + listAction.GetCustomAttributes(inherit: true).Any() + .ShouldBeFalse("the ordinary local-library catalog should retain the read-auth opt-out"); + } + [Test] public void ScannerController_Should_Be_Localhost_Only() { diff --git a/ErsatzTV/Controllers/Api/LocalLibrariesController.cs b/ErsatzTV/Controllers/Api/LocalLibrariesController.cs index a7ae9944a..d6ad63d31 100644 --- a/ErsatzTV/Controllers/Api/LocalLibrariesController.cs +++ b/ErsatzTV/Controllers/Api/LocalLibrariesController.cs @@ -7,6 +7,7 @@ using ErsatzTV.Core.Api.MediaSources; using ErsatzTV.Core.Interfaces.Locking; using ErsatzTV.Core.Interfaces.Repositories; using ErsatzTV.Extensions; +using ErsatzTV.Filters; using MediatR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -40,6 +41,7 @@ public class LocalLibrariesController( [HttpGet("/api/v1/libraries/local/{id:int}", Name = "GetLocalLibrary")] [Tags("Libraries")] [EndpointSummary("Get a local library by id")] + [RequiresAuthentication] [ProducesResponseType(typeof(LocalLibraryDetailResponseModel), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task GetById(int id, CancellationToken cancellationToken) diff --git a/docs/api-conventions.md b/docs/api-conventions.md index b22636aba..013015dd7 100644 --- a/docs/api-conventions.md +++ b/docs/api-conventions.md @@ -625,7 +625,9 @@ OpenAPI generation, so the spec can't drift). When you add an endpoint: - **Sensitive-read GETs** that disclose secrets/paths or trigger work must carry `[RequiresAuthentication]` (renamed from `[RequiresApiKey]`) so they stay gated even if an operator sets `Api:RequireKeyForReads=false`. A valid session satisfies this tier just as the key does. Current tier: `Troubleshoot`/`Logs`/`Settings`/ - `Maintenance`. `ApiControllerSecurityTests` asserts this reflectively. + `Maintenance`, plus local-library detail (`GET /api/v1/libraries/local/{id}`), whose response contains + server filesystem paths. The ordinary local-library list remains in the catalog-read opt-out tier. + `ApiControllerSecurityTests` asserts these boundaries reflectively. - **Internal loopback callbacks** (the scanner's `/api/v1/scan/*`) and the **`/api/v1/auth/*` surface itself** use `[SkipApiAuthorization]` (renamed from `[SkipApiKeyAuthorization]`). The scanner adds `[LocalhostOnly]`; the auth surface must be reachable before a caller is authenticated, and its one sensitive action -- 2.47.3 From fe4706474ec3ed901411ee7ec3b8a467fd4ae67e Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 16 Jul 2026 18:54:57 +0200 Subject: [PATCH 2/2] test(api): assert effective local library auth Refs #334 Co-Authored-By: OpenAI Codex --- ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs b/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs index f68d008dd..ed505ee3d 100644 --- a/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs +++ b/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs @@ -126,9 +126,9 @@ public class ApiControllerSecurityTests MethodInfo listAction = typeof(LocalLibrariesController).GetMethod(nameof(LocalLibrariesController.GetAll)) ?? throw new AssertionException($"Missing action {nameof(LocalLibrariesController.GetAll)}"); - detailAction.GetCustomAttributes(inherit: true).Any() + EffectiveRequiresAuthentication(typeof(LocalLibrariesController), detailAction) .ShouldBeTrue("local-library detail exposes server filesystem paths and must stay authenticated"); - listAction.GetCustomAttributes(inherit: true).Any() + EffectiveRequiresAuthentication(typeof(LocalLibrariesController), listAction) .ShouldBeFalse("the ordinary local-library catalog should retain the read-auth opt-out"); } @@ -145,6 +145,10 @@ public class ApiControllerSecurityTests private static bool IsGloballyProtected() => ApiAuthorizationFilterIsGlobal; + private static bool EffectiveRequiresAuthentication(Type controllerType, MethodInfo action) => + controllerType.GetCustomAttributes(inherit: true).Any() || + action.GetCustomAttributes(inherit: true).Any(); + private static bool IsApiAuthorizationFilterRegisteredGlobally() { var settings = new Dictionary -- 2.47.3