Skip to content

🎥 Make videos programmatically with React

feat(lambda-php): add WebhookSignature::validate()

DeryFerdbcc653e299b9
diff --git a/packages/docs/docs/lambda/php.mdx b/packages/docs/docs/lambda/php.mdx
index 65bbe94a1e..fc434dd1a4 100644
--- a/packages/docs/docs/lambda/php.mdx
+++ b/packages/docs/docs/lambda/php.mdx
@@ -124,6 +124,32 @@ $client->cancelRenderOnLambda(
);
```
+## Verifying webhooks<AvailableFrom v="4.0.538" />
+
+If you [set up a webhook](/docs/lambda/webhooks) with a secret, Remotion signs every request so you can confirm it was sent by Remotion and that the body was not modified. The PHP client ships `WebhookSignature::validate()`, the equivalent of [`validateWebhookSignature()`](/docs/lambda/validatewebhooksignature) in the JavaScript client.
+
+Pass the webhook secret, the raw request body exactly as it was received, and the `X-Remotion-Signature` header. It throws if the request cannot be verified and returns nothing if it is authentic.
+
+```php title="webhook.php"
+use Remotion\LambdaPhp\WebhookSignature;
+
+$secret = getenv('REMOTION_WEBHOOK_SECRET');
+$body = file_get_contents('php://input');
+
+WebhookSignature::validate(
+ $secret,
+ $body,
+ $_SERVER['HTTP_X_REMOTION_SIGNATURE'] ?? null
+);
+
+// The request is authentic. Decode the body only now.
+$payload = json_decode($body, true);
+
+http_response_code(200);
+```
+
+Read the body first and verify before decoding it. The signature covers the exact bytes that were sent, so decoding and re-encoding the body can change whitespace and how numbers are serialized, which would make a genuine request fail the check. Pass the body as it arrived (`file_get_contents('php://input')`) rather than a decoded array.
+
## Changelog
- `v4.0.15`: The fields are now typed. `->setInputProps()` now works as intended, serializing the input props to JSON for you.
diff --git a/packages/lambda-php/src/WebhookSignature.php b/packages/lambda-php/src/WebhookSignature.php
new file mode 100644
index 0000000000..98ff5e8d43
--- /dev/null
+++ b/packages/lambda-php/src/WebhookSignature.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Remotion\LambdaPhp;
+
+use Exception;
+use InvalidArgumentException;
+
+class WebhookSignature
+{
+ /**
+ * Validate that an incoming webhook request was sent by Remotion and that
+ * its body was not modified in transit.
+ *
+ * Pass the webhook secret that was configured when the render was started,
+ * the raw request body exactly as it was received, and the value of the
+ * X-Remotion-Signature header. Throws if the request cannot be verified.
+ *
+ * The signature covers the raw JSON bytes, so pass the body as it arrived
+ * (file_get_contents('php://input')) and decode it only after this method
+ * returns. Decoding and re-encoding the body first can change whitespace and
+ * how numbers are serialized, which would break the comparison even for a
+ * genuine request.
+ *
+ * This mirrors validateWebhookSignature() from @remotion/lambda/client and
+ * uses the same scheme: "sha512=" followed by the hex digest of an
+ * HMAC-SHA512 over the request body, keyed with the webhook secret.
+ */
+ public static function validate(string $secret, string $body, ?string $signatureHeader): void
+ {
+ if ($secret === '') {
+ throw new InvalidArgumentException("No 'secret' was provided to WebhookSignature::validate().");
+ }
+
+ if ($body === '') {
+ throw new InvalidArgumentException("No 'body' was provided to WebhookSignature::validate().");
+ }
+
+ if ($signatureHeader === null || $signatureHeader === '' || $signatureHeader === 'NO_SECRET_PROVIDED') {
+ throw new Exception('No webhook signature was provided');
+ }
+
+ $expected = 'sha512=' . hash_hmac('sha512', $body, $secret);
+
+ // hash_equals() is constant time and returns false when the lengths
+ // differ, so an attacker-controlled header cannot leak information
+ // through the comparison.
+ if (!hash_equals($expected, $signatureHeader)) {
+ throw new Exception('Signatures do not match');
+ }
+ }
+}