From 995669f01cc64fe6961f48e5325193a85458ed6e Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Wed, 1 May 2024 16:11:31 +0300 Subject: [PATCH] Catch errors when JSON is invalid WE2-925 Signed-off-by: Raul Metsma --- .github/workflows/build.yml | 14 ++--- ...CustomAction.config => CustomAction.config | 0 src/FirefoxAction.cs => FirefoxAction.cs | 46 ++++++++------ FirefoxAction.csproj | 14 +++++ .../Properties => Properties}/AssemblyInfo.cs | 0 src/FirefoxAction.csproj | 63 ------------------- src/FirefoxAction.sln | 25 -------- src/packages.config | 5 -- 8 files changed, 47 insertions(+), 120 deletions(-) rename src/CustomAction.config => CustomAction.config (100%) rename src/FirefoxAction.cs => FirefoxAction.cs (80%) create mode 100644 FirefoxAction.csproj rename {src/Properties => Properties}/AssemblyInfo.cs (100%) delete mode 100644 src/FirefoxAction.csproj delete mode 100644 src/FirefoxAction.sln delete mode 100644 src/packages.config diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9429307..b7c846a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 \ No newline at end of file + path: ./bin/Release/net40/FirefoxAction.CA.dll \ No newline at end of file diff --git a/src/CustomAction.config b/CustomAction.config similarity index 100% rename from src/CustomAction.config rename to CustomAction.config diff --git a/src/FirefoxAction.cs b/FirefoxAction.cs similarity index 80% rename from src/FirefoxAction.cs rename to FirefoxAction.cs index 109c0f1..910805b 100644 --- a/src/FirefoxAction.cs +++ b/FirefoxAction.cs @@ -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 @@ -20,9 +20,10 @@ * SOFTWARE. */ -using Microsoft.Deployment.WindowsInstaller; using Microsoft.Win32; using Newtonsoft.Json.Linq; +using System; +using WixToolset.Dtf.WindowsInstaller; namespace FirefoxAction { @@ -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", @@ -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. @@ -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)) @@ -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)) { @@ -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; } } } diff --git a/FirefoxAction.csproj b/FirefoxAction.csproj new file mode 100644 index 0000000..688eff2 --- /dev/null +++ b/FirefoxAction.csproj @@ -0,0 +1,14 @@ + + + net40 + false + + + + + + + + + + \ No newline at end of file diff --git a/src/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs similarity index 100% rename from src/Properties/AssemblyInfo.cs rename to Properties/AssemblyInfo.cs diff --git a/src/FirefoxAction.csproj b/src/FirefoxAction.csproj deleted file mode 100644 index a21ea55..0000000 --- a/src/FirefoxAction.csproj +++ /dev/null @@ -1,63 +0,0 @@ - - - - Debug - x86 - 8.0.30703 - 2.0 - {95939208-FE4A-4150-8C6A-DB2B210410FF} - Library - Properties - FirefoxAction - FirefoxAction - v4.0 - 512 - Client - - - true - bin\Debug\ - DEBUG;TRACE - full - AnyCPU - prompt - MinimumRecommendedRules.ruleset - - - bin\Release\ - TRACE - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - - - - True - - - packages\Newtonsoft.Json.13.0.1\lib\net40\Newtonsoft.Json.dll - - - packages\System.ValueTuple.4.5.0\lib\portable-net40+sl4+win8+wp8\System.ValueTuple.dll - - - - - - - Designer - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/FirefoxAction.sln b/src/FirefoxAction.sln deleted file mode 100644 index a64a565..0000000 --- a/src/FirefoxAction.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.852 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirefoxAction", "FirefoxAction.csproj", "{95939208-FE4A-4150-8C6A-DB2B210410FF}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {95939208-FE4A-4150-8C6A-DB2B210410FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {95939208-FE4A-4150-8C6A-DB2B210410FF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {95939208-FE4A-4150-8C6A-DB2B210410FF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {95939208-FE4A-4150-8C6A-DB2B210410FF}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {3A40CE65-B7CD-4F33-9BA8-CC121ED01543} - EndGlobalSection -EndGlobal diff --git a/src/packages.config b/src/packages.config deleted file mode 100644 index 669a10a..0000000 --- a/src/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file