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