Skip to content

Commit 9e66f21

Browse files
authored
Merge pull request #59 from bunq/feature/make_sure_headers_are_correctly_cased_bunq/sdk_csharp#49
Insure that headers are correctly cased before signature verification…
2 parents 118bed4 + 2944f3f commit 9e66f21

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

BunqSdk/Security/SecurityUtils.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Security;
77
using System.Security.Cryptography;
88
using System.Text;
9+
using System.Text.RegularExpressions;
910
using Bunq.Sdk.Context;
1011
using Bunq.Sdk.Exception;
1112
using Bunq.Sdk.Http;
@@ -78,6 +79,16 @@ public class SecurityUtils
7879
/// </summary>
7980
private const int INDEX_FIRST = 0;
8081

82+
/// <summary>
83+
/// The index after the firts character in a string.
84+
/// </summary>
85+
private const int INDEX_LAST_FIRST_CHAR = 1;
86+
87+
/// <summary>
88+
/// Regex constants.
89+
/// </summary>
90+
private const string REGEX_FOR_LOWERCASE_HEADERS = "(-[a-z])";
91+
8192
/// <summary>
8293
/// Generates a base64-representation of RSA/SHA256/PKCS1 signature for a given RequestMessage.
8394
/// </summary>
@@ -127,6 +138,20 @@ private static string GenerateRequestHeadersSortedString(HttpRequestMessage requ
127138
);
128139
}
129140

141+
private static string GetHeaderNameCorrectlyCased(string headerName)
142+
{
143+
headerName = headerName.ToLower();
144+
headerName = headerName.First().ToString().ToUpper() + headerName.Substring(INDEX_LAST_FIRST_CHAR);
145+
var matches = Regex.Matches(headerName, REGEX_FOR_LOWERCASE_HEADERS);
146+
147+
return matches.Cast<Match>().Aggregate(
148+
headerName,
149+
(current, match) => current.Replace(
150+
match.Groups[INDEX_FIRST].Value, match.Groups[INDEX_FIRST].Value.ToUpper()
151+
)
152+
);
153+
}
154+
130155
private static string GenerateHeadersSortedString(
131156
IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
132157
{
@@ -307,8 +332,8 @@ private static string GenerateResponseHeadersSortedString(HttpResponseMessage re
307332
{
308333
return GenerateHeadersSortedString(
309334
responseMessage.Headers.Where(x =>
310-
x.Key.StartsWith(HEADER_NAME_PREFIX_X_BUNQ) &&
311-
!x.Key.Equals(HEADER_SERVER_SIGNATURE)
335+
GetHeaderNameCorrectlyCased(x.Key).StartsWith(HEADER_NAME_PREFIX_X_BUNQ) &&
336+
!GetHeaderNameCorrectlyCased(x.Key).Equals(HEADER_SERVER_SIGNATURE)
312337
)
313338
);
314339
}

0 commit comments

Comments
 (0)