SafeBrowsing4/URI.pm

filter: Statementstmt 100% 127 / 127 Branchbran 98.1% 53 / 54 Conditioncond 100% 12 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 99.0% 214 / 216 CC 34 SCAR 20.7
CC
34
Cov
99%
_normalize
14.0
generate_lookupuris
8.0
_normalize_ip
7.0
CRAP 7.9
« SafeBrowsing4/Storage/File.pm ↑ Summary SafeBrowsing4/Storage.pm »
1 package Net::Google::SafeBrowsing4::URI;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Digest::SHA qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Net::IP::Lite qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 262 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
56 262 my @args = @_;
57  
58 262 ▶ if ((scalar(@args) == 0) || !$args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 0 or not $args[0]) 3 259 262
Condition100%3/3
scalar @args == 0 or not $args[0]
l!l&&r!l&&!r
12259
MC/DC100%2/2
scalar @args == 0 or not $args[0]
scalar @args == 0 not $args[0]
Truth table100%3/3
A or not(B)
A= scalar @args == 0B= not $args[0]
ABresult
1X1
011
000
59 3 return undef;
60 }
61  
62 259 my $self = {
63 rawuri => $args[0],
64 };
65  
66 259 bless($self, $class);
67 259 ▶ return $self->_normalize() ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_normalize ? : 238 21 259
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 432 ▶ my $self = shift;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
82  
83 432 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 8 ▶ my $self = shift;
Subroutine generate_lookupuris: called CC=8 SCAR=20.8
Pod documented
95 8 my @uris = ();
96  
97 8 $self->as_string() =~ /^(https?:\/\/)([^\/]+)(\/.*)$/i;
98 8 my ($scheme, $host, $path_query) = ($1, $2, $3);
99  
100 # Collect host suffixes
101 8 my @domains = ();
102 8 ▶ if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host =~ /^\d+\.\d+\.\d+\.\d+$/) 7 1 8
103 7 my @parts = split(/\./, $host);
104 7 splice(@parts, 0, -6); # take 5 top most compoments
105  
106 7 ▶ while (scalar(@parts) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parts > 2) 9 7 16
107 9 shift(@parts);
108 9 push(@domains, join(".", @parts) );
109 }
110 }
111 8 push(@domains, $host);
112  
113 # Collect path & query prefixes
114 8 my @paths = ();
115 8 my @parts = split(/\//, $path_query);
116 8 my $part_count = scalar(@parts);
117 8 ▶ $part_count = $part_count > 4 ? 4 : $part_count - 1; # limit to 4
Branch: 1 branch
BranchTrueFalseTotal
$part_count > 4 ? : 1 7 8
118 8 my $previous = "";
119 8 ▶ for (my $i = 0; $i < $part_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $part_count) 8 8 16
120 8 $previous .= "/" . $parts[$i] ."/";
121 8 push(@paths, $previous);
122 }
123 8 ▶ if ($path_query =~ /^([^\?]+)\?.*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_query =~ /^([^\?]+)\?.*$/) 1 7 8
124 1 push(@paths, $1);
125 }
126 8 push(@paths, $path_query);
127  
128 # Assemble the list of Net::Google::SafeBrowsing4::URI objects
129 8 foreach my $domain (@domains) {
130 17 foreach my $path (@paths) {
131 33 my $gsb_uri = Net::Google::SafeBrowsing4::URI->new($scheme . $domain . $path);
132 33 push(@uris, $gsb_uri);
133 }
134 }
135  
136 8 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 2 ▶ my $self = shift;
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
147  
148 2 my $uri = $self->as_string();
149 2 $uri =~ s/^https?:\/\///i;
150  
151 2 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 259 ▶ my $self = shift;
Subroutine _normalize: called CC=14 SCAR=26.4
168 259 my $modified_rawuri = $self->{rawuri};
169  
170 # Remove third and more slashes after the scheme
171 259 $modified_rawuri =~ s/^(\s*https?:\/\/)\/+/$1/si;
172 # Remove any Tab, CR, LF characters from the URI
173 259 $modified_rawuri =~ s/[\r\n\t]+//sgi;
174 # Recursive percent-unescape (everything but '#' not to confuse URI parser)
175 259 ▶ while ($modified_rawuri =~ s{%(?!23)([[:xdigit:]]{2})}{chr(hex($1))}esg) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($modified_rawuri =~ s/%(?!23)([[:xdigit:]]{2})/chr hex $1;/seg) 29 259 288
176  
177 # Parse URI
178 259 my $uri_obj = URI->new($modified_rawuri);
179 259 ▶ if (ref($uri_obj) !~ /^URI::https?$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($uri_obj) =~ /^URI::https?$/) 29 230 259
180 29 ▶ if (!$uri_obj->scheme() || (!$uri_obj->has_recognized_scheme() && $modified_rawuri =~ /^[^:]+:\d{1,5}(?:\/|$)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $uri_obj->scheme or not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]) 25 4 29
Condition100%6/6
not $uri_obj->scheme or not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
l!l&&r!l&&!r
2324
not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
!ll&&!rl&&r
312
MC/DC66%2/3
not $uri_obj->scheme or not $uri_obj->has_recognized_scheme and $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
not $uri_obj->scheme not $uri_obj->has_recognized_scheme $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
Truth table75%3/4
not(A) or not(B) and C
A= not $uri_obj->schemeB= not $uri_obj->has_recognized_schemeC= $modified_rawuri =~ m[^[^:]+:\d{1,5}(?:/|$)]
ABCresult
1XX1
0111
00X0
0100
181 25 $uri_obj = URI->new("http://" . $modified_rawuri);
182 }
183 }
184 # Only http and https URIs are supported
185 259 ▶ if (ref($uri_obj) !~ /^URI::https?$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($uri_obj) =~ /^URI::https?$/) 4 255 259
186 4 return undef;
187 }
188  
189 # Remove userinfo
190 255 $uri_obj->userinfo(undef);
191 # Remove port
192 255 $uri_obj->port(undef);
193 # Remove Fragment
194 255 $uri_obj->fragment(undef);
195  
196 # Host modifications
197 255 my $modified_host = $uri_obj->host();
198 # Host part cannot be empty
199 255 ▶ if ($modified_host =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified_host =~ /^\s*$/) 2 253 255
200 2 return undef;
201 }
202 # Collapse consecutive dots into one
203 253 $modified_host =~ s/\.\.+/\./sg;
204 # Remove leading and trailing dot
205 253 $modified_host =~ s/^\.|\.$//sg;
206  
207 # IPv4 canonicalizations
208 253 $modified_host = _normalize_ip($modified_host);
209 253 ▶ if (!defined($modified_host)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $modified_host) 11 242 253
210 11 return undef;
211 }
212 242 $uri_obj->host($modified_host);
213  
214 # Empty/separator-only host
215 242 ▶ if ($uri_obj->host() =~ /^[\s.\/]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_obj->host =~ m[^[\s./]*$]) 1 241 242
216 1 return undef;
217 }
218  
219 # Numeric TLD (and not IPv4)
220 241 ▶ if ($uri_obj->host() =~ /\.\d[^\.]*$/
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_obj->host =~ /\.\d[^\.]*$/ and not $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}$/) 3 238 241
Condition100%3/3
$uri_obj->host =~ /\.\d[^\.]*$/ and not $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}$/
!ll&&!rl&&r
1091293
MC/DC100%2/2
$uri_obj->host =~ /\.\d[^\.]*$/ and not $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}$/
$uri_obj->host =~ /\.\d[^\.]*$/ not $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}$/
Truth table100%3/3
A and not(B)
A= $uri_obj->host =~ /\.\d[^\.]*$/B= not $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}$/
ABresult
0X0
100
111
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 3 return undef;
223 }
224  
225 238 my $modified_path = $uri_obj->path();
226 # Eliminate current directory /./ parts
227 238 ▶ while ($modified_path =~ s/\/\.(?:\/|$)/\//sg) {};
Branch: 1 branch
BranchTrueFalseTotal
if ($modified_path =~ s[/\.(?:/|$)][/]gs) 0 238 238
228 # Eliminate parent directory /something/./ parts
229 238 ▶ while ($modified_path =~ s/\/[^\/]+\/\.\.(?:\/|$)/\//sg) {};
Branch: 1 branch
BranchTrueFalseTotal
if ($modified_path =~ s[/[^/]+/\.\.(?:/|$)][/]gs) 7 238 245
230 # Eliminate double // slashes from path
231 238 $modified_path =~ s/\/\/+/\//sg;
232 238 $uri_obj->path($modified_path);
233  
234 # Fix some percent encoding
235 238 my $modified_path_query = $uri_obj->path_query();
236 # Fix lone percent signs %
237 238 $modified_path_query =~ s/%(?![[:xdigit:]]{2})/%25/sg;
238 238 $uri_obj->path_query($modified_path_query);
239  
240 238 my $canonical = $uri_obj->canonical();
241 # Fix caret escaping
242 238 $canonical=~ s/%5E/\^/sg;
243  
244 238 $self->{uri} = $canonical;
245  
246 238 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 253 ▶ my $host = shift;
Subroutine _normalize_ip: called CC=7 SCAR=19.5
273  
274 # Shortcut: If it doesn't look like an IPv4, then return early
275 253 ▶ if ($host !~ /^(?:0x[[:xdigit:]]+|\d+)(?:\.(?:0x[[:xdigit:]]+|\d+))*$/si) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host =~ /^(?:0x[[:xdigit:]]+|\d+)(?:\.(?:0x[[:xdigit:]]+|\d+))*$/is) 113 140 253
276 113 return $host;
277 }
278  
279 # Most formats are detected and converted by Net::IP::Lite
280 140 my $ip = Net::IP::Lite->new($host);
281 140 ▶ if ($ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip) 93 47 140
282 93 return $ip->transform();
283 }
284  
285 # One and two dots case is missing: xxx.xxxxxxxxxx, xxx.xxx.xxxxxx
286 47 my $bits = 32;
287 47 my @segments = split(/\./, $host);
288 47 my $segment_count = scalar(@segments);
289  
290 47 my $decimal = 0;
291 47 ▶ for (my $i = 0; $i < $segment_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $segment_count) 122 36 158
292 122 my $is_last_segment = $i >= $segment_count - 1;
293 122 ▶ my $segment = _parse_ipv4_segment($segments[$i], !$is_last_segment ? 8 : $bits);
Branch: 1 branch
BranchTrueFalseTotal
!$is_last_segment ? : 80 42 122
294 122 ▶ if (!defined($segment)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $segment) 11 111 122
295 11 return undef;
296 }
297 111 $bits -= 8;
298 111 ▶ $decimal += $segment << (!$is_last_segment ? $bits : 0);
Branch: 1 branch
BranchTrueFalseTotal
!$is_last_segment ? : 75 36 111
299 }
300  
301 36 $ip = Net::IP::Lite->new($decimal);
302 36 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 122 ▶ my $segment = shift;
Subroutine _parse_ipv4_segment: called CC=5 SCAR=16.1
337 122 my $bits = shift;
338 122 my $decimal;
339  
340 122 ▶ if ($segment =~ /^0+([0-7]{0,10}|[0-3][0-7]{10})$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($segment =~ /^0+([0-7]{0,10}|[0-3][0-7]{10})$/) { } 34 88 122
elsif ($segment =~ /^0x0*([[:xdigit:]]{1,8})$/is) { } 35 53 88
elsif ($segment =~ /^[1-9]\d+$/) { } 49 4 53
341 34 $decimal = oct($1);
342 }
343 elsif ($segment =~ /^0x0*([[:xdigit:]]{1,8})$/si) {
344 35 $decimal = hex($1);
345 }
346 elsif ($segment =~ /^[1-9]\d+$/) {
347 49 $decimal = $segment;
348 }
349 else {
350 4 return undef;
351 }
352  
353 118 ▶ if ($decimal >= (1 << $bits)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal >= 1 << $bits) 7 111 118
354 7 return undef;
355 }
356 111 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 9 1;
« SafeBrowsing4/Storage/File.pm ↑ Summary SafeBrowsing4/Storage.pm »