Skip to content

fix(alerts): sign the webhook's timestamp with its body (#38, release gate) - #39

Merged
angela-helios merged 8 commits into
HeliosSoftware:mainfrom
Gordex2014:fix/webhook-signed-timestamp
Nov 24, 2026
Merged

angela-helios merged 8 commits into
HeliosSoftware:mainfrom
Gordex2014:fix/webhook-signed-timestamp

Conversation

@Gordex2014

Copy link
Copy Markdown
Contributor

Part of #38: item 1 (decided before the next release) and the #30 minors. This PR is the release gate. The rest of #38 follows in a second PR stacked on this one.

What changes

Item 1: the webhook signature covers a timestamp.

  • Every attempt carries X-HIE-Timestamp: the Unix seconds when that attempt was sent, decimal digits only. It is sent signed or not.
  • With a secret, X-HIE-Signature: sha256=<hex> is the HMAC-SHA256 of {X-HIE-Timestamp}.{raw body}, as Stripe-style webhooks do, in HIE's own headers. The header name and the sha256= form are unchanged; only what is signed changes.
  • A retry sends the same body bytes and the same event_id, stamped and signed again when it leaves. The receiver's tolerance therefore never has to cover the retry backoff.
  • docs/sources.md now has a Verifying step:
    • verify over the header's exact bytes, ., and the body received, in constant time, before parsing;
    • refuse a timestamp more than the tolerance away from the receiver's clock, in either direction. Use 5 minutes unless the clocks are known to drift further;
    • keep each acted-on event_id for at least twice the tolerance. A request stamped one tolerance ahead stays valid for two.
  • No compatibility mode. No release signed the body alone: v0.2.0 sent unsigned events (git show v0.2.0:src/alerts.rs has no signature code). The docs and the amendment say so.
  • docs/decisions.md: an amendment to decision 40. Its Chosen, Why and Cost lines that the amendment changes are marked.

#30 minors.

  • Secret whitespace. alerts.webhook_secret and HIE_ALERT_WEBHOOK_SECRET are trimmed of leading and trailing whitespace when read. A secret file that ends in a newline now signs with the secret it holds. The untrimmed buffer is wiped.
  • A parked fired hidden behind a later cleared. GET /api/alerts keeps webhook as the latest event, with the same fields. It adds webhook.events: every event of the alert, oldest first, each with event, event_id, state, attempts, last_error, last_status and delivered_at. The page of alerts and their events are read in one statement, so a page still counts alerts, not events.
  • Sequential sends: not done. Sending several events at once is a design change: concurrency per destination, the order of fired and cleared, and the claim leases. It is left for you to decide.

In plain words

A receiver can now reject an old delivery that someone captured and sends again, without remembering every event it has ever seen. The API also shows a webhook event that is stuck for an earlier URL, even after a later event for the same alert went through.

Side effects checked

  • Receivers. A receiver that verified the body alone must now verify {timestamp}.{body}. No release shipped the old form, so no released receiver breaks.
  • Clock skew. A receiver whose clock is off by more than its tolerance refuses every event. Each retry is refused too, until the five retries are spent. Instances already need synchronised clocks for leases.
  • Stored events. The stored body is unchanged and the signature is still computed at send time. Events written before this change are signed the new way when they are sent.
  • API. webhook.events is additive. webhook keeps its fields, and stays null for alerts from before events were recorded.
  • Secrets. A secret meant to begin or end with whitespace now signs without it. A blank secret is still refused in hie.yaml, and a blank variable still leaves the file's value.

How to verify

The CI commands, with CI=true and default parallelism, on this tree (Linux):

$ helios-ci-gate      # fmt, clippy (both feature sets), Python tests, cargo test, the no-backend tests
test: passed (579 tests, 0 failures)

The changed suites (--lib, alert_webhook, alerts, check) ran once more on SQLite and twice against a throwaway PostgreSQL 16. On PostgreSQL the only failures were the two secrets::key tests that fail there before this change too.

  • Signature (tests/alert_webhook.rs):
    • each request carries a timestamp within seconds of now;
    • the signature verifies over {timestamp}.{body};
    • a body-only HMAC does not match;
    • a signed retry with a 1.1 s backoff is stamped strictly later and signed again over the identical body.
  • API-level delivery (tests/alerts.rs): checked against the timestamp header.
  • Unit tests (src/alerts.rs):
    • the HMAC is pinned to RFC 4231;
    • the signed string is pinned to a value computed independently with Python's hmac.
  • Secrets (src/config.rs): a trailing newline is trimmed, from the environment and from a YAML block scalar.
  • webhook.events (tests/alerts.rs):
    • a fired parked for another URL is still listed after its cleared was delivered to this one;
    • an alert with two events counts once in a page.

Links

Every attempt carries X-HIE-Timestamp, and X-HIE-Signature is the
HMAC-SHA256 of "{timestamp}.{body}": a captured delivery can no longer be
replayed later to a receiver that pruned its event ids. Receivers enforce
a tolerance, 5 minutes by default. No release signed the body alone, so
there is no compatibility mode. The webhook secret is trimmed when read.

Refs HeliosSoftware#38
A fired event parked for an earlier URL, or failed, was hidden behind a
cleared event delivered to the current one. GET /api/alerts keeps the
latest event's fields in webhook and adds webhook.events, every event of
the alert oldest first, read with the page in one statement.

Refs HeliosSoftware#38
@Gordex2014
Gordex2014 marked this pull request as draft November 24, 2026 15:31
@Gordex2014
Gordex2014 marked this pull request as ready for review November 24, 2026 15:36
@angela-helios
angela-helios merged commit 329f639 into HeliosSoftware:main Nov 24, 2026
2 checks passed
@Gordex2014
Gordex2014 deleted the fix/webhook-signed-timestamp branch November 25, 2026 14:20
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.

2 participants