Skip to content

Commit

Permalink
disallow Install-PSResource with -Repository with wildcard and nonwil…
Browse files Browse the repository at this point in the history
…dcard entries (#1362)
  • Loading branch information
anamnavi authored Aug 14, 2023
1 parent bb4ddf5 commit bd1780d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
76 changes: 75 additions & 1 deletion src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,86 @@ private List<PSResourceInfo> ProcessRepositories(
bool skipDependencyCheck,
ScopeType scope)
{
List<PSResourceInfo> allPkgsInstalled = new List<PSResourceInfo>();
if (repository != null && repository.Length != 0)
{
// Write error and disregard repository entries containing wildcards.
repository = Utils.ProcessNameWildcards(repository, removeWildcardEntries:false, out string[] errorMsgs, out _);
foreach (string error in errorMsgs)
{
_cmdletPassedIn.WriteError(new ErrorRecord(
new PSInvalidOperationException(error),
"ErrorFilteringNamesForUnsupportedWildcards",
ErrorCategory.InvalidArgument,
this));
}

// If repository entries includes wildcards and non-wildcard names, write terminating error
// Ex: -Repository *Gallery, localRepo
bool containsWildcard = false;
bool containsNonWildcard = false;
foreach (string repoName in repository)
{
if (repoName.Contains("*"))
{
containsWildcard = true;
}
else
{
containsNonWildcard = true;
}
}

if (containsNonWildcard && containsWildcard)
{
string message = "Repository name with wildcard is not allowed when another repository without wildcard is specified.";
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(message),
"RepositoryNamesWithWildcardsAndNonWildcardUnsupported",
ErrorCategory.InvalidArgument,
this));
}
}

// Get repositories to search.
List<PSRepositoryInfo> repositoriesToSearch;
try
{
repositoriesToSearch = RepositorySettings.Read(repository, out string[] errorList);
if (repositoriesToSearch != null && repositoriesToSearch.Count == 0)
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSArgumentException ("Cannot resolve -Repository name. Run 'Get-PSResourceRepository' to view all registered repositories."),
"RepositoryNameIsNotResolved",
ErrorCategory.InvalidArgument,
this));
}

foreach (string error in errorList)
{
_cmdletPassedIn.WriteError(new ErrorRecord(
new PSInvalidOperationException(error),
"ErrorRetrievingSpecifiedRepository",
ErrorCategory.InvalidOperation,
this));
}
}
catch (Exception e)
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(e.Message),
"ErrorLoadingRepositoryStoreFile",
ErrorCategory.InvalidArgument,
this));

return allPkgsInstalled;
}

var listOfRepositories = RepositorySettings.Read(repository, out string[] _);
var yesToAll = false;
var noToAll = false;

var findHelper = new FindHelper(_cancellationToken, _cmdletPassedIn, _networkCredential);
List<PSResourceInfo> allPkgsInstalled = new List<PSResourceInfo>();
bool sourceTrusted = false;

// Loop through all the repositories provided (in priority order) until there no more packages to install.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ Describe 'Test Install-PSResource for searching and looping through repositories
$pkg2.Name | Should -Be $testModuleName
$pkg2.Repository | Should -Be $NuGetGalleryName
}

It "should not allow for repository name with wildcard and non-wildcard name specified in same command run" {
{Install-PSResource -Name $testModuleName -Repository "*Gallery",$localRepoName} | Should -Throw -ErrorId "RepositoryNamesWithWildcardsAndNonWildcardUnsupported,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
}

0 comments on commit bd1780d

Please sign in to comment.