Skip to content

Commit ff7f4cf

Browse files
committed
(#3503, #3513) Incorporate review feedback
- Use OrdinalIgnoreCase for command name comparison. - Log with Warn or Error where appropriate and emit to log file only as needed. - Use explicitly named parameters instead of true/false. - Remove comment that's no longer valid.
1 parent e53eb35 commit ff7f4cf

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ it is possible that incomplete package lists are returned from a command
223223
// This is done by default on the `info` command, and by request on the `list` command. As such, we are going to validate it's that scenario
224224
// to avoid needlessly decrypting the arguments file.
225225
var shouldDecryptArguments = (
226-
config.CommandName.Equals("info", StringComparison.InvariantCultureIgnoreCase) ||
227-
config.CommandName.Equals("list", StringComparison.InvariantCultureIgnoreCase)
226+
config.CommandName.Equals("info", StringComparison.OrdinalIgnoreCase) ||
227+
config.CommandName.Equals("list", StringComparison.OrdinalIgnoreCase)
228228
) &&
229229
config.Verbose &&
230230
!string.IsNullOrWhiteSpace(packageInfo.Arguments);
@@ -359,16 +359,12 @@ Package url{6}
359359
var failedPackages = string.Join(", ", decryptionFailures.Select(f => "{0} - {1}".FormatWith(f.Package.Id, f.Package.Version)));
360360
var failureMessage = "There were some failures decrypting package arguments.";
361361
var failedPackagesMessage = "Failed packages: {0}".FormatWith(failedPackages);
362-
if (config.RegularOutput)
363-
{
364-
this.Log().Warn(failureMessage);
365-
this.Log().Warn(failedPackagesMessage);
366-
}
367-
else
368-
{
369-
this.Log().Debug(failureMessage);
370-
this.Log().Debug(failedPackagesMessage);
371-
}
362+
this.Log().Warn(config.RegularOutput ?
363+
ChocolateyLoggers.Normal :
364+
ChocolateyLoggers.LogFileOnly, failureMessage);
365+
this.Log().Warn(config.RegularOutput ?
366+
ChocolateyLoggers.Normal :
367+
ChocolateyLoggers.LogFileOnly, failedPackagesMessage);
372368
}
373369
}
374370

src/chocolatey/infrastructure.app/utility/ArgumentsUtility.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static bool SensitiveArgumentsProvided(string commandArguments)
5555

5656
public static IEnumerable<string> DecryptPackageArgumentsFile(IFileSystem fileSystem, string id, string version)
5757
{
58-
return DecryptPackageArgumentsFile(fileSystem, id, version, true, false);
58+
return DecryptPackageArgumentsFile(fileSystem, id, version, redactSensitiveArguments: true, throwOnFailure: false);
5959
}
6060

6161
public static IEnumerable<string> DecryptPackageArgumentsFile(
@@ -93,8 +93,6 @@ public static IEnumerable<string> DecryptPackageArgumentsFile(
9393

9494
try
9595
{
96-
// The following code is borrowed from the Chocolatey codebase, should
97-
// be extracted to a separate location in choco executable so we can re-use it.
9896
packageArgumentsUnencrypted = arguments.Contains(" --") && arguments.ToStringSafe().Length > 4
9997
? arguments
10098
: NugetEncryptionUtility.DecryptString(arguments);
@@ -103,17 +101,18 @@ public static IEnumerable<string> DecryptPackageArgumentsFile(
103101
catch (Exception ex)
104102
{
105103
var firstMessage = "There was an error attempting to decrypt the contents of the .arguments file for version '{0}' of package '{1}'. See log file for more information.".FormatWith(version, id);
106-
var secondMessage = "We failed to decrypt {0}. Error from decryption: {1}".FormatWith(argumentsFile, ex.Message);
107-
104+
var secondMessage = "We failed to decrypt '{0}'. Error from decryption:{1} '{2}'".FormatWith(argumentsFile, Environment.NewLine, ex.Message);
105+
"chocolatey".Log().Error(throwOnFailure ?
106+
logging.ChocolateyLoggers.Normal :
107+
logging.ChocolateyLoggers.LogFileOnly, firstMessage);
108+
"chocolatey".Log().Error(throwOnFailure ?
109+
logging.ChocolateyLoggers.Normal :
110+
logging.ChocolateyLoggers.LogFileOnly, secondMessage);
111+
108112
if (throwOnFailure)
109113
{
110-
"chocolatey".Log().Error(firstMessage);
111-
"chocolatey".Log().Error(secondMessage);
112114
throw;
113115
}
114-
115-
"chocolatey".Log().Debug(firstMessage);
116-
"chocolatey".Log().Debug(secondMessage);
117116
}
118117

119118
// Lets do a global check first to see if there are any sensitive arguments

tests/pester-tests/features/ArgumentsDecryption.Tests.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@
7777
@{
7878
ErrorType = 'Base64 invalid'
7979
DecryptionError = 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.'
80+
# `!` is an invalid Base64 character: https://en.wikipedia.org/wiki/Base64#Base64_table_from_RFC_4648
8081
FileContents = 'InvalidBase64!'
8182
}
8283
@{
8384
ErrorType = 'Invalid decryption'
8485
DecryptionError = 'Key not valid for use in specified state.'
85-
# The contents of this was taken from a throw away VM.
86+
# The contents of this was taken from a throw away VM. As such, DPAPI will not be able to decrypt it, and will error.
8687
FileContents = 'AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAn1/taDnOFUqGb17fBymxHQQAAAACAAAAAAAQZgAAAAEAACAAAAAU8gmqznJYKdkuj8bgk8sgg6Le3sbGoGkZOV3YtRFfwwAAAAAOgAAAAAIAACAAAAD1I9LYxrEhx9m71eF3VqyAike+XJTePhDAcrOilAFjQlAAAAA8lfiMR5Ns/AntLdVR3eBQSduCnipRCbdu/er/+YABMTzJDMGqnXuIsKwWoNIhrB14Yit4jVPipt3a/Nx18xx+YsnUewI4P6GlDL5do1y8mkAAAABMxvyPgCtN36BwAOXvJghIh9Hs8jUZOJtQIlWci8BnJkBmaaoSZ6pTGULk4TbFXMf/FK1NPo2mPM0YVL8QgJyK'
8788
}
8889
) {
@@ -110,7 +111,8 @@
110111

111112
It 'Outputs expected messages' {
112113
$shouldContain = -not ($CommandDescription -eq 'verbose' -or $CommandDescription -eq 'remember')
113-
$Output.Lines | Should -Not:$shouldContain -Contain "We failed to decrypt $($env:ChocolateyInstall)\.chocolatey\upgradepackage.1.0.0\.arguments. Error from decryption: $DecryptionError" -Because $Output.String
114+
$Output.Lines | Should -Not:$shouldContain -Contain "We failed to decrypt '$($env:ChocolateyInstall)\.chocolatey\upgradepackage.1.0.0\.arguments'. Error from decryption:" -Because $Output.String
115+
$Output.Lines | Should -Not:$shouldContain -Contain "'$DecryptionError'" -Because $Output.String
114116
}
115117
}
116118
}

0 commit comments

Comments
 (0)