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

Avoid errors in the logs when invalid job IDs are passed #5239

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions lib/OpenQA/WebAPI/Controller/API/V1/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use OpenQA::Events;
use OpenQA::Scheduler::Client;
use OpenQA::Log qw(log_error log_info);
use List::Util qw(min);
use Scalar::Util qw(looks_like_number);
use Try::Tiny;
use DBIx::Class::Timestamps 'now';
use Mojo::Asset::Memory;
Expand Down Expand Up @@ -109,9 +110,14 @@ sub list ($self) {
# we could let query_jobs do the string splitting for us, but this is
# clearer.
for my $arg (qw(state ids result)) {
next unless defined $self->param($arg);
$args{$arg}
= index($self->param($arg), ',') != -1 ? [split(',', $self->param($arg))] : $self->every_param($arg);
next unless defined(my $value = $self->param($arg));
my $values = $args{$arg} = index($value, ',') != -1 ? [split(',', $value)] : $self->every_param($arg);
if ($arg eq 'ids') {
for my $id (@$values) {
return $self->render(json => {error => 'ids must be integers'}, status => 400)
unless looks_like_number $id;
}
}
}

my $latest = $validation->param('latest');
Expand Down
2 changes: 2 additions & 0 deletions t/api/04-jobs.t
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ subtest 'multiple ids' => sub {
is(scalar(@{$t->tx->res->json->{jobs}}), 3);
$t->get_ok('/api/v1/jobs?ids=99981&ids=99963&ids=99926');
is(scalar(@{$t->tx->res->json->{jobs}}), 3);
$t->get_ok('/api/v1/jobs?ids=99981&ids=99963&ids=99926foo')->status_is(400);
$t->json_is('/error', 'ids must be integers', 'validation error for IDs');
};

subtest 'job overview' => sub {
Expand Down
Loading