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

Added validation of count argument for search/list. #1564

Merged
merged 3 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions src/AppInstallerCLICore/Commands/ListCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
jedieaston marked this conversation as resolved.
Show resolved Hide resolved
{
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&)
jedieaston marked this conversation as resolved.
Show resolved Hide resolved
{
throw CommandException(Resource::String::CountOutOfBoundsError);
}
}
}

void ListCommand::ExecuteInternal(Execution::Context& context) const
{
context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning);
Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/ListCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
19 changes: 19 additions & 0 deletions src/AppInstallerCLICore/Commands/SearchCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/SearchCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
<value>Enables context sensitive command line completion</value>
</data>
<data name="CountArgumentDescription" xml:space="preserve">
<value>Show no more than specified number of results</value>
<value>Show no more than specified number of results (between 1 and 1000)</value>
</data>
<data name="Done" xml:space="preserve">
<value>Done</value>
Expand Down Expand Up @@ -1190,4 +1190,7 @@ Please specify one of them using the `--source` option to proceed.</value>
<data name="ImportIgnorePackageVersionsArgumentDescription" xml:space="preserve">
<value>Ignore package versions in import file</value>
</data>
</root>
<data name="CountOutOfBoundsError" xml:space="preserve">
<value>The requested number of results must be between 1 and 1000.</value>
</data>
</root>