@@ -434,7 +434,10 @@ internal static void ValidateToken(Socket socket, string token, DateTimeOffset t
434434 // Final check if we got the token before the timeout
435435 cancellationToken . ThrowIfCancellationRequested ( ) ;
436436
437- if ( string . IsNullOrEmpty ( responseString ) || ! responseString . StartsWith ( "TOKEN " , StringComparison . Ordinal ) )
437+ ReadOnlySpan < byte > responseBytes = Encoding . UTF8 . GetBytes ( responseString ) ;
438+ string responseToken = RemoteSessionHyperVSocketClient . ExtractToken ( responseBytes ) ;
439+
440+ if ( responseToken == null )
438441 {
439442 socket . Send ( "FAIL"u8 ) ;
440443 // If the response is not in the expected format, we throw an exception.
@@ -444,9 +447,6 @@ internal static void ValidateToken(Socket socket, string token, DateTimeOffset t
444447 PSRemotingErrorInvariants . FormatResourceString ( RemotingErrorIdStrings . HyperVInvalidResponse , "Client" , "Token Response" ) ) ;
445448 }
446449
447- // Extract the token from the response.
448- string responseToken = responseString . Substring ( 6 ) . Trim ( ) ;
449-
450450 if ( ! string . Equals ( responseToken , token , StringComparison . Ordinal ) )
451451 {
452452 socket . Send ( "FAIL"u8 ) ;
@@ -1059,14 +1059,18 @@ public static (bool success, string authenticationToken) ExchangeCredentialsAndC
10591059 // allowing a significant larger size, allows the broker to make almost arbitrary changes,
10601060 // without breaking the client.
10611061 string token = ReceiveResponse ( HyperVSocket , 1024 ) ; // either "PASS" or "FAIL"
1062- if ( token == null || ! token . StartsWith ( "TOKEN " , StringComparison . Ordinal ) )
1062+
1063+ ReadOnlySpan < byte > tokenResponseBytes = Encoding . UTF8 . GetBytes ( token ) ;
1064+ string extractedToken = ExtractToken ( tokenResponseBytes ) ;
1065+
1066+ if ( extractedToken == null )
10631067 {
10641068 s_tracer . WriteLine ( "ExchangeCredentialsAndConfiguration: Server did not respond with a valid token. Response: {0}" , token ) ;
10651069 throw new PSDirectException (
10661070 PSRemotingErrorInvariants . FormatResourceString ( RemotingErrorIdStrings . HyperVInvalidResponse , "Broker" , "Token " + token ) ) ;
10671071 }
10681072
1069- token = token . Substring ( 6 ) ; // remove "TOKEN " prefix
1073+ token = extractedToken ;
10701074
10711075 HyperVSocket . Send ( "PASS"u8 ) ; // acknowledge the token
10721076 return ( true , token ) ;
@@ -1122,6 +1126,25 @@ internal static string ReceiveResponse(Socket socket, int bufferSize)
11221126 }
11231127 }
11241128
1129+ internal static string ExtractToken ( ReadOnlySpan < byte > tokenResponse )
1130+ {
1131+ string token = Encoding . UTF8 . GetString ( tokenResponse ) ;
1132+
1133+ if ( token == null || ! token . StartsWith ( "TOKEN " , StringComparison . Ordinal ) )
1134+ {
1135+ return null ; // caller method will write trace (and determine when to expose token info as appropriate)
1136+ }
1137+
1138+ token = token . Substring ( 6 ) . Trim ( ) ; // remove "TOKEN " prefix
1139+
1140+ if ( token . Length == 0 )
1141+ {
1142+ return null ;
1143+ }
1144+
1145+ return token ;
1146+ }
1147+
11251148 /// <summary>
11261149 /// Sends user data (domain, username, etc.) over the HyperVSocket using Unicode encoding.
11271150 /// </summary>
0 commit comments