Skip to content

chore(security): verify Cloro webhook signatures on /cloro/callback - #229

Merged
gkhngyk merged 1 commit into
mainfrom
chore/cloro-webhook-signature
Jun 11, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
chore/cloro-webhook-signature

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Closes #146

What

Adds HMAC-SHA256 signature verification to the public /cloro/callback webhook endpoint, enforced when CLORO_WEBHOOK_SECRET is set.

  • New server/src/lib/cloro-webhook-verify.js — pure verification function: computes HMAC-SHA256(secret, \${timestamp}.${rawBody}`), parses the v1=scheme fromX-Cloro-Signature, compares with crypto.timingSafeEqual`, and rejects timestamps outside a 5-minute replay window. No env access, injectable clock — ready for unit tests once the server test runner lands (ci: add automated test job once test infra exists #125).
  • server/src/server.js — mounts express.raw({ type: 'application/json' }) on /cloro/callback ahead of the global JSON parser so the body bytes survive intact for HMAC (the global parser skips the path since the body is already consumed; all other routes are untouched). The payload is JSON-parsed only after the signature check passes. Rejections respond 401 (bad/missing signature) or 400 (stale/invalid timestamp, malformed JSON) before the 200 ack, and log the delivery's webhook id + IP so probing is visible.
  • server/.env.example — documents CLORO_WEBHOOK_SECRET with the rollout caveat.

Backward compatibility

When CLORO_WEBHOOK_SECRET is unset — every self-hosted install today, and cloud until the dashboard switch is flipped — behavior is byte-for-byte unchanged: deliveries are accepted and validated via task_id entropy + cloro_pending_tasks lookup, exactly as before. Nothing breaks on merge.

The signature scheme was verified against Cloro's published docs (https://docs.cloro.dev/guides/webhooks): the whsec_… secret is used as-is (no prefix stripping or base64 decoding), signed string is timestamp.rawBody, hex digest in v1=.

Test plan

  • 11-case unit-style run of verifyCloroWebhook: valid signature accepted; missing headers → 401; wrong secret → 401; tampered body → 401; stale (±6 min) timestamp → 400; 4 min 59 s old → accepted; non-numeric timestamp → 400; garbage/empty v1= → 401
  • Live Express wiring test replicating the exact middleware order: signed delivery → 200 with correctly parsed body; unsigned → 401; tampered → 401; other routes still receive parsed JSON
  • Regression on a running dev server with no secret configured: unsigned callback → 200 and handler runs the full path (No pending task log); malformed JSON → 400; /api/* routes unaffected

Deploy notes (cloud)

Order matters after this merges and deploys:

  1. Deploy as-is — no env change, no behavior change.
  2. Enable signed deliveries in the Cloro dashboard (https://dashboard.cloro.dev/webhooks) and copy the generated whsec_… secret — it is shown exactly once.
  3. Immediately set CLORO_WEBHOOK_SECRET on the server and restart.

Doing 3 without 2 rejects every delivery with 401. The 2→3 gap is safe (unsigned deliveries are still accepted until the env lands), and Cloro retries failed deliveries up to 5 times with exponential backoff, so a short restart window self-heals.

Add HMAC-SHA256 signature verification (X-Cloro-Signature / X-Cloro-Timestamp,
v1=<hex> over `timestamp.rawBody`) to the public Cloro callback endpoint,
enforced when CLORO_WEBHOOK_SECRET is set. Uses crypto.timingSafeEqual and a
5-minute replay window; rejections log webhook id + IP for probe detection.

The callback path is mounted with express.raw() ahead of the global JSON
parser so the body bytes survive intact for HMAC, and the payload is parsed
only after the signature check passes.

When CLORO_WEBHOOK_SECRET is unset (self-hosted installs, or signing not yet
enabled in the Cloro dashboard) behavior is unchanged: deliveries are accepted
and validated via task_id lookup, as before.

Closes #146
@gkhngyk
gkhngyk merged commit 8c8615c into main Jun 11, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the chore/cloro-webhook-signature branch June 11, 2026 12:34
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.

chore(security): verify Cloro webhook signatures on /cloro/callback

1 participant