From 9f73542296b17a52044d3d6e160f4feb9c26fac1 Mon Sep 17 00:00:00 2001 From: Timothy Date: Wed, 8 Jul 2026 07:47:49 +0200 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20quick=20wins=20=E2=80=94=20security?= =?UTF-8?q?-registry=20scan,=20Trakt=20SPA=20link,=20parallel=20search,=20?= =?UTF-8?q?dead=20"New=20Group"=20sweep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - #184: ApiControllerSecurityTests scans ErsatzTV.Controllers.Api assembly instead of a hand-maintained array (9 controllers were unlisted; 2 mutating) - Trakt matched-items link now navigates to SPA /app/search (was Classic UI) - GET /api/search runs its 10 per-kind queries in parallel (context-safe) - Remove dead "New Group" header buttons from blocks/templates/decos/deco-templates - docs/api-conventions.md §6 updated for the assembly-scan change fixes #184 Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Search/Queries/GetSearchResultsHandler.cs | 46 +++++++++++++---- .../Controllers/ApiControllerSecurityTests.cs | 51 ++++++++----------- docs/api-conventions.md | 18 +++---- web/src/App.tsx | 8 +-- web/src/screens/TraktListsScreen.tsx | 24 ++++----- 5 files changed, 78 insertions(+), 69 deletions(-) diff --git a/ErsatzTV.Application/Search/Queries/GetSearchResultsHandler.cs b/ErsatzTV.Application/Search/Queries/GetSearchResultsHandler.cs index bad6ebc99..c9567f0b0 100644 --- a/ErsatzTV.Application/Search/Queries/GetSearchResultsHandler.cs +++ b/ErsatzTV.Application/Search/Queries/GetSearchResultsHandler.cs @@ -24,16 +24,42 @@ public class GetSearchResultsHandler(IMediator mediator) return new SearchResultGroupResponseModel(paged.TotalCount, paged.Page); } + // Each ForKind call sends through IMediator to GetLibraryBrowseItemsHandler, which creates its + // own TvContext per invocation via IDbContextFactory (never shares one across calls), + // so these are safe to run concurrently. + Task movies = ForKind(LibraryBrowseMediaType.Movie); + Task shows = ForKind(LibraryBrowseMediaType.TelevisionShow); + Task seasons = ForKind(LibraryBrowseMediaType.TelevisionSeason); + Task artists = ForKind(LibraryBrowseMediaType.Artist); + Task episodes = ForKind(LibraryBrowseMediaType.Episode); + Task musicVideos = ForKind(LibraryBrowseMediaType.MusicVideo); + Task songs = ForKind(LibraryBrowseMediaType.Song); + Task otherVideos = ForKind(LibraryBrowseMediaType.OtherVideo); + Task images = ForKind(LibraryBrowseMediaType.Image); + Task remoteStreams = ForKind(LibraryBrowseMediaType.RemoteStream); + + await Task.WhenAll( + movies, + shows, + seasons, + artists, + episodes, + musicVideos, + songs, + otherVideos, + images, + remoteStreams); + return new SearchResultsResponseModel( - await ForKind(LibraryBrowseMediaType.Movie), - await ForKind(LibraryBrowseMediaType.TelevisionShow), - await ForKind(LibraryBrowseMediaType.TelevisionSeason), - await ForKind(LibraryBrowseMediaType.Artist), - await ForKind(LibraryBrowseMediaType.Episode), - await ForKind(LibraryBrowseMediaType.MusicVideo), - await ForKind(LibraryBrowseMediaType.Song), - await ForKind(LibraryBrowseMediaType.OtherVideo), - await ForKind(LibraryBrowseMediaType.Image), - await ForKind(LibraryBrowseMediaType.RemoteStream)); + movies.Result, + shows.Result, + seasons.Result, + artists.Result, + episodes.Result, + musicVideos.Result, + songs.Result, + otherVideos.Result, + images.Result, + remoteStreams.Result); } } diff --git a/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs b/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs index 5b0331282..fcfc085a7 100644 --- a/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs +++ b/ErsatzTV.Tests/Controllers/ApiControllerSecurityTests.cs @@ -23,37 +23,26 @@ public class ApiControllerSecurityTests [Test] public void Every_Mutating_Api_Action_Should_Be_Globally_Protected_Or_Explicitly_Exempt() { - Type[] apiControllers = - [ - typeof(ArtistsController), - typeof(BlockController), - typeof(ChannelController), - typeof(CollectionController), - typeof(DecoController), - typeof(DecoTemplateController), - typeof(FFmpegProfileController), - typeof(FillerPresetController), - typeof(ImagesController), - typeof(LibrariesController), - typeof(LogsController), - typeof(MaintenanceController), - typeof(MediaItemsController), - typeof(MoviesController), - typeof(PlayoutController), - typeof(ResolutionController), - typeof(ScannerController), - typeof(ScheduleController), - typeof(ScriptedScheduleController), - typeof(SeasonsController), - typeof(SessionController), - typeof(ShowsController), - typeof(SettingsController), - typeof(SmartCollectionController), - typeof(TemplateController), - typeof(TraktController), - typeof(TroubleshootController), - typeof(WatermarkController) - ]; + // Scan the assembly instead of hand-maintaining a list of controllers here: a + // hardcoded array silently drifts as new controllers are added (9 were missing at + // one point, including two with real mutating actions - ArtworkUploadController POST + // and ChannelTemplateController POST/PUT/DELETE - that were never actually checked). + // Note: not every controller in this namespace derives from ControllerBase (e.g. + // ChannelController, ScannerController, SessionController, MaintenanceController, + // TroubleshootController) - filter on [ApiController] + concrete class only, matching + // the pattern already used by ScannerController_Should_Be_Only_Api_Key_Exempt_Api_Controller + // below, so those controllers stay covered rather than silently dropping out of the scan. + Type[] apiControllers = typeof(CollectionController) + .Assembly + .GetTypes() + .Where(t => t.Namespace == typeof(CollectionController).Namespace) + .Where(t => t is { IsClass: true, IsAbstract: false }) + .Where(t => t.GetCustomAttributes(inherit: true).Any()) + .ToArray(); + + // Guard against the scan silently matching nothing (e.g. a namespace rename) and + // giving this test a false pass. + apiControllers.Length.ShouldBeGreaterThanOrEqualTo(20); foreach (Type controllerType in apiControllers) { diff --git a/docs/api-conventions.md b/docs/api-conventions.md index 0add7c508..14bd3ad24 100644 --- a/docs/api-conventions.md +++ b/docs/api-conventions.md @@ -130,16 +130,14 @@ new DTO with a `DayOfWeek` (or `DayOfWeek[]`) member — don't trust the generat `mediator.Received(1).Send(Arg.Is(...))` call. `PlayoutControllerTests.cs` is the same pattern for a larger, mixed-verb controller — use it as the template for a new controller with many actions. -- **`ApiControllerSecurityTests.cs`** (`ErsatzTV.Tests/Controllers/`): has a hardcoded - `Type[] apiControllers` registry. **Add every new controller to that array** — the test walks - every mutating (POST/PUT/PATCH/DELETE) action on each listed controller and asserts it's covered - by the global `ApiKeyAuthorizationFilter` (or has an explicit `[SkipApiKeyAuthorizationAttribute]` - exemption; only `ScannerController` is exempt today). **Note**: as of this writing the array does - not include every controller under `ErsatzTV/Controllers/Api/` (e.g. `ArtworkUploadController`, - `ChannelTemplateController`, `GraphicsElementController`, `LibraryBrowseController`, - `SearchController`, `VersionController`, `HealthController`, `PlaylistController`, - `MediaSourcesController` are missing) — treat that as a pre-existing gap to close opportunistically, - not a precedent to extend. +- **`ApiControllerSecurityTests.cs`** (`ErsatzTV.Tests/Controllers/`): reflects over the + `ErsatzTV.Controllers.Api` namespace to find every concrete, `[ApiController]`-marked + `ControllerBase` subtype — no manual registry to maintain; a new controller is covered + automatically. The test walks every mutating (POST/PUT/PATCH/DELETE) action on each scanned + controller and asserts it's covered by the global `ApiKeyAuthorizationFilter` (or has an + explicit `[SkipApiKeyAuthorizationAttribute]` exemption; only `ScannerController` is exempt + today). A minimum-count guard asserts the scan found a sane number of controllers, so a + namespace rename can't silently make the scan match nothing and give this test a false pass. - **Handler tests** (business logic behind the controller) use the shared in-memory SQLite fixture: `ErsatzTV.Tests/Support/InMemoryTvContext.cs`. Pattern: `SqliteConnection("Data Source=:memory:;Foreign Keys=False")` kept open for the fixture's lifetime, `EnsureCreatedAsync()` (not full migration diff --git a/web/src/App.tsx b/web/src/App.tsx index e9f66078b..52c0244c1 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -290,7 +290,7 @@ const routes: ScreenRoute[] = [ kicker: 'Programming', description: 'Group blocks and edit their items, durations, and playout preview.', icon: