Skip to content

MT-22022: Add webhook signature verification helper#242

Open
Rabsztok wants to merge 2 commits into
mainfrom
MT-22022-webhook-signature-verification
Open

MT-22022: Add webhook signature verification helper#242
Rabsztok wants to merge 2 commits into
mainfrom
MT-22022-webhook-signature-verification

Conversation

@Rabsztok
Copy link
Copy Markdown

@Rabsztok Rabsztok commented May 20, 2026

Motivation

Expose a helper so .NET users don't have to re-implement Mailtrap's HMAC-SHA256 webhook signature check on every receiver.

Changes

  • Mailtrap.Webhooks.WebhookSignature.Verify(string payload, string signature, string signingSecret): bool in Mailtrap.Abstractions (netstandard2.0). HMAC-SHA256 over the raw body, hand-rolled constant-time byte-array compare (since CryptographicOperations.FixedTimeEquals is unavailable on netstandard2.0), hand-rolled TryParseHex so non-hex inputs return false rather than throw. Returns false (never throws) on null / empty / wrong-length / non-hex inputs.
  • tests/Mailtrap.UnitTests/Webhooks/WebhookSignatureTests.cs pins the cross-SDK fixture (payload + signing_secret + expected digest) shared verbatim across all six official Mailtrap SDKs to guarantee byte-for-byte parity.
  • examples/Mailtrap.Example.WebhookSignature/ — new dedicated console project with a runnable usage snippet, added to Mailtrap.sln.
  • README — new "Verifying webhook signatures" subsection.

How to test

CI runs dotnet build and the full unit test suite. Manually:

dotnet run --project examples/Mailtrap.Example.WebhookSignature

The example should exit 0 with no output.

Companion PRs

Coordinated rollout across all six official SDKs (same algorithm, same shared fixture):

@Rabsztok Rabsztok requested a review from mklocek as a code owner May 20, 2026 07:33
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

Warning

Rate limit exceeded

@Rabsztok has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 58 minutes and 55 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e37745a5-1d1b-46e1-a59f-47bdb69e560c

📥 Commits

Reviewing files that changed from the base of the PR and between 6869c49 and 984e6ec.

📒 Files selected for processing (9)
  • Mailtrap.sln
  • README.md
  • examples/Mailtrap.Example.WebhookSignature/Mailtrap.Example.WebhookSignature.csproj
  • examples/Mailtrap.Example.WebhookSignature/Program.cs
  • examples/Mailtrap.Example.WebhookSignature/Properties/launchSettings.json
  • examples/Mailtrap.Example.WebhookSignature/appsettings.json
  • src/Mailtrap.Abstractions/Webhooks/WebhookSignature.cs
  • tests/Mailtrap.UnitTests/GlobalUsings.cs
  • tests/Mailtrap.UnitTests/Webhooks/WebhookSignatureTests.cs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch MT-22022-webhook-signature-verification

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Add `Mailtrap.Webhooks.WebhookSignature.Verify(string payload, string
signature, string signingSecret)` for verifying Mailtrap webhook signatures
using HMAC-SHA256 over the raw request body with a constant-time
byte-array comparison.

Returns false (no throw) for null/empty/malformed/wrong-length signatures
and non-hex characters, so a single guard at the request handler covers
every bad-input case. The helper ships in `Mailtrap.Abstractions`
(netstandard2.0) and uses its own hex parser and constant-time compare
since `CryptographicOperations.FixedTimeEquals` is not available there.

Includes the shared cross-SDK test fixture (payload + secret + expected
signature) that all six Mailtrap SDKs use to stay byte-for-byte
compatible, plus an ASP.NET Core receiver example and README subsection.

See https://railsware.atlassian.net/browse/MT-22022
@Rabsztok
Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Rabsztok
Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Comment thread README.md

Each example includes detailed comments and demonstrates best practices for error handling, configuration, and resource management.

### Verifying webhook signatures
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need it in this general README. We kept code samples here to the core functionality which is sending mail.


// --- Direct verification (e.g. for unit tests or custom routers) ----------
var payload = "{\"event\":\"delivery\",\"message_id\":\"abc-123\"}";
var signingSecret = "8d9a3c0e7f5b2d4a6c1e9f8b3a7d5c2e";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave the secret and actual signature for users to fill in. Same as we do in other examples with object IDs

@Rabsztok
Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Rabsztok Rabsztok requested a review from IgorDobryn May 21, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants