Skip to content

Commit

Permalink
Fix crashes on Mono when trying to show a message
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
luckyrat committed Aug 7, 2023
1 parent c226f49 commit 26158dd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion KeePassRPC/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static EntryConfig GetKPRPCConfig(this PwEntry entry, ProtectedStringDict
}
else
{
MessageBox.Show("There are configuration errors in this entry. To fix the entry and prevent this warning message appearing, please edit the value of the 'KPRPC JSON' advanced string. Please ask for help on https://forum.kee.pm if you're not sure how to fix this. The URL of the entry is: " + url + " and the full configuration data is: " + json, "Warning: Configuration errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Utils.ShowMonoSafeMessageBox("There are configuration errors in this entry. To fix the entry and prevent this warning message appearing, please edit the value of the 'KPRPC JSON' advanced string. Please ask for help on https://forum.kee.pm if you're not sure how to fix this. The URL of the entry is: " + url + " and the full configuration data is: " + json, "Warning: Configuration errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return null;
}
Expand Down
20 changes: 13 additions & 7 deletions KeePassRPC/Forms/OptionsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void UpdateAuthorisedConnections()
}

if (anyConnectionCompromised) {
MessageBox.Show(@"Your KeePass instance may have previously been exploited by a malicious attacker.
Utils.ShowMonoSafeMessageBox(@"Your KeePass instance may have previously been exploited by a malicious attacker.
The passwords contained within any databases that were open before this point may have been exposed so you should change them.
Expand Down Expand Up @@ -216,13 +216,19 @@ private void m_btnOK_Click(object sender, EventArgs e)
}
catch (ArgumentOutOfRangeException)
{
MessageBox.Show("Invalid listening port. Type a number between 1 and 65535 or leave empty to use the default port.");
Utils.ShowMonoSafeMessageBox("Invalid listening port. Type a number between 1 and 65535 or leave empty to use the default port.");
DialogResult = DialogResult.None;
return;
}
catch (OverflowException)
{
Utils.ShowMonoSafeMessageBox("Invalid listening port. Type a number between 1 and 65535 or leave empty to use the default port.");
DialogResult = DialogResult.None;
return;
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
Utils.ShowMonoSafeMessageBox(ex.Message);
DialogResult = DialogResult.None;
return;
}
Expand All @@ -234,21 +240,21 @@ private void m_btnOK_Click(object sender, EventArgs e)
}
catch (Exception)
{
MessageBox.Show("Invalid expiry time.");
Utils.ShowMonoSafeMessageBox("Invalid expiry time.");
DialogResult = DialogResult.None;
return;
}

if (expTime < 1)
{
expTime = 1;
MessageBox.Show("Expiry time set to 1 hour. This is the minimum allowed.");
Utils.ShowMonoSafeMessageBox("Expiry time set to 1 hour. This is the minimum allowed.");
}

if (expTime > 876000)
{
expTime = 876000;
MessageBox.Show("Expiry time set to 100 years. This is the maximum allowed.");
Utils.ShowMonoSafeMessageBox("Expiry time set to 100 years. This is the maximum allowed.");
}

long secLevel = 2;
Expand Down Expand Up @@ -279,7 +285,7 @@ private void m_btnOK_Click(object sender, EventArgs e)
_host.CustomConfig.SetULong("KeePassRPC.webSocket.port", port);
if (port != originalPort)
{
MessageBox.Show("Restart KeePass to start using the new connection port");
Utils.ShowMonoSafeMessageBox("Restart KeePass to start using the new connection port");
}
}

