Skip to content

Commit a2273b0

Browse files
committed
Fixed an issue causing request decryption to be skipped when the request didn't have a payload, fixed an issue causing request decryption to fail
1 parent 376ce31 commit a2273b0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Mastercard.Developer.ClientEncryption.RestSharpV2/Interceptors/RestSharpFieldLevelEncryptionInterceptor.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public void InterceptRequest(IRestRequest request)
3232

3333
try
3434
{
35+
// We will have to intercept the response later
36+
request.OnBeforeDeserialization = InterceptResponse;
37+
3538
// Check request actually has a payload
3639
var bodyParam = request.Parameters.FirstOrDefault(param => param.Type == ParameterType.RequestBody);
3740
if (bodyParam?.Value == null)
@@ -67,7 +70,6 @@ public void InterceptRequest(IRestRequest request)
6770

6871
// Update body and content length
6972
bodyParam.Value = JsonConvert.DeserializeObject(encryptedPayload);
70-
request.OnBeforeDeserialization = InterceptResponse;
7173
UpdateRequestHeader(request, "Content-Length", encryptedPayload.Length);
7274
}
7375
catch (EncryptionException)
@@ -149,10 +151,14 @@ private static string ReadAndRemoveHeader(IRestResponse response, string name)
149151
return null;
150152
}
151153

152-
// Headers has been made read only
153-
var headers = response.GetType().GetTypeInfo().GetDeclaredField("<Headers>k__BackingField");
154-
var newHeaders = response.Headers.ToList().FindAll(h => h.Name != name);
155-
headers.SetValue(response, newHeaders);
154+
// The "Headers" collection has been made read only, but we try to remove
155+
// the header from the response anyway.
156+
var headersField = response.GetType().GetTypeInfo().GetDeclaredField("<Headers>k__BackingField");
157+
if (headersField != null)
158+
{
159+
var updatedHeaders = response.Headers.ToList().FindAll(h => h.Name != name);
160+
headersField.SetValue(response, updatedHeaders);
161+
}
156162

157163
return header.Value?.ToString() ?? string.Empty;
158164
}

0 commit comments

Comments
 (0)