## Webhooks

Threat alerts pushed to Slack, Discord, or your own endpoint — configured under Webhooks in the console’s sidebar.

**When they fire.** On engine-detected threats (a VPN, a proxy, Apple's iCloud Private Relay (review), a Tor exit, automation, a fake or tampered browser, an emulator, a virtual machine (review)) *and* on any evaluation your own custom rules or exception pins decided to `block`. A datacenter address, a disposable email, a time-zone mismatch, the account links, and the email and phone scores send nothing on their own. Slack and Discord URLs get a channel-ready message; any other HTTPS endpoint receives a JSON event (`event: "threat.detected"` with IP, threat type, and the signal details). The dashboard's *Send test* button delivers the same shape with `event: "test"` — if your receiver switches strictly on the event type, handle both, or the test button will look like a silent failure.

**Verifying authenticity.** Every delivery is signed with your account's signing secret (shown next to the webhook URL in the dashboard, where **Replace secret** creates a new one: deliveries are signed with it at once, so update your receiver right after, and replace it whenever someone who saw it should no longer be able to sign alerts). Compute `HMAC_SHA256(secret, timestamp + "." + rawBody)` using the `X-Maskbreak-Timestamp` header and compare it (constant-time) to the hex digest in `X-Maskbreak-Signature` (after the `sha256=` prefix). Reject anything unsigned, mismatched, or older than 5 minutes.

**Copy-paste receivers.** The two classic mistakes are verifying against the re-serialized parsed body instead of the raw bytes, and comparing with `==`. These receivers do it right:

```
const crypto = require('crypto');
const express = require('express');
const app = express();

// Raw body required — verify the exact bytes Maskbreak signed,
// never a re-serialized JSON.parse() of them.
app.post('/webhooks/sentinel', express.raw({ type: 'application/json' }), (req, res) => {
  const secret = process.env.SENTINEL_WEBHOOK_SECRET;  // shown next to the URL in the dashboard
  const ts  = req.get('X-Maskbreak-Timestamp') || '';
  const sig = req.get('X-Maskbreak-Signature') || '';

  // Freshness: reject anything older than 5 minutes (replay defense)
  const age = Math.abs(Date.now() / 1000 - Number(ts));
  if (!ts || !Number.isFinite(age) || age > 300) return res.status(400).end();

  // "sha256=" + HMAC_SHA256(secret, timestamp + "." + rawBody), hex digest
  const expected = 'sha256=' + crypto.createHmac('sha256', secret)
    .update(ts + '.').update(req.body).digest('hex');

  // Constant-time compare — never ===
  const a = Buffer.from(sig), b = Buffer.from(expected);
  if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) {
    return res.status(401).end();
  }

  const event = JSON.parse(req.body);  // { event: "threat.detected", event_id, ip, threat_type, details }
  // ... handle it (queue it, alert, block the IP upstream)
  res.status(200).end();  // answer 2xx fast — deliveries time out after 10 s
});
```

```
import hashlib, hmac, os, time
from flask import Flask, request, abort

app = Flask(__name__)
SECRET = os.environ["SENTINEL_WEBHOOK_SECRET"].encode()  # shown next to the URL in the dashboard

@app.post("/webhooks/sentinel")
def sentinel_webhook():
    ts  = request.headers.get("X-Maskbreak-Timestamp", "")
    sig = request.headers.get("X-Maskbreak-Signature", "")

    # Freshness: reject anything older than 5 minutes (replay defense)
    try:
        fresh = abs(time.time() - int(ts)) <= 300
    except ValueError:
        fresh = False
    if not fresh:
        abort(400)

    # "sha256=" + HMAC_SHA256(secret, timestamp + "." + raw_body), hex digest.
    # Sign request.get_data() — the exact raw bytes, not request.json.
    expected = "sha256=" + hmac.new(
        SECRET, ts.encode() + b"." + request.get_data(), hashlib.sha256
    ).hexdigest()

    # Constant-time compare — never ==
    if not hmac.compare_digest(sig, expected):
        abort(401)

    event = request.get_json(force=True)  # {"event": "threat.detected", "event_id": ..., "ip": ..., "details": ...}
    # ... handle it (queue it, alert, block the IP upstream)
    return "", 200
```

**Delivery & retries.** Maskbreak saves each threat alert before sending it. If the receiver fails, Maskbreak tries again after roughly 1 minute and again 8 minutes later. Pending attempts resume after a restart. Admins can select **Retry delivery** on the latest failed attempt within 24 hours. Replacing or removing the receiver cancels its pending alerts. The 25 most recent deliveries (event, outcome, error) are listed in the dashboard under Webhooks, retained 30 days; the header chip shows current health, and if 5 consecutive deliveries fail we email you — a dead webhook must never read as “no threats.” Deliveries time out after 10 s and never follow redirects.

**Dedupe on `event_id`.** Every payload carries an `event_id` — 32 hex chars, unique per event, present on test events too — and every delivery also carries it in the `X-Maskbreak-Event-Id` header. Retried deliveries reuse the *same* `event_id`, so key your processing on it and each event is handled exactly once no matter how many attempts reach you.

**Generic-endpoint payload.** What your receiver actually gets (Slack/Discord URLs get a formatted message instead):

```
{
  "event": "threat.detected",
  "event_id": "9f2ce7a4c1d84b7fa3d2c05e8b6f4a19",  // unique per event; same on retries
  "timestamp": "2026-07-19T09:14:03.512Z",
  "ip": "185.220.101.34",
  "threat_type": "Tor",              // or "Custom rule (…)" / "Customer exception (…)"
  "details": {
    "ip": "185.220.101.34", "cc": "DE",
    "vpn": false, "proxied": false, "tor": true, "dch": false,
    "bot": false, "tampering": false, "antidetect": false,
    "virtual_machine": false,        // since 29 Sep 2026; tampering = tampered browser or emulator
    "privacy_relay": false,          // since 10 Oct 2026; true = Apple's iCloud Private Relay (review), proxied stays true
    "decision": "block",
    "rule_matched": null,            // signals, when your rule caused the block
    "exception_matched": null,       // pins, when your exception caused it
    "email": {"score": 85, "risk": "high"},             // only when scored; never the address
    "phone": {"score": 12, "risk": "low"}               // only when scored; never the number
  }
}
```