Expand Down
4 changes: 2 additions & 2 deletions KeePassRPC/KeePassRPCClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private KeyContainerClass KeyContainer
if (keyContainer == null ||
keyContainer.Key == "5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9")
{
MessageBox.Show(@"Your KeePass instance may have previously been exploited by a malicious attacker.
Utils.ShowMonoSafeMessageBox(@"Your KeePass instance may have previously been exploited by a malicious attacker.
The passwords contained within any databases that were open before this point may have been exposed so you should change them.
Expand Down Expand Up @@ -345,7 +345,7 @@ public void Signal(KeePassRPC.DataExchangeModel.Signal signal, string methodName
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ERROR! Please click on this box, press CTRL-C on your keyboard and paste into a new post on the Kee forum (https://forum.kee.pm). Doing this will help other people to use Kee without any unexpected error messages like this. Please briefly describe what you were doing when the problem occurred, which version of Kee, KeePass and web browser you use and what other security software you run on your machine. Thanks! Technical detail follows: " + ex.ToString());
Utils.ShowMonoSafeMessageBox("ERROR! Please click on this box, press CTRL-C on your keyboard and paste into a new post on the Kee forum (https://forum.kee.pm). Doing this will help other people to use Kee without any unexpected error messages like this. Please briefly describe what you were doing when the problem occurred, which version of Kee, KeePass and web browser you use and what other security software you run on your machine. Thanks! Technical detail follows: " + ex.ToString());
}
}

Expand Down
8 changes: 4 additions & 4 deletions KeePassRPC/KeePassRPCExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public override bool Initialize(IPluginHost host)
}
catch (Exception ex)
{
MessageBox.Show("KeePassRPC debug logger failed to initialise. No logging will be performed until KeePass is restarted with a valid debug log file location. Reason: " + ex.ToString());
Utils.ShowMonoSafeMessageBox("KeePassRPC debug logger failed to initialise. No logging will be performed until KeePass is restarted with a valid debug log file location. Reason: " + ex.ToString());
}
}
if (logger != null) logger.WriteLine("Logger initialised.");
Expand Down Expand Up @@ -178,7 +178,7 @@ public override bool Initialize(IPluginHost host)
{
if (ex.SocketErrorCode == System.Net.Sockets.SocketError.AddressAlreadyInUse)
{
MessageBox.Show(@"KeePassRPC is already listening for connections. To allow KeePassRPC clients (e.g. Kee in your web browser) to connect to this instance of KeePass, please close all other running instances of KeePass and restart this KeePass. If you want multiple instances of KeePass to be running at the same time, you'll need to configure some of them to connect using a different communication port.
Utils.ShowMonoSafeMessageBox(@"KeePassRPC is already listening for connections. To allow KeePassRPC clients (e.g. Kee in your web browser) to connect to this instance of KeePass, please close all other running instances of KeePass and restart this KeePass. If you want multiple instances of KeePass to be running at the same time, you'll need to configure some of them to connect using a different communication port.
See https://forum.kee.pm/t/connection-security-levels/1075
Expand All @@ -187,7 +187,7 @@ public override bool Initialize(IPluginHost host)
}
else
{
MessageBox.Show(@"KeePassRPC could not start listening for connections. To allow KeePassRPC clients (e.g. Kee in your web browser) to connect to this instance of KeePass, please fix the problem indicated in the technical detail below and restart KeePass.
Utils.ShowMonoSafeMessageBox(@"KeePassRPC could not start listening for connections. To allow KeePassRPC clients (e.g. Kee in your web browser) to connect to this instance of KeePass, please fix the problem indicated in the technical detail below and restart KeePass.
KeePassRPC requires this port to be available: " + portNew + ". Technical detail: " + ex.ToString());
if (logger != null) logger.WriteLine("Socket error. KeePassRPC requires this port to be available: " + portNew + ". Maybe check that you have no firewall or other third party security software interfering with your system. Technical detail: " + ex.ToString());
Expand Down Expand Up @@ -429,7 +429,7 @@ void OnMenuSetRootGroup(object sender, EventArgs e)
var rid = _host.Database.RecycleBinUuid;
if (rid != null && rid != PwUuid.Zero && pg.IsOrIsContainedIn(_host.Database.RootGroup.FindGroup(rid, true)))
{
MessageBox.Show(@"You can not set this to be the Kee Home Group. Choose a group outside of the Recycle Bin instead.");
Utils.ShowMonoSafeMessageBox(@"You can not set this to be the Kee Home Group. Choose a group outside of the Recycle Bin instead.");
return;
}
var conf = _host.Database.GetKPRPCConfig();
Expand Down
6 changes: 3 additions & 3 deletions KeePassRPC/KeePassRPCService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ public Entry[] FindLogins(string[] unsanitisedURLs, string actionURL,
}
catch (ArgumentException)
{
MessageBox.Show("'" + regexPattern + "' is not a valid regular expression. This error was found in an entry in your database called '" + pwe.Strings.ReadSafe(PwDefs.TitleField) + "'. You need to fix or delete this regular expression to prevent this warning message appearing.", "Warning: Broken regular expression", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Utils.ShowMonoSafeMessageBox("'" + regexPattern + "' is not a valid regular expression. This error was found in an entry in your database called '" + pwe.Strings.ReadSafe(PwDefs.TitleField) + "'. You need to fix or delete this regular expression to prevent this warning message appearing.", "Warning: Broken regular expression", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
}
Expand Down Expand Up @@ -1964,7 +1964,7 @@ public Entry[] FindLogins(string[] unsanitisedURLs, string actionURL,
}
catch (ArgumentException)
{
MessageBox.Show("'" + pattern + "' is not a valid regular expression. This error was found in an entry in your database called '" + pwe.Strings.ReadSafe(PwDefs.TitleField) + "'. You need to fix or delete this regular expression to prevent this warning message appearing.", "Warning: Broken regular expression", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Utils.ShowMonoSafeMessageBox("'" + pattern + "' is not a valid regular expression. This error was found in an entry in your database called '" + pwe.Strings.ReadSafe(PwDefs.TitleField) + "'. You need to fix or delete this regular expression to prevent this warning message appearing.", "Warning: Broken regular expression", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
}
Expand All @@ -1982,7 +1982,7 @@ public Entry[] FindLogins(string[] unsanitisedURLs, string actionURL,

}
if (configErrors.Count > 0)
MessageBox.Show("There are configuration errors in your database called '" + db.Name + "'. To fix the entries listed below and prevent this warning message appearing, please edit the value of the 'KeePassRPC JSON config' advanced string. Please ask for help on https://forum.kee.pm if you're not sure how to fix this. These entries are affected:" + Environment.NewLine + string.Join(Environment.NewLine, configErrors.ToArray()), "Warning: Configuration errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Utils.ShowMonoSafeMessageBox("There are configuration errors in your database called '" + db.Name + "'. To fix the entries listed below and prevent this warning message appearing, please edit the value of the 'KeePassRPC JSON config' advanced string. Please ask for help on https://forum.kee.pm if you're not sure how to fix this. These entries are affected:" + Environment.NewLine + string.Join(Environment.NewLine, configErrors.ToArray()), "Warning: Configuration errors", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
allEntries.Sort(delegate (Entry e1, Entry e2)
Expand Down
45 changes: 45 additions & 0 deletions KeePassRPC/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace KeePassRPC
{
Expand Down Expand Up @@ -54,5 +55,49 @@ internal static string GetTypeablePassword(byte[] password)
{
return EncodeToBase32(BitConverter.ToUInt32(password, 0));
}

private static Form GetTopForm()
{
FormCollection fc = Application.OpenForms;
if (fc == null || fc.Count == 0) return null;
return fc[fc.Count - 1];
}

internal static void ShowMonoSafeMessageBox(string description, string title = "KeePassRPC",
MessageBoxButtons mb = MessageBoxButtons.OK, MessageBoxIcon mi = MessageBoxIcon.Information, MessageBoxDefaultButton mdb = MessageBoxDefaultButton.Button1)
{
IWin32Window win = null;
try
{
Form f = GetTopForm();
if (f != null && f.InvokeRequired)
{
f.Invoke(new Action(() => MessageBox.Show(description, title, mb, mi, mdb)));
return;
} else
{
win = f;
}
} catch (Exception)
{
}

if (win == null)
{
MessageBox.Show(description, title, mb, mi, mdb);
return;
}

try
{
MessageBox.Show(win, description, title, mb, mi, mdb);
return;
}
catch (Exception)
{
}
MessageBox.Show(description, title, mb, mi, mdb);
}

}
}

0 comments on commit 26158dd

Please sign in to comment.