From f067ba7a9f91758bb84dff8efc51836018f09d72 Mon Sep 17 00:00:00 2001 From: Easton Pillay Date: Fri, 8 Oct 2021 11:38:45 -0400 Subject: [PATCH 1/3] Added validation of count argument for search/list. --- .../Commands/ListCommand.cpp | 19 +++++++++++++++++++ .../Commands/ListCommand.h | 1 + .../Commands/SearchCommand.cpp | 19 +++++++++++++++++++ .../Commands/SearchCommand.h | 1 + src/AppInstallerCLICore/Resources.h | 1 + .../Shared/Strings/en-us/winget.resw | 5 ++++- 6 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/AppInstallerCLICore/Commands/ListCommand.cpp b/src/AppInstallerCLICore/Commands/ListCommand.cpp index 536dd78010..62071f71ed 100644 --- a/src/AppInstallerCLICore/Commands/ListCommand.cpp +++ b/src/AppInstallerCLICore/Commands/ListCommand.cpp @@ -68,6 +68,25 @@ namespace AppInstaller::CLI return "https://aka.ms/winget-command-list"; } + void ListCommand::ValidateArgumentsInternal(Execution::Args& execArgs) const + { + if (execArgs.Contains(Execution::Args::Type::Count)) + { + try + { + int countRequested = std::stoi(std::string(execArgs.GetArg(Execution::Args::Type::Count))); + if (countRequested < 1 || countRequested > 1000) + { + throw CommandException(Resource::String::CountOutOfBoundsError); + } + } + catch (const std::out_of_range&) + { + throw CommandException(Resource::String::CountOutOfBoundsError); + } + } + } + void ListCommand::ExecuteInternal(Execution::Context& context) const { context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning); diff --git a/src/AppInstallerCLICore/Commands/ListCommand.h b/src/AppInstallerCLICore/Commands/ListCommand.h index 8e0e22b6e2..b83316953c 100644 --- a/src/AppInstallerCLICore/Commands/ListCommand.h +++ b/src/AppInstallerCLICore/Commands/ListCommand.h @@ -20,6 +20,7 @@ namespace AppInstaller::CLI std::string HelpLink() const override; protected: + void ValidateArgumentsInternal(Execution::Args& execArgs) const override; void ExecuteInternal(Execution::Context& context) const override; }; } diff --git a/src/AppInstallerCLICore/Commands/SearchCommand.cpp b/src/AppInstallerCLICore/Commands/SearchCommand.cpp index 96c7d3d1ba..459e8eadd4 100644 --- a/src/AppInstallerCLICore/Commands/SearchCommand.cpp +++ b/src/AppInstallerCLICore/Commands/SearchCommand.cpp @@ -66,6 +66,25 @@ namespace AppInstaller::CLI return "https://aka.ms/winget-command-search"; } + void SearchCommand::ValidateArgumentsInternal(Args& execArgs) const + { + if (execArgs.Contains(Execution::Args::Type::Count)) + { + try + { + int countRequested = std::stoi(std::string(execArgs.GetArg(Execution::Args::Type::Count))); + if (countRequested < 1 || countRequested > 1000) + { + throw CommandException(Resource::String::CountOutOfBoundsError); + } + } + catch (const std::out_of_range&) + { + throw CommandException(Resource::String::CountOutOfBoundsError); + } + } + } + void SearchCommand::ExecuteInternal(Context& context) const { context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning); diff --git a/src/AppInstallerCLICore/Commands/SearchCommand.h b/src/AppInstallerCLICore/Commands/SearchCommand.h index be91775cb9..e9cbdc90e2 100644 --- a/src/AppInstallerCLICore/Commands/SearchCommand.h +++ b/src/AppInstallerCLICore/Commands/SearchCommand.h @@ -19,6 +19,7 @@ namespace AppInstaller::CLI std::string HelpLink() const override; protected: + void ValidateArgumentsInternal(Execution::Args& execArgs) const override; void ExecuteInternal(Execution::Context& context) const override; }; } diff --git a/src/AppInstallerCLICore/Resources.h b/src/AppInstallerCLICore/Resources.h index a006b24259..84d3c907ca 100644 --- a/src/AppInstallerCLICore/Resources.h +++ b/src/AppInstallerCLICore/Resources.h @@ -43,6 +43,7 @@ namespace AppInstaller::CLI::Resource WINGET_DEFINE_RESOURCE_STRINGID(CompleteCommandLongDescription); WINGET_DEFINE_RESOURCE_STRINGID(CompleteCommandShortDescription); WINGET_DEFINE_RESOURCE_STRINGID(CountArgumentDescription); + WINGET_DEFINE_RESOURCE_STRINGID(CountOutOfBoundsError); WINGET_DEFINE_RESOURCE_STRINGID(DisabledByGroupPolicy); WINGET_DEFINE_RESOURCE_STRINGID(Done); WINGET_DEFINE_RESOURCE_STRINGID(ExactArgumentDescription); diff --git a/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw b/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw index df63107237..cefd19e9c2 100644 --- a/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw +++ b/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw @@ -167,7 +167,7 @@ Enables context sensitive command line completion - Show no more than specified number of results + Show no more than specified number of results (between 1 and 1000). Done @@ -1190,4 +1190,7 @@ Please specify one of them using the `--source` option to proceed. Ignore package versions in import file + + The requested number of results must be between 1 and 1000. + \ No newline at end of file From 42be5e197bcd3741ea81729dd6c3cfe32ad52020 Mon Sep 17 00:00:00 2001 From: Easton Pillay Date: Fri, 8 Oct 2021 11:03:32 -0500 Subject: [PATCH 2/3] Remove out-of-place period. --- src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw b/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw index cefd19e9c2..afdfabd198 100644 --- a/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw +++ b/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw @@ -167,7 +167,7 @@ Enables context sensitive command line completion - Show no more than specified number of results (between 1 and 1000). + Show no more than specified number of results (between 1 and 1000) Done @@ -1193,4 +1193,4 @@ Please specify one of them using the `--source` option to proceed. The requested number of results must be between 1 and 1000. - \ No newline at end of file + From f68beccda461e0e0273bd606aec6318c458d7da2 Mon Sep 17 00:00:00 2001 From: Easton Pillay Date: Fri, 8 Oct 2021 16:47:09 -0400 Subject: [PATCH 3/3] Moved logic to Command::ValidateArguments, generalized catch. --- src/AppInstallerCLICore/Command.cpp | 16 ++++++++++++++++ .../Commands/ListCommand.cpp | 19 ------------------- .../Commands/ListCommand.h | 1 - .../Commands/SearchCommand.cpp | 19 ------------------- .../Commands/SearchCommand.h | 1 - 5 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/AppInstallerCLICore/Command.cpp b/src/AppInstallerCLICore/Command.cpp index d0988c10aa..8d7eb8b719 100644 --- a/src/AppInstallerCLICore/Command.cpp +++ b/src/AppInstallerCLICore/Command.cpp @@ -675,6 +675,22 @@ namespace AppInstaller::CLI throw CommandException(Resource::String::HeaderArgumentNotApplicableWithoutSource, Argument::ForType(Execution::Args::Type::CustomHeader).Name()); } + if (execArgs.Contains(Execution::Args::Type::Count)) + { + try + { + int countRequested = std::stoi(std::string(execArgs.GetArg(Execution::Args::Type::Count))); + if (countRequested < 1 || countRequested > 1000) + { + throw CommandException(Resource::String::CountOutOfBoundsError); + } + } + catch (...) + { + throw CommandException(Resource::String::CountOutOfBoundsError); + } + } + ValidateArgumentsInternal(execArgs); } diff --git a/src/AppInstallerCLICore/Commands/ListCommand.cpp b/src/AppInstallerCLICore/Commands/ListCommand.cpp index 62071f71ed..536dd78010 100644 --- a/src/AppInstallerCLICore/Commands/ListCommand.cpp +++ b/src/AppInstallerCLICore/Commands/ListCommand.cpp @@ -68,25 +68,6 @@ namespace AppInstaller::CLI return "https://aka.ms/winget-command-list"; } - void ListCommand::ValidateArgumentsInternal(Execution::Args& execArgs) const - { - if (execArgs.Contains(Execution::Args::Type::Count)) - { - try - { - int countRequested = std::stoi(std::string(execArgs.GetArg(Execution::Args::Type::Count))); - if (countRequested < 1 || countRequested > 1000) - { - throw CommandException(Resource::String::CountOutOfBoundsError); - } - } - catch (const std::out_of_range&) - { - throw CommandException(Resource::String::CountOutOfBoundsError); - } - } - } - void ListCommand::ExecuteInternal(Execution::Context& context) const { context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning); diff --git a/src/AppInstallerCLICore/Commands/ListCommand.h b/src/AppInstallerCLICore/Commands/ListCommand.h index b83316953c..8e0e22b6e2 100644 --- a/src/AppInstallerCLICore/Commands/ListCommand.h +++ b/src/AppInstallerCLICore/Commands/ListCommand.h @@ -20,7 +20,6 @@ namespace AppInstaller::CLI std::string HelpLink() const override; protected: - void ValidateArgumentsInternal(Execution::Args& execArgs) const override; void ExecuteInternal(Execution::Context& context) const override; }; } diff --git a/src/AppInstallerCLICore/Commands/SearchCommand.cpp b/src/AppInstallerCLICore/Commands/SearchCommand.cpp index 459e8eadd4..96c7d3d1ba 100644 --- a/src/AppInstallerCLICore/Commands/SearchCommand.cpp +++ b/src/AppInstallerCLICore/Commands/SearchCommand.cpp @@ -66,25 +66,6 @@ namespace AppInstaller::CLI return "https://aka.ms/winget-command-search"; } - void SearchCommand::ValidateArgumentsInternal(Args& execArgs) const - { - if (execArgs.Contains(Execution::Args::Type::Count)) - { - try - { - int countRequested = std::stoi(std::string(execArgs.GetArg(Execution::Args::Type::Count))); - if (countRequested < 1 || countRequested > 1000) - { - throw CommandException(Resource::String::CountOutOfBoundsError); - } - } - catch (const std::out_of_range&) - { - throw CommandException(Resource::String::CountOutOfBoundsError); - } - } - } - void SearchCommand::ExecuteInternal(Context& context) const { context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning); diff --git a/src/AppInstallerCLICore/Commands/SearchCommand.h b/src/AppInstallerCLICore/Commands/SearchCommand.h index e9cbdc90e2..be91775cb9 100644 --- a/src/AppInstallerCLICore/Commands/SearchCommand.h +++ b/src/AppInstallerCLICore/Commands/SearchCommand.h @@ -19,7 +19,6 @@ namespace AppInstaller::CLI std::string HelpLink() const override; protected: - void ValidateArgumentsInternal(Execution::Args& execArgs) const override; void ExecuteInternal(Execution::Context& context) const override; }; }