Skip to content
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

csharp: allow verification of webhook signature without the need of a WebHeaderCollection #1617

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions csharp/Svix.Tests/WebhookTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@ public void TestNewTimestampThrowsException()

Assert.Throws<WebhookVerificationException>(() => wh.Verify(testPayload.payload, testPayload.headers));
}

[Fact]
public void TestWebHeaderCollectionNullThrowsException()
{
var testPayload = new TestPayload(DateTimeOffset.UtcNow);

var wh = new Webhook(testPayload.secret);

Assert.Throws<ArgumentNullException>(() => wh.Verify(testPayload.payload, (WebHeaderCollection)null));
}

[Fact]
public void TestWebHeadersProviderNullThrowsException()
{
var testPayload = new TestPayload(DateTimeOffset.UtcNow);

var wh = new Webhook(testPayload.secret);

Assert.Throws<ArgumentNullException>(() => wh.Verify(testPayload.payload, (Func<string, string>)null));
}

[Fact]
public void TestPayloadNullThrowsException()
{
var testPayload = new TestPayload(DateTimeOffset.UtcNow);

var wh = new Webhook(testPayload.secret);

Assert.Throws<ArgumentNullException>(() => wh.Verify(null, testPayload.headers));
}

[Fact]
public void TestMultiSigPayloadIsValid()
Expand All @@ -153,7 +183,7 @@ public void TestMultiSigPayloadIsValid()
}

[Fact]
public void TestSivnatureVerificationWorksWithoutPrefix()
public void TestSignatureVerificationWorksWithoutPrefix()
{
var testPayload = new TestPayload(DateTimeOffset.UtcNow);

Expand All @@ -165,7 +195,7 @@ public void TestSivnatureVerificationWorksWithoutPrefix()
}

[Fact]
public void verifyWebhookSignWorks()
public void VerifyWebhookSignWorks()
{
var key = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw";
var msgId = "msg_p5jXN8AQM9LWM0D4loKWxJek";
Expand Down
22 changes: 16 additions & 6 deletions csharp/Svix/Webhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,25 @@ public Webhook(byte[] key)

public void Verify(string payload, WebHeaderCollection headers)
{
string msgId = headers.Get(SVIX_ID_HEADER_KEY);
string msgSignature = headers.Get(SVIX_SIGNATURE_HEADER_KEY);
string msgTimestamp = headers.Get(SVIX_TIMESTAMP_HEADER_KEY);
ArgumentNullException.ThrowIfNull(headers);

Verify(payload, headers.Get);
}

public void Verify(string payload, Func<string, string> headersProvider)
{
ArgumentNullException.ThrowIfNull(payload);
ArgumentNullException.ThrowIfNull(headersProvider);

string msgId = headersProvider(SVIX_ID_HEADER_KEY);
string msgSignature = headersProvider(SVIX_SIGNATURE_HEADER_KEY);
string msgTimestamp = headersProvider(SVIX_TIMESTAMP_HEADER_KEY);

if (String.IsNullOrEmpty(msgId) || String.IsNullOrEmpty(msgSignature) || String.IsNullOrEmpty(msgTimestamp))
{
msgId = headers.Get(UNBRANDED_ID_HEADER_KEY);
msgSignature = headers.Get(UNBRANDED_SIGNATURE_HEADER_KEY);
msgTimestamp = headers.Get(UNBRANDED_TIMESTAMP_HEADER_KEY);
msgId = headersProvider(UNBRANDED_ID_HEADER_KEY);
msgSignature = headersProvider(UNBRANDED_SIGNATURE_HEADER_KEY);
msgTimestamp = headersProvider(UNBRANDED_TIMESTAMP_HEADER_KEY);
if (String.IsNullOrEmpty(msgId) || String.IsNullOrEmpty(msgSignature) || String.IsNullOrEmpty(msgTimestamp))
{
throw new WebhookVerificationException("Missing Required Headers");
Expand Down
Loading