🎥 Make videos programmatically with React
feat(lambda-ruby): add validate_webhook_signature()
DeryFerd0f6c8712b280
diff --git a/packages/docs/docs/lambda/ruby.mdx b/packages/docs/docs/lambda/ruby.mdxindex 2bbeb683c7..8896faa2aa 100644--- a/packages/docs/docs/lambda/ruby.mdx+++ b/packages/docs/docs/lambda/ruby.mdx@@ -93,6 +93,34 @@ res = client.render_media_on_lambda(function_name, payload)client.cancel_render_on_lambda(res["bucketName"], res["renderId"])```+## Verifying webhooks<AvailableFrom v="4.0.537" />++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 Ruby client ships `validate_webhook_signature()`, 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 raises if the request cannot be verified and returns `nil` if it is authentic.++```ruby title="webhook.rb"+require 'sinatra'+require 'json'+require 'remotion_lambda'++post '/webhook' do+ body = request.body.read++ RemotionLambda.validate_webhook_signature(+ ENV.fetch('REMOTION_WEBHOOK_SECRET'),+ body,+ request.env['HTTP_X_REMOTION_SIGNATURE']+ )++ # The request is authentic. Parse the body only now.+ payload = JSON.parse(body)+ status 200+end+```++Read the body first and verify before parsing it. The signature covers the exact bytes that were sent, so parsing the JSON and regenerating it can change whitespace or number formatting, which would make a genuine request fail the check. Pass the body as it arrived (`request.body.read` in Sinatra, `request.raw_post` in Rails) rather than a parsed hash.+## Rendering a still```ruby title="Rendering a still from Ruby"diff --git a/packages/lambda-ruby/lib/remotion_lambda.rb b/packages/lambda-ruby/lib/remotion_lambda.rbindex 537b8f564c..7bc8f3238f 100644--- a/packages/lambda-ruby/lib/remotion_lambda.rb+++ b/packages/lambda-ruby/lib/remotion_lambda.rb@@ -7,6 +7,7 @@ require_relative 'remotion_lambda/render_frames_on_lambda_payload'require_relative 'remotion_lambda/render_progress_payload'require_relative 'remotion_lambda/render_still_on_lambda_payload'require_relative 'remotion_lambda/s3_output_provider'+require_relative 'remotion_lambda/webhook'require_relative 'remotion_lambda/sdk'module RemotionLambdadiff --git a/packages/lambda-ruby/lib/remotion_lambda/webhook.rb b/packages/lambda-ruby/lib/remotion_lambda/webhook.rbnew file mode 100644index 0000000000..a079b8812c--- /dev/null+++ b/packages/lambda-ruby/lib/remotion_lambda/webhook.rb@@ -0,0 +1,50 @@+require 'openssl'++module RemotionLambda+ # Validates 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. Raises if the request cannot be verified and+ # returns nil if it is authentic.+ #+ # The signature covers the raw JSON bytes, so pass the body as it arrived+ # (request.body.read in Sinatra, request.raw_post in Rails) and parse it only+ # after this method returns. Parsing and regenerating the JSON first can change+ # whitespace and how numbers are formatted, 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.+ def self.validate_webhook_signature(secret, body, signature_header)+ if secret.nil? || secret.empty?+ raise ArgumentError, "No 'secret' was provided to validate_webhook_signature()."+ end++ if body.nil? || body.empty?+ raise ArgumentError, "No 'body' was provided to validate_webhook_signature()."+ end++ if signature_header.nil? || signature_header.empty? ||+ signature_header == 'NO_SECRET_PROVIDED'+ raise 'No webhook signature was provided'+ end++ expected = "sha512=#{OpenSSL::HMAC.hexdigest('SHA512', secret, body)}"++ # The header is attacker-controlled, so force both sides to binary before+ # comparing: fixed_length_secure_compare() compares bytes and must fail the+ # check instead of raising when the header has an unexpected encoding.+ signature = signature_header.b++ matches =+ expected.bytesize == signature.bytesize &&+ OpenSSL.fixed_length_secure_compare(expected.b, signature)++ raise 'Signatures do not match' unless matches++ nil+ end+end