Skip to content
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
23 changes: 23 additions & 0 deletions doc/release-notes/1.31/powershell-agreement-consent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## New Features

### PowerShell module asks for consent to source and package agreements

Microsoft.WinGet.Client now shows source and package agreements and asks for consent before
accepting them, instead of accepting them silently. Accepted source agreements are saved, so each
source asks only once until its agreements change. If you decline a source's agreements, that source
is left out of the results. If you decline a package's agreements, the package is not installed.

* `-AcceptSourceAgreements` is available on `Find-`, `Get-`, `Install-`, `Update-`, `Uninstall-`,
`Repair-` and `Export-WinGetPackage`.
* `-AcceptPackageAgreements` is available on `Install-`, `Update-`, `Repair-` and
`Export-WinGetPackage`. `Repair-WinGetPackage` can now repair packages that have agreements.
* When the host cannot prompt, such as with `-NonInteractive`, the agreements are still accepted but
are shown in a warning. This behavior is deprecated: a future release will require the switches
in non-interactive use, so update scripts and automation to pass them.
* The `WinGetPackage` resource in Microsoft.WinGet.DSC has a new `AcceptAgreements` property, which
defaults to `true` and passes both switches. The `true` default is also deprecated: a future
release will require `AcceptAgreements` to be set explicitly to accept agreements, so set it in
configurations that rely on it.

Accepting a package's agreements also accepts the agreements of any dependencies that are installed
with it.
52 changes: 10 additions & 42 deletions src/AppInstallerCLICore/Workflows/RepairFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,13 @@ namespace AppInstaller::CLI::Workflow

bool IsInstallerMappingRequired(Execution::Context& context)
{
InstallerTypeEnum installerTypeEnum = GetInstalledType(context);

switch (installerTypeEnum)
std::shared_ptr<ICompositePackage> package;
if (context.Contains(Execution::Data::Package))
{
case InstallerTypeEnum::Msi:
return false;
case InstallerTypeEnum::Msix:
// For MSIX packages that are from the Microsoft Store, selecting an installer is required.
if (context.Contains(Execution::Data::Package))
{
auto availablePackages = context.Get<Execution::Data::Package>()->GetAvailable();

if (availablePackages.size() == 1 && availablePackages[0]->GetSource() == WellKnownSource::MicrosoftStore)
{
return true;
}
}

// For MSIX packages that are not from the Microsoft Store, selecting an installer is not required.
return false;
default:
return true;
package = context.Get<Execution::Data::Package>();
}

return IsAvailableVersionRequiredForRepair(package, context.Get<Execution::Data::InstalledPackageVersion>());
}

void HandleModifyOrUninstallerRepair(Execution::Context& context, RepairBehaviorEnum repairBehavior)
Expand Down Expand Up @@ -471,33 +455,17 @@ namespace AppInstaller::CLI::Workflow
}

const auto& installedPackage = context.Get<Execution::Data::InstalledPackageVersion>();
std::string_view targetVersion = context.Args.Contains(Execution::Args::Type::TargetVersion) ? context.Args.GetArg(Execution::Args::Type::TargetVersion) : std::string_view{};
std::string_view channel = context.Args.GetArg(Execution::Args::Type::Channel);

Utility::Version installedVersion = Utility::Version(installedPackage->GetProperty(PackageVersionProperty::Version));
if (installedVersion.IsUnknown())
auto key = GetRepairVersionKey(context.Get<Execution::Data::Package>(), installedPackage, targetVersion, channel);
if (!key)
{
context.Reporter.Error() << Resource::String::NoApplicableInstallers << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_NO_APPLICABLE_INSTALLER);
}

