Appearance
Verify the signature
Every webhook is signed with your connection's signing secret (hpw_…). Check the signature before you trust a message. Anyone can send a POST to your address, but only Host Port knows the secret.
How it's made
- Take the
X-HostPort-Timestampheader. - Join it to the raw request body with a full stop:
timestamp + "." + body. - Take the HMAC-SHA256 of that, using the signing secret as the key, written as lowercase hex.
- Put
sha256=in front. That's theX-HostPort-Signatureheader.
Work out the same value, then compare:
- Use the raw body bytes, exactly as they arrived. Parsing the JSON and writing it out again changes spacing or key order, and the signature won't match.
- Compare in constant time, so the comparison can't leak the answer a byte at a time.
- Reject old messages. If the timestamp is more than five minutes from now, treat the message as stale.
Code
js
import { createHmac, timingSafeEqual } from 'node:crypto'
// rawBody: the request body as a string, before JSON.parse
export function verifyHostPort(headers, rawBody, secret) {
const ts = headers['x-hostport-timestamp']
const sig = headers['x-hostport-signature'] ?? ''
if (!ts || Math.abs(Date.now() / 1000 - Number(ts)) > 300) return false
const expected = 'sha256=' + createHmac('sha256', secret).update(`${ts}.${rawBody}`).digest('hex')
return sig.length === expected.length && timingSafeEqual(Buffer.from(sig), Buffer.from(expected))
}py
import hmac, hashlib, time
# raw_body: the request body as bytes, before json.loads
def verify_hostport(headers, raw_body: bytes, secret: str) -> bool:
ts = headers.get("X-HostPort-Timestamp", "")
sig = headers.get("X-HostPort-Signature", "")
if not ts.isdigit() or abs(time.time() - int(ts)) > 300:
return False
mac = hmac.new(secret.encode(), ts.encode() + b"." + raw_body, hashlib.sha256)
return hmac.compare_digest(sig, "sha256=" + mac.hexdigest())php
<?php
// $rawBody = file_get_contents('php://input');
function verify_hostport(string $rawBody, string $secret): bool {
$ts = $_SERVER['HTTP_X_HOSTPORT_TIMESTAMP'] ?? '';
$sig = $_SERVER['HTTP_X_HOSTPORT_SIGNATURE'] ?? '';
if (!ctype_digit($ts) || abs(time() - (int) $ts) > 300) return false;
$expected = 'sha256=' . hash_hmac('sha256', $ts . '.' . $rawBody, $secret);
return hash_equals($expected, $sig);
}In Express
Express parses JSON for you, which loses the raw body. Ask for the raw bytes on the webhook route:
js
import express from 'express'
const app = express()
app.post('/hostport', express.raw({ type: 'application/json' }), (req, res) => {
const raw = req.body.toString('utf8')
if (!verifyHostPort(req.headers, raw, process.env.HOSTPORT_WEBHOOK_SECRET)) {
return res.sendStatus(401)
}
res.sendStatus(200) // answer first
handle(JSON.parse(raw)) // then do the slow work
})When the secret changes
Edit webhook makes a new secret, and the old one stops working at once. Update your endpoint's secret straight after you save, or messages will fail the check. Failed messages are retried, so a short gap loses nothing.
