From 8778d404d7e65112906cd6281faaa17a06dac5ce Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Tue, 9 Dec 2025 19:09:44 +0100 Subject: [PATCH] fix: delete CLI-created session settings from registry after disconnect Refs #2162 --- source/apphelpers.pas | 10 +++++++--- source/dbconnection.pas | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/source/apphelpers.pas b/source/apphelpers.pas index 2782915d..9b720ea8 100644 --- a/source/apphelpers.pas +++ b/source/apphelpers.pas @@ -2703,9 +2703,13 @@ begin ConnectionParams.SSHTimeout := StrToIntDef(SshTimeout, ConnectionParams.SSHTimeout); end; - // Ensure we have a session name to pass to InitConnection - if (ConnectionParams.SessionPath = '') and (ConnectionParams.Hostname <> '') then - ConnectionParams.SessionPath := ConnectionParams.Hostname; + if ConnectionParams.SessionPath.IsEmpty then begin + // Ensure we have a (random) session name to pass to InitConnection + ConnectionParams.SessionPath := IfEmpty(ConnectionParams.Hostname, 'temp')+'-'+GeneratePassword(4); + end; + + // Delete stored session in Destroy: + ConnectionParams.DeleteAfterUse := True; end; // Check for valid filename(s) in parameters. diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 7d7512e1..12fdffb3 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -310,6 +310,8 @@ type TConnectionParameters = class(TObject) strict private + FDeleteAfterUse: Boolean; + FLoadedFromSettings: Boolean; FNetType: TNetType; FHostname, FUsername, FPassword, FAllDatabases, FLibraryOrProvider, FComment, FStartupScriptFilename, FSessionPath, FSSLPrivateKey, FSSLCertificate, FSSLCACertificate, FSSLCipher, FServerVersion, @@ -330,7 +332,9 @@ type public constructor Create; overload; constructor Create(SessionRegPath: String); overload; + destructor Destroy; override; procedure SaveToRegistry; + property DeleteAfterUse: Boolean read FDeleteAfterUse write FDeleteAfterUse; function CreateConnection(AOwner: TComponent): TDBConnection; function CreateQuery(Connection: TDbConnection): TDBQuery; function NetTypeName(LongFormat: Boolean): String; @@ -1281,6 +1285,8 @@ constructor TConnectionParameters.Create; begin inherited Create; FIsFolder := False; + FDeleteAfterUse := False; + FLoadedFromSettings := False; FNetType := TNetType(AppSettings.GetDefaultInt(asNetType)); FHostname := DefaultHost; @@ -1354,6 +1360,7 @@ begin ); FNetType := ntMySQL_TCPIP; end; + FLoadedFromSettings := True; FHostname := AppSettings.ReadString(asHost); FUsername := AppSettings.ReadString(asUser); FPassword := decrypt(AppSettings.ReadString(asPassword)); @@ -1410,6 +1417,16 @@ begin end; end; +destructor TConnectionParameters.Destroy; +begin + if FDeleteAfterUse and (not FLoadedFromSettings) and (not FSessionPath.IsEmpty) then begin + if AppSettings.SessionPathExists(FSessionPath) then begin + AppSettings.SessionPath := FSessionPath; + AppSettings.DeleteCurrentKey; + end; + end; +end; + procedure TConnectionParameters.SaveToRegistry; var @@ -2130,6 +2147,7 @@ begin FKeepAliveTimer.Free; FFavorites.Free; FInformationSchemaObjects.Free; + FParameters.Free; inherited; end;