Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(workflows): sign cancel JWTs with a dedicated secret (#87847)

Harley Alexander63b1c7f41e91
diff --git a/nodejs/src/cdp/cdp-api.test.ts b/nodejs/src/cdp/cdp-api.test.ts
index ddbc6f5f8434..deeb468da1a9 100644
--- a/nodejs/src/cdp/cdp-api.test.ts
+++ b/nodejs/src/cdp/cdp-api.test.ts
@@ -1737,7 +1737,7 @@ describe('CDP API', () => {
const mintCancelToken = (
teamId: number,
hogFlowId: string,
- { secret = 'local-dev-workflows-reschedule-jwt', audience = 'posthog:workflows:cancel_invocations' } = {}
+ { secret = 'local-dev-workflows-cancel-jwt', audience = 'posthog:workflows:cancel_invocations' } = {}
) => jwt.sign({ team_id: teamId, hog_flow_id: hogFlowId }, secret, { audience, expiresIn: '2m' })
// No hog flow row exists for this id: cancel deliberately skips the flow lookup so it
@@ -1789,8 +1789,8 @@ describe('CDP API', () => {
],
["another team's token", () => ({ Authorization: `Bearer ${mintCancelToken(team.id + 1, cancelFlowId)}` })],
[
- // The cancel purpose shares its signing key with reschedule_parked, so the audience
- // is the only thing keeping a reschedule token out of cancel.
+ // Cancel and reschedule use separate keys now, so a reschedule-audience token is
+ // rejected on audience regardless of which key signed it.
'a reschedule-audience token',
() => ({
Authorization: `Bearer ${mintCancelToken(team.id, cancelFlowId, {
@@ -1798,6 +1798,16 @@ describe('CDP API', () => {
})}`,
}),
],
+ [
+ // The cancel key is dedicated: a cancel-audience token signed with the reschedule
+ // sweep's key must be rejected, or splitting the keys would buy no real isolation.
+ 'a token signed with the reschedule key',
+ () => ({
+ Authorization: `Bearer ${mintCancelToken(team.id, cancelFlowId, {
+ secret: 'local-dev-workflows-reschedule-jwt',
+ })}`,
+ }),
+ ],
])('rejects a request with %s', async (_desc, headers) => {
const res = await supertest(app)
.post(`/api/projects/${team.id}/hog_flows/${cancelFlowId}/invocations/cancel`)
@@ -1837,7 +1847,7 @@ describe('CDP API', () => {
teamId: number,
hogFlowId: string,
{
- secret = 'local-dev-workflows-reschedule-jwt',
+ secret = 'local-dev-workflows-cancel-jwt',
audience = 'posthog:workflows:cancel_batch',
batchJob = batchJobId,
} = {}
@@ -1901,8 +1911,8 @@ describe('CDP API', () => {
}),
],
[
- // The batch purpose shares its signing key with the other workflows purposes, so
- // the audience is the only thing keeping an invocations-cancel token out of here.
+ // The two cancel purposes share the cancel key, so the audience is the only thing
+ // keeping an invocations-cancel token out of the batch route.
'an invocations-cancel-audience token',
() => ({
Authorization: `Bearer ${mintBatchToken(team.id, batchFlowId, {
diff --git a/nodejs/src/cdp/cdp-api.ts b/nodejs/src/cdp/cdp-api.ts
index 14fed64116d4..50bce174e39e 100644
--- a/nodejs/src/cdp/cdp-api.ts
+++ b/nodejs/src/cdp/cdp-api.ts
@@ -208,11 +208,11 @@ export class CdpApi {
)
this.cancelInvocationsJwt = new ScopedServiceJwt(
PosthogJwtAudience.WORKFLOWS_CANCEL_INVOCATIONS,
- config.WORKFLOWS_RESCHEDULE_JWT_SECRET || ''
+ config.WORKFLOWS_CANCEL_JWT_SECRET || ''
)
this.cancelBatchJwt = new ScopedServiceJwt(
PosthogJwtAudience.WORKFLOWS_CANCEL_BATCH,
- config.WORKFLOWS_RESCHEDULE_JWT_SECRET || ''
+ config.WORKFLOWS_CANCEL_JWT_SECRET || ''
)
}
@@ -1169,7 +1169,7 @@ export class CdpApi {
}
if (!this.cancelInvocationsJwt.enabled) {
return res.status(503).json({
- error: 'Workflows scoped auth not configured (WORKFLOWS_RESCHEDULE_JWT_SECRET unset)',
+ error: 'Workflows scoped auth not configured (WORKFLOWS_CANCEL_JWT_SECRET unset)',
})
}
@@ -1254,7 +1254,7 @@ export class CdpApi {
}
if (!this.cancelBatchJwt.enabled) {
return res.status(503).json({
- error: 'Workflows scoped auth not configured (WORKFLOWS_RESCHEDULE_JWT_SECRET unset)',
+ error: 'Workflows scoped auth not configured (WORKFLOWS_CANCEL_JWT_SECRET unset)',
})
}
diff --git a/nodejs/src/cdp/config.ts b/nodejs/src/cdp/config.ts
index 177686e9c44f..e4ef7ef56c73 100644
--- a/nodejs/src/cdp/config.ts
+++ b/nodejs/src/cdp/config.ts
@@ -179,6 +179,11 @@ export type CdpConfig = ClickhouseConfig & {
// newest first (first signs, all verify). Deliberately NOT the fleet-wide INTERNAL_API_SECRET
// (see .agents/security.md): empty in prod means the route fails closed until provisioned.
WORKFLOWS_RESCHEDULE_JWT_SECRET: string
+ // Scoped JWT keys verifying Django's calls to the cancel routes (invocations/cancel and
+ // batch_jobs/:id/cancel). A dedicated key, separate from the reschedule sweep's above: the
+ // web tier mints cancels while the worker mints reschedules, so neither tier's key can forge
+ // the other's calls. Same comma-separated rotation and fail-closed-when-empty semantics.
+ WORKFLOWS_CANCEL_JWT_SECRET: string
// Scoped JWT keys signing the workflow engine's task-create calls to Django, with the same
// comma-separated rotation and fail-closed-when-empty semantics as the secret above.
TASKS_CREATE_JWT_SECRET: string
@@ -339,6 +344,8 @@ export function getDefaultCdpConfig(): CdpConfig {
// Dev/test default must match Django's (posthog/settings/data_stores.py).
WORKFLOWS_RESCHEDULE_JWT_SECRET: isTestEnv() || isDevEnv() ? 'local-dev-workflows-reschedule-jwt' : '',
// Dev/test default must match Django's (posthog/settings/data_stores.py).
+ WORKFLOWS_CANCEL_JWT_SECRET: isTestEnv() || isDevEnv() ? 'local-dev-workflows-cancel-jwt' : '',
+ // Dev/test default must match Django's (posthog/settings/data_stores.py).
TASKS_CREATE_JWT_SECRET: isTestEnv() || isDevEnv() ? 'local-dev-tasks-create-jwt' : '',
CYCLOTRON_NODE_RESCHEDULE_FLOOR_SECONDS: 600,
CYCLOTRON_NODE_RESCHEDULE_WAKE_RATE_PER_SECOND: 200,
diff --git a/nodejs/src/common/api/middleware/internal-api-auth.ts b/nodejs/src/common/api/middleware/internal-api-auth.ts
index b6b8e96a4592..ff7c1d79f378 100644
--- a/nodejs/src/common/api/middleware/internal-api-auth.ts
+++ b/nodejs/src/common/api/middleware/internal-api-auth.ts
@@ -30,9 +30,9 @@ const PUBLIC_PATH_PREFIXES = ['/public/', '/_health', '/_ready', '/_metrics', '/
const SCOPED_AUTH_PATH_PATTERNS = [
// CdpApi.postHogFlowRescheduleParked, verified against WORKFLOWS_RESCHEDULE_JWT_SECRET.
/^\/api\/projects\/[^/]+\/hog_flows\/[^/]+\/reschedule_parked$/,
- // CdpApi.postHogFlowCancelInvocations, verified against the same key, audience-pinned.
+ // CdpApi.postHogFlowCancelInvocations, verified against WORKFLOWS_CANCEL_JWT_SECRET, audience-pinned.
/^\/api\/projects\/[^/]+\/hog_flows\/[^/]+\/invocations\/cancel$/,
- // CdpApi.postHogFlowCancelBatchJob, verified against the same key, audience-pinned.
+ // CdpApi.postHogFlowCancelBatchJob, verified against WORKFLOWS_CANCEL_JWT_SECRET, audience-pinned.
/^\/api\/projects\/[^/]+\/hog_flows\/[^/]+\/batch_jobs\/[^/]+\/cancel$/,
]
diff --git a/posthog/plugins/plugin_server_api.py b/posthog/plugins/plugin_server_api.py
index 8aede4b9aa65..4e54b289ad4b 100644
--- a/posthog/plugins/plugin_server_api.py
+++ b/posthog/plugins/plugin_server_api.py
@@ -161,12 +161,13 @@ def reschedule_hog_flow_parked_jobs(
)
-# Same signing key as reschedule_parked: every workflows route on the CDP API verifies against
-# WORKFLOWS_RESCHEDULE_JWT_SECRET, so the key already spans that one caller/callee surface. The
-# distinct audience still keeps a cancel token useless for reschedule (and vice versa).
+# Dedicated cancel key, distinct from reschedule_parked's: the scoped-JWT rule is one key per
+# caller/callee surface, and cancel mints from the web tier while reschedule mints from the
+# worker, so a leak of one tier's key can't forge the other's calls. The distinct audience
+# still keeps a cancel-invocations token useless for cancel-batch (and vice versa).
WORKFLOWS_CANCEL_INVOCATIONS_JWT_PURPOSE = ScopedServiceJwtPurpose(
audience=PosthogJwtAudience.WORKFLOWS_CANCEL_INVOCATIONS,
- settings_name="WORKFLOWS_RESCHEDULE_JWT_SECRETS",
+ settings_name="WORKFLOWS_CANCEL_JWT_SECRETS",
default_ttl=timedelta(minutes=2),
)
@@ -176,14 +177,14 @@ def _mint_cancel_invocations_jwt(team_id: int, hog_flow_id: str) -> str:
of this one team + workflow. Raises when unprovisioned so the call fails closed. Verified in
the plugin server's CdpApi.postHogFlowCancelInvocations."""
if not WORKFLOWS_CANCEL_INVOCATIONS_JWT_PURPOSE.enabled():
- raise RuntimeError("WORKFLOWS_RESCHEDULE_JWT_SECRET is not configured — cannot cancel invocations")
+ raise RuntimeError("WORKFLOWS_CANCEL_JWT_SECRET is not configured — cannot cancel invocations")
return WORKFLOWS_CANCEL_INVOCATIONS_JWT_PURPOSE.mint({"team_id": team_id, "hog_flow_id": hog_flow_id})
-# Same shared workflows signing key as the purposes above, on its own audience.
+# Same dedicated cancel key as cancel-invocations (both mint from the web tier), on its own audience.
WORKFLOWS_CANCEL_BATCH_JWT_PURPOSE = ScopedServiceJwtPurpose(
audience=PosthogJwtAudience.WORKFLOWS_CANCEL_BATCH,
- settings_name="WORKFLOWS_RESCHEDULE_JWT_SECRETS",
+ settings_name="WORKFLOWS_CANCEL_JWT_SECRETS",
default_ttl=timedelta(minutes=2),
)
@@ -193,7 +194,7 @@ def _mint_cancel_batch_jwt(team_id: int, hog_flow_id: str, batch_job_id: str) ->
this one batch run of this one team + workflow. Raises when unprovisioned so the call fails
closed. Verified in the plugin server's CdpApi.postHogFlowCancelBatchJob."""
if not WORKFLOWS_CANCEL_BATCH_JWT_PURPOSE.enabled():
- raise RuntimeError("WORKFLOWS_RESCHEDULE_JWT_SECRET is not configured — cannot stop the batch run")
+ raise RuntimeError("WORKFLOWS_CANCEL_JWT_SECRET is not configured — cannot stop the batch run")
return WORKFLOWS_CANCEL_BATCH_JWT_PURPOSE.mint(
{"team_id": team_id, "hog_flow_id": hog_flow_id, "batch_job_id": batch_job_id}
)
diff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.py
index a8c80d47a155..8f3f37d5f19c 100644
--- a/posthog/settings/data_stores.py
+++ b/posthog/settings/data_stores.py
@@ -557,6 +557,17 @@ WORKFLOWS_RESCHEDULE_JWT_SECRETS = get_list(
get_from_env("WORKFLOWS_RESCHEDULE_JWT_SECRET", "local-dev-workflows-reschedule-jwt" if DEBUG or TEST else "")
)
+# Scoped JWT keys for the workflows cancel routes (invocations/cancel and batch_jobs/:id/cancel).
+# Web mints, the plugin server's cancel routes verify. A dedicated key per the scoped-JWT rule
+# (one key per caller/callee surface), so cancel from the web tier never carries the reschedule
+# sweep's key, and a leak of one can't forge the other. Comma-separated, newest first: the first
+# key signs, the plugin server verifies against all. Empty outside dev/test, so the cancel routes
+# fail closed until provisioned. The dev/test value must match the plugin server's default
+# (nodejs/src/cdp/config.ts).
+WORKFLOWS_CANCEL_JWT_SECRETS = get_list(
+ get_from_env("WORKFLOWS_CANCEL_JWT_SECRET", "local-dev-workflows-cancel-jwt" if DEBUG or TEST else "")
+)
+
# Signs the tokens a workflow's "Create AI task" action calls back with. The dev/test value
# must match the plugin server's minting default so local workflows work with no setup.
TASKS_CREATE_JWT_SECRETS = get_list(