Skip to content

Commit ce05f04

Browse files
authored
Fix: Handle edge case where OpenSubKey results in SecurityException (#16075)
1 parent b0d8ee2 commit ce05f04

File tree

3 files changed

+80
-32
lines changed

3 files changed

+80
-32
lines changed

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Sentry;
1414
using Sentry.Protocol;
1515
using System.IO;
16+
using System.Security;
1617
using System.Text;
1718
using Windows.ApplicationModel;
1819
using Windows.Storage;
@@ -348,15 +349,23 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
348349
/// </summary>
349350
public static bool IsAutoHideTaskbarEnabled()
350351
{
351-
const string registryKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3";
352-
const string valueName = "Settings";
352+
try
353+
{
354+
const string registryKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3";
355+
const string valueName = "Settings";
353356

354-
using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registryKey);
357+
using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registryKey);
355358

356-
var value = key?.GetValue(valueName) as byte[];
359+
var value = key?.GetValue(valueName) as byte[];
357360

358-
// The least significant bit of the 9th byte controls the auto-hide setting
359-
return value != null && ((value[8] & 0x01) == 1);
361+
// The least significant bit of the 9th byte controls the auto-hide setting
362+
return value != null && ((value[8] & 0x01) == 1);
363+
}
364+
catch (SecurityException)
365+
{
366+
// Handle edge case where OpenSubKey results in SecurityException
367+
return false;
368+
}
360369
}
361370

362371
/// <summary>

src/Files.App/Helpers/Environment/SoftwareHelpers.cs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,79 @@
22
// Licensed under the MIT License. See the LICENSE.
33

44
using Microsoft.Win32;
5-
using System.IO;
5+
using System.Security;
66

77
namespace Files.App.Helpers
88
{
99
internal static class SoftwareHelpers
1010
{
1111
private const string UninstallRegistryKey = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
1212
private const string VsRegistryKey = @"SOFTWARE\Microsoft\VisualStudio";
13-
13+
1414
private const string VsCodeName = "Microsoft Visual Studio Code";
1515

1616

1717
public static bool IsVSCodeInstalled()
1818
{
19-
return
20-
ContainsName(Registry.CurrentUser.OpenSubKey(UninstallRegistryKey), VsCodeName) ||
21-
ContainsName(Registry.LocalMachine.OpenSubKey(UninstallRegistryKey), VsCodeName);
19+
try
20+
{
21+
return
22+
ContainsName(Registry.CurrentUser.OpenSubKey(UninstallRegistryKey), VsCodeName) ||
23+
ContainsName(Registry.LocalMachine.OpenSubKey(UninstallRegistryKey), VsCodeName);
24+
}
25+
catch (SecurityException)
26+
{
27+
// Handle edge case where OpenSubKey results in SecurityException
28+
return false;
29+
}
2230
}
2331

2432
public static bool IsVSInstalled()
2533
{
26-
var key = Registry.LocalMachine.OpenSubKey(VsRegistryKey);
27-
if (key is null)
28-
return false;
34+
try
35+
{
36+
var key = Registry.LocalMachine.OpenSubKey(VsRegistryKey);
37+
if (key is null)
38+
return false;
2939

30-
key.Close();
40+
key.Close();
3141

32-
return true;
42+
return true;
43+
}
44+
catch (SecurityException)
45+
{
46+
// Handle edge case where OpenSubKey results in SecurityException
47+
return false;
48+
}
3349
}
3450

3551
private static bool ContainsName(RegistryKey? key, string find)
3652
{
3753
if (key is null)
3854
return false;
3955

40-
foreach (var subKey in key.GetSubKeyNames().Select(key.OpenSubKey))
56+
try
4157
{
42-
var displayName = subKey?.GetValue("DisplayName") as string;
43-
if (!string.IsNullOrWhiteSpace(displayName) && displayName.StartsWith(find))
58+
foreach (var subKey in key.GetSubKeyNames().Select(key.OpenSubKey))
4459
{
45-
key.Close();
60+
var displayName = subKey?.GetValue("DisplayName") as string;
61+
if (!string.IsNullOrWhiteSpace(displayName) && displayName.StartsWith(find))
62+
{
63+
key.Close();
4664

47-
return true;
65+
return true;
66+
}
4867
}
49-
}
5068

51-
key.Close();
69+
key.Close();
5270

53-
return false;
71+
return false;
72+
}
73+
catch (SecurityException)
74+
{
75+
// Handle edge case where OpenSubKey results in SecurityException
76+
return false;
77+
}
5478
}
5579
}
5680
}

src/Files.App/Utils/FileTags/FileTagsDatabase.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using Microsoft.Win32;
55
using System.Runtime.CompilerServices;
6+
using System.Security;
67
using Windows.ApplicationModel;
7-
using JsonSerializer = System.Text.Json.JsonSerializer;
88
using static Files.App.Helpers.RegistryHelpers;
99
using static Files.App.Utils.FileTags.TaggedFileRegistry;
10+
using JsonSerializer = System.Text.Json.JsonSerializer;
1011

1112
namespace Files.App.Utils.FileTags
1213
{
@@ -158,7 +159,16 @@ public IEnumerable<TaggedFile> GetAll()
158159
var list = new List<TaggedFile>();
159160

160161
if (FileTagsKey is not null)
161-
IterateKeys(list, FileTagsKey, 0);
162+
{
163+
try
164+
{
165+
IterateKeys(list, FileTagsKey, 0);
166+
}
167+
catch (SecurityException)
168+
{
169+
// Handle edge case where IterateKeys results in SecurityException
170+
}
171+
}
162172

163173
return list;
164174
}
@@ -169,7 +179,16 @@ public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
169179
var list = new List<TaggedFile>();
170180

171181
if (FileTagsKey is not null)
172-
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);
182+
{
183+
try
184+
{
185+
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);
186+
}
187+
catch (SecurityException)
188+
{
189+
// Handle edge case where IterateKeys results in SecurityException
190+
}
191+
}
173192

174193
return list;
175194
}
@@ -212,9 +231,7 @@ private void IterateKeys(List<TaggedFile> list, string path, int depth)
212231
{
213232
using var key = Registry.CurrentUser.OpenSubKey(path);
214233
if (key is null)
215-
{
216234
return;
217-
}
218235

219236
if (key.ValueCount > 0)
220237
{
@@ -225,11 +242,9 @@ private void IterateKeys(List<TaggedFile> list, string path, int depth)
225242

226243
foreach (var subKey in key.GetSubKeyNames())
227244
{
245+
// Skip FRN key
228246
if (depth == 0 && subKey == "FRN")
229-
{
230-
// Skip FRN key
231247
continue;
232-
}
233248

234249
IterateKeys(list, CombineKeys(path, subKey), depth + 1);
235250
}

0 commit comments

Comments
 (0)