diff --git a/.scratch/movi-telemetry-csrf-20261016/spec.md b/.scratch/movi-telemetry-csrf-20261016/spec.md new file mode 100644 index 00000000..2589166d --- /dev/null +++ b/.scratch/movi-telemetry-csrf-20261016/spec.md @@ -0,0 +1,17 @@ +# Player telemetry CSRF correction + +Status: resolved + +## Problem and evidence + +Opening a task repeatedly reports POST /api/client/movi-stats 403. The reporter sent session cookies without the matching CSRF header. A production read-only diagnostic (empty telemetry payload) returned `invalid CSRF token`; the empty payload cannot insert a statistics record. Browser LanguageDetector information is unrelated. + +## Compatibility scope + +Reuse the existing API request-header builder and current CSRF cookie. Skip telemetry when no CSRF token is available. Keep server authentication, CSRF enforcement, payload normalization, four-second throttling and best-effort failure handling unchanged. No business records, migrations, vendor player changes or deployment are included. + +## Answer + +The reporter now attaches the current CSRF token on every request. A skipped report does not consume the throttle interval. HTTP and transport failures do not retry or escape into playback operations. + +Verification: the two new executable reporter regressions failed before the fix and pass afterward; eight related frontend tests pass. The frontend baseline gate passes with 13 existing failures against 14 allowed baseline cases and no new failures. Lint, formatting, production web build, TestClientMoviStats API tests and git diff checks pass. Evidence is retained in local-data/pr-checks/movi-csrf-*.log (untracked). No production deployment or task mutation was performed. diff --git a/src/lib/movi-player.ts b/src/lib/movi-player.ts index 1fdc7139..4493c301 100644 --- a/src/lib/movi-player.ts +++ b/src/lib/movi-player.ts @@ -1,3 +1,5 @@ +import { buildAPIRequestHeaders } from "../services/api/http.js"; + /** * movi-player runtime loader + runtime mode flags + telemetry reporter. * @@ -162,7 +164,6 @@ let lastReportAt = 0; export function reportMoviStats(event: MoviStatsEvent): void { const now = Date.now(); if (now - lastReportAt < MIN_REPORT_INTERVAL_MS) return; - lastReportAt = now; const payload = { ...event, src: normalizeSrc(event.src), @@ -171,9 +172,14 @@ export function reportMoviStats(event: MoviStatsEvent): void { try { const body = JSON.stringify(payload); if (body.length > 4096) return; + const headers = buildAPIRequestHeaders({ method: "POST" }); + // This endpoint only accepts authenticated, CSRF-protected reports. Skip + // reports during logout or missing-cookie transitions instead of issuing 403s. + if (!headers.has("X-Droboticflow-CSRF-Token")) return; + lastReportAt = now; void fetch(REPORT_ENDPOINT, { method: "POST", - headers: { "Content-Type": "application/json" }, + headers, body, credentials: "include", keepalive: true, diff --git a/tests/frontend/movi-stats-request.test.mjs b/tests/frontend/movi-stats-request.test.mjs new file mode 100644 index 00000000..73d96da2 --- /dev/null +++ b/tests/frontend/movi-stats-request.test.mjs @@ -0,0 +1,74 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import ts from "typescript"; +import { buildAPIRequestHeaders } from "../../src/services/api/http.js"; +const source = fs + .readFileSync(new URL("../../src/lib/movi-player.ts", import.meta.url), "utf8") + .replaceAll("import.meta.env.DEV", "false"); +const code = ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, +}).outputText; +function reporter(fetch) { + let cookie = "droboticflow_csrf=synthetic%2Dcsrf", + now = 10000; + const exports = {}; + const require = (path) => { + assert.equal(path, "../services/api/http.js"); + return { buildAPIRequestHeaders: (options) => buildAPIRequestHeaders({ ...options, cookieString: cookie }) }; + }; + new Function("exports", "require", "fetch", "Date", code)(exports, require, fetch, { now: () => now }); + return { + report: exports.reportMoviStats, + cookie: (value) => { + cookie = value; + }, + advance: () => { + now += 4001; + }, + }; +} +test("player stats pairs authenticated cookies with current CSRF headers on every report", () => { + const calls = []; + const r = reporter((url, options) => { + calls.push({ url, options }); + return Promise.resolve(new Response(null, { status: 202 })); + }); + r.report({ kind: "native-fallback", src: "/video.mp4?secret=value#frame", message: "x".repeat(350) }); + assert.equal(calls.length, 1); + assert.equal(calls[0].url, "/api/client/movi-stats"); + assert.equal(new Headers(calls[0].options.headers).get("X-Droboticflow-CSRF-Token"), "synthetic-csrf"); + assert.equal(calls[0].options.credentials, "include"); + assert.equal(calls[0].options.keepalive, true); + const payload = JSON.parse(calls[0].options.body); + assert.equal(payload.src, "/video.mp4"); + assert.equal(payload.message.length, 300); + r.report({ kind: "load" }); + assert.equal(calls.length, 1, "existing throttle remains active"); + r.advance(); + r.cookie("droboticflow_csrf=rotated"); + r.report({ kind: "load" }); + assert.equal(new Headers(calls[1].options.headers).get("X-Droboticflow-CSRF-Token"), "rotated"); +}); +test("missing CSRF skips telemetry; failed HTTP and transport reports never retry or escape", async () => { + let calls = 0; + const r = reporter(() => { + calls++; + return Promise.resolve(new Response(null, { status: 403 })); + }); + r.cookie(""); + r.report({ kind: "load" }); + assert.equal(calls, 0); + r.cookie("droboticflow_csrf=token"); + assert.doesNotThrow(() => r.report({ kind: "load" })); + assert.equal(calls, 1); + await Promise.resolve(); + assert.equal(calls, 1); + const offline = reporter(() => Promise.reject(Error("offline"))); + assert.doesNotThrow(() => offline.report({ kind: "load" })); + await Promise.resolve(); + const broken = reporter(() => { + throw Error("fetch unavailable"); + }); + assert.doesNotThrow(() => broken.report({ kind: "load" })); +});