Skip to main content

Webhook Security

To ensure a secure integration of webhooks with your custom implementation or the third-party provider, we propose the following safety measures.

Only allow HTTPS requests

freispace will only send webhook requests over HTTPS and check that endpoints have a valid SSL certificate.

Ensure to block any non-secure HTTP requests on the receiving end.

Ensure validity using HMAC

All requests are signed with HMAC using the sha256 algorithm. Check any incoming webhook's validity using the pre-shared key.

<?php
// The following example uses PHP to verify a webhook request:

// The freispace webhook secret, viewable from the Webhook details page. In a production environment, set the secret as an environment variable to prevent exposing it in code.
define('FREISPACE_SECRET', 'webhook_secret_key');

// Get the raw POST data
$data = file_get_contents('php://input');

// Get the HMAC signature from the request header
$signature_header = $_SERVER['HTTP_X_FREISPACE_SIGNATURE'];

// Return if the signature header is missing
if (!$signature_header) {
http_response_code(400);
echo json_encode(['message' => 'Missing signature']);
exit();
}

// Compute the HMAC signature of the request payload
$computed_signature = hash_hmac('sha256', $data, FREISPACE_SECRET);

// Verify the computed signature with the signature in the request header
if (!hash_equals($computed_signature, $signature_header)) {
http_response_code(403);
echo json_encode(['message' => 'Invalid signature']);
exit();
}

http_response_code(200);
echo json_encode(['message' => 'Webhook processed successfully']);

// Process the webhook payload
$payload = json_decode($data, true);
?>