Skip to content

Commit

Permalink
Fix GetFullNameFromFamilyName for non-elevated context (#2922)
Browse files Browse the repository at this point in the history
  • Loading branch information
yao-msft authored Feb 6, 2023
1 parent 88bc528 commit 665ddd9
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/AppInstallerCommonCore/MsixInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerStrings.h"
#include "Public/AppInstallerDownloader.h"
#include "Public/AppInstallerRuntime.h"

using namespace winrt::Windows::Storage::Streams;
using namespace Microsoft::WRL;
Expand Down Expand Up @@ -367,21 +368,63 @@ namespace AppInstaller::Msix
PackageManager packageManager;

std::wstring pfn = Utility::ConvertToUTF16(familyName);
auto packages = packageManager.FindPackages(pfn);

std::optional<std::string> result;
for (const auto& package : packages)
// PackageManager.FindPackages() can find all packages (including provisioned ones) but requires admin.
// For non admin callers, use FindPackagesByPackageFamily where only packages registered to current user will be found.
if (Runtime::IsRunningAsAdmin())
{
if (result.has_value())
auto packages = packageManager.FindPackages(pfn);

std::optional<std::string> result;
for (const auto& package : packages)
{
// More than 1 package found. Don't directly error, let caller deal with it.
return {};
if (result.has_value())
{
// More than 1 package found. Don't directly error, let caller deal with it.
AICLI_LOG(Core, Error, << "Multiple packages found for family name: " << familyName);
return {};
}

result = Utility::ConvertToUTF8(package.Id().FullName());
}

result = Utility::ConvertToUTF8(package.Id().FullName());
return result;
}
else
{
UINT32 fullNameCount = 0;
UINT32 bufferLength = 0;
UINT32 properties = 0;
LONG findResult = FindPackagesByPackageFamily(pfn.c_str(), PACKAGE_FILTER_HEAD, &fullNameCount, nullptr, &bufferLength, nullptr, &properties);
if (findResult == ERROR_SUCCESS || fullNameCount == 0)
{
// No package found
return {};
}
else if (findResult != ERROR_INSUFFICIENT_BUFFER)
{
THROW_WIN32(findResult);
}
else if (fullNameCount != 1)
{
// Don't directly error, let caller deal with it
AICLI_LOG(Core, Error, << "Multiple packages found for family name: " << fullNameCount);
return {};
}

return result;
// fullNameCount == 1 at this point
PWSTR fullNamePtr;
std::wstring buffer(static_cast<size_t>(bufferLength) + 1, '\0');
THROW_IF_WIN32_ERROR(FindPackagesByPackageFamily(pfn.c_str(), PACKAGE_FILTER_HEAD, &fullNameCount, &fullNamePtr, &bufferLength, &buffer[0], &properties));
if (fullNameCount != 1 || bufferLength == 0)
{
// Something changed in between, abandon
AICLI_LOG(Core, Error, << "Packages found for family name: " << fullNameCount);
return {};
}
buffer.resize(bufferLength - 1);
return Utility::ConvertToUTF8(buffer);
}
}

std::string GetPackageFamilyNameFromFullName(std::string_view fullName)
Expand Down

0 comments on commit 665ddd9

Please sign in to comment.