Skip to content

Winget COM API - Allow lower trust registration #2714

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

Merged
merged 3 commits into from
Sep 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public NativeWinGetHelper()

if (CoreTools.IsAdministrator())
{
Logger.Info("Running elevated, WinGet class registration is likely to fail");
Logger.Info("Running elevated, WinGet class registration is likely to fail unless using lower trust class registration is allowed in settings");
}

Factory = new WindowsPackageManagerStandardFactory();
Factory = new WindowsPackageManagerStandardFactory(allowLowerTrustRegistration:Settings.Get("AllowLowerTrustRegistration"));
WinGetManager = Factory.CreatePackageManager();
}

Expand Down
9 changes: 9 additions & 0 deletions src/UniGetUI/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public SettingsInterface()
CoreTools.LaunchBatchFile(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Utilities", "reset_winget_sources.cmd"), CoreTools.Translate("Resetting Winget sources - WingetUI"), RunAsAdmin: true);
};

CheckboxCard Winget_AllowLowerTrustRegistration = new()
{
Text = CoreTools.Translate("Allow using class registered with lower trust"),
SettingName = "AllowLowerTrustRegistration",
};
Winget_AllowLowerTrustRegistration.StateChanged += (s, e) => PackageManagerExpanders[PEInterface.WinGet].ShowRestartRequiredBanner();
Winget_AllowLowerTrustRegistration.IsEnabled = !Settings.Get("ForceLegacyBundledWinGet");

CheckboxCard Winget_DisableCOM = new()
{
Text = CoreTools.Translate("Use the WinGet PowerShell Module instead of the WinGet COM API"),
Expand All @@ -133,6 +141,7 @@ public SettingsInterface()
};

ExtraSettingsCards[PEInterface.WinGet].Add(Winget_UseBundled);
ExtraSettingsCards[PEInterface.WinGet].Add(Winget_AllowLowerTrustRegistration);
ExtraSettingsCards[PEInterface.WinGet].Add(Winget_DisableCOM);
ExtraSettingsCards[PEInterface.WinGet].Add(Winget_ResetSources);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Management.Deployment;
Expand All @@ -13,10 +13,12 @@ namespace WindowsPackageManager.Interop;
public abstract class WindowsPackageManagerFactory
{
private readonly ClsidContext _clsidContext;
protected readonly bool _allowLowerTrustRegistration;

public WindowsPackageManagerFactory(ClsidContext clsidContext)
public WindowsPackageManagerFactory(ClsidContext clsidContext, bool allowLowerTrustRegistration = false)
{
_clsidContext = clsidContext;
_allowLowerTrustRegistration = allowLowerTrustRegistration;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace WindowsPackageManager.Interop;

public class WindowsPackageManagerStandardFactory : WindowsPackageManagerFactory
{
public WindowsPackageManagerStandardFactory(ClsidContext clsidContext = ClsidContext.Prod)
: base(clsidContext)
public WindowsPackageManagerStandardFactory(ClsidContext clsidContext = ClsidContext.Prod, bool allowLowerTrustRegistration = false)
: base(clsidContext, allowLowerTrustRegistration)
{
}

Expand All @@ -20,12 +20,19 @@ protected override T CreateInstance<T>(Guid clsid, Guid iid)
nint pUnknown = IntPtr.Zero;
try
{
Windows.Win32.Foundation.HRESULT hr = PInvoke.CoCreateInstance(clsid, null, CLSCTX.CLSCTX_LOCAL_SERVER, iid, out object result);
CLSCTX clsctx = CLSCTX.CLSCTX_LOCAL_SERVER;
if(_allowLowerTrustRegistration)
{
clsctx |= CLSCTX.CLSCTX_ALLOW_LOWER_TRUST_REGISTRATION;
}

Windows.Win32.Foundation.HRESULT hr = PInvoke.CoCreateInstance(clsid, null, clsctx, iid, out object result);

// !! WARNING !!
// An exception may be thrown on the line below if UniGetUI
// runs as administrator or when WinGet is not installed on the
// system. It can be safely ignored if any of the conditions
// runs as administrator and AllowLowerTrustRegistration settings is not checked
// or when WinGet is not installed on the system.
// It can be safely ignored if any of the conditions
// above are met.
Marshal.ThrowExceptionForHR(hr);

Expand Down
Loading