Zonemaster/Backend/RPCAPI.pm untested

filter: Statementstmt 0.0% 0 / 434 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 0.0% 0 / 59 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 735 CC 52 SCAR 36.5
CC
52
Cov
0%
get_test_results
210.0
add_api_user
42.0
jsonrpc_validate
42.0
CRAP 38.5
1 package Zonemaster::Backend::RPCAPI;
2  
3 use 5.14.2;
4 use warnings;
5  
6 use Carp qw( croak );
7 use DBI qw( :utils );
8 use Digest::MD5 qw( md5_hex );
9 use Encode;
10 use File::Slurp qw( append_file );
11 use HTML::Entities;
12 use JSON::PP;
13 use JSON::Validator::Joi;
14 use JSON::Validator::Schema::Draft7;
15 use Locale::Messages qw( LC_MESSAGES LC_ALL );
16 use Locale::TextDomain qw( Zonemaster-Backend );
17 use Log::Any qw( $log );
18 use Mojo::JSON::Pointer;
19 use POSIX qw( setlocale );
20 use Scalar::Util qw( blessed );
21 use Zonemaster::Backend::Errors;
22 use Zonemaster::Backend::TLD_URL;
23 use Zonemaster::Backend::Translator;
24 use Zonemaster::Backend::Validator;
25 use Zonemaster::Backend;
26 use Zonemaster::Engine::Normalization qw( normalize_name trim_space );
27 use Zonemaster::Engine::Profile;
28 use Zonemaster::Engine::Recursor;
29 use Zonemaster::Engine;
30  
31 my $zm_validator = Zonemaster::Backend::Validator->new;
32 our %json_schemas;
33 my $recursor = Zonemaster::Engine::Recursor->new;
34  
35 sub joi {
36 return JSON::Validator::Joi->new;
37 }
38  
39 sub new {
40 my ( $class, %args ) = @_;
41  
42 my $config = delete $args{config}
43 or croak "Missing 'config' argument";
44  
45 my $db = delete $args{db}
46 or croak "Missing 'db' argument";
47  
48 my $profiles = delete $args{profiles}
49 or croak "Missing 'profiles' argument";
50  
51 if ( %args ) {
52 croak 'Unrecognized arguments: ' . join( ', ', sort keys %args );
53 }
54  
55 $db->assert_compatible_schema;
56  
57 my $obj = {
58 config => $config,
59 db => $db,
60 _profiles => $profiles,
61 };
62  
63 return bless $obj, $class;
64 }
65  
66 sub handle_exception {
67 my ( $exception ) = @_;
68  
69 if ( !$exception->isa( 'Zonemaster::Backend::Error' ) ) {
70 my $reason = $exception;
71 $exception = Zonemaster::Backend::Error::Internal->new( reason => $reason );
72 }
73  
74 my $log_extra = $exception->as_hash;
75 delete $log_extra->{message};
76  
77 if ( $exception->isa( 'Zonemaster::Backend::Error::Internal' ) ) {
78 $log->error( $exception->as_string, $log_extra );
79 }
80 else {
81 $log->info( $exception->as_string, $log_extra );
82 }
83  
84 die $exception->as_hash;
85 }
86  
87 $json_schemas{version_info} = joi->object->strict;
88  
89 sub version_info {
90 my ( $self ) = @_;
91  
92 my %ver;
93 eval {
94 $ver{zonemaster_ldns} = Zonemaster::LDNS->VERSION;
95 $ver{zonemaster_engine} = Zonemaster::Engine->VERSION;
96 $ver{zonemaster_backend} = Zonemaster::Backend->VERSION;
97 };
98 if ( $@ ) {
99 handle_exception( $@ );
100 }
101  
102 return \%ver;
103 }
104  
105 # Experimental
106 $json_schemas{system_versions} = $json_schemas{version_info};
107  
108 sub system_versions {
109 return version_info( @_ );
110 }
111  
112 $json_schemas{profile_names} = joi->object->strict;
113  
114 sub profile_names {
115 my ( $self ) = @_;
116  
117 my %profiles;
118 eval { %profiles = $self->{config}->PUBLIC_PROFILES };
119 if ( $@ ) {
120 handle_exception( $@ );
121 }
122  
123 return [ keys %profiles ];
124 }
125  
126 # Experimental
127 $json_schemas{conf_profiles} = $json_schemas{profile_names};
128  
129 sub conf_profiles {
130 my $result = { profiles => profile_names( @_ ) };
131 return $result;
132 }
133  
134 # Return the list of language tags supported by get_test_results(). The tags are
135 # derived from the locale tags set in the configuration file.
136 $json_schemas{get_language_tags} = joi->object->strict;
137  
138 sub get_language_tags {
139 my ( $self ) = @_;
140  
141 my @lang_tags;
142 eval {
143 my %locales = $self->{config}->LANGUAGE_locale;
144  
145 @lang_tags = sort keys %locales;
146 };
147 if ( $@ ) {
148 handle_exception( $@ );
149 }
150  
151 return \@lang_tags;
152 }
153  
154 # Experimental
155 $json_schemas{conf_languages} = $json_schemas{get_language_tags};
156  
157 sub conf_languages {
158 my $result = { languages => get_language_tags( @_ ) };
159 return $result;
160 }
161  
162 =head2 get_tld_url
163  
164 Handles the RPCAPI with the same name. All the "dirty work" is done in
165 a separate subroutine Zonemaster::Backend::TLD_URL::process in a
166 separate Perl module.
167  
168 =cut
169  
170 $json_schemas{get_tld_url} = {
171 type => 'object',
172 additionalProperties => 0,
173 required => ['domain'],
174 properties => {
175 domain => $zm_validator->domain_name
176 }
177 };
178  
179 sub get_tld_url {
180 my ( $self, $params ) = @_;
181 my $domain;
182 ( undef, $domain ) = normalize_name( trim_space( $params->{domain} ) );
183  
184 return Zonemaster::Backend::TLD_URL::process( $self, $domain );
185 }
186  
187 $json_schemas{get_host_by_name} = {
188 type => 'object',
189 additionalProperties => 0,
190 required => ['hostname'],
191 properties => {
192 hostname => $zm_validator->domain_name
193 }
194 };
195  
196 sub get_host_by_name {
197 my ( $self, $params ) = @_;
198 my @adresses;
199  
200 eval {
201 my $ns_name = $params->{hostname};
202  
203 @adresses = map {
204 { $ns_name => $_->short }
205 } $recursor->get_addresses_for( $ns_name );
206 @adresses = { $ns_name => '0.0.0.0' } if not @adresses;
207  
208 };
209 if ( $@ ) {
210 handle_exception( $@ );
211 }
212  
213 return \@adresses;
214 }
215  
216 # Experimental
217 $json_schemas{lookup_address_records} = $json_schemas{get_host_by_name};
218  
219 sub lookup_address_records {
220 my $result = { address_records => get_host_by_name( @_ ) };
221 return $result;
222 }
223  
224 $json_schemas{get_data_from_parent_zone} = {
225 type => 'object',
226 additionalProperties => 0,
227 required => ['domain'],
228 properties => {
229 domain => $zm_validator->domain_name,
230 language => $zm_validator->language_tag,
231 }
232 };
233  
234 sub get_data_from_parent_zone {
235 my ( $self, $params ) = @_;
236  
237 my $result = eval {
238 my %result;
239 my $domain = $params->{domain};
240 my ( $_errors, $normalized_domain ) = normalize_name( trim_space( $domain ) );
241  
242 my @ns_list;
243 my @ns_names;
244  
245 my $zone = Zonemaster::Engine->zone( $normalized_domain );
246 push @ns_list, { ns => $_->name->string, ip => $_->address->short } for @{ $zone->glue };
247  
248 my @ds_list;
249  
250 $zone = Zonemaster::Engine->zone( $normalized_domain );
251 my $ds_p = $zone->parent->query_one( $zone->name, 'DS', { dnssec => 1, cd => 1, recurse => 1 } );
252 if ( $ds_p ) {
253 my @ds = $ds_p->get_records( 'DS', 'answer' );
254  
255 foreach my $ds ( @ds ) {
256 next unless $ds->type eq 'DS';
257 push( @ds_list, { keytag => $ds->keytag, algorithm => $ds->algorithm, digtype => $ds->digtype, digest => $ds->hexdigest } );
258 }
259 }
260  
261 $result{ns_list} = \@ns_list;
262 $result{ds_list} = \@ds_list;
263 return \%result;
264 };
265 if ( $@ ) {
266 handle_exception( $@ );
267 }
268 elsif ( $result ) {
269 return $result;
270 }
271 }
272  
273 # Experimental
274 $json_schemas{lookup_delegation_data} = $json_schemas{get_data_from_parent_zone};
275  
276 sub lookup_delegation_data {
277 return get_data_from_parent_zone( @_ );
278 }
279  
280 $json_schemas{start_domain_test} = {
281 type => 'object',
282 additionalProperties => 0,
283 required => ['domain'],
284 properties => {
285 domain => $zm_validator->domain_name,
286 ipv4 => joi->boolean->compile,
287 ipv6 => joi->boolean->compile,
288 nameservers => {
289 type => 'array',
290 items => $zm_validator->nameserver
291 },
292 ds_info => {
293 type => 'array',
294 items => $zm_validator->ds_info
295 },
296 profile => $zm_validator->profile_name,
297 client_id => $zm_validator->client_id->compile,
298 client_version => $zm_validator->client_version->compile,
299 config => joi->string->compile,
300 priority => $zm_validator->priority->compile,
301 queue => $zm_validator->queue->compile,
302 language => $zm_validator->language_tag,
303 }
304 };
305  
306 sub start_domain_test {
307 my ( $self, $params ) = @_;
308  
309 my $result = 0;
310 eval {
311 $params->{profile} //= "default";
312 $params->{priority} //= 10;
313 $params->{queue} //= 0;
314  
315 my $profile = $self->{_profiles}{ $params->{profile} };
316 $params->{ipv4} //= $profile->get( "net.ipv4" );
317 $params->{ipv6} //= $profile->get( "net.ipv6" );
318  
319 $result = $self->{db}->create_new_test( $params->{domain}, $params, $self->{config}->ZONEMASTER_age_reuse_previous_test );
320 };
321 if ( $@ ) {
322 handle_exception( $@ );
323 }
324  
325 return $result;
326 }
327  
328 # Experimental
329 $json_schemas{job_create} = $json_schemas{start_domain_test};
330  
331 sub job_create {
332 my $result = { job_id => start_domain_test( @_ ) };
333 return $result;
334 }
335  
336 $json_schemas{test_progress} = joi->object->strict->props( #
337 test_id => $zm_validator->test_id->required
338 );
339  
340 sub test_progress {
341 my ( $self, $params ) = @_;
342  
343 my $result = 0;
344 eval {
345 my $test_id = $params->{test_id};
346 $result = $self->{db}->test_progress( $test_id );
347 };
348 if ( $@ ) {
349 handle_exception( $@ );
350 }
351  
352 return $result;
353 }
354  
355 # Experimental
356 $json_schemas{job_status} = joi->object->strict->props( #
357 job_id => $zm_validator->test_id->required
358 );
359  
360 sub job_status {
361 my ( $self, $params ) = @_;
362  
363 $params->{test_id} = delete $params->{job_id};
364  
365 my $result = { progress => $self->test_progress( $params ) };
366 return $result;
367 }
368  
369 $json_schemas{get_test_params} = joi->object->strict->props( #
370 test_id => $zm_validator->test_id->required
371 );
372  
373 sub get_test_params {
374 my ( $self, $params ) = @_;
375  
376 my $result;
377 eval {
378 my $test_id = $params->{test_id};
379  
380 $result = $self->{db}->get_test_params( $test_id );
381 };
382 if ( $@ ) {
383 handle_exception( $@ );
384 }
385  
386 return $result;
387 }
388  
389 # Experimental
390 $json_schemas{job_params} = joi->object->strict->props( #
391 job_id => $zm_validator->test_id->required
392 );
393  
394 sub job_params {
395 my ( $self, $params ) = @_;
396  
397 $params->{test_id} = delete $params->{job_id};
398  
399 return $self->get_test_params( $params );
400 }
401  
402 $json_schemas{get_test_results} = {
403 type => 'object',
404 additionalProperties => 0,
405 required => [ 'id', 'language' ],
406 properties => {
407 id => $zm_validator->test_id->required->compile,
408 language => $zm_validator->language_tag,
409 }
410 };
411  
412 sub get_test_results {
413 my ( $self, $params ) = @_;
414  
415 my $result;
416 eval {
417  
418 my $locale = $self->_get_locale( $params );
419  
420 my $translator;
421 $translator = Zonemaster::Backend::Translator->instance();
422  
423 my $previous_locale = $translator->locale;
424 if ( !$translator->locale( $locale ) ) {
425 die "Failed to set locale: $locale";
426 }
427  
428 eval { $translator->data } if $translator; # Provoke lazy loading of translation data
429  
430 my @zm_results;
431 my %testcases;
432  
433 my $test_info = $self->{db}->test_results( $params->{id} );
434 foreach my $test_res ( @{ $test_info->{results} } ) {
435 my $res;
436 if ( $test_res->{module} eq 'Nameserver' ) {
437 $res->{ns} = ( $test_res->{args}->{ns} ) ? ( $test_res->{args}->{ns} ) : ( 'All' );
438 }
439 elsif ($test_res->{module} eq 'SYSTEM'
440 && $test_res->{tag} eq 'POLICY_DISABLED'
441 && $test_res->{args}->{name} eq 'Example' )
442 {
443 next;
444 }
445  
446 $res->{module} = $test_res->{module};
447 $res->{message} = $translator->translate_tag( $test_res ) . "\n";
448 $res->{message} =~ s/,/, /isg;
449 $res->{message} =~ s/;/; /isg;
450 $res->{level} = $test_res->{level};
451 $res->{testcase} = $test_res->{testcase} // 'UNSPECIFIED';
452 $testcases{ $res->{testcase} } = $translator->test_case_description( $res->{testcase} );
453  
454 if ( $test_res->{module} eq 'SYSTEM' ) {
455 if ( $res->{message} =~ /policy\.json/ ) {
456 my ( $policy ) = ( $res->{message} =~ /\s(\/.*)$/ );
457 if ( $policy ) {
458 my $policy_description = 'DEFAULT POLICY';
459 $policy_description = 'SOME OTHER POLICY' if ( $policy =~ /some\/other\/policy\/path/ );
460 $res->{message} =~ s/$policy/$policy_description/;
461 }
462 else {
463 $res->{message} = 'UNKNOWN POLICY FORMAT';
464 }
465 }
466 elsif ( $res->{message} =~ /config\.json/ ) {
467 my ( $config ) = ( $res->{message} =~ /\s(\/.*)$/ );
468 if ( $config ) {
469 my $config_description = 'DEFAULT CONFIGURATION';
470 $config_description = 'SOME OTHER CONFIGURATION' if ( $config =~ /some\/other\/configuration\/path/ );
471 $res->{message} =~ s/$config/$config_description/;
472 }
473 else {
474 $res->{message} = 'UNKNOWN CONFIG FORMAT';
475 }
476 }
477 }
478  
479 push( @zm_results, $res );
480 }
481  
482 $result = $test_info;
483 $result->{testcase_descriptions} = \%testcases;
484 $result->{results} = \@zm_results;
485  
486 $translator->locale( $previous_locale );
487  
488 $result = $test_info;
489 $result->{results} = \@zm_results;
490 };
491 if ( $@ ) {
492 handle_exception( $@ );
493 }
494  
495 return $result;
496 }
497  
498 # Experimental
499 $json_schemas{job_results} = {
500 type => 'object',
501 additionalProperties => 0,
502 required => [ 'job_id', 'language' ],
503 properties => {
504 job_id => $zm_validator->test_id->required->compile,
505 language => $zm_validator->language_tag,
506 }
507 };
508  
509 sub job_results {
510 my ( $self, $params ) = @_;
511  
512 $params->{id} = delete $params->{job_id};
513  
514 my $result = $self->get_test_results( $params );
515  
516 return {
517 created_at => $result->{created_at},
518 job_id => $result->{hash_id},
519 results => $result->{results},
520 params => $result->{params},
521 testcase_descriptions => $result->{testcase_descriptionsd},
522 };
523 }
524  
525 $json_schemas{get_test_history} = {
526 type => 'object',
527 additionalProperties => 0,
528 required => ['frontend_params'],
529 properties => {
530 offset => joi->integer->min( 0 )->compile,
531 limit => joi->integer->min( 0 )->compile,
532 filter => joi->string->regex( '^(?:all|delegated|undelegated)$' )->compile,
533 frontend_params => {
534 type => 'object',
535 additionalProperties => 0,
536 required => ['domain'],
537 properties => {
538 domain => $zm_validator->domain_name
539 }
540 }
541 }
542 };
543  
544 sub get_test_history {
545 my ( $self, $params ) = @_;
546  
547 my $results;
548  
549 eval {
550 $params->{offset} //= 0;
551 $params->{limit} //= 200;
552 $params->{filter} //= "all";
553  
554 $results = $self->{db}->get_test_history( $params );
555 my @results = map {
556 { %$_, undelegated => $_->{undelegated} ? JSON::PP::true : JSON::PP::false }
557 } @$results;
558 $results = \@results;
559  
560 };
561 if ( $@ ) {
562 handle_exception( $@ );
563 }
564  
565 return $results;
566 }
567  
568 # Experimental
569 $json_schemas{domain_history} = {
570 type => 'object',
571 additionalProperties => 0,
572 required => ['params'],
573 properties => {
574 offset => joi->integer->min( 0 )->compile,
575 limit => joi->integer->min( 0 )->compile,
576 filter => joi->string->regex( '^(?:all|delegated|undelegated)$' )->compile,
577 params => {
578 type => 'object',
579 additionalProperties => 0,
580 required => ['domain'],
581 properties => {
582 domain => $zm_validator->domain_name
583 }
584 }
585 }
586 };
587  
588 sub domain_history {
589 my ( $self, $params ) = @_;
590  
591 $params->{frontend_params} = delete $params->{params};
592  
593 my $results = $self->get_test_history( $params );
594  
595 return {
596 history => [
597 map {
598 { #
599 job_id => $_->{id},
600 created_at => $_->{created_at},
601 overall_result => $_->{overall_result},
602 undelegated => $_->{undelegated},
603 }
604 } @$results
605 ],
606 };
607 }
608  
609 $json_schemas{add_api_user} = joi->object->strict->props(
610 username => $zm_validator->username->required,
611 api_key => $zm_validator->api_key->required,
612 );
613  
614 sub add_api_user {
615 my ( $self, $params, undef, $remote_ip ) = @_;
616  
617 my $result = 0;
618  
619 eval {
620 my $allow = 0;
621 if ( defined $remote_ip ) {
622 $allow = 1 if ( $remote_ip eq '::1' || $remote_ip eq '127.0.0.1' || $remote_ip eq '::ffff:127.0.0.1' );
623 }
624 else {
625 $allow = 1;
626 }
627  
628 if ( $allow ) {
629 $result = 1 if ( $self->{db}->add_api_user( $params->{username}, $params->{api_key} ) eq '1' );
630 }
631 else {
632 die Zonemaster::Backend::Error::PermissionDenied->new(
633 message => 'Call to "add_api_user" method not permitted from a remote IP',
634 data => { remote_ip => $remote_ip }
635 );
636 }
637 };
638 if ( $@ ) {
639 handle_exception( $@ );
640 }
641  
642 return $result;
643 }
644  
645 # Experimental
646 $json_schemas{user_create} = $json_schemas{add_api_user};
647  
648 sub user_create {
649 my $result = { success => add_api_user( @_ ) };
650 return $result;
651 }
652  
653 $json_schemas{add_batch_job} = {
654 type => 'object',
655 additionalProperties => 0,
656 required => [ 'username', 'api_key', 'domains' ],
657 properties => {
658 username => $zm_validator->username->required->compile,
659 api_key => $zm_validator->api_key->required->compile,
660 domains => {
661 type => "array",
662 additionalItems => 0,
663 items => $zm_validator->domain_name,
664 minItems => 1
665 },
666 test_params => {
667 type => 'object',
668 additionalProperties => 0,
669 properties => {
670 ipv4 => joi->boolean->compile,
671 ipv6 => joi->boolean->compile,
672 nameservers => {
673 type => 'array',
674 items => $zm_validator->nameserver
675 },
676 ds_info => {
677 type => 'array',
678 items => $zm_validator->ds_info
679 },
680 profile => $zm_validator->profile_name,
681 client_id => $zm_validator->client_id->compile,
682 client_version => $zm_validator->client_version->compile,
683 config => joi->string->compile,
684 priority => $zm_validator->priority->compile,
685 queue => $zm_validator->queue->compile,
686 }
687 }
688 }
689 };
690  
691 sub add_batch_job {
692 my ( $self, $params ) = @_;
693  
694 my $results;
695 eval {
696 $params->{test_params}{profile} //= "default";
697 $params->{test_params}{priority} //= 5;
698 $params->{test_params}{queue} //= 0;
699  
700 my $profile = $self->{_profiles}{ $params->{test_params}{profile} };
701 $params->{test_params}{ipv4} //= $profile->get( "net.ipv4" );
702 $params->{test_params}{ipv6} //= $profile->get( "net.ipv6" );
703  
704 $results = $self->{db}->add_batch_job( $params );
705 };
706 if ( $@ ) {
707 handle_exception( $@ );
708 }
709  
710 return $results;
711 }
712  
713 # Experimental
714 $json_schemas{batch_create} = {
715 type => 'object',
716 additionalProperties => 0,
717 required => [ 'username', 'api_key', 'domains' ],
718 properties => {
719 username => $zm_validator->username->required->compile,
720 api_key => $zm_validator->api_key->required->compile,
721 domains => {
722 type => "array",
723 additionalItems => 0,
724 items => $zm_validator->domain_name,
725 minItems => 1
726 },
727 job_params => {
728 type => 'object',
729 additionalProperties => 0,
730 properties => {
731 ipv4 => joi->boolean->compile,
732 ipv6 => joi->boolean->compile,
733 nameservers => {
734 type => 'array',
735 items => $zm_validator->nameserver
736 },
737 ds_info => {
738 type => 'array',
739 items => $zm_validator->ds_info
740 },
741 profile => $zm_validator->profile_name,
742 client_id => $zm_validator->client_id->compile,
743 client_version => $zm_validator->client_version->compile,
744 config => joi->string->compile,
745 priority => $zm_validator->priority->compile,
746 queue => $zm_validator->queue->compile,
747 }
748 }
749 }
750 };
751  
752 sub batch_create {
753 my ( $self, $params ) = @_;
754  
755 $params->{test_params} = delete $params->{job_params};
756  
757 my $result = { batch_id => $self->add_batch_job( $params ) };
758  
759 return $result;
760 }
761  
762 $json_schemas{batch_status} = {
763 type => 'object',
764 additionalProperties => 0,
765 required => ['batch_id'],
766 properties => {
767 batch_id => $zm_validator->batch_id->required,
768 list_waiting_tests => joi->boolean->compile,
769 list_running_tests => joi->boolean->compile,
770 list_finished_tests => joi->boolean->compile,
771 }
772 };
773  
774 sub batch_status {
775 my ( $self, $params ) = @_;
776  
777 my $result;
778 eval { $result = $self->{db}->batch_status( $params ); };
779 if ( $@ ) {
780 handle_exception( $@ );
781 }
782 return $result;
783 }
784  
785 sub _get_locale {
786 my ( $self, $params ) = @_;
787 my @error;
788  
789 if ( ref $params ne 'HASH' ) {
790 return undef;
791 }
792  
793 my $language = $params->{language};
794 if ( !defined $language ) {
795 return undef;
796 }
797  
798 my %locales = $self->{config}->LANGUAGE_locale;
799  
800 my $locale = $locales{$language};
801 if ( !defined $locale ) {
802 return undef;
803 }
804  
805 return $locale . '.UTF-8';
806 }
807  
808 sub _set_error_message_locale {
809 my ( $self, $params ) = @_;
810  
811 my @error_response = ();
812 my $locale = $self->_get_locale( $params );
813  
814 if ( not defined $locale or $locale eq "" ) {
815  
816 # Don't translate message if locale is not defined
817 $locale = "C";
818 }
819  
820 # Use POSIX implementation instead of Locale::Messages wrapper
821 setlocale( LC_ALL, $locale );
822 return @error_response;
823 }
824  
825 my $rpc_request = joi->object->props(
826 jsonrpc => joi->string->required,
827 method => $zm_validator->jsonrpc_method()->required,
828 id => joi->type( [qw(null number string)] )
829 );
830  
831 sub jsonrpc_validate {
832 my ( $self, $jsonrpc_request ) = @_;
833  
834 my @error_rpc = $rpc_request->validate( $jsonrpc_request );
835 if ( ( ref( $jsonrpc_request ) eq 'HASH' && !exists $jsonrpc_request->{id} ) || @error_rpc ) {
836 $self->_set_error_message_locale;
837 return {
838 jsonrpc => '2.0',
839 id => undef,
840 error => {
841 code => '-32600',
842 message => 'The JSON sent is not a valid request object.',
843 data => "@error_rpc"
844 }
845 };
846 }
847  
848 my $method_schema = $json_schemas{ $jsonrpc_request->{method} };
849 if ( blessed $method_schema) {
850 $method_schema = $method_schema->compile;
851 }
852  
853 # The "params" key of the JSONRPC object is optional per the JSONRPC 2.0
854 # specification, but if the method being called requires at least one
855 # parameter, omitting it is an error.
856  
857 if ( exists $method_schema->{required} and not exists $jsonrpc_request->{params} ) {
858 return {
859 jsonrpc => '2.0',
860 id => $jsonrpc_request->{id},
861 error => {
862 code => '-32602',
863 message => "Missing 'params' object",
864 }
865 };
866 }
867 elsif ( exists $jsonrpc_request->{params} ) {
868 my @error_response = $self->validate_params( $method_schema, $jsonrpc_request->{params} );
869  
870 if ( scalar @error_response ) {
871 return {
872 jsonrpc => '2.0',
873 id => $jsonrpc_request->{id},
874 error => {
875 code => '-32602',
876 message => decode_utf8( __ 'Invalid method parameter(s).' ),
877 data => \@error_response
878 }
879 };
880 }
881 }
882  
883 return '';
884 }
885  
886 sub validate_params {
887 my ( $self, $method_schema, $params ) = @_;
888 my @error_response = ();
889  
890 push @error_response, $self->_set_error_message_locale( $params );
891  
892 if ( blessed $method_schema) {
893 $method_schema = $method_schema->compile;
894 }
895 my $jv = JSON::Validator::Schema::Draft7->new->coerce( 'booleans,numbers,strings' )->data( $method_schema );
896 $jv->formats( Zonemaster::Backend::Validator::formats( $self->{config} ) );
897 my @json_validation_error = $jv->validate( $params );
898  
899 # Customize error message from json validation
900 foreach my $err ( @json_validation_error ) {
901 my $message = $err->message;
902 my @details = @{ $err->details };
903  
904 # Handle 'required' errors globally so it does not get overwritten
905 if ( $details[1] eq 'required' ) {
906 $message = N__ 'Missing property';
907 }
908 else {
909 my @path = split '/', $err->path, -1;
910 shift @path; # first item is an empty string
911 my $found = 1;
912 my $data = Mojo::JSON::Pointer->new( $method_schema );
913  
914 foreach my $p ( @path ) {
915 if ( $data->contains( "/properties/$p" ) ) {
916 $data = $data->get( "/properties/$p" );
917 }
918 elsif ( $p =~ /^\d+$/ and $data->contains( "/items" ) ) {
919 $data = $data->get( "/items" );
920 }
921 else {
922 $found = 0;
923 last;
924 }
925 $data = Mojo::JSON::Pointer->new( $data );
926 }
927  
928 if ( $found and exists $data->data->{'x-error-message'} ) {
929 $message = $data->data->{'x-error-message'};
930 }
931 }
932  
933 push @error_response, { path => $err->path, message => $message };
934  
935 }
936  
937 # Translate messages
938 @error_response = map {
939 { %$_, ( message => decode_utf8 __ $_->{message} ) }
940 } @error_response;
941  
942 return @error_response;
943 }
944  
945 1;