Skip to content

Commit

Permalink
Throw if additional unencrypted bytes are left after SSL negotiation (#…
Browse files Browse the repository at this point in the history
…4131)

Closes #4130

(cherry picked from commit b3a5345)
  • Loading branch information
roji committed Nov 12, 2021
1 parent fb60757 commit 85f1a77
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Npgsql/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ async Task RawOpen(NpgsqlTimeout timeout, bool async, CancellationToken cancella
break;
}

ReadBuffer.Clear(); // Reset to empty after reading single SSL char
if (ReadBuffer.ReadBytesLeft > 0)
throw new NpgsqlException("Additional unencrypted data received after SSL negotiation - this should never happen, and may be an indication of a man-in-the-middle attack.");
}

Log.Trace($"Socket connected to {Host}:{Port}");
Expand Down Expand Up @@ -1272,7 +1273,7 @@ void DoCancelRequest(int backendProcessId, int backendSecretKey)
.GetAwaiter().GetResult();
SendMessage(new CancelRequestMessage(backendProcessId, backendSecretKey));

Debug.Assert(ReadBuffer.ReadPosition == 0);
Debug.Assert(ReadBuffer.ReadBytesLeft == 0);

// Now wait for the server to close the connection, better chance of the cancellation
// actually being delivered before we continue with the user's logic.
Expand Down

0 comments on commit 85f1a77

Please sign in to comment.