Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Ginger/GingerCoreNET/Database/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,34 @@ public Boolean Connect(bool displayErrorPopup = false)
Password = EncryptionHandler.DecryptwithKey(PassCalculated)
};
if (port1.HasValue) my.Port = port1.Value;
Database.ConnectionString = my.ConnectionString;
if (!string.IsNullOrEmpty(ConnectionStringCalculated))
{
foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
}

string key = segment.Substring(0, eq).Trim();
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}
Comment on lines +505 to +509
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Fail fast on invalid SslMode values.

If Enum.TryParse fails here, the code silently keeps the builder default and still opens the connection. A typo in SslMode then becomes a hidden config downgrade instead of an actionable error.

Suggested fix
                                 if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
                                 {
                                     string value = segment.Substring(eq + 1).Trim();
-                                    if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
-                                    {
-                                        my.SslMode = sslMode;
-                                    }
+                                    if (!Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
+                                    {
+                                        throw new ArgumentException($"Invalid MySQL SslMode '{value}'.");
+                                    }
+
+                                    my.SslMode = sslMode;
 
                                     break;
                                 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 505 - 509,
The parsing of SslMode using Enum.TryParse silently ignores invalid values so a
typo downgrades the config; update the block around Enum.TryParse(value,
ignoreCase: true, out MySqlSslMode sslMode) (the code that assigns my.SslMode)
to fail fast: if TryParse returns false, throw an informative exception (e.g.,
ArgumentException or FormatException) that includes the invalid value and
expected MySqlSslMode enum names; otherwise assign my.SslMode as before. This
ensures misconfigured SslMode is surfaced immediately.


break;
}
}
}

if (string.IsNullOrEmpty(ConnectionStringCalculated))
{
Database.ConnectionString = my.ConnectionString;
}
Comment on lines +492 to +519
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Keep Database.ConnectionString aligned with the effective MySQL connection.

This now leaves Database.ConnectionString untouched whenever ConnectionStringCalculated is non-empty. For inputs like SslMode=Required, Connect() succeeds with my.ConnectionString, but ConnectionStringCalculated/GetConnectionString() still resolve to that partial fragment afterward instead of the real connection string. That is a regression from the previous invariant and can break callers that inspect the effective value after Connect().

Suggested fix
-                        if (!string.IsNullOrEmpty(ConnectionStringCalculated))
+                        string calculatedConnectionString = ConnectionStringCalculated;
+                        if (!string.IsNullOrEmpty(calculatedConnectionString))
                         {
-                            foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
+                            foreach (string segment in calculatedConnectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
                             {
                                 int eq = segment.IndexOf('=');
                                 if (eq <= 0)
                                 {
                                     continue;
@@
                                 }
                             }
                         }
-
-                        if (string.IsNullOrEmpty(ConnectionStringCalculated))
-                        {
-                            Database.ConnectionString = my.ConnectionString;
-                        }
+                        Database.ConnectionString = my.ConnectionString;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!string.IsNullOrEmpty(ConnectionStringCalculated))
{
foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
}
string key = segment.Substring(0, eq).Trim();
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}
break;
}
}
}
if (string.IsNullOrEmpty(ConnectionStringCalculated))
{
Database.ConnectionString = my.ConnectionString;
}
string calculatedConnectionString = ConnectionStringCalculated;
if (!string.IsNullOrEmpty(calculatedConnectionString))
{
foreach (string segment in calculatedConnectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
}
string key = segment.Substring(0, eq).Trim();
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}
break;
}
}
}
Database.ConnectionString = my.ConnectionString;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 492 - 519,
The Connect() path currently parses SslMode from ConnectionStringCalculated but
never updates Database.ConnectionString when ConnectionStringCalculated is
non-empty, leaving Database.ConnectionString out of sync; after you finish
parsing/applying values from ConnectionStringCalculated (i.e. after the loop
where you handle SslMode in DatabaseOperations.Connect or equivalent), set
Database.ConnectionString = my.ConnectionString so Database.ConnectionString
always reflects the effective connection string used by the MySql connection
(ensure this is done both when ConnectionStringCalculated is empty and when it
is non-empty so GetConnectionString()/Connect() invariants hold).


oConn = new MySqlConnection
{
Expand Down
Loading