Skip to content

Commit c831849

Browse files
Add check for unset correlation ID when configuring request headers (#2435)
If a correlation ID hasn't been configured for a request, the 'UNSET' value will be sent to the server, and the server will then return that 'UNSET' value from the response. To prevent this, the correlation ID should only be appended if it is a valid value. Additionally, tests have been added to ensure that an 'UNSET' value isn't added to the request headers, and valid correlation IDs are being added to the request headers on a valid request. Also, there are a few tweaks made to `NativeAuthRequestHandlerTest` - there are some tests where a request is configured and created, but there are no assertions made on those requests. I've added a few checks to tests that were missing validations, and some other small tweaks. Update: “If a correlation ID hasn't been configured for a request, the 'UNSET' value will be sent to the server, and the server will then return that 'UNSET' value from the response.” -> After experiment, this assumption is wrong. The header used on the SDK side is client-request-id not ms-client-request-id, these two values are different if they both attached in the response header. The interesting thing is that, if client sends invalid correlation id like "UNSET", the server would not return the client-request-id to the client. On the contrary, if the correlation id is valid and client sent it in the client-request-id header, the server will return it as the same value in the client-request-id header. SDK made the assumption that if no client-request-id header is sent to the server from client, the server will generate one for the client seems untrue. The correlation id is dependant on client-request-id x-ms-request-id is the request specific, it targets the Service Request Id not the CorrelationId, while x-ms-request-id is returned from the server in all times. --------- Co-authored-by: yuxin <yuxin@microsoft.com>
1 parent 44d9252 commit c831849

4 files changed

Lines changed: 265 additions & 20 deletions

File tree

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Version 17.6.1
1515
- [PATCH] Return API error and errorDescription in case of unexpected response (#2431)
1616
- [MINOR] Support certificate with password (#2405)
1717
- [MINOR] Classifying TimeoutException as timed_out (#2441)
18+
- [PATCH] Add check for unset correlation ID when sending Native Auth requests (#2435)
1819

1920
Version 17.5.0
2021
---------

common4j/src/main/com/microsoft/identity/common/java/logging/DiagnosticContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
// THE SOFTWARE.
2323
package com.microsoft.identity.common.java.logging;
2424

25+
import java.util.UUID;
26+
2527
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2628

2729
public enum DiagnosticContext {
@@ -76,8 +78,8 @@ public IRequestContext getRequestContext() {
7678
public String getThreadCorrelationId() {
7779
IRequestContext context = getRequestContext();
7880
String correlationId = context.get(DiagnosticContext.CORRELATION_ID);
79-
if (correlationId == null) {
80-
correlationId = UNSET;
81+
if (correlationId == null || correlationId.equals("UNSET")) {
82+
correlationId = UUID.randomUUID().toString();
8183
}
8284
return correlationId;
8385
}

common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/NativeAuthRequestProvider.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package com.microsoft.identity.common.java.nativeauth.providers
2424

2525
import com.microsoft.identity.common.java.AuthenticationConstants
2626
import com.microsoft.identity.common.java.eststelemetry.EstsTelemetry
27+
import com.microsoft.identity.common.java.logging.LibraryInfoHelper
2728
import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordStartCommandParameters
2829
import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordSubmitCodeCommandParameters
2930
import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordSubmitNewPasswordCommandParameters
@@ -34,7 +35,6 @@ import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpS
3435
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpSubmitCodeCommandParameters
3536
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpSubmitPasswordCommandParameters
3637
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpSubmitUserAttributesCommandParameters
37-
import com.microsoft.identity.common.java.logging.LibraryInfoHelper
3838
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInStartCommandParameters
3939
import com.microsoft.identity.common.java.net.HttpConstants
4040
import com.microsoft.identity.common.java.nativeauth.providers.requests.resetpassword.ResetPasswordChallengeRequest
@@ -49,6 +49,7 @@ import com.microsoft.identity.common.java.nativeauth.providers.requests.signup.S
4949
import com.microsoft.identity.common.java.nativeauth.providers.requests.signup.SignUpContinueRequest
5050
import com.microsoft.identity.common.java.nativeauth.providers.requests.signup.SignUpStartRequest
5151
import com.microsoft.identity.common.java.platform.Device
52+
import com.microsoft.identity.common.java.util.StringUtil
5253
import java.util.TreeMap
5354

5455
/**
@@ -309,7 +310,9 @@ class NativeAuthRequestProvider(private val config: NativeAuthOAuth2Configuratio
309310
//region helpers
310311
private fun getRequestHeaders(correlationId: String): Map<String, String?> {
311312
val headers: MutableMap<String, String?> = TreeMap()
312-
headers[AuthenticationConstants.AAD.CLIENT_REQUEST_ID] = correlationId
313+
if (correlationId != "UNSET") {
314+
headers[AuthenticationConstants.AAD.CLIENT_REQUEST_ID] = correlationId
315+
}
313316
headers[AuthenticationConstants.SdkPlatformFields.PRODUCT] = LibraryInfoHelper.getLibraryName()
314317
headers[AuthenticationConstants.SdkPlatformFields.VERSION] = LibraryInfoHelper.getLibraryVersion()
315318
headers.putAll(Device.getPlatformIdParameters())

0 commit comments

Comments
 (0)