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

Update webhook validation #16

Merged
merged 2 commits into from
May 13, 2024
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
24 changes: 15 additions & 9 deletions Kontent.Ai.AspNetCore.Tests/SignatureMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ public async Task RequestWithoutSignature_ReturnsUnauthorized()
Assert.Equal(ctx.Response.StatusCode, (int)HttpStatusCode.Unauthorized);
}

[Fact]
public async Task RequestWithInvalidSignatureEmptyBody_ReturnsUnauthorized()
[Theory]
[InlineData("X-KC-Signature")]
[InlineData("X-Kontent-ai-Signature")]
public async Task RequestWithInvalidSignatureEmptyBody_ReturnsUnauthorized(string headerName)
{
// Arrange
var options = Options.Create(new WebhookOptions { });
var middleware = new SignatureMiddleware(null, options);
var ctx = new DefaultHttpContext();
ctx.Request.Headers.Add("X-KC-Signature", "ABC");
ctx.Request.Headers.Add(headerName, "ABC");

// Act
await middleware.InvokeAsync(ctx);
Expand All @@ -47,12 +49,16 @@ public async Task RequestWithInvalidSignatureEmptyBody_ReturnsUnauthorized()
}

[Theory]
[InlineData("ezKcSmuYrugdCN73QVWDXREaNY7AkUhFWksUFlp9Tnc=", "PublishDeliveryTriggerWebhookBody.json", "fiJ+MiJxiqmzbnlVzWnR+7Rgas7aDSzsqIApflYiZ4o=", HttpStatusCode.NotFound)]
[InlineData("8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishManagementTriggerWebhookBody.json", "khBZy02UmiUrp2bl1ooPJdILKUmmL2Q7kx318+arMhM=", HttpStatusCode.NotFound)]
[InlineData("8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishDeliveryTriggerWebhookBody.json", "tampered-body-hash", HttpStatusCode.Unauthorized)]
[InlineData("8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishManagementTriggerWebhookBody.json", "tampered-body-hash", HttpStatusCode.Unauthorized)]
[InlineData("X-KC-Signature", "ezKcSmuYrugdCN73QVWDXREaNY7AkUhFWksUFlp9Tnc=", "PublishDeliveryTriggerWebhookBody.json", "fiJ+MiJxiqmzbnlVzWnR+7Rgas7aDSzsqIApflYiZ4o=", HttpStatusCode.NotFound)]
[InlineData("X-KC-Signature", "8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishManagementTriggerWebhookBody.json", "khBZy02UmiUrp2bl1ooPJdILKUmmL2Q7kx318+arMhM=", HttpStatusCode.NotFound)]
[InlineData("X-KC-Signature", "8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishDeliveryTriggerWebhookBody.json", "tampered-body-hash", HttpStatusCode.Unauthorized)]
[InlineData("X-KC-Signature", "8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishManagementTriggerWebhookBody.json", "tampered-body-hash", HttpStatusCode.Unauthorized)]
[InlineData("X-Kontent-ai-Signature", "ezKcSmuYrugdCN73QVWDXREaNY7AkUhFWksUFlp9Tnc=", "PublishDeliveryTriggerWebhookBody.json", "fiJ+MiJxiqmzbnlVzWnR+7Rgas7aDSzsqIApflYiZ4o=", HttpStatusCode.NotFound)]
[InlineData("X-Kontent-ai-Signature", "8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishManagementTriggerWebhookBody.json", "khBZy02UmiUrp2bl1ooPJdILKUmmL2Q7kx318+arMhM=", HttpStatusCode.NotFound)]
[InlineData("X-Kontent-ai-Signature", "8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishDeliveryTriggerWebhookBody.json", "tampered-body-hash", HttpStatusCode.Unauthorized)]
[InlineData("X-Kontent-ai-Signature", "8UXIHkZ6KuarukFbZDhzyeVAHcKkFnlLabScv9VyNww=", "PublishManagementTriggerWebhookBody.json", "tampered-body-hash", HttpStatusCode.Unauthorized)]
// https://docs.microsoft.com/en-us/aspnet/core/test/middleware
public async Task TriggerRequest_WithValidSignature_CorrectStatusSet(string authorizationSecret, string bodyFilename, string signature, HttpStatusCode resultStatus)
public async Task TriggerRequest_WithValidSignature_CorrectStatusSet(string headerName, string authorizationSecret, string bodyFilename, string signature, HttpStatusCode resultStatus)
{
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder =>
Expand All @@ -77,7 +83,7 @@ public async Task TriggerRequest_WithValidSignature_CorrectStatusSet(string auth
var context = await server.SendAsync(c =>
{
c.Request.Method = HttpMethods.Post;
c.Request.Headers.Add("X-KC-Signature", signature);
c.Request.Headers.Add(headerName, signature);
c.Request.Body = new StringContent(File.ReadAllText(responsePath)).ReadAsStream();
});

Expand Down
2 changes: 1 addition & 1 deletion Kontent.Ai.AspNetCore/Webhooks/SignatureMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task InvokeAsync(HttpContext httpContext)
request.Body.Seek(0, SeekOrigin.Begin);

var generatedSignature = GenerateHash(content, WebhookOptions.Value.Secret);
var signature = request.Headers["X-KC-Signature"].FirstOrDefault();
var signature = request.Headers["X-KC-Signature"].FirstOrDefault() ?? request.Headers["X-Kontent-ai-Signature"].FirstOrDefault();

if (generatedSignature != signature)
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Package provides a model for webhook deserialization: `WebhookNotification`. Leg

### Webhook signature verification middleware

This middleware verifies the `X-KC-Signature` header. Returns 401 response if the signature is invalid.
This middleware verifies the `X-Kontent-ai-Signature` (and the legacy `X-KC-Signature`) header. Returns 401 response if the signature is invalid.

`appsettings.json`:

Expand Down
Loading