Receiver security / HMAC-SHA256
Verify the bytes you actually received.
A receiver should authenticate the raw body before trusting fields, check the delivery timestamp and apply durable deduplication before performing side effects.
Header and signing input
PulseFlow-Signature: t=1794643200,v1=<64 hexadecimal characters>
signed_bytes = UTF8(timestamp + '.') || raw_request_body
signature = HMAC_SHA256(destination_secret, signed_bytes)The timestamp is Unix seconds in the signature header, not the event's occurred_at value. A replay can legitimately deliver an older event with a new signing timestamp. Reject timestamps more than 300 seconds in either direction from the receiver's synchronized clock.
Node.js reference function
import { createHmac, timingSafeEqual } from 'node:crypto';
export function verify(rawBody, header, secret, nowSeconds = Math.floor(Date.now() / 1000)) {
if (!Buffer.isBuffer(rawBody) || typeof header !== 'string' ||
typeof secret !== 'string' || secret.length === 0) return false;
const parts = header.split(',').map(part => part.trim());
const timestamps = parts.filter(part => part.startsWith('t='));
if (timestamps.length !== 1) return false;
const rawTimestamp = timestamps[0].slice(2);
if (!/^\d{10}$/.test(rawTimestamp)) return false;
const timestamp = Number(rawTimestamp);
if (Math.abs(nowSeconds - timestamp) > 300) return false;
const expected = createHmac('sha256', secret)
.update(rawTimestamp + '.')
.update(rawBody)
.digest();
return parts.filter(part => part.startsWith('v1=')).some(part => {
const candidate = part.slice(3);
if (!/^[a-f0-9]{64}$/i.test(candidate)) return false;
return timingSafeEqual(Buffer.from(candidate, 'hex'), expected);
});
}Capture a bounded raw Buffer before JSON middleware transforms the body. Enforce a request-size limit before buffering and reject malformed headers rather than throwing an uncaught exception. The reference function assumes a finite, trusted server clock; callers must not obtain nowSeconds from user input.
Correct receiver order
- Apply method, content-type and body-size limits.
- Read the exact bytes and validate signature-header structure.
- Verify freshness and HMAC against an authorized destination secret.
- Parse JSON and validate the envelope and event-specific fields.
- Atomically record the inbox key with the side effect or durable work item.
- Acknowledge duplicates without reapplying the effect.
Rotation and replay defense
During a bounded rotation overlap, verify against the active and immediately previous secret. Remove the old secret after all valid in-flight deliveries can complete. Log only secret version identifiers, never key values. Timestamp checks limit capture-and-replay windows but do not replace deduplication; an attacker or network can resend a fresh valid delivery.
Common verification failures
| Symptom | Likely cause |
|---|---|
| JSON is equal but HMAC differs | Whitespace, encoding or key order changed after signing |
| Only one environment fails | Wrong destination secret or clock skew |
| Old events always rejected | Confusing occurrence time with signing time |
| Valid duplicate repeats a side effect | No durable, transactional inbox |
Use the local inspector to alter the body or timestamp deliberately. The extended integration notes are available under the receiver authentication handbook.