@@ -45,13 +45,16 @@ public bool Validate(string url, NameValueCollection parameters, string expected
45
45
/// <returns>true if the signature matches the result; false otherwise</returns>
46
46
public bool Validate ( string url , IDictionary < string , string > parameters , string expected )
47
47
{
48
- var signature = GetValidationSignature ( url , parameters ) ;
49
- return SecureCompare ( signature , expected ) ;
48
+ // check signature of url with and without port, since sig generation on back end is inconsistent
49
+ var signatureWithoutPort = GetValidationSignature ( RemovePort ( new UriBuilder ( url ) ) , parameters ) ;
50
+ var signatureWithPort = GetValidationSignature ( AddPort ( new UriBuilder ( url ) ) , parameters ) ;
51
+ // If either url produces a valid signature, we accept the request as valid
52
+ return SecureCompare ( signatureWithoutPort , expected ) || SecureCompare ( signatureWithPort , expected ) ;
50
53
}
51
54
52
55
public bool Validate ( string url , string body , string expected )
53
56
{
54
- var paramString = new Uri ( url ) . Query . TrimStart ( '?' ) ;
57
+ var paramString = new UriBuilder ( url ) . Query . TrimStart ( '?' ) ;
55
58
var bodyHash = "" ;
56
59
foreach ( var param in paramString . Split ( '&' ) )
57
60
{
@@ -121,5 +124,23 @@ private static bool SecureCompare(string a, string b)
121
124
return mismatch == 0 ;
122
125
}
123
126
127
+ private string RemovePort ( UriBuilder uri )
128
+ {
129
+ // UriBuilder.ToString() will not display the port
130
+ // if the Port property is set to -1
131
+ uri . Port = - 1 ;
132
+ return uri . ToString ( ) ;
133
+ }
134
+
135
+ private string AddPort ( UriBuilder uri )
136
+ {
137
+ if ( uri . Port != - 1 )
138
+ {
139
+ return uri . ToString ( ) ;
140
+ }
141
+ uri . Port = uri . Scheme == "https" ? 443 : 80 ;
142
+ return uri . ToString ( ) ;
143
+ }
144
+
124
145
}
125
146
}
0 commit comments