Skip to content

Commit 1567075

Browse files
author
Henrik Frystyk Nielsen
committed
Simplified webhook secret validation in WebHookManager.ValidateWebHookAsync
1 parent 690b54e commit 1567075

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/Microsoft.AspNet.WebHooks.Custom/WebHooks/WebHookManager.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.ComponentModel.DataAnnotations;
76
using System.Globalization;
87
using System.Linq;
98
using System.Net.Http;
@@ -73,9 +72,11 @@ public async Task VerifyWebHookAsync(WebHook webHook)
7372
throw new ArgumentNullException("webHook");
7473
}
7574

76-
// Validate data annotations explicitly as the WebHook may not have gone through deserialization
77-
ValidationContext context = new ValidationContext(webHook);
78-
Validator.ValidateObject(webHook, context, validateAllProperties: true);
75+
// Check that we have a valid secret
76+
if (string.IsNullOrEmpty(webHook.Secret) || webHook.Secret.Length < 32 || webHook.Secret.Length > 64)
77+
{
78+
throw new InvalidOperationException(CustomResources.WebHook_InvalidSecret);
79+
}
7980

8081
// Check that WebHook URI is either 'http' or 'https'
8182
if (!(webHook.WebHookUri.IsHttp() || webHook.WebHookUri.IsHttps()))

test/Microsoft.AspNet.WebHooks.Custom.Test/WebHooks/WebHookManagerTests.cs

+28-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
77
using System.Collections.Specialized;
8-
using System.ComponentModel.DataAnnotations;
98
using System.Globalization;
109
using System.Linq;
1110
using System.Net;
@@ -44,20 +43,31 @@ public WebHookManagerTests()
4443
_response = new HttpResponseMessage();
4544
}
4645

47-
public static TheoryData<WebHook, string> WebHookValidationData
46+
public static TheoryData<string> WebHookSecretData
4847
{
4948
get
5049
{
51-
Uri webHookUri = new Uri("http://localhost");
52-
string secret = new string('a', 32);
53-
return new TheoryData<WebHook, string>
50+
return new TheoryData<string>
5451
{
55-
{ new WebHook(), "The WebHookUri field is required." },
56-
{ new WebHook { WebHookUri = null, Secret = secret }, "The WebHookUri field is required." },
57-
{ new WebHook { WebHookUri = webHookUri, Secret = null }, "The Secret field is required." },
58-
{ new WebHook { WebHookUri = webHookUri, Secret = string.Empty }, "The Secret field is required." },
59-
{ new WebHook { WebHookUri = webHookUri, Secret = "a" }, "The WebHook secret key parameter must be between 32 and 64 characters long." },
60-
{ new WebHook { WebHookUri = webHookUri, Secret = new string('a', 65) }, "The WebHook secret key parameter must be between 32 and 64 characters long." },
52+
null,
53+
string.Empty,
54+
new string('a', 31),
55+
new string('a', 65),
56+
};
57+
}
58+
}
59+
60+
public static TheoryData<string> WebHookUriData
61+
{
62+
get
63+
{
64+
return new TheoryData<string>
65+
{
66+
null,
67+
"ftp://localhost",
68+
"telnet://localhost",
69+
"htp://localhost",
70+
"invalid://localhost",
6171
};
6272
}
6373
}
@@ -104,24 +114,23 @@ public static TheoryData<IEnumerable<WebHook>, IEnumerable<NotificationDictionar
104114
}
105115

106116
[Theory]
107-
[MemberData("WebHookValidationData")]
108-
public async Task VerifyWebHookAsync_Throws_IfInvalidWebHook(WebHook webHook, string expected)
117+
[MemberData("WebHookSecretData")]
118+
public async Task VerifyWebHookAsync_Throws_IfInvalidWebHookSecret(string secret)
109119
{
110120
// Arrange
111121
_manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
122+
WebHook webHook = CreateWebHook();
123+
webHook.Secret = secret;
112124

113125
// Act
114-
ValidationException ex = await Assert.ThrowsAsync<ValidationException>(() => _manager.VerifyWebHookAsync(webHook));
126+
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));
115127

116128
// Assert
117-
Assert.Equal(expected, ex.Message);
129+
Assert.Equal("The WebHook secret key parameter must be between 32 and 64 characters long.", ex.Message);
118130
}
119131

120132
[Theory]
121-
[InlineData("ftp://localhost")]
122-
[InlineData("telnet://localhost")]
123-
[InlineData("htp://localhost")]
124-
[InlineData("invalid://localhost")]
133+
[MemberData("WebHookUriData")]
125134
public async Task VerifyWebHookAsync_Throws_IfNotHttpOrHttpsUri(string webHookUri)
126135
{
127136
// Arrange

0 commit comments

Comments
 (0)