Skip to content

Commit 256cf9d

Browse files
committed
Use Marshal.SecureStringToGlobalAllocUnicode
Relates: #3806 (comment) This commit updates the SecureStrings implementation to use the cross platform Marshal methods that deal with unmanaged unicode strings. Implement IDisposable on BasicAuthenticationCredentials and dispose of SecureString instances when ConnectionConfiguration is disposed.
1 parent 97b3882 commit 256cf9d

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ protected virtual void DisposeManagedResources()
513513
_connectionPool?.Dispose();
514514
_connection?.Dispose();
515515
_semaphore?.Dispose();
516+
_proxyPassword?.Dispose();
517+
_basicAuthCredentials?.Dispose();
516518
}
517519
}
518520
}

src/Elasticsearch.Net/Configuration/Security/BasicAuthenticationCredentials.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Security;
1+
using System;
2+
using System.Security;
23

34
namespace Elasticsearch.Net
45
{
56
/// <summary>
67
/// Credentials for Basic Authentication
78
/// </summary>
8-
public class BasicAuthenticationCredentials
9+
public class BasicAuthenticationCredentials : IDisposable
910
{
1011
public BasicAuthenticationCredentials()
1112
{
@@ -32,5 +33,7 @@ public BasicAuthenticationCredentials(string username, SecureString password)
3233
/// The username with which to authenticate
3334
/// </summary>
3435
public string Username { get; set; }
36+
37+
public void Dispose() => Password?.Dispose();
3538
}
3639
}

src/Elasticsearch.Net/Connection/SecureStrings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public static string CreateString(this SecureString secureString)
1919
{
2020
try
2121
{
22-
num = Marshal.SecureStringToBSTR(secureString);
23-
return Marshal.PtrToStringBSTR(num);
22+
num = Marshal.SecureStringToGlobalAllocUnicode(secureString);
23+
return Marshal.PtrToStringUni(num);
2424
}
2525
finally
2626
{
2727
if (num != IntPtr.Zero)
28-
Marshal.ZeroFreeBSTR(num);
28+
Marshal.ZeroFreeGlobalAllocUnicode(num);
2929
}
3030
}
3131

0 commit comments

Comments
 (0)