MT-22022: Add webhook signature verification helper#110
Conversation
📝 WalkthroughWalkthroughThis PR adds webhook signature verification to the Mailtrap Ruby gem. A new ChangesWebhook Signature Verification
Sequence DiagramsequenceDiagram
participant Client as Webhook Client
participant Receiver as WebhookReceiver
participant Verify as Mailtrap::Webhooks
participant OpenSSL as OpenSSL::HMAC
participant Compare as fixed_length_secure_compare
Client->>Receiver: POST raw_body + Mailtrap-Signature header
Receiver->>Verify: verify_signature(raw_body, signature, secret)
Verify->>OpenSSL: hexdigest(secret, raw_body)
OpenSSL-->>Verify: expected_hex
Verify->>Compare: fixed_length_secure_compare(expected, signature)
alt Signature matches
Compare-->>Verify: true
Verify-->>Receiver: true
Receiver->>Receiver: parse JSON payload
Receiver-->>Client: 200 OK
else Signature invalid
Compare-->>Verify: false or ArgumentError
Verify-->>Receiver: false
Receiver-->>Client: 401 Unauthorized
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/webhooks_signature_verification.rb`:
- Around line 1-2: Add explicit requires for Rack and JSON at the top of the
example: include require 'rack' and require 'json' alongside the existing
require 'mailtrap' so that Rack::Request and JSON.parse are defined when running
the standalone script (this affects the usage sites of Rack::Request and
JSON.parse in the file).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f503201f-688f-4346-89a4-536e35b0c132
📒 Files selected for processing (5)
README.mdexamples/webhooks_signature_verification.rblib/mailtrap.rblib/mailtrap/webhooks.rbspec/mailtrap/webhooks_spec.rb
2c0513e to
f43cb34
Compare
Motivation
Follow-up to mailtrap-ruby#108 review comment: expose a helper so users don't have to re-implement Mailtrap's HMAC-SHA256 webhook signature check on every receiver.
Changes
Mailtrap::Webhooks.verify_signature(payload:, signature:, signing_secret:)→true/false. HMAC-SHA256 over the raw body, constant-time compare viaOpenSSL.fixed_length_secure_compare. Returnsfalse(never raises) on empty / wrong-length / non-hex / non-string inputs.spec/mailtrap/webhooks_spec.rbpins a cross-SDK fixture (payload + signing_secret + expected digest) shared verbatim across all six official Mailtrap SDKs to guarantee byte-for-byte parity.examples/webhooks_signature_verification.rb— runnable usage snippet.How to test
CI runs the full spec suite and lint. Manually:
bundle exec ruby examples/webhooks_signature_verification.rbThe script should exit 0 with no output.
Companion PRs
Coordinated rollout across all six official SDKs (same algorithm, same shared fixture):