Skip to content

Commit 6c97a23

Browse files
author
Kevin Hellemun
committed
Insure that headers are correclty cased before signature verification. (#49)
1 parent fddccd2 commit 6c97a23

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

BunqSdk/Security/SecurityUtils.cs

Lines changed: 22 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;
@@ -77,6 +78,11 @@ public class SecurityUtils
7778
/// Number of the very first index in an array or a string.
7879
/// </summary>
7980
private const int INDEX_FIRST = 0;
81+
82+
/// <summary>
83+
/// Regex constants.
84+
/// </summary>
85+
private const string REGEX_FOR_LOWERCASE_HEADERS = "(-[a-z])";
8086

8187
/// <summary>
8288
/// Generates a base64-representation of RSA/SHA256/PKCS1 signature for a given RequestMessage.
@@ -127,6 +133,20 @@ private static string GenerateRequestHeadersSortedString(HttpRequestMessage requ
127133
);
128134
}
129135

136+
private static string GetHeaderNameCorrectyCased(string headerName)
137+
{
138+
headerName = headerName.ToLower();
139+
headerName = headerName.First().ToString().ToUpper() + headerName.Substring(1);
140+
var matches = Regex.Matches(headerName, REGEX_FOR_LOWERCASE_HEADERS);
141+
142+
return matches.Cast<Match>().Aggregate(
143+
headerName,
144+
(current, match) => current.Replace(
145+
match.Groups[INDEX_FIRST].Value, match.Groups[INDEX_FIRST].Value.ToUpper()
146+
)
147+
);
148+
}
149+
130150
private static string GenerateHeadersSortedString(
131151
IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
132152
{
@@ -307,8 +327,8 @@ private static string GenerateResponseHeadersSortedString(HttpResponseMessage re
307327
{
308328
return GenerateHeadersSortedString(
309329
responseMessage.Headers.Where(x =>
310-
x.Key.StartsWith(HEADER_NAME_PREFIX_X_BUNQ) &&
311-
!x.Key.Equals(HEADER_SERVER_SIGNATURE)
330+
GetHeaderNameCorrectyCased(x.Key).StartsWith(HEADER_NAME_PREFIX_X_BUNQ) &&
331+
!GetHeaderNameCorrectyCased(x.Key).Equals(HEADER_SERVER_SIGNATURE)
312332
)
313333
);
314334
}

0 commit comments

Comments
 (0)