Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 61f3a2a

Browse filesBrowse files
committed
Always dispose RemoteCertificate on SslStream (#6022)
Fixes #5993 (cherry picked from commit 44a7ab1)
1 parent a13fc41 commit 61f3a2a
Copy full SHA for 61f3a2a

File tree

Expand file treeCollapse file tree

2 files changed

+17
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-1
lines changed

‎src/Npgsql/Internal/NpgsqlConnector.Auth.cs

Copy file name to clipboardExpand all lines: src/Npgsql/Internal/NpgsqlConnector.Auth.cs
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ internal void AuthenticateSASLSha256Plus(ref string mechanism, ref string cbindF
202202
return;
203203
}
204204

205+
// While SslStream.RemoteCertificate is X509Certificate2, it actually returns X509Certificate2
206+
// But to be on the safe side we'll just create a new instance of it
205207
using var remoteCertificate = new X509Certificate2(sslStream.RemoteCertificate);
206208
// Checking for hashing algorithms
207209
HashAlgorithm? hashAlgorithm = null;

‎src/Npgsql/Internal/NpgsqlConnector.cs

Copy file name to clipboardExpand all lines: src/Npgsql/Internal/NpgsqlConnector.cs
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,8 +2209,9 @@ void FullCleanup()
22092209
/// </remarks>
22102210
void Cleanup()
22112211
{
2212+
var sslStream = _stream as SslStream;
22122213
#if !NETSTANDARD2_0
2213-
if (_stream is SslStream sslStream)
2214+
if (sslStream is not null)
22142215
{
22152216
try
22162217
{
@@ -2226,6 +2227,19 @@ void Cleanup()
22262227
}
22272228
#endif
22282229

2230+
// After we access SslStream.RemoteCertificate (like for SASLSha256Plus)
2231+
// SslStream will no longer dispose it for us automatically
2232+
// Which is why we have to do it ourselves before disposing the stream
2233+
// As otherwise accessing RemoteCertificate will throw an exception
2234+
try
2235+
{
2236+
sslStream?.RemoteCertificate?.Dispose();
2237+
}
2238+
catch
2239+
{
2240+
// ignored
2241+
}
2242+
22292243
try
22302244
{
22312245
_stream?.Dispose();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.