Skip to content

Commit 98d3a34

Browse files
committed
Validate the test name when creating new jobs
* Use the same regex we already use when validating the YAML schema for job templates * See https://progress.opensuse.org/issues/177267
1 parent 8d6811c commit 98d3a34

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

lib/OpenQA/Jobs/Constants.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use Mojo::Base -base, -signatures;
66

77
use Exporter 'import';
88

9+
# define regex for validating test names in accordance with `JobScenarios-01.yaml` and `JobTemplates-01.yaml`
10+
use constant TEST_NAME_REGEX => qr/^[A-Za-z\s0-9_*.+-]+$/;
11+
912
# job states
1013
use constant {
1114
# initial job state; the job is supposed to be assigned to a worker by the scheduler
@@ -103,6 +106,7 @@ use constant DEFAULT_JOB_PRIORITY => 50;
103106
use constant TAG_ID_COLUMN => "concat(VERSION, '-', BUILD)";
104107

105108
our @EXPORT = qw(
109+
TEST_NAME_REGEX
106110
ASSIGNED
107111
CANCELLED
108112
COMPLETE_RESULTS

lib/OpenQA/Schema/ResultSet/Jobs.pm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ sub create_from_settings {
125125
die 'The ' . join(',', @invalid_keys) . " cannot include / in value\n" if @invalid_keys;
126126

127127
# validate special settings
128-
my %special_settings = (_PRIORITY => delete $settings{_PRIORITY});
128+
my %special_settings = (TEST => delete $settings{TEST}, _PRIORITY => delete $settings{_PRIORITY});
129129
my $validator = Mojolicious::Validator->new;
130130
my $v = Mojolicious::Validator::Validation->new(validator => $validator, input => \%special_settings);
131+
my $test = $v->required('TEST')->like(TEST_NAME_REGEX)->param;
131132
my $prio = $v->optional('_PRIORITY')->num->param;
132133
die 'The following settings are invalid: ' . join(', ', @{$v->failed}) . "\n" if $v->has_error;
133134

@@ -176,9 +177,9 @@ sub create_from_settings {
176177
my $value = $settings{$key};
177178
$settings{$key} = decode_utf8 encode_json $value if (ref $value eq 'ARRAY' || ref $value eq 'HASH');
178179
}
179-
$new_job_args{TEST} = $settings{TEST};
180180

181181
# move important keys from the settings directly to the job
182+
$new_job_args{TEST} = $test;
182183
for my $key (OpenQA::Schema::Result::Jobs::MAIN_SETTINGS) {
183184
if (my $value = delete $settings{$key}) { $new_job_args{$key} = $value }
184185
}

t/api/04-jobs.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,16 @@ subtest 'get job status' => sub {
825825
->json_is('/error_status' => 404, 'Status code correct')->json_is('/error' => 'Job does not exist');
826826
};
827827

828+
subtest 'validation of test name' => sub {
829+
my @disallowed = ('spam@eggs', 'spam:eggs');
830+
my @allowed = ('spam.eggs', 'spam+eggs');
831+
$t->post_ok('/api/v1/jobs', form => {TEST => $_})->status_is(400, "test name $_ disallowed") for @disallowed;
832+
$t->json_is('/error' => 'The following settings are invalid: TEST', 'error for invalid test name returned');
833+
$t->post_ok('/api/v1/jobs', form => {TEST => $_})->status_is(200, "test name $_ allowed") for @allowed;
834+
is $jobs->search({TEST => {-in => \@disallowed}})->count, 0, 'no jobs with disallowed names created';
835+
is $jobs->search({TEST => {-in => \@allowed}})->count, @allowed, 'all jobs with allowed names created';
836+
};
837+
828838
subtest 'cancel job' => sub {
829839
$t->post_ok('/api/v1/jobs/99963/cancel')->status_is(200);
830840
is_deeply(

0 commit comments

Comments
 (0)