diff --git a/worker/src/lib/__tests__/spark-worker-link.test.ts b/worker/src/lib/__tests__/spark-worker-link.test.ts new file mode 100644 index 0000000..b09b68b --- /dev/null +++ b/worker/src/lib/__tests__/spark-worker-link.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest' +import { rewriteSparkWorkerLink, rewriteSparkWorkerLinksInJson } from '../spark-worker-link' + +describe('rewriteSparkWorkerLink', () => { + it('rewrites the bare, http, https and protocol-relative Worker UI spellings', () => { + expect(rewriteSparkWorkerLink('spark:8082/logPage/?appId=app-1&executorId=0&logType=stdout')).toBe( + '/spark-worker/logPage/?appId=app-1&executorId=0&logType=stdout' + ) + expect(rewriteSparkWorkerLink('http://spark:8082/logPage/?appId=app-1&executorId=0&logType=stderr')).toBe( + '/spark-worker/logPage/?appId=app-1&executorId=0&logType=stderr' + ) + expect(rewriteSparkWorkerLink('https://spark:8082/logPage/?appId=app-1')).toBe('/spark-worker/logPage/?appId=app-1') + expect(rewriteSparkWorkerLink('//spark:8082/logPage/?appId=app-1')).toBe('/spark-worker/logPage/?appId=app-1') + }) + + it('leaves unrelated values untouched', () => { + expect(rewriteSparkWorkerLink('https://spark.apache.org/docs')).toBeNull() + expect(rewriteSparkWorkerLink('spark:80820/logPage/?appId=app-1')).toBeNull() + expect(rewriteSparkWorkerLink('spark:8082oops')).toBeNull() + expect(rewriteSparkWorkerLink('')).toBeNull() + expect(rewriteSparkWorkerLink(undefined as unknown as string)).toBeNull() + }) +}) + +describe('rewriteSparkWorkerLinksInJson', () => { + it('rewrites executorLogs links inside a real allexecutors-shaped payload without a JSON round-trip', () => { + // Raw JSON text so the >2^53 integer stays byte-for-byte; a JSON round-trip would collapse it. + const raw = + '[{"id":"0","hostPort":"worker-0.internal:8082","totalInputBytes":9007199254740993,' + + '"executorLogs":{"stdout":"http://spark:8082/logPage/?appId=app-1&executorId=0&logType=stdout",' + + '"stderr":"spark:8082/logPage/?appId=app-1&executorId=0&logType=stderr"}}]' + const rewritten = rewriteSparkWorkerLinksInJson(raw) + expect(rewritten).toContain('/spark-worker/logPage/?appId=app-1&executorId=0&logType=stdout') + expect(rewritten).toContain('/spark-worker/logPage/?appId=app-1&executorId=0&logType=stderr') + expect(rewritten).not.toContain('spark:8082') + // Non-string fields must survive byte-for-byte, including integers beyond 2^53. + expect(rewritten).toContain('9007199254740993') + }) + + it('leaves similar host:port tokens and unrelated JSON alone', () => { + const raw = '{"a":"spark:80820/logPage/?appId=app-1","b":"http://spark:80820/logPage","c":123}' + expect(rewriteSparkWorkerLinksInJson(raw)).toBe(raw) + }) +}) diff --git a/worker/src/lib/spark-worker-link.ts b/worker/src/lib/spark-worker-link.ts index 4b6dd78..da26a38 100644 --- a/worker/src/lib/spark-worker-link.ts +++ b/worker/src/lib/spark-worker-link.ts @@ -58,3 +58,20 @@ export function rewriteSparkWorkerLink(val: string): string | null { return `${SPARK_WORKER_MOUNT}${path}${query}${hash}` } + +/** + * Rewrites Spark Worker UI (`spark:8082`) links embedded in the string values of a JSON document. + * Spark carries the executor stdout/stderr links in REST JSON (for example the History Server's + * `allexecutors` response under each executor's `executorLogs`), not only in HTML, so the JSON + * proxy branch has to run the same rewriteSparkWorkerLink treatment. This stays at the raw-text + * level — like the JSON origin sanitisation already in place — so non-target fields keep their + * exact bytes and JSON is never re-serialised. The boundary lookahead keeps `spark:80820` and + * similar host:port spellings intact. + */ +export function rewriteSparkWorkerLinksInJson(rawJson: string): string { + if (!rawJson || typeof rawJson !== 'string') return rawJson + // Spark emits the Worker UI address with no scheme, `http://`, `https://`, or `//`; the URL then + // runs to the enclosing JSON string boundary (a quote, backslash, or whitespace). + const pattern = /(?:https?:\/\/|\/\/)?spark:8082(?=[/?#"\\\s]|$)(?:[/?#][^"\\\s]*)?/gi + return rawJson.replace(pattern, (match) => rewriteSparkWorkerLink(match) ?? match) +} diff --git a/worker/src/routes/__tests__/spark-history.test.ts b/worker/src/routes/__tests__/spark-history.test.ts index 90c06f1..104b869 100644 --- a/worker/src/routes/__tests__/spark-history.test.ts +++ b/worker/src/routes/__tests__/spark-history.test.ts @@ -368,6 +368,46 @@ describe('/spark-history proxying with signed cookie', () => { expect(JSON.stringify(json)).not.toContain(HISTORY_URL) }) + it('rewrites spark:8082 executor log links inside the real allexecutors JSON response', async () => { + const e = env() + const cookie = await setupSession(e) + + // Spark 3.5.x /api/v1/applications//allexecutors: an array of executor summaries whose + // executorLogs map carries the stdout/stderr links the frontend turns into cells. + const allexecutors = [ + { + id: '0', + hostPort: 'worker-0.internal:8082', + isActive: true, + totalCores: 2, + executorLogs: { + stdout: 'http://spark:8082/logPage/?appId=app-20261129-0001&executorId=0&logType=stdout', + stderr: 'http://spark:8082/logPage/?appId=app-20261129-0001&executorId=0&logType=stderr', + }, + }, + ] + + fetchMock.mockResolvedValueOnce( + new Response(JSON.stringify(allexecutors), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + ) + + const res = await callWorker(e, '/spark-history/api/v1/applications/app-20261129-0001/allexecutors', { + headers: { Cookie: `spark_history_session=${cookie}` }, + }) + expect(res.status).toBe(200) + const json = (await res.json()) as typeof allexecutors + expect(json[0]!.executorLogs.stdout).toBe( + '/spark-worker/logPage/?appId=app-20261129-0001&executorId=0&logType=stdout' + ) + expect(json[0]!.executorLogs.stderr).toBe( + '/spark-worker/logPage/?appId=app-20261129-0001&executorId=0&logType=stderr' + ) + expect(JSON.stringify(json)).not.toContain('spark:8082') + }) + it('returns 502 when upstream History Server is unreachable, without leaking tokens', async () => { const e = env() const cookie = await setupSession(e) diff --git a/worker/src/routes/__tests__/spark-ui.test.ts b/worker/src/routes/__tests__/spark-ui.test.ts index 420f02a..19fb72a 100644 --- a/worker/src/routes/__tests__/spark-ui.test.ts +++ b/worker/src/routes/__tests__/spark-ui.test.ts @@ -868,5 +868,45 @@ describe('/spark-ui proxying with signed cookie', () => { const raw = JSON.stringify(json) expect(raw).not.toContain(UI_URL) }) + + it('rewrites spark:8082 executor log links inside the real allexecutors JSON response', async () => { + const e = env() + const cookie = await setupSession(e) + + // Spark 3.5.x /api/v1/applications//allexecutors: an array of executor summaries whose + // executorLogs map carries the stdout/stderr links the frontend turns into cells. + const allexecutors = [ + { + id: '0', + hostPort: 'worker-0.internal:8082', + isActive: true, + totalCores: 2, + executorLogs: { + stdout: 'http://spark:8082/logPage/?appId=app-20261128-0001&executorId=0&logType=stdout', + stderr: 'http://spark:8082/logPage/?appId=app-20261128-0001&executorId=0&logType=stderr', + }, + }, + ] + + fetchMock.mockResolvedValueOnce( + new Response(JSON.stringify(allexecutors), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + ) + + const res = await callWorker(e, '/spark-ui/api/v1/applications/app-20261128-0001/allexecutors', { + headers: { Cookie: `spark_ui_session=${cookie}` }, + }) + expect(res.status).toBe(200) + const json = (await res.json()) as typeof allexecutors + expect(json[0]!.executorLogs.stdout).toBe( + '/spark-worker/logPage/?appId=app-20261128-0001&executorId=0&logType=stdout' + ) + expect(json[0]!.executorLogs.stderr).toBe( + '/spark-worker/logPage/?appId=app-20261128-0001&executorId=0&logType=stderr' + ) + expect(JSON.stringify(json)).not.toContain('spark:8082') + }) }) }) diff --git a/worker/src/routes/spark-history.ts b/worker/src/routes/spark-history.ts index 5a123fb..abc7398 100644 --- a/worker/src/routes/spark-history.ts +++ b/worker/src/routes/spark-history.ts @@ -8,7 +8,12 @@ import { import { tables } from '../lib/db' import { openSecret } from '../lib/secrets' import { normalizeSparkAccess } from '../lib/spark-access' -import { rewriteSparkWorkerLink, SPARK_WORKER_MOUNT, SPARK_WORKER_SESSION_COOKIE } from '../lib/spark-worker-link' +import { + rewriteSparkWorkerLink, + rewriteSparkWorkerLinksInJson, + SPARK_WORKER_MOUNT, + SPARK_WORKER_SESSION_COOKIE, +} from '../lib/spark-worker-link' import { requireAuth } from '../middleware/auth' import { isSecure } from '../middleware/security' import type { DatabaseRow, Env, Variables } from '../types' @@ -420,6 +425,7 @@ async function handleSparkHistory(c: Context<{ Bindings: Env; Variables: Variabl sanitizedJson = sanitizedJson.replaceAll(parsedTarget.origin, HISTORY_MOUNT) } } catch {} + sanitizedJson = rewriteSparkWorkerLinksInJson(sanitizedJson) return new Response(sanitizedJson, { status: proxyRes.status, statusText: proxyRes.statusText, diff --git a/worker/src/routes/spark-ui.ts b/worker/src/routes/spark-ui.ts index 8a39974..c197e16 100644 --- a/worker/src/routes/spark-ui.ts +++ b/worker/src/routes/spark-ui.ts @@ -4,7 +4,12 @@ import { createSparkUiSessionToken, createWorkerUiSessionToken, verifySparkUiSes import { tables } from '../lib/db' import { openSecret } from '../lib/secrets' import { normalizeSparkAccess } from '../lib/spark-access' -import { rewriteSparkWorkerLink, SPARK_WORKER_MOUNT, SPARK_WORKER_SESSION_COOKIE } from '../lib/spark-worker-link' +import { + rewriteSparkWorkerLink, + rewriteSparkWorkerLinksInJson, + SPARK_WORKER_MOUNT, + SPARK_WORKER_SESSION_COOKIE, +} from '../lib/spark-worker-link' import { requireAuth } from '../middleware/auth' import { isSecure } from '../middleware/security' import type { DatabaseRow, Env, Variables } from '../types' @@ -428,6 +433,7 @@ async function handleSparkUi(c: Context<{ Bindings: Env; Variables: Variables }> sanitizedJson = sanitizedJson.replaceAll(parsedTarget.origin, '/spark-ui') } } catch {} + sanitizedJson = rewriteSparkWorkerLinksInJson(sanitizedJson) return new Response(sanitizedJson, { status: proxyRes.status, statusText: proxyRes.statusText,