std::string_view requestedVersion = context.Args.Contains(Execution::Args::Type::TargetVersion) ? context.Args.GetArg(Execution::Args::Type::TargetVersion) : installedVersion.ToString();
// If it's Store source with only one version unknown, use the unknown version for available version mapping.
const auto& package = context.Get<Execution::Data::Package>();
auto packageVersions = GetAvailableVersionsForInstalledVersion(package, installedPackage);
auto versionKeys = packageVersions->GetVersionKeys();
if (versionKeys.size() == 1)
{
auto packageVersion = packageVersions->GetVersion(versionKeys.at(0));
if (packageVersion->GetSource().IsWellKnownSource(WellKnownSource::MicrosoftStore) &&
Utility::Version{ packageVersion->GetProperty(PackageVersionProperty::Version) }.IsUnknown())
{
requestedVersion = "";
}
}

context <<
GetManifestWithVersionFromPackage(
requestedVersion,
context.Args.GetArg(Execution::Args::Type::Channel), false);
context << GetManifestWithVersionFromPackage(key->Version, key->Channel, false);
}

void SelectApplicableInstallerIfNecessary(Execution::Context& context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Manifest for verifying package agreement consent. Uses a unique product code so that it does not collide with other test packages.
# yaml-language-server: $schema=https://aka.ms/winget-manifest.singleton.1.5.0.schema.json

PackageIdentifier: AppInstallerTest.PackageAgreements
PackageVersion: 1.0.0.0
PackageName: TestPackageAgreements
PackageLocale: en-US
Publisher: AppInstallerTest
License: Test
ShortDescription: Test package with agreements
Installers:
- Architecture: x86
InstallerUrl: https://localhost:5001/TestKit/AppInstallerTestExeInstaller/AppInstallerTestExeInstaller.exe
InstallerType: exe
InstallerSha256: <EXEHASH>
ProductCode: '{3B911E81-F86F-41DD-8FEA-C9B43B281FB9}'
InstallerSwitches:
Custom: '/ProductID {3B911E81-F86F-41DD-8FEA-C9B43B281FB9} /DisplayName TestPackageAgreements'
SilentWithProgress: /exeswp
Silent: /exesilent
Interactive: /exeinteractive
Language: /exeenus
Log: /LogFile <LOGPATH>
InstallLocation: /InstallDir <INSTALLPATH>
Agreements:
- AgreementLabel: testAgreementLabel
Agreement: testAgreementText
AgreementUrl: https://testAgreementUrl.net
ManifestType: singleton
ManifestVersion: 1.5.0
68 changes: 68 additions & 0 deletions src/AppInstallerRepositoryCore/PackageVersionSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,72 @@ namespace AppInstaller::Repository

return std::nullopt;
}

bool IsAvailableVersionRequiredForRepair(const std::shared_ptr<ICompositePackage>& composite, const std::shared_ptr<IPackageVersion>& installedVersion)
{
std::string installedType = installedVersion->GetMetadata()[PackageVersionMetadata::InstalledType];

switch (Manifest::ConvertToInstallerTypeEnum(installedType))
{
case Manifest::InstallerTypeEnum::Msi:
return false;
case Manifest::InstallerTypeEnum::Msix:
if (composite)
{
auto availablePackages = composite->GetAvailable();
return availablePackages.size() == 1 && availablePackages[0]->GetSource().IsWellKnownSource(WellKnownSource::MicrosoftStore);
}

return false;
default:
return true;
}
}

std::optional<PackageVersionKey> GetRepairVersionKey(
const std::shared_ptr<ICompositePackage>& composite,
const std::shared_ptr<IPackageVersion>& installedVersion,
std::string_view requestedVersion,
std::string_view requestedChannel)
{
Utility::Version installed{ installedVersion->GetProperty(PackageVersionProperty::Version) };
if (installed.IsUnknown())
{
return std::nullopt;
}

std::string version{ requestedVersion.empty() ? std::string_view{ installed.ToString() } : requestedVersion };

// If it's Store source with only one version unknown, use the unknown version for available version mapping.
auto packageVersions = GetAvailableVersionsForInstalledVersion(composite, installedVersion);
auto versionKeys = packageVersions->GetVersionKeys();
if (versionKeys.size() == 1)
{
auto packageVersion = packageVersions->GetVersion(versionKeys.at(0));
if (packageVersion && packageVersion->GetSource().IsWellKnownSource(WellKnownSource::MicrosoftStore) &&
Utility::Version{ packageVersion->GetProperty(PackageVersionProperty::Version) }.IsUnknown())
{
version.clear();
}
}

return PackageVersionKey{ "", version, std::string{ requestedChannel } };
}

