Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions worker/src/lib/__tests__/spark-worker-link.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
17 changes: 17 additions & 0 deletions worker/src/lib/spark-worker-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
40 changes: 40 additions & 0 deletions worker/src/routes/__tests__/spark-history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/<appId>/allexecutors: an array of executor summaries whose
// executorLogs map carries the stdout/stderr links the frontend turns into <a href> 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)
Expand Down
40 changes: 40 additions & 0 deletions worker/src/routes/__tests__/spark-ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/<appId>/allexecutors: an array of executor summaries whose
// executorLogs map carries the stdout/stderr links the frontend turns into <a href> 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')
})
})
})
8 changes: 7 additions & 1 deletion worker/src/routes/spark-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion worker/src/routes/spark-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down