Skip to content

Commit

Permalink
boost(Autoupdate): improve logged error message when signature check …
Browse files Browse the repository at this point in the history
…fails

see SOUNDSWITCH-11
  • Loading branch information
Belphemur committed Jul 24, 2022
1 parent c6e1c6b commit e017bc5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
44 changes: 22 additions & 22 deletions SoundSwitch/Framework/Updater/AutoUpdater.cs
Expand Up @@ -12,13 +12,15 @@
* GNU General Public License for more details.
********************************************************************/

#nullable enable
using System;
using System.Threading;
using System.Windows.Forms;
using Serilog;
using SoundSwitch.Framework.Updater.Installer;
using SoundSwitch.Framework.Updater.Releases;
using SoundSwitch.Localization;
using SoundSwitch.Util;

namespace SoundSwitch.Framework.Updater
{
Expand All @@ -29,6 +31,7 @@ public class AutoUpdater
{
private readonly SynchronizationContext _context = SynchronizationContext.Current ?? new SynchronizationContext();
public string InstallerParameters { get; }

/// <summary>
/// Constructor of the AutoUpdater
/// </summary>
Expand All @@ -40,29 +43,26 @@ public AutoUpdater(string installerParameters)

public void Update(AppRelease appRelease, bool closeApp)
{

var file = new WebFile(new Uri(appRelease.Asset.BrowserDownloadUrl));
file.Downloaded += (sender, args) =>
var file = new WebFile(new Uri(appRelease.Asset.BrowserDownloadUrl));
file.Downloaded += (sender, args) =>
{
Log.Information("Update downloaded: {File}", file);
var signatureResult = SignatureChecker.IsValid(file.FilePath).UnwrapFailure();
if (signatureResult != null)
{
Log.Information("Update downloaded: {File}", file);
if (!SignatureChecker.IsValid(file.FilePath))
{
Log.Error("The file has the wrong signature. Update cancelled.");
_context.Send(state =>
{
MessageBox.Show(string.Format(UpdateDownloadStrings.wrongSignature, "https://soundswitch.aaflalo.me"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}, null);
return;
}
Log.Error("The file has the wrong signature. Update cancelled. {signatureResult}", signatureResult);
_context.Send(state => { MessageBox.Show(string.Format(UpdateDownloadStrings.wrongSignature, "https://soundswitch.aaflalo.me"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); },
null);
return;
}
new UpdateRunner().RunUpdate(file, InstallerParameters);
if (closeApp)
{
_context.Send(s => { Application.Exit(); }, null);
}
};
file.DownloadFile();

new UpdateRunner().RunUpdate(file, InstallerParameters);
if (closeApp)
{
_context.Send(s => { Application.Exit(); }, null);
}
};
file.DownloadFile();
}
}
}
}
12 changes: 10 additions & 2 deletions SoundSwitch/Framework/Updater/SignatureChecker.cs
Expand Up @@ -13,6 +13,8 @@
********************************************************************/

using AuthenticodeExaminer;
using RailSharp;
using RailSharp.Internal.Result;

namespace SoundSwitch.Framework.Updater
{
Expand All @@ -23,10 +25,16 @@ public static class SignatureChecker
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static bool IsValid(string filename)
public static Result<SignatureCheckResult, VoidSuccess> IsValid(string filename)
{
var inspector = new FileInspector(filename);
return inspector.Validate() == SignatureCheckResult.Valid;
var result = inspector.Validate(RevocationChecking.Online);
if (result != SignatureCheckResult.Valid)
{
return result;
}

return Result.Success();
}
}
}
26 changes: 22 additions & 4 deletions SoundSwitch/Util/ResultTypeExtension.cs
@@ -1,7 +1,7 @@
using System;
using RailSharp.Internal.Result;
#nullable enable

#nullable enable
using System;
using RailSharp.Internal.Result;

namespace SoundSwitch.Util
{
Expand All @@ -22,7 +22,25 @@ public static class ResultTypeExtension
/// <typeparam name="TFailure"></typeparam>
/// <typeparam name="TSuccess"></typeparam>
/// <returns></returns>
public static TFailure? UnwrapFailure<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TFailure defaultValue = default)
public static TFailure? UnwrapFailure<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TFailure? defaultValue = null) where TFailure : class
{
if (result is Failure<TFailure, TSuccess> failure)
{
return failure;
}

return defaultValue;
}

/// <summary>
/// Unwrap the failure case
/// </summary>
/// <param name="result"></param>
/// <param name="defaultValue"></param>
/// <typeparam name="TFailure"></typeparam>
/// <typeparam name="TSuccess"></typeparam>
/// <returns></returns>
public static TFailure? UnwrapFailure<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TFailure? defaultValue = null) where TFailure : struct
{
if (result is Failure<TFailure, TSuccess> failure)
{
Expand Down

0 comments on commit e017bc5

Please sign in to comment.