Skip to content

Commit 3353300

Browse files
committed
Make SecureSettings also be Key-based
1 parent 46dcbb4 commit 3353300

8 files changed

Lines changed: 46 additions & 22 deletions

File tree

src/UniGetUI.Core.SecureSettings/SecureSettings.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
using System.Diagnostics;
22
using UniGetUI.Core.Data;
33
using UniGetUI.Core.Tools;
4+
using YamlDotNet.Core.Tokens;
45
using YamlDotNet.Serialization;
56

67
namespace UniGetUI.Core.SettingsEngine.SecureSettings;
78

89
public static class SecureSettings
910
{
1011
// Various predefined secure settings keys
11-
public const string ALLOW_CLI_ARGUMENTS = "AllowCLIArguments";
12-
public const string ALLOW_IMPORTING_CLI_ARGUMENTS = "AllowImportingCLIArguments";
13-
public const string ALLOW_PREPOST_OPERATIONS = "AllowPrePostInstallCommands";
14-
public const string ALLOW_IMPORT_PREPOST_OPERATIONS = "AllowImportingPrePostInstallCommands";
15-
public const string FORCE_USER_GSUDO = "ForceUserGSudo";
12+
public enum K
13+
{
14+
AllowCLIArguments,
15+
AllowImportingCLIArguments,
16+
AllowPrePostOpCommand,
17+
AllowImportPrePostOpCommands,
18+
ForceUserGSudo,
19+
Unset
20+
};
21+
22+
public static string ResolveKey(K key)
23+
{
24+
return key switch
25+
{
26+
K.AllowCLIArguments => "AllowCLIArguments",
27+
K.AllowImportingCLIArguments => "AllowImportingCLIArguments",
28+
K.AllowPrePostOpCommand => "AllowPrePostInstallCommands",
29+
K.AllowImportPrePostOpCommands => "AllowImportingPrePostInstallCommands",
30+
K.ForceUserGSudo => "ForceUserGSudo",
31+
32+
K.Unset => throw new InvalidDataException("SecureSettings key was unset!"),
33+
_ => throw new KeyNotFoundException($"The SecureSettings key {key} was not found on the ResolveKey map")
34+
};
35+
}
1636

1737

1838
private static readonly Dictionary<string, bool> _cache = new();
@@ -23,9 +43,9 @@ public static class Args
2343
public const string DISABLE_FOR_USER = "--disable-secure-setting-for-user";
2444
}
2545

