Commit 9e92160e authored by Savorboard's avatar Savorboard

Add subscription name validation for the AzureServerBus. (#344)

parent 93ab8c6f
...@@ -52,6 +52,8 @@ namespace DotNetCore.CAP.AzureServiceBus ...@@ -52,6 +52,8 @@ namespace DotNetCore.CAP.AzureServiceBus
foreach (var newRule in topics.Except(allRuleNames)) foreach (var newRule in topics.Except(allRuleNames))
{ {
CheckValidSubscriptionName(newRule);
_consumerClient.AddRuleAsync(new RuleDescription _consumerClient.AddRuleAsync(new RuleDescription
{ {
Filter = new CorrelationFilter { Label = newRule }, Filter = new CorrelationFilter { Label = newRule },
...@@ -99,7 +101,7 @@ namespace DotNetCore.CAP.AzureServiceBus ...@@ -99,7 +101,7 @@ namespace DotNetCore.CAP.AzureServiceBus
public void Dispose() public void Dispose()
{ {
_consumerClient.CloseAsync().Wait(); _consumerClient?.CloseAsync().Wait(1500);
} }
#region private methods #region private methods
...@@ -168,6 +170,45 @@ namespace DotNetCore.CAP.AzureServiceBus ...@@ -168,6 +170,45 @@ namespace DotNetCore.CAP.AzureServiceBus
return Task.CompletedTask; return Task.CompletedTask;
} }
private static void CheckValidSubscriptionName(string subscriptionName)
{
const string pathDelimiter = @"/";
const int ruleNameMaximumLength = 50;
char[] invalidEntityPathCharacters = { '@', '?', '#', '*' };
if (string.IsNullOrWhiteSpace(subscriptionName))
{
throw new ArgumentNullException(subscriptionName);
}
// and "\" will be converted to "/" on the REST path anyway. Gateway/REST do not
// have to worry about the begin/end slash problem, so this is purely a client side check.
var tmpName = subscriptionName.Replace(@"\", pathDelimiter);
if (tmpName.Length > ruleNameMaximumLength)
{
throw new ArgumentOutOfRangeException(subscriptionName, $@"Subscribe name '{subscriptionName}' exceeds the '{ruleNameMaximumLength}' character limit.");
}
if (tmpName.StartsWith(pathDelimiter, StringComparison.OrdinalIgnoreCase) ||
tmpName.EndsWith(pathDelimiter, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException($@"The subscribe name cannot contain '/' as prefix or suffix. The supplied value is '{subscriptionName}'", subscriptionName);
}
if (tmpName.Contains(pathDelimiter))
{
throw new ArgumentException($@"The subscribe name contains an invalid character '{pathDelimiter}'", subscriptionName);
}
foreach (var uriSchemeKey in invalidEntityPathCharacters)
{
if (subscriptionName.IndexOf(uriSchemeKey) >= 0)
{
throw new ArgumentException($@"'{subscriptionName}' contains character '{uriSchemeKey}' which is not allowed because it is reserved in the Uri scheme.", subscriptionName);
}
}
}
#endregion private methods #endregion private methods
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment