Skip to content

fix(tts-audio): sign a JWT for the Admin API and fix two hidden read bugs - #282

Merged
ngeorger merged 1 commit into
mainfrom
fix/tts-audio-jwt-auth
Nov 27, 2026
Merged

ngeorger merged 1 commit into
mainfrom
fix/tts-audio-jwt-auth

Conversation

@sredevopsorgagents

@sredevopsorgagents sredevopsorgagents commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Three defects, all found by inspecting the running cluster rather than the code.

The original bug

Every Admin API call sent Authorization: Ghost <id>:<secret>. That header takes a signed JWT, so Ghost answered 400 INVALID_JWT on every request — a message that reads exactly like a revoked key. Ghost's source is unambiguous:

const decoded = jwt.decode(token, { complete: true });
if (!decoded || !decoded.header) throw new BadRequestError({ code: 'INVALID_JWT' });
const apiKeyId = decoded.header.kid;

Unit tests passed because the fake fetch never inspected headers.

Fix: sign an HS256 token per request (header { alg, typ, kid }, payload { iat, exp, aud: "/admin/" }, HMAC key = the hex-decoded secret). New test/auth.test.mjs decodes the header the worker builds and verifies the signature the way Ghost does.

Two more, exposed once auth worked

  • Locale matched on tag name. This publication names its locale tags "Español"/"English" and slugs them es/en, so every real post was ineligible — the live backfill reported 0 of 221. Now matches the slug, falling back to the name only when a webhook payload omits it.
  • ?formats=html,md is invalid. Ghost accepts html, mobiledoc, lexical, plaintext and answers 422 for md. Using plaintext.

Three more, from the cluster

  • A NetworkPolicy blocked the backfill from the API server. The egress rule allowed component=webhook; the backfill pod is labelled component=backfill, so it listed all 221 posts and then failed to create a single Job:
    failed for <slug>: connect ECONNREFUSED 10.43.0.1:443   (×221)
    
    Now selects component in (webhook, backfill). The worker stays excluded — it runs without a service-account token and has no business reaching the API server.
  • The summary line lied. That run ended with {"result":"ok","scanned":221,"created":0,"failed":221}. Exit code was already 1; the log now says failed. The new test fails without this fix.
  • amd64-only image in a mixed-architecture cluster. Pods run on the amd64 k3s worker, but the control-plane node is arm64 and only cordoned with an unschedulable taint. Uncordoning it would put a pod into ImagePullBackOff. The workflow now publishes linux/amd64,linux/arm64; the local build stays amd64 because load: true cannot take a multi-platform build.

Verified against the live publication

Step Result
signed token → /settings/, /posts/ 200
make backfill-plan 221 scanned, 221 eligible (was 0)
readPost on a real post 15.7 kB HTML, locale es
code-injection PUT accepted
read → extract → Kokoro → ffmpeg → GCS 219 kB MP3, 18.2 s audio
anonymous GET on that object 200 (public)

Gates

deploy/tts-audio/test.sh ✅ (79 tests) · yarn verify ✅ · all six PR checks ✅

Still blocking audio in production (infra, not code)

  1. tts-audio-gateway does not exist, so the HTTPRoute has no status and tts-audio.sredevops.org returns Cloudflare 1016. The receiver has never received a webhook — no worker Job has ever been created.
  2. CORS is not configured on tts-sredevopsorg; a ranged seek would fail in the browser.

Throughput note: a 15.7k-char post takes ~32 min at ~15 chars/s, against a 60-min Job deadline — 1.9× headroom, tight for the longest posts.

…bugs

Every Admin API call sent `Authorization: Ghost <id>:<secret>`. That header takes a
**signed JWT**, so Ghost answered `400 INVALID_JWT` on every request — a message
that reads exactly like a revoked key, which sent hours of debugging at the
credential instead of at the request. Ghost's own source is unambiguous:

    const decoded = jwt.decode(token, { complete: true });
    if (!decoded || !decoded.header) throw BadRequestError({ code: 'INVALID_JWT' });

The unit tests never caught it because the fake `fetch` did not inspect headers.

- sign an HS256 token per request: header `{ alg, typ, kid }`, payload
  `{ iat, exp, aud: "/admin/" }`, HMAC key = the **hex-decoded** secret.
  A non-`id:secret` key or a non-hex secret now fails loudly instead of producing
  a token that looks fine and is rejected.
- new test/auth.test.mjs decodes the header the worker builds and verifies the
  signature the way Ghost does, plus asserts no call path sends the raw key.
- backfill shares the same header builder instead of its own copy.

Two more bugs the fix immediately exposed, both only reachable once auth worked:

- **locale was matched on tag *name***. This publication names its locale tags
  "Español"/"English" and slugs them `es`/`en`, so every real post was judged
  ineligible: the live backfill reported 0 of 221 eligible. Match the slug, which
  is what routes.yaml filters on, and fall back to the name only when a webhook
  payload omits it.
- **`?formats=html,md` is not a valid request**. Ghost accepts html, mobiledoc,
  lexical and plaintext and answers 422 for `md`. Use `plaintext`, which is
  derived from the same stored source and is what the drift check wants.

Verified against the live publication with a real key: the signed token returns
200 on /settings/ and /posts/, readPost returns a real 15.7k-character post with
locale `es`, the code-injection PUT is accepted, and the backfill dry-run now
classifies all 221 posts (was 0).
Comment thread deploy/tts-audio/worker/src/ghost.mjs Dismissed
@sredevopsorgagents

Copy link
Copy Markdown
Collaborator Author

On the CodeQL alert (js/insufficient-password-hash)

CodeQL flagged createHmac("sha256", …) in ghost.mjs:111 and the required CodeQL check went red. It is a false positive, dismissed with justification:

Not password storage: this HMAC-SHA256 signs a JWT (Ghost Admin API token, RFC 7518 HS256) with the hex-decoded integration secret. No password is hashed or stored, and the token expires in 5 minutes.

The rule matches any createHmac('sha256', …) as password hashing. Signing a JWT is HMAC's canonical use — Ghost verifies the same signature on every request, so it has to be exactly this primitive. All six checks are green now.

@ngeorger
ngeorger merged commit f83ec59 into main Nov 27, 2026
6 checks passed
@ngeorger
ngeorger deleted the fix/tts-audio-jwt-auth branch November 27, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants