Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch errors when JSON is invalid #7

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
53 changes: 31 additions & 22 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,14 +20,18 @@
* SOFTWARE.
*/

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

namespace FirefoxAction
{
public class FirefoxActions
{
const string KEY_NAME = "ExtensionSettings";

[CustomAction]
public static ActionResult ExtensionSettingsInstall(Session session)
{
Expand All @@ -36,44 +40,45 @@ 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(KEY_NAME, "{}");
json[extensionSettings.UUID] = new JObject
{
["installation_mode"] = "normal_installed",
["install_url"] = extensionSettings.URL
};
firefox.SetValue("ExtensionSettings", json.ToString().Split('\n'));
firefox.SetValue(KEY_NAME, json.ToString().Split('\n'));
return ActionResult.Success;
}
}

[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(KEY_NAME);
if (json != null)
{
JObject json = JObject.Parse(value);
json[extensionSettings.UUID] = new JObject
{
["installation_mode"] = "blocked"
};
firefox.SetValue("ExtensionSettings", json.ToString().Split('\n'));
firefox.SetValue(KEY_NAME, 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 +87,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 +103,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 (!registryKey.GetValueNames().Any(name.Equals))
return defaultValue;
switch (registryKey.GetValueKind(name))
{
Expand All @@ -114,14 +116,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)
metsma marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (string value in key.GetValueNames())
string value = registryKey.GetStringValue(name, defaultValue);
if (value == null)
{
return null;
}
try
{
return JObject.Parse(value);
}
catch (JsonReaderException)
{
if (value == name)
return true;
return new JObject();
}
return false;
}
}
}
15 changes: 15 additions & 0 deletions FirefoxAction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net40</TargetFramework>
<Copyright>Copyright © 2019</Copyright>
<Company>RIA</Company>
</PropertyGroup>
<ItemGroup>
<Content Include="CustomAction.config" CopyToOutputDirectory="PreserveNewest" />
</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>
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ The custom actions must be scheduled in `InstallExecuteSequence` as follows:
<InstallExecuteSequence>
<Custom Action="SetExtensionSettingsForInstall" Before="InstallInitialize" />
<Custom Action="ExtensionSettingsInstall" Before="InstallFinalize">
NOT ((REMOVE="ALL") AND (NOT UPGRADINGPRODUCTCODE))
NOT REMOVE="ALL"
</Custom>
<Custom Action="SetExtensionSettingsForRemove" Before="InstallInitialize" />
<Custom Action="ExtensionSettingsRemove" Before="InstallFinalize">
(REMOVE="ALL") AND (NOT UPGRADINGPRODUCTCODE)
REMOVE="ALL" AND NOT UPGRADINGPRODUCTCODE
</Custom>
</InstallExecuteSequence>

Expand Down
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.

35 changes: 0 additions & 35 deletions src/Properties/AssemblyInfo.cs

This file was deleted.

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

This file was deleted.