Repository navigation
Expand file tree
/
Copy pathOpenWithSourceInspection.cs
More file actions
201 lines (188 loc) · 11.5 KB
/
Copy pathOpenWithSourceInspection.cs
File metadata and controls
201 lines (188 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Security;
using System.Xml;
using System.Xml.Linq;
using ContextMenuMgr.Contracts;
using Microsoft.Win32;
using Windows.Management.Deployment;
namespace ContextMenuMgr.Backend.Services;
/// <summary>On-demand evidence for one extension. Never a mutation target or an Explorer visibility prediction.</summary>
internal static class OpenWithSourceInspection
{
private const int Limit = 4096;
internal static OpenWithAssociationInspection Inspect(string extension, BackendUserContext context,
Func<string, SpecialMenuEntry> readApplication, CancellationToken cancellationToken)
{
extension = extension.Trim().ToLowerInvariant();
if (extension.Length is < 2 or > 64 || extension[0] != '.'
|| extension.EndsWith('.') || extension.Contains("..", StringComparison.Ordinal)
|| extension[1..].Any(c => !char.IsLetterOrDigit(c) && c is not ('-' or '_' or '.')))
throw new ArgumentException("Enter a file extension such as .txt.");
OpenWithRegistration.ValidateUser(context);
var found = new List<(string Identifier, OpenWithSourceReference Reference)>();
var diagnostics = new HashSet<OpenWithInspectionDiagnostic>();
var user = $@"HKEY_USERS\{context.Sid}";
var classes = new[] { user + @"\Software\Classes", @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes" };
void Diagnostic(string code, string path = "")
{
if (diagnostics.Count < 64) diagnostics.Add(new(code, path));
}
void TryRead(string path, Action action)
{
cancellationToken.ThrowIfCancellationRequested();
try { action(); }
catch (Exception ex) when (ex is UnauthorizedAccessException or SecurityException or IOException or System.ComponentModel.Win32Exception)
{ Diagnostic("OpenWithInspectionIncomplete", path); }
}
void Add(string id, OpenWithSourceKind kind, string path, string value, bool application = false, string? applicationRoot = null)
{
cancellationToken.ThrowIfCancellationRequested();
if (found.Count >= Limit) { Diagnostic("OpenWithInspectionLimited"); return; }
var paths = new List<string>();
var manageable = new List<string>();
var applicationReference = !application && id.StartsWith(@"Applications\", StringComparison.OrdinalIgnoreCase)
&& ValidIdentifier(id[@"Applications\".Length..], application: true);
var resolution = ValidIdentifier(id, application) || applicationReference
? OpenWithSourceResolution.Unresolved : OpenWithSourceResolution.Invalid;
if (resolution != OpenWithSourceResolution.Invalid)
{
foreach (var root in classes)
{
if (applicationRoot is not null && root != applicationRoot) continue;
var target = root + "\\" + (application ? @"Applications\" : "") + id;
try
{
using var key = OpenWithRegistration.Open(target);
if (key is null) continue;
paths.Add(target);
if (application || applicationReference)
{
var item = readApplication(target);
if (item.CanEdit && item.CanDelete) manageable.Add(item.Id);
if (item.Metadata.GetValueOrDefault("ReadOnlyReason") == "OpenWithInaccessibleRegistration")
resolution = OpenWithSourceResolution.Inaccessible;
}
}
catch (Exception ex) when (ex is UnauthorizedAccessException or SecurityException or IOException or System.ComponentModel.Win32Exception)
{ resolution = OpenWithSourceResolution.Inaccessible; }
catch (InvalidOperationException)
{ resolution = OpenWithSourceResolution.Inaccessible; Diagnostic("OpenWithInspectionIncomplete", target); }
}
if (resolution != OpenWithSourceResolution.Inaccessible && paths.Count > 0)
resolution = paths.Count > 1 ? OpenWithSourceResolution.MultipleRegistrations : OpenWithSourceResolution.Resolved;
}
found.Add((id, new(kind, path, value, resolution, paths, manageable)));
}
foreach (var root in classes)
{
var progIds = root + $@"\{extension}\OpenWithProgids";
TryRead(progIds, () =>
{
using var key = OpenWithRegistration.Open(root);
var references = ContextMenuRegistryCatalog.ReadOpenWithProgIdReferences(key, extension);
if (references.Count > Limit) Diagnostic("OpenWithInspectionLimited");
foreach (var reference in references.Take(Limit))
Add(reference.ProgId, OpenWithSourceKind.ClassesProgIds, reference.SourcePath, reference.ProgId);
});
var applications = root + @"\Applications";
TryRead(applications, () =>
{
using var key = OpenWithRegistration.Open(applications);
var names = key?.GetSubKeyNames() ?? [];
if (names.Length > Limit) Diagnostic("OpenWithInspectionLimited");
foreach (var name in names.Take(Limit).Where(n => ValidIdentifier(n, true)))
{
var supported = applications + $@"\{name}\SupportedTypes";
TryRead(supported, () =>
{
using var types = OpenWithRegistration.Open(supported);
if (types?.GetValueNames().Contains(extension, StringComparer.OrdinalIgnoreCase) == true)
Add(name, OpenWithSourceKind.Applications, supported, extension, application: true, applicationRoot: root);
});
}
});
}
var fileExts = user + $@"\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{extension}";
TryRead(fileExts, () =>
{
using var ids = OpenWithRegistration.Open(fileExts);
var references = ContextMenuRegistryCatalog.ReadOpenWithProgIdReferences(ids);
if (references.Count > Limit) Diagnostic("OpenWithInspectionLimited");
foreach (var reference in references.Take(Limit))
Add(reference.ProgId, OpenWithSourceKind.ExplorerProgIds, reference.SourcePath, reference.ProgId);
using var list = OpenWithRegistration.Open(fileExts + @"\OpenWithList");
foreach (var name in (list?.GetValueNames() ?? []).Take(Limit))
{
if (name.Equals("MRUList", StringComparison.OrdinalIgnoreCase)) continue;
var app = list!.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string;
if (app is not null) Add(app, OpenWithSourceKind.ExplorerList, fileExts + @"\OpenWithList", name, application: true);
else Diagnostic("OpenWithInspectionIncomplete", fileExts + @"\OpenWithList");
}
});
foreach (var hive in new[] { user, "HKEY_LOCAL_MACHINE" })
{
var registered = hive + @"\Software\RegisteredApplications";
TryRead(registered, () =>
{
using var key = OpenWithRegistration.Open(registered);
var names = key?.GetValueNames() ?? [];
if (names.Length > Limit) Diagnostic("OpenWithInspectionLimited");
foreach (var name in names.Take(Limit))
{
var relative = key!.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string;
if (relative is null || !relative.StartsWith("Software\\", StringComparison.OrdinalIgnoreCase)
|| relative.Length > 2048 || relative.Split('\\').Any(p => p is "" or "." or "..") || relative.Contains('/'))
{ Diagnostic("OpenWithInspectionIncomplete", registered + " [" + name + "]"); continue; }
var associations = hive + "\\" + relative + @"\FileAssociations";
TryRead(associations, () =>
{
using var values = OpenWithRegistration.Open(associations);
var id = values?.GetValue(extension, null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string;
if (id is null) return;
// Keep the declaration and its indirection as separate physical evidence.
Add(id, OpenWithSourceKind.Capabilities, registered, name);
Add(id, OpenWithSourceKind.Capabilities, associations, extension);
});
}
});
}
try
{
var count = 0;
foreach (var package in new PackageManager().FindPackagesForUser(context.Sid))
{
cancellationToken.ThrowIfCancellationRequested();
if (++count > 512) { Diagnostic("OpenWithInspectionLimited"); break; }
try
{
var manifest = Path.Combine(package.InstalledLocation.Path, "AppxManifest.xml");
using var reader = XmlReader.Create(manifest, new XmlReaderSettings
{ DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null, MaxCharactersInDocument = 8 * 1024 * 1024 });
var doc = XDocument.Load(reader);
foreach (var app in doc.Descendants().Where(e => e.Name.LocalName == "Application"))
foreach (var association in app.Descendants().Where(e => e.Name.LocalName == "FileTypeAssociation"))
{
cancellationToken.ThrowIfCancellationRequested();
if (!association.Descendants().Any(e => e.Name.LocalName == "FileType" && e.Value.Trim().Equals(extension, StringComparison.OrdinalIgnoreCase))) continue;
if (found.Count >= Limit) { Diagnostic("OpenWithInspectionLimited"); break; }
var id = package.Id.FamilyName + "!" + (string?)app.Attribute("Id");
found.Add((id, new(OpenWithSourceKind.Package, manifest,
(string?)association.Attribute("Name") ?? string.Empty,
OpenWithSourceResolution.Resolved, [manifest], [])));
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{ Diagnostic("OpenWithInspectionIncomplete"); }
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{ Diagnostic("OpenWithInspectionIncomplete"); }
return new(extension, found.GroupBy(x => x.Identifier, StringComparer.OrdinalIgnoreCase)
.OrderBy(g => g.Key, StringComparer.OrdinalIgnoreCase)
.Select(g => new OpenWithAssociationCandidate(g.Key, g.Select(x => x.Reference)
.DistinctBy(r => (r.Kind, r.SourcePath.ToUpperInvariant(), r.ValueName.ToUpperInvariant())).ToArray())).ToArray(), diagnostics.ToArray());
}
private static bool ValidIdentifier(string value, bool application)
=> value.Length is > 0 and <= 255 && value == value.Trim()
&& value is not ("." or "..") && !value.Any(c => char.IsControl(c) || c is '\\' or '/' or ':')
&& (!application || value.Contains('.') && !value.EndsWith(".deleted", StringComparison.OrdinalIgnoreCase));
}