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 995669f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 120 deletions.
14 changes: 7 additions & 7 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
- run: nuget restore src/FirefoxAction.sln
- run: msbuild src/FirefoxAction.sln -t:rebuild -property:Configuration=Release
- uses: actions/upload-artifact@v3
- uses: actions/checkout@v4
- uses: microsoft/setup-msbuild@v2
- uses: NuGet/setup-nuget@v2
- run: nuget restore FirefoxAction.csproj
- run: msbuild FirefoxAction.csproj -t:rebuild -property:Configuration=Release
- uses: actions/upload-artifact@v4
with:
name: dll
path: ./src/bin/Release/FirefoxAction.CA.dll
path: ./bin/Release/net40/FirefoxAction.CA.dll
File renamed without changes.
46 changes: 26 additions & 20 deletions src/FirefoxAction.cs → FirefoxAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 Estonian Information System Authority
* Copyright (c) 2019-2024 Estonian Information System Authority
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,9 +20,10 @@
* SOFTWARE.
*/

using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using System;
using WixToolset.Dtf.WindowsInstaller;

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 @@ -55,25 +55,27 @@ 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)
internal static JObject GetJSON(this RegistryKey registryKey, string name, string defaultValue = null)
{
foreach (string value in key.GetValueNames())
string value = registryKey.GetStringValue(name, defaultValue);
if (value == null)
{
return null;
}
try
{
return JObject.Parse(value);
}
catch
{
if (value == name)
return true;
return new JObject();
}
return false;
}
}
}
14 changes: 14 additions & 0 deletions FirefoxAction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net40</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<Content Include="CustomAction.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="WixToolset.Dtf.CustomAction" Version="4.0.5" />
</ItemGroup>
</Project>
File renamed without changes.
63 changes: 0 additions & 63 deletions src/FirefoxAction.csproj

This file was deleted.

25 changes: 0 additions & 25 deletions src/FirefoxAction.sln

This file was deleted.

5 changes: 0 additions & 5 deletions src/packages.config

This file was deleted.

0 comments on commit 995669f

Please sign in to comment.