Skip to content

Commit ae3309f

Browse files
authored
Fix a possible timeout with gss/sspi auth (#5028)
Fixes #4888
1 parent 37ed406 commit ae3309f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/Npgsql/Internal/NpgsqlConnector.Auth.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,15 @@ async Task AuthenticateGSS(bool async)
300300
var response = ExpectAny<AuthenticationRequestMessage>(await ReadMessage(async), this);
301301
if (response.AuthRequestType == AuthenticationRequestType.AuthenticationOk)
302302
break;
303-
var gssMsg = response as AuthenticationGSSContinueMessage;
304-
if (gssMsg == null)
303+
if (response is not AuthenticationGSSContinueMessage gssMsg)
305304
throw new NpgsqlException($"Received unexpected authentication request message {response.AuthRequestType}");
306305
data = authContext.GetOutgoingBlob(gssMsg.AuthenticationData.AsSpan(), out statusCode)!;
307-
if (statusCode == NegotiateAuthenticationStatusCode.Completed)
306+
if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded)
307+
throw new NpgsqlException($"Error while authenticating GSS/SSPI: {statusCode}");
308+
// We might get NegotiateAuthenticationStatusCode.Completed but the data will not be null
309+
// This can happen if it's the first cycle, in which case we have to send that data to complete handshake (#4888)
310+
if (data is null)
308311
continue;
309-
Debug.Assert(statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded);
310312
await WritePassword(data, 0, data.Length, async, UserCancellationToken);
311313
await Flush(async, UserCancellationToken);
312314
}

0 commit comments

Comments
 (0)