| 1 | package Net::Google::SafeBrowsing4::URI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Digest::SHA qw(sha256); | ||
| 7 | use Net::IP::Lite qw(); | ||
| 8 | use URI; | ||
| 9 | |||
| 10 | =encoding utf-8 | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Net::Google::SafeBrowsing4::URI - URI management Class for the Google SafeBrowsing (version 4) API. | ||
| 15 | |||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use Net::Google::SafeBrowsing4::URI; | ||
| 20 | |||
| 21 | my $gsb_uri = Net::Google::SafeBrowsing4::URI->new('http://my.example.site:80/path/to/file.html?query=param#fragment'); | ||
| 22 | my @uris = $gsb_uri->generate_lookupuris(); | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | Net::Google::SafeBrowsing4::URI takes care of normalizing URLs, extracting suffix/prefix expressions, calculating hashes. | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item new | ||
| 33 | |||
| 34 | =back | ||
| 35 | |||
| 36 | my $gsb_uri = Net::Google::SafeBrowsing4::URI->new('http://my.example.site:80/path/to/file.html?query=param#fragment'); | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | Initializes the object. | ||
| 41 | |||
| 42 | Arguments: | ||
| 43 | |||
| 44 | =over | ||
| 45 | |||
| 46 | =item $uri The URL to parse | ||
| 47 | |||
| 48 | =back | ||
| 49 | |||
| 50 | =back | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my $class = shift; | ||
| 56 | my @args = @_; | ||
| 57 | |||
| 58 | if ((scalar(@args) == 0) || !$args[0]) { | ||
| 59 | return undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $self = { | ||
| 63 | rawuri => $args[0], | ||
| 64 | }; | ||
| 65 | |||
| 66 | bless($self, $class); | ||
| 67 | return $self->_normalize() ? $self : undef; | ||
| 68 | } | ||
| 69 | |||
| 70 | =over | ||
| 71 | |||
| 72 | =item as_string | ||
| 73 | |||
| 74 | Returns the normalized URI as string. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub as_string { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | return '' . $self->{uri}; | ||
| 84 | } | ||
| 85 | |||
| 86 | =item generate_lookupuris | ||
| 87 | |||
| 88 | Generates all partial/full URIs supported by Google SafeBrowsing. See "suffix/prefix expressions" topic in GSBv4 API reference. | ||
| 89 | Returns a list of L<Net::Google::SafeBrowsing4::URI> objects. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub generate_lookupuris { | ||
| 94 | my $self = shift; | ||
| 95 | my @uris = (); | ||
| 96 | |||
| 97 | $self->as_string() =~ /^(https?:\/\/)([^\/]+)(\/.*)$/i; | ||
| 98 | my ($scheme, $host, $path_query) = ($1, $2, $3); | ||
| 99 | |||
| 100 | # Collect host suffixes | ||
| 101 | my @domains = (); | ||
| 102 | if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) { | ||
| 103 | my @parts = split(/\./, $host); | ||
| 104 | splice(@parts, 0, -6); # take 5 top most compoments | ||
| 105 | |||
| 106 | while (scalar(@parts) > 2) { | ||
| 107 | shift(@parts); | ||
| 108 | push(@domains, join(".", @parts) ); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | push(@domains, $host); | ||
| 112 | |||
| 113 | # Collect path & query prefixes | ||
| 114 | my @paths = (); | ||
| 115 | my @parts = split(/\//, $path_query); | ||
| 116 | my $part_count = scalar(@parts); | ||
| 117 | $part_count = $part_count > 4 ? 4 : $part_count - 1; # limit to 4 | ||
| 118 | my $previous = ""; | ||
| 119 | for (my $i = 0; $i < $part_count; $i++) { | ||
| 120 | $previous .= "/" . $parts[$i] ."/"; | ||
| 121 | push(@paths, $previous); | ||
| 122 | } | ||
| 123 | if ($path_query =~ /^([^\?]+)\?.*$/) { | ||
| 124 | push(@paths, $1); | ||
| 125 | } | ||
| 126 | push(@paths, $path_query); | ||
| 127 | |||
| 128 | # Assemble the list of Net::Google::SafeBrowsing4::URI objects | ||
| 129 | foreach my $domain (@domains) { | ||
| 130 | foreach my $path (@paths) { | ||
| 131 | my $gsb_uri = Net::Google::SafeBrowsing4::URI->new($scheme . $domain . $path); | ||
| 132 | push(@uris, $gsb_uri); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | return @uris; | ||
| 137 | } | ||
| 138 | |||
| 139 | =item hash | ||
| 140 | |||
| 141 | Generates the SHA-256 hash of the URI (with scheme removed). | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub hash { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | my $uri = $self->as_string(); | ||
| 149 | $uri =~ s/^https?:\/\///i; | ||
| 150 | |||
| 151 | return sha256($uri); | ||
| 152 | } | ||
| 153 | |||
| 154 | =head1 PRIVATE METHODS | ||
| 155 | |||
| 156 | =over | ||
| 157 | |||
| 158 | =item _normalize | ||
| 159 | |||
| 160 | Parses and normalizes the URI. | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub _normalize { | ||
| 167 | my $self = shift; | ||
| 168 | my $modified_rawuri = $self->{rawuri}; | ||
| 169 | |||
| 170 | # Remove third and more slashes after the scheme | ||
| 171 | $modified_rawuri =~ s/^(\s*https?:\/\/)\/+/$1/si; | ||
| 172 | # Remove any Tab, CR, LF characters from the URI | ||
| 173 | $modified_rawuri =~ s/[\r\n\t]+//sgi; | ||
| 174 | # Recursive percent-unescape (everything but '#' not to confuse URI parser) | ||
| 175 | while ($modified_rawuri =~ s{%(?!23)([[:xdigit:]]{2})}{chr(hex($1))}esg) { } | ||
| 176 | |||
| 177 | # Parse URI | ||
| 178 | my $uri_obj = URI->new($modified_rawuri); | ||
| 179 | if (ref($uri_obj) !~ /^URI::https?$/) { | ||
| 180 | if (!$uri_obj->scheme() || (!$uri_obj->has_recognized_scheme() && $modified_rawuri =~ /^[^:]+:\d{1,5}(?:\/|$)/)) { | ||
| 181 | $uri_obj = URI->new("http://" . $modified_rawuri); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | # Only http and https URIs are supported | ||
| 185 | if (ref($uri_obj) !~ /^URI::https?$/) { | ||
| 186 | return undef; | ||
| 187 | } | ||
| 188 | |||
| 189 | # Remove userinfo | ||
| 190 | $uri_obj->userinfo(undef); | ||
| 191 | # Remove port | ||
| 192 | $uri_obj->port(undef); | ||
| 193 | # Remove Fragment | ||
| 194 | $uri_obj->fragment(undef); | ||
| 195 | |||
| 196 | # Host modifications | ||
| 197 | my $modified_host = $uri_obj->host(); | ||
| 198 | # Host part cannot be empty | ||
| 199 | if ($modified_host =~ /^\s*$/) { | ||
| 200 | return undef; | ||
| 201 | } | ||
| 202 | # Collapse consecutive dots into one | ||
| 203 | $modified_host =~ s/\.\.+/\./sg; | ||
| 204 | # Remove leading and trailing dot | ||
| 205 | $modified_host =~ s/^\.|\.$//sg; | ||
| 206 | |||
| 207 | # IPv4 canonicalizations | ||
| 208 | $modified_host = _normalize_ip($modified_host); | ||
| 209 | if (!defined($modified_host)) { | ||
| 210 | return undef; | ||
| 211 | } | ||
| 212 | $uri_obj->host($modified_host); | ||
| 213 | |||
| 214 | # Empty/separator-only host | ||
| 215 | if ($uri_obj->host() =~ /^[\s.\/]*$/) { | ||
| 216 | return undef; | ||
| 217 | } | ||
| 218 | |||
| 219 | # Numeric TLD (and not IPv4) | ||
| 220 | if ($uri_obj->host() =~ /\.\d[^\.]*$/ | ||
| 221 | && $uri_obj->host() !~ /^(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}$/) { | ||
| 222 | return undef; | ||
| 223 | } | ||
| 224 | |||
| 225 | my $modified_path = $uri_obj->path(); | ||
| 226 | # Eliminate current directory /./ parts | ||
| 227 | while ($modified_path =~ s/\/\.(?:\/|$)/\//sg) {}; | ||
| 228 | # Eliminate parent directory /something/./ parts | ||
| 229 | while ($modified_path =~ s/\/[^\/]+\/\.\.(?:\/|$)/\//sg) {}; | ||
| 230 | # Eliminate double // slashes from path | ||
| 231 | $modified_path =~ s/\/\/+/\//sg; | ||
| 232 | $uri_obj->path($modified_path); | ||
| 233 | |||
| 234 | # Fix some percent encoding | ||
| 235 | my $modified_path_query = $uri_obj->path_query(); | ||
| 236 | # Fix lone percent signs % | ||
| 237 | $modified_path_query =~ s/%(?![[:xdigit:]]{2})/%25/sg; | ||
| 238 | $uri_obj->path_query($modified_path_query); | ||
| 239 | |||
| 240 | my $canonical = $uri_obj->canonical(); | ||
| 241 | # Fix caret escaping | ||
| 242 | $canonical=~ s/%5E/\^/sg; | ||
| 243 | |||
| 244 | $self->{uri} = $canonical; | ||
| 245 | |||
| 246 | return $self->{uri}; | ||
| 247 | } | ||
| 248 | |||
| 249 | =head1 PRIVATE FUNCTIONS | ||
| 250 | |||
| 251 | =over | ||
| 252 | |||
| 253 | =item _normalize_ip | ||
| 254 | |||
| 255 | Function for recognising various IPv4 formatted addresses and convert them to I<dotted-decimal-quad> format (111.11.1.1) | ||
| 256 | |||
| 257 | Arguments: | ||
| 258 | |||
| 259 | =over | ||
| 260 | |||
| 261 | =item $host | ||
| 262 | |||
| 263 | Hostname to parse as IP Address | ||
| 264 | |||
| 265 | =back | ||
| 266 | |||
| 267 | =back | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | sub _normalize_ip { | ||
| 272 | my $host = shift; | ||
| 273 | |||
| 274 | # Shortcut: If it doesn't look like an IPv4, then return early | ||
| 275 | if ($host !~ /^(?:0x[[:xdigit:]]+|\d+)(?:\.(?:0x[[:xdigit:]]+|\d+))*$/si) { | ||
| 276 | return $host; | ||
| 277 | } | ||
| 278 | |||
| 279 | # Most formats are detected and converted by Net::IP::Lite | ||
| 280 | my $ip = Net::IP::Lite->new($host); | ||
| 281 | if ($ip) { | ||
| 282 | return $ip->transform(); | ||
| 283 | } | ||
| 284 | |||
| 285 | # One and two dots case is missing: xxx.xxxxxxxxxx, xxx.xxx.xxxxxx | ||
| 286 | my $bits = 32; | ||
| 287 | my @segments = split(/\./, $host); | ||
| 288 | my $segment_count = scalar(@segments); | ||
| 289 | |||
| 290 | my $decimal = 0; | ||
| 291 | for (my $i = 0; $i < $segment_count; $i++) { | ||
| 292 | my $is_last_segment = $i >= $segment_count - 1; | ||
| 293 | my $segment = _parse_ipv4_segment($segments[$i], !$is_last_segment ? 8 : $bits); | ||
| 294 | if (!defined($segment)) { | ||
| 295 | return undef; | ||
| 296 | } | ||
| 297 | $bits -= 8; | ||
| 298 | $decimal += $segment << (!$is_last_segment ? $bits : 0); | ||
| 299 | } | ||
| 300 | |||
| 301 | $ip = Net::IP::Lite->new($decimal); | ||
| 302 | return $ip->transform(); | ||
| 303 | } | ||
| 304 | |||
| 305 | =over | ||
| 306 | |||
| 307 | =item _parse_ipv4_segment | ||
| 308 | |||
| 309 | =back | ||
| 310 | |||
| 311 | my $decimal = _parse_ipv4_part($segment, $bits) | ||
| 312 | |||
| 313 | =over | ||
| 314 | |||
| 315 | Transforms one IPv4 segment to decimal with range checking. | ||
| 316 | |||
| 317 | Arguments: | ||
| 318 | |||
| 319 | =over | ||
| 320 | |||
| 321 | =item $segment | ||
| 322 | |||
| 323 | Decimal/octal/hexadecimal value to parse | ||
| 324 | |||
| 325 | =item $bits | ||
| 326 | |||
| 327 | Bit length for range checking | ||
| 328 | |||
| 329 | =back | ||
| 330 | |||
| 331 | =back | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | sub _parse_ipv4_segment { | ||
| 336 | my $segment = shift; | ||
| 337 | my $bits = shift; | ||
| 338 | my $decimal; | ||
| 339 | |||
| 340 | if ($segment =~ /^0+([0-7]{0,10}|[0-3][0-7]{10})$/) { | ||
| 341 | $decimal = oct($1); | ||
| 342 | } | ||
| 343 | elsif ($segment =~ /^0x0*([[:xdigit:]]{1,8})$/si) { | ||
| 344 | $decimal = hex($1); | ||
| 345 | } | ||
| 346 | elsif ($segment =~ /^[1-9]\d+$/) { | ||
| 347 | $decimal = $segment; | ||
| 348 | } | ||
| 349 | else { | ||
| 350 | return undef; | ||
| 351 | } | ||
| 352 | |||
| 353 | if ($decimal >= (1 << $bits)) { | ||
| 354 | return undef; | ||
| 355 | } | ||
| 356 | return $decimal; | ||
| 357 | } | ||
| 358 | |||
| 359 | =head1 BUGS | ||
| 360 | |||
| 361 | Some URI normalizatuion cases are still missing: | ||
| 362 | |||
| 363 | =over | ||
| 364 | |||
| 365 | =item Highbit characters in hostname are punycoded, not percent encoded. | ||
| 366 | |||
| 367 | Google's GO Client percent-escapes based on the hostname is a valid unicode string or not | ||
| 368 | |||
| 369 | =back | ||
| 370 | |||
| 371 | =head1 AUTHORS | ||
| 372 | |||
| 373 | Tamás Fehérvári, E<lt>geever@users.sourceforge.net<gt> | ||
| 374 | Julien Sobrier, E<lt>julien@sobrier.netE<gt>, | ||
| 375 | |||
| 376 | =head1 COPYRIGHT AND LICENSE | ||
| 377 | |||
| 378 | Copyright (C) 2016 by Julien Sobrier, Tamás Fehérvári | ||
| 379 | |||
| 380 | This library is free software; you can redistribute it and/or modify | ||
| 381 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 382 | at your option, any later version of Perl 5 you may have available. | ||
| 383 | |||
| 384 | =cut | ||
| 385 | |||
| 386 | 1; |