26-
public static bool Get(string setting)
46+
public static bool Get(K key)
2747
{
28-
string purifiedSetting = CoreTools.MakeValidFileName(setting);
48+
string purifiedSetting = CoreTools.MakeValidFileName(ResolveKey(key));
2949
if (_cache.TryGetValue(purifiedSetting, out var value))
3050
{
3151
return value;
@@ -48,9 +68,9 @@ public static bool Get(string setting)
4868
return exists;
4969
}
5070

51-
public static async Task<bool> TrySet(string setting, bool enabled)
71+
public static async Task<bool> TrySet(K key, bool enabled)
5272
{
53-
string purifiedSetting = CoreTools.MakeValidFileName(setting);
73+
string purifiedSetting = CoreTools.MakeValidFileName(ResolveKey(key));
5474
_cache.Remove(purifiedSetting);
5575

5676
string purifiedUser = CoreTools.MakeValidFileName(Environment.UserName);

src/UniGetUI.PackageEngine.PackageManagerClasses/Packages/Classes/InstallOptionsFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static InstallOptions _loadFromDisk(string key)
186186

187187
private static InstallOptions EnsureSecureOptions(InstallOptions options)
188188
{
189-
if (SecureSettings.Get(SecureSettings.ALLOW_CLI_ARGUMENTS))
189+
if (SecureSettings.Get(SecureSettings.K.AllowCLIArguments))
190190
{
191191
// If CLI arguments are allowed, sanitize them
192192
for (int i = 0; i < options.CustomParameters_Install.Count; i++)

src/UniGetUI/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private static async void LoadGSudo()
9999
Logger.Warn($"Using bundled GSudo at {CoreData.ElevatorPath} since UniGetUI Elevator is not available!");
100100
CoreData.ElevatorPath = (await CoreTools.WhichAsync("gsudo.exe")).Item2;
101101
#else
102-
if (SecureSettings.Get(SecureSettings.FORCE_USER_GSUDO))
102+
if (SecureSettings.Get(SecureSettings.ForceUserGSudo))
103103
{
104104
var res = await CoreTools.WhichAsync("gsudo.exe");
105105
if (res.Item1)

src/UniGetUI/CLIHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ public static int EnableSecureSetting()
256256
return (int)HRESULT.STATUS_INVALID_PARAMETER; // The file parameter does not exist (export settings requires "--export-settings file")
257257

258258
var setting = args[basePos + 1].Trim('"').Trim('\'');
259+
if (!Enum.TryParse(setting, out SecureSettings.K validKey))
260+
return (int)HRESULT.STATUS_UNKNOWN__SETTINGS_KEY;
259261

260262
try
261263
{
262-
bool success = SecureSettings.TrySet(setting, true).GetAwaiter().GetResult();
264+
bool success = SecureSettings.TrySet(validKey, true).GetAwaiter().GetResult();
263265
if (!success) return (int)HRESULT.STATUS_FAILED;
264266
else return (int)HRESULT.SUCCESS;
265267
}
@@ -281,10 +283,12 @@ public static int DisableSecureSetting()
281283
return (int)HRESULT.STATUS_INVALID_PARAMETER; // The first positional argument does not exist
282284

283285
var setting = args[basePos + 1].Trim('"').Trim('\'');
286+
if (!Enum.TryParse(setting, out SecureSettings.K validKey))
287+
return (int)HRESULT.STATUS_UNKNOWN__SETTINGS_KEY;
284288

285289
try
286290
{
287-
bool success = SecureSettings.TrySet(setting, false).GetAwaiter().GetResult();
291+
bool success = SecureSettings.TrySet(validKey, false).GetAwaiter().GetResult();
288292
if (!success) return (int)HRESULT.STATUS_FAILED;
289293
else return (int)HRESULT.SUCCESS;
290294
}

src/UniGetUI/Controls/SettingsWidgets/SecureCheckboxCard.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ public partial class SecureCheckboxCard : SettingsCard
1717
public TextBlock _textblock;
1818
public TextBlock _warningBlock;
1919
public ProgressRing _loading;
20-
protected bool IS_INVERTED;
20+
private bool IS_INVERTED;
2121

22-
protected string setting_name = "";
23-
public virtual string SettingName
22+
private SecureSettings.K setting_name = SecureSettings.K.Unset;
23+
public SecureSettings.K SettingName
2424
{
2525
set
2626
{
2727
_checkbox.IsEnabled = false;
2828
setting_name = value;
29-
IS_INVERTED = value.StartsWith("Disable");
29+
IS_INVERTED = SecureSettings.ResolveKey(value).StartsWith("Disable");
3030
_checkbox.IsOn = SecureSettings.Get(setting_name) ^ IS_INVERTED ^ ForceInversion;
3131
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
3232
_checkbox.IsEnabled = true;

src/UniGetUI/Pages/DialogPages/InstallOptions_Manager.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private async Task LoadOptions()
139139
CustomInstallLocation.Text = CoreTools.Translate("Install location can't be changed for {0} packages", Manager.DisplayName);
140140
}
141141

142-
bool IsCLIEnabled = SecureSettings.Get(SecureSettings.ALLOW_CLI_ARGUMENTS);
142+
bool IsCLIEnabled = SecureSettings.Get(SecureSettings.K.AllowCLIArguments);
143143
CustomParameters1.IsEnabled = IsCLIEnabled;
144144
CustomParameters2.IsEnabled = IsCLIEnabled;
145145
CustomParameters3.IsEnabled = IsCLIEnabled;

src/UniGetUI/Pages/DialogPages/InstallOptions_Package.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async Task LoadImage()
158158
if (Options.CustomInstallLocation == "") CustomInstallLocation.Text = packageInstallLocation;
159159
else CustomInstallLocation.Text = Options.CustomInstallLocation;
160160

161-
161+
162162
CustomParameters1.Text = string.Join(' ', Options.CustomParameters_Install);
163163
CustomParameters2.Text = string.Join(' ', Options.CustomParameters_Update);
164164
CustomParameters3.Text = string.Join(' ', Options.CustomParameters_Uninstall);
@@ -200,7 +200,7 @@ private void EnableDisableControls(OperationType operation)
200200
SelectDir.IsEnabled = Package.Manager.Capabilities.SupportsCustomLocations;
201201
}
202202

203-
bool IsCLIEnabled = SecureSettings.Get(SecureSettings.ALLOW_CLI_ARGUMENTS);
203+
bool IsCLIEnabled = SecureSettings.Get(SecureSettings.K.AllowCLIArguments);
204204
CustomParameters1.IsEnabled = IsCLIEnabled;
205205
CustomParameters2.IsEnabled = IsCLIEnabled;
206206
CustomParameters3.IsEnabled = IsCLIEnabled;

src/UniGetUI/Pages/SoftwarePages/PackageBundlesPage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ public async Task<double> AddFromBundle(string content, BundleFormatType format)
662662
bool showReport = false;
663663
var packageReport = new Dictionary<string, List<BundleReportEntry>>();
664664
bool AllowCLIParameters =
665-
SecureSettings.Get(SecureSettings.ALLOW_CLI_ARGUMENTS) &&
666-
SecureSettings.Get(SecureSettings.ALLOW_IMPORTING_CLI_ARGUMENTS);
665+
SecureSettings.Get(SecureSettings.K.AllowCLIArguments) &&
666+
SecureSettings.Get(SecureSettings.K.AllowImportingCLIArguments);
667667

668668

669669
foreach (var pkg in DeserializedData.packages)

0 commit comments

Comments
 (0)