Skip to content

Commit

Permalink
Catch errors when JSON is invalid
Browse files Browse the repository at this point in the history
WE2-925

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma committed May 1, 2024
1 parent 1605cf5 commit 0bc7e73
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ jobs:
build:
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
- uses: microsoft/setup-msbuild@v1.1
- uses: NuGet/setup-nuget@v1
- uses: actions/checkout@v4
- uses: microsoft/setup-msbuild@v2
- uses: NuGet/setup-nuget@v2
- run: nuget restore src/FirefoxAction.sln
- run: msbuild src/FirefoxAction.sln -t:rebuild -property:Configuration=Release
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: dll
path: ./src/bin/Release/FirefoxAction.CA.dll
50 changes: 28 additions & 22 deletions src/FirefoxAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using System;

namespace FirefoxAction
{
Expand All @@ -36,12 +37,11 @@ public static ActionResult ExtensionSettingsInstall(Session session)
// with error status 1603, but when letting exceptions through the raw exception
// stack trace will be logged in installer log which gives more information.

var extensionSettings = GetExtensionSettingsFromSession(session);
var extensionSettings = session.GetExtensionSettings();
session.Log("Begin ExtensionSettingsInstall " + extensionSettings.UUID);
using (RegistryKey firefox = Utils.FirefoxKey())
{
string value = firefox.GetStringValue("ExtensionSettings", "{}");
JObject json = JObject.Parse(value);
var json = firefox.GetJSON("ExtensionSettings", "{}");
json[extensionSettings.UUID] = new JObject
{
["installation_mode"] = "normal_installed",
Expand All @@ -54,26 +54,28 @@ public static ActionResult ExtensionSettingsInstall(Session session)

[CustomAction]
public static ActionResult ExtensionSettingsRemove(Session session)
{
var extensionSettings = GetExtensionSettingsFromSession(session);
{
var extensionSettings = session.GetExtensionSettings();
session.Log("Begin ExtensionSettingsRemove " + extensionSettings.UUID);
using (RegistryKey firefox = Utils.FirefoxKey())
{
string value = firefox.GetStringValue("ExtensionSettings");
if (value != null)
var json = firefox.GetJSON("ExtensionSettings");
if (json != null)
{
JObject json = JObject.Parse(value);
json[extensionSettings.UUID] = new JObject
{
["installation_mode"] = "blocked"
};
firefox.SetValue("ExtensionSettings", json.ToString().Split('\n'));
}
return ActionResult.Success;
}
return ActionResult.Success;
}
}

private static (string UUID, string URL) GetExtensionSettingsFromSession(Session session)
internal static class Utils
{
internal static (string UUID, string URL) GetExtensionSettings(this Session session)
{
// Deferred custom actions cannot directly access installer properties from session,
// only the CustomActionData property is available, see README how to populate it.
Expand All @@ -82,10 +84,7 @@ private static (string UUID, string URL) GetExtensionSettingsFromSession(Session
session.CustomActionData["EXTENSIONSETTINGS_URL"]
);
}
}

internal static class Utils
{
internal static RegistryKey FirefoxKey()
{
using (RegistryKey mozilla = Registry.LocalMachine.OpenOrCreateSubKey(@"Software\Policies\Mozilla", true))
Expand All @@ -101,7 +100,7 @@ internal static RegistryKey OpenOrCreateSubKey(this RegistryKey registryKey, str

internal static string GetStringValue(this RegistryKey registryKey, string name, string defaultValue = null)
{
if (!registryKey.ContainsName(name))
if (Array.IndexOf(registryKey.GetValueNames(), name) < 0)
return defaultValue;
switch (registryKey.GetValueKind(name))
{
Expand All @@ -114,14 +113,21 @@ internal static string GetStringValue(this RegistryKey registryKey, string name,
}
}

internal static bool ContainsName(this RegistryKey key, string name)
{
foreach (string value in key.GetValueNames())
{
if (value == name)
return true;
}
return false;
internal static JObject GetJSON(this RegistryKey registryKey, string name, string defaultValue = null)
{
string value = registryKey.GetStringValue(name, defaultValue);
if (value == null)
{
return null;
}
try
{
return JObject.Parse(value);
}
catch
{
return new JObject();
}
}
}
}

0 comments on commit 0bc7e73

Please sign in to comment.