Skip to content

Commit f5bf086

Browse files
committed
(#2410) Warn on unbalanced quotes in package arguments for upgrades
Add detection of unbalanced single or double quotes in package arguments during install, upgrade, and uninstall commands. Emit warnings to alert users that failures may be related to these potentially problematic arguments. For upgrades and uninstalls, include troubleshooting advice to run `choco info` with local-only flag. Add a test verifying upgrade behavior when package parameters contain single quotes and remembered arguments are enabled, ensuring warnings and diagnostic messages appear as expected. NOTE: Install and uninstall warnings and diagnostics are for future implementations, and will not be shown during normal execution today.
1 parent c01febc commit f5bf086

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/chocolatey/infrastructure.app/services/NugetService.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,41 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
19801980
}
19811981

19821982
var originalConfig = config.DeepCopy();
1983+
1984+
if (CountCharacter(packageArgumentsUnencrypted, '\'') % 2 != 0 || CountCharacter(packageArgumentsUnencrypted, '"') % 2 != 0)
1985+
{
1986+
if (config.CommandName.IsEqualTo("install"))
1987+
{
1988+
// Install normally do not have any remembered arguments,
1989+
// but we add this for future warnings if it becomes supported.
1990+
this.Log().Warn(@"Potentially problematic package or install arguments detected.
1991+
Install failures may be related to this issue.
1992+
");
1993+
}
1994+
else if (config.CommandName.IsEqualTo("upgrade"))
1995+
{
1996+
this.Log().Warn(@"Potentially problematic package or install arguments detected.
1997+
Upgrade failures may be related to this issue.
1998+
1999+
To troubleshoot, run: `choco info {0} --local-only` and review the package
2000+
and argument details.
2001+
",
2002+
config.PackageNames);
2003+
}
2004+
else if (config.CommandName.IsEqualTo("uninstall"))
2005+
{
2006+
// Remembered arguments are not used during uninstallations,
2007+
// but we add it here for future warnings if it becomes added.
2008+
this.Log().Warn(@"Potentially problematic package or uninstall arguments detected.
2009+
Uninstall failures may be related to this issue.
2010+
2011+
To troubleshoot, run: `choco info {0} --local-only` and review the package
2012+
and argument details.
2013+
",
2014+
config.PackageNames);
2015+
}
2016+
}
2017+
19832018
// this changes config globally
19842019
ConfigurationOptions.OptionSet.Parse(packageArguments);
19852020

@@ -2007,6 +2042,26 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
20072042
return originalConfig;
20082043
}
20092044

2045+
private int CountCharacter(string value, char character)
2046+
{
2047+
if (string.IsNullOrWhiteSpace(value))
2048+
{
2049+
return 0;
2050+
}
2051+
2052+
var characterCount = 0;
2053+
2054+
for (var i = 0; i < value.Length; i++)
2055+
{
2056+
if (value[i] == character)
2057+
{
2058+
characterCount++;
2059+
}
2060+
}
2061+
2062+
return characterCount;
2063+
}
2064+
20102065
private bool HasMissingDependency(PackageResult package, List<PackageResult> allLocalPackages)
20112066
{
20122067
foreach (var dependency in package.PackageMetadata.DependencyGroups.SelectMany(d => d.Packages))

tests/pester-tests/commands/choco-upgrade.Tests.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,29 @@ To upgrade a local, or remote file, you may use:
834834
}
835835
}
836836

837+
Context 'Upgrading a package using a single quote in the parameters and remembering arguments' -Tag Arguments {
838+
BeforeAll {
839+
Restore-ChocolateyInstallSnapshot
840+
841+
Enable-ChocolateyFeature -Name "useRememberedArgumentsForUpgrades"
842+
843+
$null = Invoke-Choco install test-environment --package-parameters="/Comment:It's Great! /SubmittedBy:Kim" --version 0.9
844+
$Output = Invoke-Choco upgrade test-environment
845+
}
846+
847+
It "Exits successfully (0)" {
848+
$Output.ExitCode | Should -Be 0 -Because $Output.String
849+
}
850+
851+
It "Should warn about possible upgrade failure" {
852+
$Output.Lines | Should -Contain "Upgrade failures may be related to this issue." -Because $Output.String
853+
}
854+
855+
It "Should give diagnostic information" {
856+
$Output.Lines | Should -Contain "To troubleshoot, run: ``choco info test-environment --local-only`` and review the package" -Because $Output.String
857+
}
858+
}
859+
837860
Context 'Upgrading a package using double-dash arguments in package arguments' -Tag Arguments {
838861
BeforeAll {
839862
Restore-ChocolateyInstallSnapshot

0 commit comments

Comments
 (0)