/usr/share/perl5/Data/Validate/URI.pm is in libdata-validate-uri-perl 0.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | package Data::Validate::URI;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
require Exporter;
use AutoLoader 'AUTOLOAD';
use Data::Validate::Domain;
use Data::Validate::IP;
@ISA = qw(Exporter);
# no functions are exported by default. See EXPORT_OK
@EXPORT = qw();
@EXPORT_OK = qw(
is_uri
is_http_uri
is_https_uri
is_web_uri
is_tel_uri
);
%EXPORT_TAGS = ();
$VERSION = '0.06';
# No preloads
1;
__END__
=head1 NAME
Data::Validate::URI - common url validation methods
=head1 SYNOPSIS
use Data::Validate::URI qw(is_uri);
if(is_uri($suspect)){
print "Looks like an URI\n";
} else {
print "Not a URI\n";
}
# or as an object
my $v = Data::Validate::URI->new();
die "not a URI" unless ($v->is_uri('foo'));
=head1 DESCRIPTION
This module collects common URI validation routines to make input validation,
and untainting easier and more readable.
All functions return an untainted value if the test passes, and undef if
it fails. This means that you should always check for a defined status explicitly.
Don't assume the return will be true.
The value to test is always the first (and often only) argument.
There are a number of other URI validation modules out there as well (see below.)
This one focuses on being fast, lightweight, and relatively 'real-world'. i.e.
it's good if you want to check user input, and don't need to parse out the URI/URL
into chunks.
Right now the module focuses on HTTP URIs, since they're arguably the most common.
If you have a specialized scheme you'd like to have supported, let me know.
=head1 FUNCTIONS
=cut
# -------------------------------------------------------------------------------
=pod
=over 4
=item B<new> - constructor for OO usage
new();
=over 4
=item I<Description>
Returns a Data::Validator::URI object. This lets you access all the validator function
calls as methods without importing them into your namespace or using the clumsy
Data::Validate::URI::function_name() format.
=item I<Arguments>
None
=item I<Returns>
Returns a Data::Validate::URI object
=back
=cut
sub new{
my $class = shift;
return bless {}, $class;
}
# -------------------------------------------------------------------------------
=pod
=item B<is_uri> - is the value a well-formed uri?
is_uri($value);
=over 4
=item I<Description>
Returns the untainted URI if the test value appears to be well-formed. Note that
you may really want one of the more practical methods like is_http_uri or is_https_uri,
since the URI standard (RFC 3986) allows a lot of things you probably don't want.
=item I<Arguments>
=over 4
=item $value
The potential URI to test.
=back
=item I<Returns>
Returns the untainted URI on success, undef on failure.
=item I<Notes, Exceptions, & Bugs>
This function does not make any attempt to check whether the URI is accessible
or 'makes sense' in any meaningful way. It just checks that it is formatted
correctly.
=back
=cut
sub is_uri{
my $self = shift if ref($_[0]);
my $value = shift;
return unless defined($value);
# check for illegal characters
return if $value =~ /[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i;
# check for hex escapes that aren't complete
return if $value =~ /%[^0-9a-f]/i;
return if $value =~ /%[0-9a-f](:?[^0-9a-f]|$)/i;
# from RFC 3986
my($scheme, $authority, $path, $query, $fragment) = _split_uri($value);
# scheme and path are required, though the path can be empty
return unless (defined($scheme) && length($scheme) && defined($path));
# if authority is present, the path must be empty or begin with a /
if(defined($authority) && length($authority)){
return unless(length($path) == 0 || $path =~ m!^/!);
} else {
# if authority is not present, the path must not start with //
return if $path =~ m!^//!;
}
# scheme must begin with a letter, then consist of letters, digits, +, ., or -
return unless lc($scheme) =~ m!^[a-z][a-z0-9\+\-\.]*$!;
# re-assemble the URL per section 5.3 in RFC 3986
my $out = $scheme . ':';
if(defined $authority && length($authority)){
$out .= '//' . $authority;
}
$out .= $path;
if(defined $query && length($query)){
$out .= '?' . $query;
}
if(defined $fragment && length($fragment)){
$out .= '#' . $fragment;
}
return $out;
}
# -------------------------------------------------------------------------------
=pod
=item B<is_http_uri> - is the value a well-formed HTTP uri?
is_http_uri($value);
=over 4
=item I<Description>
Specialized version of is_uri() that only likes http:// urls. As a result, it can
also do a much more thorough job validating. Also, unlike is_uri() it is more
concerned with only allowing real-world URIs through. Things like relative
hostnames are allowed by the standards, but probably aren't wise. Conversely,
null paths aren't allowed per RFC 2616 (should be '/' instead), but are allowed
by this function.
This function only works for fully-qualified URIs. /bob.html won't work.
See RFC 3986 for the appropriate method to turn a relative URI into an absolute
one given its context.
Returns the untainted URI if the test value appears to be well-formed.
Note that you probably want to either call this in combo with is_https_uri(). i.e.
print "Good" if(is_http_uri($uri) || is_https_uri($uri));
or use the convenience method is_web_uri which is equivalent.
=item I<Arguments>
=over 4
=item $value
The potential URI to test.
=back
=item I<Returns>
Returns the untainted URI on success, undef on failure.
=item I<Notes, Exceptions, & Bugs>
This function does not make any attempt to check whether the URI is accessible
or 'makes sense' in any meaningful way. It just checks that it is formatted
correctly.
=back
=cut
sub is_http_uri{
my $self = shift if ref($_[0]);
my $value = shift;
my $allow_https = shift;
return unless is_uri($value);
my($scheme, $authority, $path, $query, $fragment) = _split_uri($value);
return unless $scheme;
if($allow_https){
return unless lc($scheme) eq 'https';
} else {
return unless lc($scheme) eq 'http';
}
# fully-qualified URIs must have an authority section that is
# a valid host
return unless($authority);
# allow a port component
my($port) = $authority =~ /:(\d+)$/;
$authority =~ s/:\d+$//;
# modifying this to allow the (discouraged, but still legal) use of IP addresses
unless(Data::Validate::Domain::is_domain($authority) || Data::Validate::IP::is_ipv4($authority)){
return;
}
# re-assemble the URL per section 5.3 in RFC 3986
my $out = $scheme . ':';
$out .= '//' . $authority;
$out .= ':' . $port if $port;
$out .= $path;
if(defined $query && length($query)){
$out .= '?' . $query;
}
if(defined $fragment && length($fragment)){
$out .= '#' . $fragment;
}
return $out;
}
# -------------------------------------------------------------------------------
=pod
=item B<is_https_uri> - is the value a well-formed HTTPS uri?
is_https_uri($value);
=over 4
=item I<Description>
See is_http_uri() for details. This version only likes the https URI scheme.
Otherwise it's identical to is_http_uri()
=item I<Arguments>
=over 4
=item $value
The potential URI to test.
=back
=item I<Returns>
Returns the untainted URI on success, undef on failure.
=item I<Notes, Exceptions, & Bugs>
This function does not make any attempt to check whether the URI is accessible
or 'makes sense' in any meaningful way. It just checks that it is formatted
correctly.
=back
=cut
sub is_https_uri{
my $self = shift if ref($_[0]);
my $value = shift;
return is_http_uri($value, 1);
}
# -------------------------------------------------------------------------------
=pod
=item B<is_web_uri> - is the value a well-formed HTTP or HTTPS uri?
is_web_uri($value);
=over 4
=item I<Description>
This is just a convinience method that combines is_http_uri and is_https_uri
to accept most common real-world URLs.
=item I<Arguments>
=over 4
=item $value
The potential URI to test.
=back
=item I<Returns>
Returns the untainted URI on success, undef on failure.
=item I<Notes, Exceptions, & Bugs>
This function does not make any attempt to check whether the URI is accessible
or 'makes sense' in any meaningful way. It just checks that it is formatted
correctly.
=back
=cut
sub is_web_uri{
my $self = shift if ref($_[0]);
my $value = shift;
my $h = is_http_uri($value);
return $h if defined $h;
return is_https_uri($value);
}
# -------------------------------------------------------------------------------
=pod
=item B<is_tel_uri> - is the value a well-formed telephone uri?
is_tel_uri($value);
=over 4
=item I<Description>
Specialized version of is_uri() that only likes tel: urls. As a result, it can
also do a much more thorough job validating according to RFC 3966.
Returns the untainted URI if the test value appears to be well-formed.
=item I<Arguments>
=over 4
=item $value
The potential URI to test.
=back
=item I<Returns>
Returns the untainted URI on success, undef on failure.
=item I<Notes, Exceptions, & Bugs>
This function does not make any attempt to check whether the URI is accessible
or 'makes sense' in any meaningful way. It just checks that it is formatted
correctly.
=back
=cut
sub is_tel_uri{
my $self = shift if ref($_[0]);
my $value = shift;
# extracted from http://tools.ietf.org/html/rfc3966#section-3
my $hex_digit = '[a-fA-F0-9]'; # strictly hex digit does not allow lower case letters according to http://tools.ietf.org/html/rfc2234#section-6.1
my $reserved = '[;/?:@&=+$,]';
my $alphanum = '[A-Za-z0-9]';
my $visual_separator = '[\-\.\(\)]';
my $phonedigit_hex = '(?:' . $hex_digit . '|\*|\#|' . $visual_separator . ')';
my $phonedigit = '(?:' . '\d' . '|' . $visual_separator . ')';
my $param_unreserved = '[\[\]\/:&+$]';
my $pct_encoded = '\\%' . $hex_digit . $hex_digit;
my $mark = "[\-_\.!~*'()]";
my $unreserved = '(?:' . $alphanum . '|' . $mark . ')';
my $paramchar = '(?:' . $param_unreserved . '|' . $unreserved . '|' . $pct_encoded . ')';
my $pvalue = $paramchar . '{1,}';
my $pname = '(?:' . $alphanum . '|\\-){1,}';
my $uric = '(?:' . $reserved . '|' . $unreserved . '|' . $pct_encoded . ')';
my $alpha = '[A-Za-z]';
my $toplabel = '(?:' . $alpha . '|' . $alpha . '(?:' . $alphanum . '|' . '\\-){0,}' . $alpha . ')';
my $domainlabel = '(?:' . $alphanum . '|' . $alphanum . '(?:' . $alphanum . '|\\-){0,}' . $alphanum . ')';
my $domainname = '(?:' . $domainlabel . '\\.){0,}' . $toplabel . '\\.{0,1}';
# extracted from http://tools.ietf.org/html/rfc4694#section-4
my $npdi = ';npdi';
my $hex_phonedigit = '(?:' . $hex_digit . '|' . $visual_separator . ')';
my $global_hex_digits = '\\+' . '\\d{1,3}' . $hex_phonedigit . '{0,}';
my $global_rn = $global_hex_digits;
my $rn_descriptor = '(?:' . $domainname . '|' . $global_hex_digits . ')';
my $rn_context = ';rn-context=' . $rn_descriptor;
my $local_rn = $hex_phonedigit . '{1,}' . $rn_context;
my $global_cic = $global_hex_digits;
my $cic_context = ';cic-context=' . $rn_descriptor;
my $local_cic = $hex_phonedigit . '{1,}' . $cic_context;
my $cic = ';cic=' . '(?:' . $global_cic . '|' . $local_cic . '){0,1}';
my $rn = ';rn=' . '(?:' . $global_rn . '|' . $local_rn . '){0,1}';
if ($value =~ /$rn.*$rn/xsm) {
return;
}
if ($value =~ /$npdi.*$npdi/xsm) {
return;
}
if ($value =~ /$cic.*$cic/xsm) {
return;
}
my $parameter = '(?:;' . $pname . '(?:=' . $pvalue . ')|' . $rn . '|' . $cic . '|' . $npdi . ')';
# end of http://tools.ietf.org/html/rfc4694#section-4
my $local_number_digits = '(?:' . $phonedigit_hex . '{0,}' . '(?:' . $hex_digit . '|\*|\#)' . $phonedigit_hex . '{0,})';
my $global_number_digits = '\+' . $phonedigit . '{0,}' . '[0-9]' . $phonedigit . '{0,}';
my $descriptor = '(?:' . $domainname . '|' . $global_number_digits . ')';
my $context = ';phone\-context=' . $descriptor;
my $extension = ';ext=' . $phonedigit . '{1,}';
my $isdn_subaddress = ';isub=' . $uric . '{1,}';
# extracted from http://tools.ietf.org/html/rfc4759
my $enum_dip_indicator = ';enumdi';
if ($value =~ /$enum_dip_indicator.*$enum_dip_indicator/xsm) { # http://tools.ietf.org/html/rfc4759#section-3
return;
}
# extracted from http://tools.ietf.org/html/rfc4904#section-5
my $trunk_group_unreserved = '[/&+$]';
my $escaped = '\\%' . $hex_digit . $hex_digit; # according to http://tools.ietf.org/html/rfc3261#section-25.1
my $trunk_group_label = '(?:' . $unreserved . '|' . $escaped . '|' . $trunk_group_unreserved . '){1,}';
my $trunk_group = ';tgrp=' . $trunk_group_label;
my $trunk_context = ';trunk\-context=' . $descriptor;
my $par = '(?:' . $parameter . '|' . $extension . '|' . $isdn_subaddress . '|' . $enum_dip_indicator . '|' . $trunk_context . '|' . $trunk_group . ')';
my $local_number = $local_number_digits . $par . '{0,}' . $context . $par . '{0,}';
my $global_number = $global_number_digits . $par . '{0,}';
my $telephone_subscriber = '(?:' . $global_number . '|' . $local_number . ')';
my $telephone_uri = 'tel:' . $telephone_subscriber;
if ($value =~ /^($telephone_uri)$/xsm) {
my ($untainted) = ($1);
return $untainted;
} else {
return;
}
}
# internal URI spitter method - direct from RFC 3986
sub _split_uri{
my $value = shift;
my @bits = $value =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
return @bits;
}
=pod
=back
=head1 SEE ALSO
L<URI>, RFC 3986, RFC 3966, RFC 4694, RFC 4759, RFC 4904
=head1 AUTHOR
Richard Sonnen <F<sonnen@richardsonnen.com>>.
is_tel_uri by David Dick <F<ddick@cpan.org>>.
=head1 COPYRIGHT
Copyright (c) 2005 Richard Sonnen. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|