-
Notifications
You must be signed in to change notification settings - Fork 2
MT-22022: Add webhook signature verification helper #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rabsztok
wants to merge
2
commits into
main
Choose a base branch
from
MT-22022-webhook-signature-verification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
examples/Mailtrap.Example.WebhookSignature/Mailtrap.Example.WebhookSignature.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using Mailtrap.Webhooks; | ||
|
|
||
| var payload = "{\"event\":\"delivery\",\"message_id\":\"abc-123\"}"; | ||
| var signingSecret = "8d9a3c0e7f5b2d4a6c1e9f8b3a7d5c2e"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| string signature; | ||
| using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(signingSecret))) | ||
| { | ||
| signature = Convert.ToHexStringLower(hmac.ComputeHash(Encoding.UTF8.GetBytes(payload))); | ||
| } | ||
|
|
||
| if (!WebhookSignature.Verify(payload, signature, signingSecret)) | ||
| { | ||
| throw new InvalidOperationException("Signature verification failed!"); | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
examples/Mailtrap.Example.WebhookSignature/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "profiles": { | ||
| "Project": { | ||
| "commandName": "Project", | ||
| "environmentVariables": { | ||
| "DOTNET_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "System": "Warning", | ||
| "Microsoft": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
|
|
||
| namespace Mailtrap.Webhooks; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Helpers for verifying inbound Mailtrap webhook signatures. | ||
| /// </summary> | ||
| /// | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Mailtrap signs every outbound webhook by computing | ||
| /// <c>HMAC-SHA256(signing_secret, raw_request_body)</c> and sending the lowercase hex digest | ||
| /// in the <c>Mailtrap-Signature</c> HTTP header. To authenticate a webhook on the receiver | ||
| /// side, compute the same digest using the <c>signing_secret</c> returned when the webhook | ||
| /// was created and compare it to the value of the header in constant time. | ||
| /// </para> | ||
| /// <para> | ||
| /// The comparison is performed in constant time to avoid timing side-channels. | ||
| /// </para> | ||
| /// <para> | ||
| /// <see cref="Verify(string, string, string)"/> never throws on inputs that could plausibly | ||
| /// arrive over the wire (<see langword="null"/> / empty strings, wrong-length signatures, | ||
| /// non-hex characters, missing secret) — it simply returns <see langword="false"/>. This | ||
| /// makes it safe to call directly from a request handler without wrapping in try/catch. | ||
| /// </para> | ||
| /// <para> | ||
| /// See the | ||
| /// <a href="https://docs.mailtrap.io/email-api-smtp/advanced/webhooks#verifying-the-signature"> | ||
| /// Mailtrap documentation — Verifying the signature</a>. | ||
| /// </para> | ||
| /// </remarks> | ||
| public static class WebhookSignature | ||
| { | ||
| /// <summary> | ||
| /// Hex-encoded HMAC-SHA256 signature length (SHA-256 produces 32 bytes / 64 hex chars). | ||
| /// </summary> | ||
| public const int SignatureHexLength = 64; | ||
|
|
||
| /// <summary> | ||
| /// Verifies the HMAC-SHA256 signature of a Mailtrap webhook payload. | ||
| /// </summary> | ||
| /// | ||
| /// <param name="payload"> | ||
| /// The raw request body, exactly as received. <strong>Do not</strong> parse and | ||
| /// re-serialize the JSON — re-encoding may reorder keys or alter whitespace and | ||
| /// invalidate the signature. In ASP.NET Core, call | ||
| /// <c>HttpRequest.EnableBuffering()</c> and read the body via | ||
| /// <c>new StreamReader(Request.Body).ReadToEndAsync()</c>, or bind directly to a | ||
| /// <c>byte[]</c> / <see cref="System.IO.Stream"/> on the webhook endpoint so the | ||
| /// body is preserved verbatim. | ||
| /// </param> | ||
| /// <param name="signature"> | ||
| /// The value of the <c>Mailtrap-Signature</c> HTTP header (lowercase hex string). | ||
| /// </param> | ||
| /// <param name="signingSecret"> | ||
| /// The webhook's <c>signing_secret</c>, returned by the Webhooks API on webhook creation. | ||
| /// </param> | ||
| /// | ||
| /// <returns> | ||
| /// <see langword="true"/> if <paramref name="signature"/> is valid for the given | ||
| /// <paramref name="payload"/> and <paramref name="signingSecret"/>; <see langword="false"/> | ||
| /// otherwise (including any <see langword="null"/> / empty input, wrong-length or | ||
| /// non-hex signatures). | ||
| /// </returns> | ||
| public static bool Verify(string payload, string signature, string signingSecret) | ||
| { | ||
| if (string.IsNullOrEmpty(signature)) | ||
| { | ||
| return false; | ||
| } | ||
| if (string.IsNullOrEmpty(signingSecret)) | ||
| { | ||
| return false; | ||
| } | ||
| if (string.IsNullOrEmpty(payload)) | ||
| { | ||
| return false; | ||
| } | ||
| if (signature.Length != SignatureHexLength) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!TryParseHex(signature, out var providedBytes)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| byte[] expectedBytes; | ||
| using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(signingSecret))) | ||
| { | ||
| expectedBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload)); | ||
| } | ||
|
|
||
| // Defensive: the hex-length check above already pins providedBytes to 32 bytes | ||
| // (= expectedBytes.Length), but reassert before the constant-time compare so a | ||
| // future change to SignatureHexLength can't accidentally introduce a timing leak. | ||
| if (expectedBytes.Length != providedBytes.Length) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return FixedTimeEquals(expectedBytes, providedBytes); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parses a lowercase/uppercase hex string into bytes without throwing on invalid input. | ||
| /// </summary> | ||
| private static bool TryParseHex(string hex, out byte[] bytes) | ||
| { | ||
| if ((hex.Length & 1) != 0) | ||
| { | ||
| bytes = []; | ||
| return false; | ||
| } | ||
|
|
||
| var result = new byte[hex.Length / 2]; | ||
| for (var i = 0; i < result.Length; i++) | ||
| { | ||
| if (!TryParseHexDigit(hex[(i * 2) + 0], out var high) || | ||
| !TryParseHexDigit(hex[(i * 2) + 1], out var low)) | ||
| { | ||
| bytes = []; | ||
| return false; | ||
| } | ||
|
|
||
| result[i] = (byte)((high << 4) | low); | ||
| } | ||
|
|
||
| bytes = result; | ||
| return true; | ||
| } | ||
|
|
||
| private static bool TryParseHexDigit(char c, out int value) | ||
| { | ||
| if (c is >= '0' and <= '9') | ||
| { | ||
| value = c - '0'; | ||
| return true; | ||
| } | ||
| if (c is >= 'a' and <= 'f') | ||
| { | ||
| value = c - 'a' + 10; | ||
| return true; | ||
| } | ||
| if (c is >= 'A' and <= 'F') | ||
| { | ||
| value = c - 'A' + 10; | ||
| return true; | ||
| } | ||
|
|
||
| value = 0; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Constant-time byte-array equality. Equivalent to | ||
| /// <c>CryptographicOperations.FixedTimeEquals</c>, which is unavailable on | ||
| /// <c>netstandard2.0</c>. | ||
| /// </summary> | ||
| private static bool FixedTimeEquals(byte[] left, byte[] right) | ||
| { | ||
| // Lengths are checked up-front by the caller; equal-length guarantees a constant | ||
| // number of loop iterations and avoids leaking length information via timing. | ||
| var diff = 0; | ||
| for (var i = 0; i < left.Length; i++) | ||
| { | ||
| diff |= left[i] ^ right[i]; | ||
| } | ||
|
|
||
| return diff == 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.