Skip to content
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

Fix for Source Argument Validation in SourceWorkflow for Default Source Type #4891

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
12 changes: 12 additions & 0 deletions src/AppInstallerCLICore/Workflows/SourceFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ namespace AppInstaller::CLI::Workflow
std::string_view arg = context.Args.GetArg(Args::Type::SourceArg);
std::string_view type = context.Args.GetArg(Args::Type::SourceType);

// In the absence of a specified type, the default is Microsoft.PreIndexed.Package for comparison.
// The default type assignment to the source takes place during the add operation (Source::Add in Repository.cpp).
// This is necessary for the comparison to function correctly; otherwise, it would allow the addition of multiple
// sources with different names but the same argument for all default type cases.
// For example, the following commands would be allowed, but they acts as different alias to same source:
// winget source add "mysource1" "https:\\mysource" --trust - level trusted
// winget source add "mysource2" "https:\\mysource" --trust - level trusted
if (type.empty())
{
type = Repository::Source::GetDefaultSourceType();
}

for (const auto& details : sourceList)
{
if (Utility::ICUCaseInsensitiveEquals(details.Name, name))
Expand Down
18 changes: 18 additions & 0 deletions src/AppInstallerCLIE2ETests/SourceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public void SourceAdd()
[Test]
public void SourceAddWithTrustLevel()
{
// Remove the test source.
TestCommon.RunAICLICommand("source remove", Constants.TestSourceName);

var result = TestCommon.RunAICLICommand("source add", $"SourceTest {Constants.TestSourceUrl} --trust-level trusted");
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(result.StdOut.Contains("Done"));
Expand All @@ -62,6 +65,9 @@ public void SourceAddWithTrustLevel()
[Test]
public void SourceAddWithStoreOriginTrustLevel()
{
// Remove the test source.
TestCommon.RunAICLICommand("source remove", Constants.TestSourceName);

var result = TestCommon.RunAICLICommand("source add", $"SourceTest {Constants.TestSourceUrl} --trust-level storeOrigin");
Assert.AreEqual(Constants.ErrorCode.ERROR_SOURCE_DATA_INTEGRITY_FAILURE, result.ExitCode);
Assert.True(result.StdOut.Contains("The source data is corrupted or tampered"));
Expand Down Expand Up @@ -103,6 +109,18 @@ public void SourceAddWithDuplicateName()
Assert.True(result.StdOut.Contains("A source with the given name already exists and refers to a different location"));
}

/// <summary>
/// Test source add with duplicate source url.
/// </summary>
[Test]
public void SourceAddWithDuplicateSourceUrl()
{
// Add source with duplicate url should fail
var result = TestCommon.RunAICLICommand("source add", $"TestSource2 {Constants.TestSourceUrl}");
Assert.AreEqual(Constants.ErrorCode.ERROR_SOURCE_ARG_ALREADY_EXISTS, result.ExitCode);
Assert.True(result.StdOut.Contains("A source with a different name already refers to this location"));
}

/// <summary>
/// Test source add with invalid url.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ namespace AppInstaller::Repository
// Get a list of all available SourceDetails.
static std::vector<SourceDetails> GetCurrentSources();

// Get a default source type is the source type used when adding a source without specifying a type.
static std::string_view GetDefaultSourceType();

private:
void InitializeSourceReference(std::string_view name);

Expand Down
7 changes: 6 additions & 1 deletion src/AppInstallerRepositoryCore/RepositorySource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ namespace AppInstaller::Repository
// AddSourceForDetails will also check for empty, but we need the actual type before that for validation.
if (sourceDetails.Type.empty())
{
sourceDetails.Type = ISourceFactory::GetForType("")->TypeName();
sourceDetails.Type = GetDefaultSourceType();
}

AICLI_LOG(Repo, Info, << "Adding source: Name[" << sourceDetails.Name << "], Type[" << sourceDetails.Type << "], Arg[" << sourceDetails.Arg << "]");
Expand Down Expand Up @@ -1040,6 +1040,11 @@ namespace AppInstaller::Repository
}
}

std::string_view Source::GetDefaultSourceType()
{
return ISourceFactory::GetForType("")->TypeName();
}

#ifndef AICLI_DISABLE_TEST_HOOKS
void TestHook_SetSourceFactoryOverride(const std::string& type, std::function<std::unique_ptr<ISourceFactory>()>&& factory)
{
Expand Down
Loading