From fc7f23b2299a0b931555d6b45591216d8a340686 Mon Sep 17 00:00:00 2001 From: 4gray <4gray@users.noreply.github.com> Date: Sun, 2 Aug 2026 10:51:12 +0200 Subject: [PATCH] feat(playback): forward portal Cookie/Authorization to built-in players (#1335) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(playback): forward portal Cookie/Authorization to built-in players The web players (HTML5/hls.js, Video.js, ArtPlayer, Shaka) could only ever receive User-Agent/Referer/Origin, so any Stalker stream gated on the portal session cookie or Bearer token played exclusively in external MPV/VLC — the long-running "only VLC works" cluster (#849, #910, #732). - request-header-overrides.service: the scoped override now carries Cookie and Authorization, attached only to requests on the exact stream origin, in-memory only, dropped on replace/clear. Unscoped (playlist-level) calls drop credentials fail-closed; control characters in header values are rejected. Chosen over session.cookies.set(): jar cookies attach only to credentialed requests, which would force withCredentials into every engine and break against the Access-Control-Allow-Origin:* IPTV panels send, and jar scoping is port-blind. - WebPlayerViewComponent is now the single owner of the scoped override for every built-in player: it extracts the full header set from the resolved playback, configures the override BEFORE handing the source over (players render only once the source exists), and clears the scoped layer on destroy. HtmlVideoPlayerComponent's own three-header call is removed — it would overwrite the credentialed override. - Stalker VOD, series episodes and radio now build the same portal header set ITV already had (they previously carried no portal headers at all); same-origin playback sends the real User-Agent alongside X-User-Agent. - Stream classification is host-based via one shared predicate (isStalkerStreamCredentialSafe): same-host port changes and scheme upgrades keep the portal profile (the #1158 class), a foreign host or https->http downgrade keeps the credential-free KSPlayer profile. The main-process fallback context uses the same predicate so isStalkerDirectStreamProfile can no longer discard renderer headers. - setUserAgent bridge gains an optional credentials parameter; preload, ipcMain handler and ElectronBridgeApi updated together. - stalker-mock-server: gated-stream scenario (MAC 00:1A:79:00:00:09) whose create_link returns a local /stream/gated/video.mp4 that 403s without the mac cookie + current Bearer token; new Electron e2e proves a built-in player actually plays it (and that the gate refuses bare requests). Co-Authored-By: Claude Fable 5 * fix(playback): apply header override to Stalker radio, redact mock cookie log Address Codex review feedback on #1335: - The radio branch of the Stalker live layout renders the dedicated audio player, never WebPlayerViewComponent, so the resolved portal headers were built but never applied — an auth-gated radio stream still 403'd. The override sync is extracted into ElectronStreamHeadersService (single owner of the scoped override slot, with clear-only-while-owning semantics so a destroyed consumer cannot wipe a newer consumer's override), applied by WebPlayerViewComponent for video players and by the radio branch before the audio element gets its URL. The service feature-detects the bridge method so partial bridges behave like the PWA instead of throwing. - The gated-stream mock no longer logs the raw Cookie header on 403 — presence only, matching the Authorization logging. - The gated scenario now serves an audio fixture for radio create_link and the Electron e2e covers the radio path end-to-end (bare request 403s, built-in audio player advances past the gate). Co-Authored-By: Claude Fable 5 * fix(playback): claim radio header ownership before awaiting the IPC Codex round-2 P2: leaving the radio route while the header IPC was still in flight left the portal cookie/token installed — ngOnDestroy saw a null scope URL (it was recorded only after the await) and could not clear the override. Ownership is now claimed synchronously before awaiting, destroy invalidates the pending playback continuation, and the apply's stillCurrent verdict is honored. Regression test covers destroy-during-pending-IPC. Co-Authored-By: Claude Fable 5 * fix(playback): carry portal headers into collection playback Codex round-3 P1: Stalker channels opened from Favorites/Recently Viewed resolved through StreamResolverService.resolveStalker(), which returned no portal headers — the video path handed the header owner an empty set and collection radio bypassed it entirely, so auth-gated streams still 403'd from collections. - resolveStalker() now builds the same profile as the live layout via the shared classifier: portal-owned streams get mac cookie/Bearer token/MAG UA/portal Origin+Referer, foreign hosts keep the credential-free KSPlayer profile (both create_link results and direct radio URLs). - UnifiedLiveTabComponent applies the scoped override for radio before the audio element gets its URL (ownership claimed before awaiting the IPC, round-2 lesson), and clears it on close and destroy. - Regression tests: resolver header profiles for portal-host and foreign streams; unified tab radio apply-then-clear. Co-Authored-By: Claude Fable 5 * fix(playback): release the radio override when a new selection mounts no player Codex round-4 P2: after radio installed its credentials, selecting an item that never mounts a player surface (external video playback, failed resolution) left the old Cookie/Authorization installed — no WebPlayerViewComponent, close, or destroy cleanup runs on that path. Both radio hosts (unified collection tab and the Stalker live layout, which has the identical hole) now release the previously owned radio scope at the start of every new selection; the slot-ownership semantics keep this a no-op when another playback already owns the override. Regression test in the live-layout spec pins the failed-selection path. Co-Authored-By: Claude Fable 5 * docs(playback): state the exact override release points Codex round-5 P2 flagged that the media 'ended' event does not clear the scoped override while the player stays mounted. That is deliberate, not a gap: a mounted player still owns the session — replay or a seek into an unbuffered range must keep working against a gated stream, and clearing on 'ended' would 403 exactly the streams this PR fixes. The credentials only ever travel to the exact origin that issued them, and every dismount path (channel/source change, player close/destroy, radio close, playerless selection) releases them. The security doc and the release note now say precisely that instead of the ambiguous "cleared when playback ends". Co-Authored-By: Claude Fable 5 * docs(playback): fit the release note back under the 400-character cap Co-Authored-By: Claude Fable 5 --------- Co-authored-by: Claude Fable 5 --- .../playback-stalker-stream-credentials.md | 12 + .../src/stalker-playback-headers.e2e.ts | 148 ++++++++++++ .../src/app/api/main.preload.spec.ts | 28 ++- .../src/app/api/main.preload.ts | 15 +- .../src/app/events/shared.events.ts | 42 +++- .../request-header-overrides.service.spec.ts | 217 ++++++++++++++++++ .../request-header-overrides.service.ts | 79 ++++++- .../stalker-playback-context.service.spec.ts | 54 +++++ .../stalker-playback-context.service.ts | 24 +- .../src/app/handlers/create-link.handler.ts | 18 +- apps/stalker-mock-server/src/app/scenarios.ts | 21 ++ apps/stalker-mock-server/src/main.ts | 58 ++++- docs/architecture/electron-security.md | 57 ++++- docs/architecture/stalker-portal.md | 49 ++++ .../stream-resolver.service.spec.ts | 92 +++++++- .../lib/collection/stream-resolver.service.ts | 71 +++++- .../unified-live-tab.component.spec.ts | 34 +++ .../unified-live-tab.component.ts | 37 ++- .../lib/stalker-live-playback.utils.spec.ts | 121 +++++++++- .../src/lib/stalker-live-playback.utils.ts | 33 ++- .../with-stalker-player.feature.spec.ts | 83 +++++++ .../features/with-stalker-player.feature.ts | 59 ++++- ...alker-live-stream-layout.component.spec.ts | 116 ++++++++++ .../stalker-live-stream-layout.component.ts | 35 +++ libs/shared/interfaces/src/index.ts | 1 + .../src/lib/electron-api.interface.ts | 14 +- .../lib/stalker-stream-profile.util.spec.ts | 75 ++++++ .../src/lib/stalker-stream-profile.util.ts | 44 ++++ libs/ui/playback/src/index.ts | 1 + .../html-video-player.component.spec.ts | 38 +-- .../html-video-player.component.ts | 17 +- .../electron-stream-headers.service.spec.ts | 141 ++++++++++++ .../electron-stream-headers.service.ts | 104 +++++++++ .../web-player-view.component.html | 97 ++++---- .../web-player-view.component.spec.ts | 116 ++++++++++ .../web-player-view.component.ts | 49 +++- 36 files changed, 2048 insertions(+), 152 deletions(-) create mode 100644 .changes/playback-stalker-stream-credentials.md create mode 100644 apps/electron-backend-e2e/src/stalker-playback-headers.e2e.ts create mode 100644 libs/shared/interfaces/src/lib/stalker-stream-profile.util.spec.ts create mode 100644 libs/shared/interfaces/src/lib/stalker-stream-profile.util.ts create mode 100644 libs/ui/playback/src/lib/web-player-view/electron-stream-headers.service.spec.ts create mode 100644 libs/ui/playback/src/lib/web-player-view/electron-stream-headers.service.ts diff --git a/.changes/playback-stalker-stream-credentials.md b/.changes/playback-stalker-stream-credentials.md new file mode 100644 index 000000000..dc4bfde8e --- /dev/null +++ b/.changes/playback-stalker-stream-credentials.md @@ -0,0 +1,12 @@ +--- +type: fix +area: playback +issues: [849, 910, 732] +--- + +Stalker streams that require the portal session now play in the built-in +players (HTML5, Video.js, ArtPlayer), not only in VLC/MPV: the player's +requests carry the portal cookie and token, scoped to that stream and +dropped when the player closes or the channel changes. VOD, series and +radio get the same headers live TV had — also from Favorites and Recently +Viewed. diff --git a/apps/electron-backend-e2e/src/stalker-playback-headers.e2e.ts b/apps/electron-backend-e2e/src/stalker-playback-headers.e2e.ts new file mode 100644 index 000000000..c8e7e8f1d --- /dev/null +++ b/apps/electron-backend-e2e/src/stalker-playback-headers.e2e.ts @@ -0,0 +1,148 @@ +import { + addStalkerPortal, + closeElectronApp, + expect, + launchElectronApp, + resetMockServers, + stalkerMockServer, + test, + waitForStalkerCatalog, +} from './electron-test-fixtures'; + +/** + * End-to-end proof that a BUILT-IN player's media requests carry the portal + * credentials (mac cookie + Bearer token) — the root of the long-running + * "only VLC works" cluster (#849, #910, #732): the web players used to + * receive only User-Agent/Referer/Origin, so any stream gated on the portal + * session could never play inline. + * + * The mock's `gated-stream` scenario makes `create_link` return this + * server's own `/stream/gated/video.mp4`, which answers 403 unless the + * request presents the mac cookie AND the MAC's current access token. A unit + * test cannot show that a header reached the video element; playback + * advancing past that gate can only happen when the scoped Electron header + * override attached the credentials to the actual media request. + */ + +const GATED_MAC = '00:1A:79:00:00:09'; +const GATED_STREAM_URL = `${stalkerMockServer}/stream/gated/video.mp4`; +// The full-portal URL shape: the app handshakes and holds a Bearer token, +// which is exactly what the gated stream endpoint demands. +const FULL_PORTAL_URL = `${stalkerMockServer}/stalker_portal/server/load.php`; + +test('@electron @stalker built-in player plays an auth-gated portal stream', async ({ + dataDir, + request, +}) => { + await resetMockServers(request, ['stalker']); + + // First prove the gate is real: a credential-less request is refused, so + // a green playback assertion below cannot be a permissive-mock artifact. + const bareResponse = await request.get(GATED_STREAM_URL); + expect(bareResponse.status()).toBe(403); + + const app = await launchElectronApp(dataDir); + + try { + await addStalkerPortal(app.mainWindow, { + macAddress: GATED_MAC, + portalUrl: FULL_PORTAL_URL, + }); + await waitForStalkerCatalog(app.mainWindow); + + // The portal lands on Movies; live playback lives in the ITV layout. + await app.mainWindow + .getByRole('link', { name: /live|itv/i }) + .click(); + await app.mainWindow.waitForURL(/stalker.*itv/); + + // The ITV view renders channels only after a category is selected; + // index 0 is the "All channels" pseudo-category. + const categories = app.mainWindow.locator('.category-item'); + await expect(categories.first()).toBeVisible({ timeout: 10_000 }); + await categories.first().click(); + + const channels = app.mainWindow.locator( + '[data-test-id="channel-item"]' + ); + await expect(channels.first()).toBeVisible({ timeout: 20_000 }); + await channels.first().click(); + + const video = app.mainWindow + .locator('app-web-player-view video') + .first(); + await expect(video).toBeVisible({ timeout: 15_000 }); + + // Advancing playback past the 403 gate is only possible when the + // media requests carried the portal cookie and Authorization header. + await expect + .poll( + () => + video.evaluate( + (element: HTMLVideoElement) => element.currentTime + ), + { timeout: 20_000 } + ) + .toBeGreaterThan(0.5); + await expect( + app.mainWindow.getByTestId('playback-diagnostic-banner') + ).toBeHidden(); + } finally { + await closeElectronApp(app); + } +}); + +test('@electron @stalker built-in audio player plays an auth-gated radio stream', async ({ + dataDir, + request, +}) => { + await resetMockServers(request, ['stalker']); + + // The radio branch renders the dedicated audio player instead of + // WebPlayerViewComponent, so it exercises the Stalker live layout's own + // header wiring — a gap the ITV test above cannot catch. + const bareResponse = await request.get( + `${stalkerMockServer}/stream/gated/audio.mp4` + ); + expect(bareResponse.status()).toBe(403); + + const app = await launchElectronApp(dataDir); + + try { + await addStalkerPortal(app.mainWindow, { + macAddress: GATED_MAC, + portalUrl: FULL_PORTAL_URL, + }); + await waitForStalkerCatalog(app.mainWindow); + + await app.mainWindow.getByRole('link', { name: /radio/i }).click(); + await app.mainWindow.waitForURL(/stalker.*radio/); + + const categories = app.mainWindow.locator('.category-item'); + await expect(categories.first()).toBeVisible({ timeout: 10_000 }); + await categories.first().click(); + + const channels = app.mainWindow.locator( + '[data-test-id="channel-item"]' + ); + await expect(channels.first()).toBeVisible({ timeout: 20_000 }); + await channels.first().click(); + + // The bare