Skip to content

Commit 2eb7f55

Browse files
author
Brian Crowell
committed
IMPORTANT: Freeze user name in connection string when using integrated security
When connection pooling is turned on, connections are cached by connection string. But when integrated security is turned on, the user's identity isn't part of the connection string, so two users can get each other's connections back from the connection pool. This patch forces the username to appear in the connection string if integrated security is on, before any pooling takes place.
1 parent 1968ab8 commit 2eb7f55

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

Npgsql/Npgsql/NpgsqlConnection.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,15 +1058,14 @@ private void LogConnectionString()
10581058
/// <param name="connectionString">The connection string to load the builder from</param>
10591059
private void LoadConnectionStringBuilder(string connectionString)
10601060
{
1061-
settings = cache[connectionString];
1062-
if (settings == null)
1061+
NpgsqlConnectionStringBuilder newSettings = cache[connectionString];
1062+
if (newSettings == null)
10631063
{
1064-
settings = new NpgsqlConnectionStringBuilder(connectionString);
1065-
cache[connectionString] = settings;
1064+
newSettings = new NpgsqlConnectionStringBuilder(connectionString);
1065+
cache[connectionString] = newSettings;
10661066
}
10671067

1068-
RefreshConnectionString();
1069-
LogConnectionString();
1068+
LoadConnectionStringBuilder(newSettings);
10701069
}
10711070

10721071
/// <summary>
@@ -1075,7 +1074,13 @@ private void LoadConnectionStringBuilder(string connectionString)
10751074
/// <param name="connectionString">The connection string to load the builder from</param>
10761075
private void LoadConnectionStringBuilder(NpgsqlConnectionStringBuilder connectionString)
10771076
{
1078-
settings = connectionString;
1077+
// Clone the settings, because if Integrated Security is enabled, user ID can be different
1078+
settings = connectionString.Clone();
1079+
1080+
// Set the UserName explicitly to freeze any Integrated Security-determined names
1081+
if (settings.IntegratedSecurity)
1082+
settings.UserName = settings.UserName;
1083+
10791084
RefreshConnectionString();
10801085
LogConnectionString();
10811086
}

0 commit comments

Comments
 (0)