Zonemaster/Backend/Validator.pm

filter: Statementstmt 93.7% 30 / 32 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.3% 41 / 43 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Zonemaster/Backend/Config.pm ↑ Summary
1 package Zonemaster::Backend::Validator;
2  
3 our $VERSION = '0.1.0';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.14.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec::Functions qw( file_name_is_absolute );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON::Validator::Joi;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Locale::TextDomain qw[Zonemaster-Backend];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::IP::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Zonemaster::Engine::Logger::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Zonemaster::Engine::Normalization qw( normalize_name trim_space );
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use Zonemaster::LDNS;
18  
19 our @EXPORT_OK = qw(
20 untaint_abs_path
21 untaint_bool
22 untaint_engine_type
23 untaint_ip_address
24 untaint_ipv4_address
25 untaint_ipv6_address
26 untaint_host
27 untaint_ldh_domain
28 untaint_locale_tag
29 untaint_mariadb_database
30 untaint_mariadb_user
31 untaint_non_negative_int
32 untaint_password
33 untaint_postgresql_ident
34 untaint_profile_name
35 untaint_strictly_positive_int
36 untaint_strictly_positive_millis
37 check_domain
38 check_ip
39 check_profile
40 check_language_tag
41 );
42  
43 our %EXPORT_TAGS = (
44 untaint => [
45 qw(
46 untaint_abs_path
47 untaint_bool
48 untaint_engine_type
49 untaint_ip_address
50 untaint_ipv4_address
51 untaint_ipv6_address
52 untaint_host
53 untaint_ldh_domain
54 untaint_locale_tag
55 untaint_mariadb_database
56 untaint_mariadb_user
57 untaint_non_negative_int
58 untaint_password
59 untaint_postgresql_ident
60 untaint_profile_name
61 untaint_strictly_positive_int
62 untaint_strictly_positive_millis
63 )
64 ],
65 format => [
66 qw(
67 check_domain
68 check_ip
69 check_profile
70 check_language_tag
71 )
72 ]
73 );
74  
75 # Does not check value ranges within the groups
76 Readonly my $IPV4_RE => qr/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/;
77  
78 # Does not check the length and number of the hex groups, nor the value ranges in the IPv4 groups
79 Readonly my $IPV6_RE => qr/^[0-9a-f:]*:[0-9a-f:]+(:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})?$/i;
80  
81 Readonly my $API_KEY_RE => qr/^[a-z0-9-_]{1,512}$/i;
82 Readonly my $CLIENT_ID_RE => qr/^[a-z0-9-+~_.: ]{1,50}$/i;
83 Readonly my $CLIENT_VERSION_RE => qr/^[a-z0-9-+~_.: ]{1,50}$/i;
84 Readonly my $DIGEST_RE => qr/^[a-f0-9]{40}$|^[a-f0-9]{64}$|^[a-f0-9]{96}$/i;
85 Readonly my $ENGINE_TYPE_RE => qr/^(?:mysql|postgresql|sqlite)$/i;
86 Readonly my $IPADDR_RE => qr/^$|$IPV4_RE|$IPV6_RE/;
87 Readonly my $JSONRPC_METHOD_RE => qr/^[a-z0-9_-]*$/i;
88 Readonly my $LANGUAGE_RE => qr/^[a-z]{2}$/;
89 Readonly my $LDH_DOMAIN_RE1 => qr{^[a-z0-9_./-]{1,253}[.]?$}i;
90 Readonly my $LDH_DOMAIN_RE2 => qr{^(?:[.]|[^.]{1,63}(?:[.][^.]{1,63})*[.]?)$};
91 Readonly my $LOCALE_TAG_RE => qr/^[a-z]{2}_[A-Z]{2}$/;
92 Readonly my $MARIADB_DATABASE_LENGTH_RE => qr/^.{1,64}$/;
93  
94 # See: https://mariadb.com/kb/en/identifier-names/#unquoted
95 Readonly my $MARIADB_IDENT_RE => qr/^[0-9a-z\$_]+$/i;
96 Readonly my $MARIADB_USER_LENGTH_RE => qr/^.{1,80}$/u;
97  
98 # Up to 5 and 3 digits in the integer and fraction components respectively
99 Readonly my $MILLIS_RE => qr/^(?:0|[1-9][0-9]{0,4})(?:[.][0-9]{1,3})?$/;
100  
101 # Up to 5 digits
102 Readonly my $NON_NEGATIVE_INT_RE => qr/^(?:0|[1-9][0-9]{0,4})$/;
103  
104 # At least one non-zero digit
105 Readonly my $NON_ZERO_NUM_RE => qr/[1-9]/;
106  
107 # Printable ASCII but first character must not be space or '<'
108 Readonly my $PASSWORD_RE => qr/^(?:[\x21-\x3b\x3d-\x7e][\x20-\x7e]{0,99})?$/;
109  
110 # See: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
111 Readonly my $POSTGRESQL_IDENT_RE => qr/^[a-z_][a-z0-9_\$]{0,62}$/i;
112 Readonly my $PROFILE_NAME_RE => qr/^[a-z0-9]$|^[a-z0-9][a-z0-9_-]{0,30}[a-z0-9]$/i;
113 Readonly my $RELAXED_DOMAIN_NAME_RE => qr/^[.]$|^.{2,254}$/;
114 Readonly my $TEST_ID_RE => qr/^[0-9a-f]{16}$/;
115 Readonly my $USERNAME_RE => qr/^[a-z0-9-.@]{1,50}$/i;
116  
117 # Boolean
118 Readonly my $BOOL_TRUE_RE => qr/^(true|yes)$/i;
119 Readonly my $BOOL_FALSE_RE => qr/^(false|no)$/i;
120 Readonly my $BOOL_RE => qr/^$BOOL_TRUE_RE|$BOOL_FALSE_RE$/i;
121  
122 sub joi {
123 return JSON::Validator::Joi->new;
124 }
125  
126 sub new {
127 my ( $type ) = @_;
128  
129 my $self = {};
130 bless( $self, $type );
131  
132 return ( $self );
133 }
134  
135 sub api_key {
136 return joi->string->regex( $API_KEY_RE );
137 }
138 sub batch_id {
139 return joi->integer->positive;
140 }
141 sub client_id {
142 return joi->string->regex( $CLIENT_ID_RE );
143 }
144 sub client_version {
145 return joi->string->regex( $CLIENT_VERSION_RE );
146 }
147 sub domain_name {
148 return {
149 type => 'string',
150 format => 'domain',
151 };
152 }
153 sub ds_info {
154 return {
155 type => 'object',
156 additionalProperties => 0,
157 required => [ 'digest', 'algorithm', 'digtype', 'keytag' ],
158 properties => {
159 digest => {
160 type => 'string',
161 pattern => $DIGEST_RE,
162 'x-error-message' => N__ 'Invalid digest format'
163 },
164 algorithm => {
165 type => 'number',
166 minimum => 0,
167 'x-error-message' => N__ 'Algorithm must be a positive integer'
168 },
169 digtype => {
170 type => 'number',
171 minimum => 0,
172 'x-error-message' => N__ 'Digest type must be a positive integer'
173 },
174 keytag => {
175 type => 'number',
176 minimum => 0,
177 'x-error-message' => N__ 'Keytag must be a positive integer'
178 }
179 }
180 };
181 }
182 sub ip_address {
183 return {
184 type => 'string',
185 format => 'ip',
186 };
187 }
188 sub nameserver {
189 return {
190 type => 'object',
191 required => [ 'ns' ],
192 additionalProperties => 0,
193 properties => {
194 ns => domain_name,
195 ip => ip_address
196 }
197 };
198 }
199 sub priority {
200 return joi->integer;
201 }
202 sub profile_name {
203 return {
204 type => 'string',
205 format => 'profile',
206 };
207 }
208 sub queue {
209 return joi->integer;
210 }
211 sub test_id {
212 return joi->string->regex( $TEST_ID_RE );
213 }
214 sub language_tag {
215 return {
216 type => 'string',
217 format => 'language_tag',
218 };
219 }
220 sub username {
221 return joi->string->regex( $USERNAME_RE );
222 }
223 sub jsonrpc_method {
224 return joi->string->regex( $JSONRPC_METHOD_RE );
225 }
226  
227 =head1 FORMAT INTERFACE
228  
229 This module contains a set of procedures for validating data types.
230 The C<check_*> procedures take the value to validate and potential extra
231 arguments and return either undef if the validation succeeded or the reason of
232 the failure.
233  
234 use Zonemaster::Backend::Validator qw( :format );
235  
236 # prints "invalid value: The domain name character(s) are not supported"
237 if ( defined ( my $error = check_domain( 'not a domain' ) ) ) {
238 print "invalid value: $error\n";
239 } else {
240 print "value is valid\n";
241 }
242  
243 # prints "value is valid"
244 if ( defined ( my $error = check_domain( 'zonemaster.net' ) ) ) {
245 print "invalid value: $error\n";
246 } else {
247 print "value is valid\n";
248 }
249  
250 =cut
251  
252 =head2 formats($config)
253  
254 Returns a hashref to be used with the L<"format" method in JSON::Validator|JSON::Validator::Schema/formats>.
255 The keys are the names of the custom formats, supports: C<domain>,
256 C<language_tag>, C<ip> and C<profile>.
257  
258 The method takes a L<Config|Zonemaster::Backend::Config> object as argument.
259  
260 =cut
261  
262 sub formats {
263 my ( $config ) = @_;
264 return {
265 domain => \&check_domain,
266 language_tag => sub { check_language_tag( @_, $config->LANGUAGE_locale ) },
267 ip => \&check_ip,
268 profile => sub { check_profile( @_, ( $config->PUBLIC_PROFILES, $config->PRIVATE_PROFILES ) ) },
269 };
270 }
271  
272 =head2 check_domain(%value)
273  
274 Validates a L<domain name|https://github.com/zonemaster/zonemaster/blob/master/docs/public/using/backend/rpcapi-reference.md#domain-name>.
275  
276 =cut
277  
278 sub check_domain {
279 my ( $domain ) = @_;
280  
281 if ( !defined( $domain ) ) {
282 return N__ 'Domain name required';
283 }
284  
285 my ( $errors, $_domain ) = normalize_name( trim_space( $domain ) );
286  
287 if ( @{$errors} ) {
288 return $errors->[0]->message;
289 }
290  
291 return undef
292 }
293  
294 =head2 check_language_tag($value, %locales)
295  
296 Validates a L<https://github.com/zonemaster/zonemaster/blob/master/docs/public/using/backend/rpcapi-reference.md#language-tag>.
297  
298 =over
299  
300 =item %locales
301  
302 A hash of configured locales, as returned by L<Zonemaster::Backend::Config::LANGUAGE_locale>.
303  
304 =back
305  
306 =cut
307  
308 sub check_language_tag {
309 my ( $language, %locales ) = @_;
310  
311 my @error;
312  
313 if ( $language !~ $LANGUAGE_RE ) {
314 return N__ 'Invalid language tag format';
315 }
316 elsif ( !exists $locales{$language} ) {
317 return N__ "Unkown language string";
318 }
319  
320 return undef;
321 }
322  
323 =head2 check_ip($value)
324  
325 Validates an L<IP address|https://github.com/zonemaster/zonemaster/blob/master/docs/public/using/backend/rpcapi-reference.md#ip-address>.
326  
327 =cut
328  
329 sub check_ip {
330 my ( $ip ) = @_;
331  
332 return N__ 'Invalid IP address' unless untaint_ip_address($ip) ;
333  
334 return undef
335  
336 }
337  
338 =head2 check_profile($value, %profiles)
339  
340 Validates a L<profile name|https://github.com/zonemaster/zonemaster/blob/master/docs/public/using/backend/rpcapi-reference.md#profile-name>.
341  
342 =over
343  
344 =item %profiles
345  
346 A hash of configured profiles, as returned by L<Zonemaster::Backend::Config::PUBLIC_PROFILES>.
347  
348 =back
349  
350 =cut
351  
352 sub check_profile {
353 my ( $profile, %profiles ) = @_;
354  
355 if ( $profile !~ $PROFILE_NAME_RE ) {
356 return N__ "Invalid profile format";
357 }
358  
359 if ( !exists $profiles{ lc($profile) } ) {
360 return N__ "Unknown profile";
361 }
362 }
363  
364 =head1 UNTAINT INTERFACE
365  
366 This module contains a set of procedures for validating and untainting strings.
367  
368 use Zonemaster::Backend::Validator qw( :untaint );
369  
370 # prints "untainted: sqlite"
371 if ( defined ( my $value = untaint_engine_type( 'sqlite' ) ) ) {
372 print "untainted: $value\n";
373 }
374  
375 # does not print anything
376 if ( defined ( my $value = untaint_engine_type( 'Excel' ) ) ) {
377 print "untainted: $value\n";
378 }
379  
380 These procedures all take a possibly tainted single string argument.
381 If the string is accepted an untainted copy of the string is returned.
382  
383 =cut
384  
385 sub untaint_abs_path {
386 my ( $value ) = @_;
387 return _untaint_pred( $value, \&file_name_is_absolute );
388 }
389  
390 =head2 untaint_engine_type
391  
392 Accepts the strings C<"MySQL">, C<"PostgreSQL"> and C<"SQLite">,
393 case-insensitively.
394  
395 =cut
396  
397 sub untaint_engine_type {
398 my ( $value ) = @_;
399 return _untaint_pat( $value , $ENGINE_TYPE_RE );
400 }
401  
402 =head2 untaint_ip_address
403  
404 Accepts an IPv4 or IPv6 address.
405  
406 =cut
407  
408 sub untaint_ip_address {
409 my ( $value ) = @_;
410 return untaint_ipv4_address( $value ) // untaint_ipv6_address( $value );
411 }
412  
413 =head2 untaint_ipv4_address
414  
415 Accepts an IPv4 address.
416  
417 =cut
418  
419 sub untaint_ipv4_address {
420 my ( $value ) = @_;
421 if ( $value =~ /($IPV4_RE)/
422 && Net::IP::XS::ip_is_ipv4( $value ) )
423 {
424 return $1;
425 }
426 return;
427 }
428  
429 =head2 untaint_ipv6_address
430  
431 Accepts an IPv6 address.
432  
433 =cut
434  
435 sub untaint_ipv6_address {
436 my ( $value ) = @_;
437 if ( $value =~ /($IPV6_RE)/
438 && Net::IP::XS::ip_is_ipv6( $value ) )
439 {
440 return $1;
441 }
442 return;
443 }
444  
445 =head2 untaint_host
446  
447 Accepts an LDH domain name or an IPv4 or IPv6 address.
448  
449 =cut
450  
451 sub untaint_host {
452 my ( $value ) = @_;
453 return untaint_ldh_domain( $value ) // untaint_ip_address( $value );
454 }
455  
456 =head2 untaint_ldh_domain
457  
458 Accepts an LDH domain name.
459  
460 =cut
461  
462 sub untaint_ldh_domain {
463 my ( $value ) = @_;
464 return _untaint_pat( $value, $LDH_DOMAIN_RE1, $LDH_DOMAIN_RE2 );
465 }
466  
467 =head2 untaint_locale_tag
468  
469 Accepts a locale tag.
470  
471 =cut
472  
473 sub untaint_locale_tag {
474 my ( $value ) = @_;
475 return _untaint_pat( $value, $LOCALE_TAG_RE );
476 }
477  
478 sub untaint_mariadb_database {
479 my ( $value ) = @_;
480 return _untaint_pat( $value, $MARIADB_IDENT_RE, $MARIADB_DATABASE_LENGTH_RE );
481 }
482  
483 sub untaint_mariadb_user {
484 my ( $value ) = @_;
485 return _untaint_pat( $value, $MARIADB_IDENT_RE, $MARIADB_USER_LENGTH_RE );
486 }
487  
488 sub untaint_password {
489 my ( $value ) = @_;
490 return _untaint_pat( $value, $PASSWORD_RE );
491 }
492  
493 sub untaint_strictly_positive_int {
494 my ( $value ) = @_;
495 return _untaint_pat( $value, $NON_NEGATIVE_INT_RE, $NON_ZERO_NUM_RE );
496 }
497  
498 sub untaint_strictly_positive_millis {
499 my ( $value ) = @_;
500 return _untaint_pat( $value, $MILLIS_RE, $NON_ZERO_NUM_RE );
501 }
502  
503 sub untaint_postgresql_ident {
504 my ( $value ) = @_;
505 return _untaint_pat( $value, $POSTGRESQL_IDENT_RE );
506 }
507  
508 sub untaint_non_negative_int {
509 my ( $value ) = @_;
510 return _untaint_pat( $value, $NON_NEGATIVE_INT_RE );
511 }
512  
513 sub untaint_profile_name {
514 my ( $value ) = @_;
515 return _untaint_pat( $value, $PROFILE_NAME_RE );
516 }
517  
518 sub untaint_bool {
519 my ( $value ) = @_;
520  
521 my $ret;
522 $ret = 1 if defined _untaint_pat( $value, $BOOL_TRUE_RE );
523 $ret = 0 if defined _untaint_pat( $value, $BOOL_FALSE_RE );
524 return $ret;
525 }
526  
527 sub _untaint_pat {
528 my ( $value, @patterns ) = @_;
529  
530 for my $pattern ( @patterns ) {
531 if ( $value !~ /($pattern)/ ) {
532 return;
533 }
534 }
535  
536 $value =~ qr/(.*)/;
537 return $1;
538 }
539  
540 sub _untaint_pred {
541 my ( $value, $predicate ) = @_;
542  
543 if ( $predicate->( $value ) ) {
544 $value =~ qr/(.*)/;
545 return $1;
546 }
547 else {
548 return;
549 }
550 }
551  
552  
553  
554 1;
« Zonemaster/Backend/Config.pm ↑ Summary