std::shared_ptr<IPackageVersion> GetRepairVersion(const std::shared_ptr<ICompositePackage>& composite)
{
auto installedVersion = GetInstalledVersion(composite);
if (!installedVersion || !IsAvailableVersionRequiredForRepair(composite, installedVersion))
{
return {};
}

auto key = GetRepairVersionKey(composite, installedVersion);
if (!key)
{
return {};
}

return GetAvailableVersionsForInstalledVersion(composite, installedVersion)->GetVersion(key.value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ namespace AppInstaller::Repository

// Gets the source priority for a given composite package, taking into account installed relationships.
std::optional<int32_t> GetSourcePriority(const std::shared_ptr<ICompositePackage>& composite);

// Determines whether repairing the installed version uses an available version of the package.
// MSI and MSIX packages not from the Microsoft Store are repaired by the platform without one.
// The composite may be null, such as when repairing from a manifest.
bool IsAvailableVersionRequiredForRepair(const std::shared_ptr<ICompositePackage>& composite, const std::shared_ptr<IPackageVersion>& installedVersion);

// Gets the key of the available version used to repair the installed version.
// The requested version, if not empty, is used in place of the installed version.
// Returns nullopt if the installed version is unknown, as no available version can be mapped to it.
std::optional<PackageVersionKey> GetRepairVersionKey(
const std::shared_ptr<ICompositePackage>& composite,
const std::shared_ptr<IPackageVersion>& installedVersion,
std::string_view requestedVersion = {},
std::string_view requestedChannel = {});

// Gets the available version used to repair the installed version of the package, or null if there isn't one.
std::shared_ptr<IPackageVersion> GetRepairVersion(const std::shared_ptr<ICompositePackage>& composite);
}
14 changes: 14 additions & 0 deletions src/Microsoft.Management.Deployment/CatalogPackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,18 @@ namespace winrt::Microsoft::Management::Deployment::implementation
{
return AppInstaller::Repository::GetSourcePriority(m_package);
}

Microsoft::Management::Deployment::PackageVersionInfo CatalogPackage::RepairVersion()
{
std::shared_ptr<::AppInstaller::Repository::IPackageVersion> repairVersion = ::AppInstaller::Repository::GetRepairVersion(m_package);
if (!repairVersion)
{
return nullptr;
}

auto packageVersionInfoImpl = winrt::make_self<wil::details::module_count_wrapper<
winrt::Microsoft::Management::Deployment::implementation::PackageVersionInfo>>();
packageVersionInfoImpl->Initialize(std::move(repairVersion));
return *packageVersionInfoImpl;
}
}
2 changes: 2 additions & 0 deletions src/Microsoft.Management.Deployment/CatalogPackage.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace winrt::Microsoft::Management::Deployment::implementation
winrt::Microsoft::Management::Deployment::CheckInstalledStatusResult CheckInstalledStatus();

Windows::Foundation::IReference<int32_t> CatalogPriority();
// Contract 31
winrt::Microsoft::Management::Deployment::PackageVersionInfo RepairVersion();

#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
private:
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.Management.Deployment/PackageManager.idl
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ namespace Microsoft.Management.Deployment
/// May be null if the package refers only to an installed item.
Windows.Foundation.IReference<Int32> CatalogPriority { get; };
}

