Skip to content

🎥 Make videos programmatically with React

feat(lambda-go): add ValidateWebhookSignature()

DeryFerd1549554dfad6
diff --git a/packages/docs/docs/lambda/go.mdx b/packages/docs/docs/lambda/go.mdx
index c6914aac00..3d14f94620 100644
--- a/packages/docs/docs/lambda/go.mdx
+++ b/packages/docs/docs/lambda/go.mdx
@@ -166,6 +166,43 @@ if err != nil {
}
```
+## Verifying webhooks<AvailableFrom v="4.0.536" />
+
+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 Go client ships `ValidateWebhookSignature()`, 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 returns an error if the request cannot be verified and `nil` if it is authentic.
+
+```go title="webhook.go"
+func webhookHandler(w http.ResponseWriter, r *http.Request) {
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "could not read body", http.StatusBadRequest)
+ return
+ }
+
+ err = lambda_go_sdk.ValidateWebhookSignature(
+ os.Getenv("REMOTION_WEBHOOK_SECRET"),
+ body,
+ r.Header.Get("X-Remotion-Signature"),
+ )
+ if err != nil {
+ http.Error(w, "invalid signature", http.StatusUnauthorized)
+ return
+ }
+
+ // The request is authentic. Parse the body only now.
+ var payload map[string]interface{}
+ if err := json.Unmarshal(body, &payload); err != nil {
+ http.Error(w, "could not parse payload", http.StatusBadRequest)
+ return
+ }
+
+ w.WriteHeader(http.StatusOK)
+}
+```
+
+Read the body first and verify before decoding it. The signature covers the exact bytes that were sent, so unmarshalling the JSON and re-marshalling it can reorder keys and change whitespace, which would make a genuine request fail the check.
+
## Changelog
`v4.0.6`: The response payload structure has changed. See the history of this page to see the previous structure.
diff --git a/packages/lambda-go/webhook.go b/packages/lambda-go/webhook.go
new file mode 100644
index 0000000000..ba3e0b5ec4
--- /dev/null
+++ b/packages/lambda-go/webhook.go
@@ -0,0 +1,47 @@
+package lambda_go_sdk
+
+import (
+ "crypto/hmac"
+ "crypto/sha512"
+ "encoding/hex"
+ "errors"
+)
+
+// ValidateWebhookSignature verifies 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. The function returns an error if the request
+// cannot be verified and nil if it is authentic.
+//
+// The signature covers the raw JSON bytes, so decode the body only after this
+// function returns. Decoding and re-encoding first can change key order and
+// whitespace, 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.
+func ValidateWebhookSignature(secret string, body []byte, signatureHeader string) error {
+ if secret == "" {
+ return errors.New("no webhook secret was provided to ValidateWebhookSignature()")
+ }
+
+ if len(body) == 0 {
+ return errors.New("no webhook body was provided to ValidateWebhookSignature()")
+ }
+
+ if signatureHeader == "" || signatureHeader == "NO_SECRET_PROVIDED" {
+ return errors.New("no webhook signature was provided")
+ }
+
+ mac := hmac.New(sha512.New, []byte(secret))
+ mac.Write(body)
+ expected := "sha512=" + hex.EncodeToString(mac.Sum(nil))
+
+ if !hmac.Equal([]byte(expected), []byte(signatureHeader)) {
+ return errors.New("webhook signature does not match")
+ }
+
+ return nil
+}