[contract(Microsoft.Management.Deployment.WindowsPackageManagerContract, 31)]
{
/// Gets the available version whose manifest is used when repairing the installed version, such as
/// to present its agreements before setting RepairOptions.AcceptPackageAgreements.
/// Null if the package is not installed, the installed version has no matching available version,
/// or the repair does not use an available version (such as for MSI and non-Store MSIX packages).
PackageVersionInfo RepairVersion { get; };
}
}

/// IMPLEMENTATION NOTE: CompositeSearchBehavior from winget/RepositorySource.h
Expand Down
45 changes: 45 additions & 0 deletions src/PowerShell/CommonFiles/PowerShellCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,51 @@ internal bool ShouldProcess(string target)
return shouldProcess;
}

/// <summary>
/// Asks the user to confirm an action. Each prompt is answered on its own, so that one answer
/// cannot apply to content the user has not seen.
/// </summary>
/// <param name="query">The question to ask.</param>
/// <param name="caption">The caption of the prompt.</param>
/// <returns>The user's answer, or null if the host cannot prompt the user.</returns>
internal bool? ShouldContinue(string query, string caption)
{
if (!this.IsInteractive())
{
return null;
}

bool? result = null;
this.ExecuteInPowerShellThread(() =>
{
try
{
result = this.psCmdlet.ShouldContinue(query, caption);
}
catch (PSInvalidOperationException)
{
// The host is in non-interactive mode.
result = null;
}
catch (System.Management.Automation.Host.HostException)
{
// The host does not support prompting.
result = null;
}
});
return result;
}

/// <summary>
/// Determines whether the user can be prompted.
/// </summary>
/// <returns>True if the host can prompt the user.</returns>
internal bool IsInteractive()
{
// A host started with -NonInteractive refuses to prompt, which ShouldContinue handles.
return Environment.UserInteractive && this.psCmdlet.Host?.UI != null;
}

private void Complete()
{
this.queuedStreams.CompleteAdding();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Export-WinGetPackage [-DownloadDirectory <String>] [-AllowHashMismatch]
[-Architecture <PSProcessorArchitecture>] [-InstallerType <PSPackageInstallerType>] [-Locale <String>]
[-Scope <PSPackageInstallScope>] [-SkipDependencies] [-Version <String>] [-Id <String>] [-Name <String>]
[-Moniker <String>] [-Source <String>] [[-Query] <String[]>] [-MatchOption <PSPackageFieldMatchOption>]
[-WhatIf] [-Confirm] [<CommonParameters>]
[-AcceptPackageAgreements] [-AcceptSourceAgreements] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### GivenSet
Expand All @@ -30,7 +30,7 @@ Export-WinGetPackage [-DownloadDirectory <String>] [-AllowHashMismatch]
Export-WinGetPackage [-DownloadDirectory <String>] [-AllowHashMismatch]
[-Architecture <PSProcessorArchitecture>] [-InstallerType <PSPackageInstallerType>] [-Locale <String>]
[-Scope <PSPackageInstallScope>] [-SkipDependencies] [[-PSCatalogPackage] <PSCatalogPackage>]
[-Version <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-Version <String>] [-AcceptPackageAgreements] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -81,6 +81,45 @@ folder name is created by concatenating the package identifier and version numbe

## PARAMETERS

### -AcceptPackageAgreements

Accepts the agreements of the package that is selected. The agreements are shown as a warning.
Without this parameter, an interactive session asks you to accept or decline them; declining stops
the operation for that package. In a non-interactive session, the agreements are currently accepted
and shown as a warning. A future release will require this parameter instead.

```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```

### -AcceptSourceAgreements

Accepts the agreements of every source that is searched. When the agreements of a source have not
been accepted before, they are shown as a warning and the acceptance is recorded, so later commands
don't ask again. Without this parameter, an interactive session asks you to accept or decline them;
declined sources are excluded from the search. In a non-interactive session, the agreements are
currently accepted and shown as a warning. A future release will require this parameter instead.

```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: FoundSet
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```

### -AllowHashMismatch

Allows you to download package even when the SHA256 hash for an installer or a dependency does not
Expand Down
Loading
Loading