blib/lib/HTML/Scrubber.pm

filter: Statementstmt 98.2% 166 / 169 Branchbran 89.0% 98 / 110 Conditioncond 72.2% 13 / 18 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 25 / 25 Podpod 100% 11 / 11 totaltotal 92.7% 320 / 345 CC 67 SCAR 23.0
CC
67
Cov
93%
_scrub_str
22.0
_validate
15.8
scrub_file
5.9
CRAP 10.0
↑ Summary
1 package HTML::Scrubber;
2  
3 # ABSTRACT: Perl extension for scrubbing/sanitizing HTML
4  
5  
6 16 ▶use 5.008; # enforce minimum perl version of 5.8
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use HTML::Parser 3.47 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Scalar::Util ('weaken');
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use List::Util 1.33 qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 16 our ( @_scrub, @_scrub_fh );
15  
16 16 our $VERSION = '0.19'; # VERSION
17 16 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
18  
19 # my my my my, these here to prevent foolishness like
20 # http://perlmonks.org/index.pl?node_id=251127#Stealing+Lexicals
21 16 (@_scrub) = ( \&_scrub, "self, event, tagname, attr, attrseq, text" );
22 16 (@_scrub_fh) = ( \&_scrub_fh, "self, event, tagname, attr, attrseq, text" );
23  
24  
25 23 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
26 23 my $package = shift;
27 23 my $p = HTML::Parser->new(
28 api_version => 3,
29 default_h => \@_scrub,
30 marked_sections => 0,
31 strict_comment => 0,
32 unbroken_text => 1,
33 case_sensitive => 0,
34 boolean_attribute_value => undef,
35 empty_element_tags => 1,
36 );
37  
38 23 my $self = {
39 _p => $p,
40 _rules => { '*' => 0, },
41 _comment => 0,
42 _process => 0,
43 _r => "",
44 _optimize => 1,
45 _script => 0,
46 _style => 0,
47 };
48  
49 23 $p->{"\0_s"} = bless $self, $package;
50 23 weaken( $p->{"\0_s"} );
51  
52 23 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 13 10 23
53  
54 10 my (%args) = @_;
55  
56 10 for my $f (qw[ default allow deny rules process comment ]) {
57 60 ▶ next unless exists $args{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$f} 48 12 60
58 12 ▶ if ( ref $args{$f} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{$f}) { } 10 2 12
59 10 $self->$f( @{ $args{$f} } );
60 }
61 else {
62 2 $self->$f( $args{$f} );
63 }
64 }
65  
66 10 return $self;
67 }
68  
69  
70 16 ▶sub comment {
Subroutine comment: called CC=2 SCAR=6.9
Pod documented
71 return $_[0]->{_comment}
72 16 ▶ if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 9 7 16
73 7 $_[0]->{_comment} = $_[1];
74 7 return;
75 }
76  
77  
78 10 ▶sub process {
Subroutine process: called CC=2 SCAR=6.9
Pod documented
79 return $_[0]->{_process}
80 10 ▶ if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 9 1 10
81 1 $_[0]->{_process} = $_[1];
82 1 return;
83 }
84  
85  
86 3 ▶sub script {
Subroutine script: called CC=2 SCAR=6.9
Pod documented
87 return $_[0]->{_script}
88 3 ▶ if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 1 3
89 1 $_[0]->{_script} = $_[1];
90 1 return;
91 }
92  
93  
94 3 ▶sub style {
Subroutine style: called CC=2 SCAR=6.9
Pod documented
95 return $_[0]->{_style}
96 3 ▶ if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 1 3
97 1 $_[0]->{_style} = $_[1];
98 1 return;
99 }
100  
101  
102 10 ▶sub allow {
Subroutine allow: called CC=2 SCAR=6.9
Pod documented
103 10 my $self = shift;
104 10 for my $k (@_) {
105 43 $self->{_rules}{ lc $k } = 1;
106 }
107 10 $self->{_optimize} = 1; # each time a rule changes, reoptimize when parse
108  
109 10 return;
110 }
111  
112  
113 2 ▶sub deny {
Subroutine deny: called CC=2 SCAR=6.9
Pod documented
114 2 my $self = shift;
115  
116 2 for my $k (@_) {
117 7 $self->{_rules}{ lc $k } = 0;
118 }
119  
120 2 $self->{_optimize} = 1; # each time a rule changes, reoptimize when parse
121  
122 2 return;
123 }
124  
125  
126 2 ▶sub rules {
Subroutine rules: called CC=2 SCAR=6.9
Pod documented
127 2 my $self = shift;
128 2 my (%rules) = @_;
129 2 for my $k ( keys %rules ) {
130 2 $self->{_rules}{ lc $k } = $rules{$k};
131 }
132  
133 2 $self->{_optimize} = 1; # each time a rule changes, reoptimize when parse
134  
135 2 return;
136 }
137  
138  
139 19 ▶sub default {
Subroutine default: called CC=5 SCAR=16.1
Pod documented
140 19 ▶ return $_[0]->{_rules}{'*'}
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 7 12 19
141 if @_ == 1;
142  
143 12 ▶ $_[0]->{_rules}{'*'} = $_[1] if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 10 2 12
144 12 ▶ $_[0]->{_rules}{'_'} = $_[2] if defined $_[2] and ref $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] and ref $_[2] 6 6 12
Condition66%2/3
defined $_[2] and ref $_[2]
!ll&&!rl&&r
606
MC/DC50%1/2
defined $_[2] and ref $_[2]
defined $_[2] ref $_[2]
Truth table66%2/3
A and B
A= defined $_[2]B= ref $_[2]
ABresult
0X0
100
111
145 12 $_[0]->{_optimize} = 1; # each time a rule changes, reoptimize when parse
146  
147 12 return;
148 }
149  
150  
151 2 ▶sub scrub_file {
Subroutine scrub_file: called CC=5 SCAR=17.8
Pod documented
152 2 ▶ if ( @_ > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 2 0 2
153 2 ▶ return unless defined $_[0]->_out( $_[2] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]->_out($_[2]) 0 2 2
154 }
155 else {
156 0 $_[0]->{_p}->handler( default => @_scrub );
157 }
158  
159 2 $_[0]->_optimize(); #if $_[0]->{_optimize};
160  
161 2 $_[0]->{_p}->parse_file( $_[1] );
162  
163 2 ▶ return delete $_[0]->{_r} unless exists $_[0]->{_out};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"_out"} 0 2 2
164 0 ▶ print { $_[0]->{_out} } $_[0]->{_r} if length $_[0]->{_r};
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0]{"_r"} 0 2 2
165 2 delete $_[0]->{_out};
166 2 return 1;
167 }
168  
169  
170 63 ▶sub scrub {
Subroutine scrub: called CC=5 SCAR=16.1
Pod documented
171 63 ▶ if ( @_ > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 2 61 63
172 2 ▶ return unless defined $_[0]->_out( $_[2] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]->_out($_[2]) 0 2 2
173 }
174 else {
175 61 $_[0]->{_p}->handler( default => @_scrub );
176 }
177  
178 63 $_[0]->_optimize(); # if $_[0]->{_optimize};
179  
180 63 ▶ $_[0]->{_p}->parse( $_[1] ) if defined( $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 62 1 63
181 63 $_[0]->{_p}->eof();
182  
183 63 ▶ return delete $_[0]->{_r} unless exists $_[0]->{_out};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"_out"} 61 2 63
184 2 delete $_[0]->{_out};
185 2 return 1;
186 }
187  
188  
189 4 ▶sub _out {
Subroutine _out: called CC=4 SCAR=14.0
190 4 my ( $self, $o ) = @_;
191  
192 4 ▶ unless ( ref $o and ref \$o ne 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $o and ref \$o ne 'GLOB') { } 2 2 4
Condition66%2/3
ref $o and ref \$o ne "GLOB"
!ll&&!rl&&r
202
MC/DC50%1/2
ref $o and ref \$o ne "GLOB"
ref $o ref \$o ne "GLOB"
Truth table66%2/3
A and B
A= ref $oB= ref \$o ne "GLOB"
ABresult
0X0
100
111
193 2 ▶ open my $F, '>', $o or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $F, ">", $o or return 2 0 2
194 2 binmode $F;
195 2 $self->{_out} = $F;
196 }
197 else {
198 2 $self->{_out} = $o;
199 }
200  
201 4 $self->{_p}->handler( default => @_scrub_fh );
202  
203 4 return 1;
204 }
205  
206  
207 91 ▶sub _validate {
Subroutine _validate: called CC=15 SCAR=27.6
208 91 my ( $s, $t, $r, $a, $as ) = @_;
209 91 ▶ return "<$t>" unless %$a;
Branch: 1 branch
BranchTrueFalseTotal
unless %$a 50 41 91
210  
211 41 $r = $s->{_rules}->{$r};
212 41 my %f;
213  
214 41 for my $k ( keys %$a ) {
215 61 ▶ my $check = exists $r->{$k} ? $r->{$k} : exists $r->{'*'} ? $r->{'*'} : next;
Branch: 2 branches
BranchTrueFalseTotal
exists $r->{$k} ? : 16 45 61
exists $r->{'*'} ? : 37 8 45
216  
217 53 ▶ if ( ref $check eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $check eq 'CODE') { } 4 49 53
elsif (ref $check or length $check > 1) { } 0 49 49
elsif ($check) { } 47 2 49
218 4 my @v = $check->( $s, $t, $k, $a->{$k}, $a, \%f );
219 4 ▶ next unless @v;
Branch: 1 branch
BranchTrueFalseTotal
unless @v 1 3 4
220 3 $f{$k} = shift @v;
221 }
222 ▶ elsif ( ref $check || length($check) > 1 ) {
Condition33%1/3
ref $check or length $check > 1
l!l&&r!l&&!r
0049
MC/DC0%0/2
ref $check or length $check > 1
ref $check length $check > 1
Truth table33%1/3
A or B
A= ref $checkB= length $check > 1
ABresult
1X1
011
000
223 0 ▶ $f{$k} = $a->{$k} if $a->{$k} =~ m{$check};
Branch: 1 branch
BranchTrueFalseTotal
if $a->{$k} =~ /$check/ 0 0 0
224 }
225 elsif ($check) {
226 47 $f{$k} = $a->{$k};
227 }
228 }
229  
230 41 ▶ if (%f) {
Branch: 1 branch
BranchTrueFalseTotal
if (%f) 31 10 41
231 31 my %seen;
232 return "<$t $r>"
233 if $r = join ' ', map {
234 defined $f{$_}
235 50 ▶ ? qq[$_="] . encode_entities( $f{$_} ) . q["]
Branch: 1 branch
BranchTrueFalseTotal
defined $f{$_} ? : 40 10 50
236 : $_; # boolean attribute (TODO?)
237 31 ▶ } grep { exists $f{$_} and !$seen{$_}++; } @$as;
Branch: 1 branch
BranchTrueFalseTotal
if $r = join(" ", map({defined $f{$_} ? qq[$_="] . encode_entities($f{$_}) . '"' : $_;} grep({exists $f{$_} and not $seen{$_}++;} @$as))) 31 0 31
Condition100%3/3
exists $f{$_} && !$seen{$_}++
!ll&&!rl&&r
1450
MC/DC100%2/2
exists $f{$_} && !$seen{$_}++
exists $f{$_} !$seen{$_}++
Truth table100%3/3
A && not(B)
A= exists $f{$_}B= !$seen{$_}++
ABresult
0X0
100
111
238 }
239  
240 10 return "<$t>";
241 }
242  
243  
244 504 ▶sub _scrub_str {
Subroutine _scrub_str: called CC=22 SCAR=30.9
245 504 my ( $p, $e, $t, $a, $as, $text ) = @_;
246  
247 504 my $s = $p->{"\0_s"};
248 504 my $outstr = '';
249  
250 504 ▶ if ( $e eq 'start' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($e eq 'start') { } 109 395 504
elsif ($e eq 'end') { } 79 316 395
elsif ($e eq 'comment') { } 14 302 316
elsif ($e eq 'process') { } 10 292 302
elsif ($e eq 'text' or $e eq 'default') { } 162 130 292
elsif ($e eq 'start_document') { } 65 65 130
251 109 ▶ if ( exists $s->{_rules}->{$t} ) # is there a specific rule
Branch: 2 branches
BranchTrueFalseTotal
if (exists $s->{'_rules'}{$t}) { } 61 48 109
elsif ($s->{'_rules'}{'*'}) { } 30 18 48
252 {
253 61 ▶ if ( ref $s->{_rules}->{$t} ) # is it complicated?(not simple;)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $s->{'_rules'}{$t}) { } 5 56 61
elsif ($s->{'_rules'}{$t}) { } 56 0 56
254 {
255 5 $outstr .= $s->_validate( $t, $t, $a, $as );
256 }
257 elsif ( $s->{_rules}->{$t} ) # validate using default attribute rule
258 {
259 56 $outstr .= $s->_validate( $t, '_', $a, $as );
260 }
261 }
262 elsif ( $s->{_rules}->{'*'} ) # default allow tags
263 {
264 30 $outstr .= $s->_validate( $t, '_', $a, $as );
265 }
266 }
267 elsif ( $e eq 'end' ) {
268  
269 # empty tags list taken from
270 # https://developer.mozilla.org/en/docs/Glossary/empty_element
271 79 my @empty_tags = qw(area base br col embed hr img input link meta param source track wbr);
272 79 ▶ return "" if $text ne '' && any { $t eq $_ } @empty_tags; # skip false closing empty tags
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $text ne "" and &any(sub { $t eq $_; } , @empty_tags) 16 63 79
Condition100%3/3
$text ne "" and &any(sub { $t eq $_; } , @empty_tags)
!ll&&!rl&&r
45916
MC/DC100%2/2
$text ne "" and &any(sub { $t eq $_; } , @empty_tags)
$text ne "" &any(sub { $t eq $_; } , @empty_tags)
Truth table100%3/3
A and B
A= $text ne ""B= &any(sub { $t eq $_; } , @empty_tags)
ABresult
0X0
100
111
273  
274 63 my $place = 0;
275 63 ▶ if ( exists $s->{_rules}->{$t} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $s->{'_rules'}{$t}) { } 27 36 63
elsif ($s->{'_rules'}{'*'}) { } 23 13 36
276 27 ▶ $place = 1 if $s->{_rules}->{$t};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_rules"}{$t} 27 0 27
277 }
278 elsif ( $s->{_rules}->{'*'} ) {
279 23 $place = 1;
280 }
281 63 ▶ if ($place) {
Branch: 1 branch
BranchTrueFalseTotal
if ($place) 50 13 63
282 50 ▶ if ( length $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) { } 46 4 50
283 46 $outstr .= "</$t>";
284 }
285 else {
286 4 substr $s->{_r}, -1, 0, ' /';
287 }
288 }
289 }
290 elsif ( $e eq 'comment' ) {
291 14 ▶ if ( $s->{_comment} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"_comment"}) 6 8 14
292  
293 # only copy comments through if they are well formed...
294 6 ▶ $outstr .= $text if ( $text =~ m|^<!--.*-->$|ms );
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^<!--.*-->$/ms 5 1 6
295 }
296 }
297 elsif ( $e eq 'process' ) {
298 10 ▶ $outstr .= $text if $s->{_process};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_process"} 2 8 10
299 }
300 ▶ elsif ( $e eq 'text' or $e eq 'default' ) {
Condition66%2/3
$e eq "text" or $e eq "default"
l!l&&r!l&&!r
1620130
MC/DC50%1/2
$e eq "text" or $e eq "default"
$e eq "text" $e eq "default"
Truth table66%2/3
A or B
A= $e eq "text"B= $e eq "default"
ABresult
1X1
011
000
301 162 $text =~ s/</&lt;/g; #https://rt.cpan.org/Public/Ticket/Attachment/83958/10332/scrubber.patch
302 162 $text =~ s/>/&gt;/g;
303  
304 162 $outstr .= $text;
305 }
306 elsif ( $e eq 'start_document' ) {
307 65 $outstr = "";
308 }
309  
310 488 return $outstr;
311 }
312  
313  
314 38 ▶sub _scrub_fh {
Subroutine _scrub_fh: called CC=2 SCAR=6.9
315 38 my $self = $_[0]->{"\0_s"};
316 38 ▶ print { $self->{_out} } $self->{'_r'} if length $self->{_r};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_r"} 12 26 38
317 38 $self->{'_r'} = _scrub_str(@_);
318 }
319  
320  
321 466 ▶sub _scrub {
Subroutine _scrub: called CC=1 SCAR=0.0
322  
323 466 $_[0]->{"\0_s"}->{_r} .= _scrub_str(@_);
324 }
325  
326 65 ▶sub _optimize {
Subroutine _optimize: called CC=5 SCAR=16.1
327 65 my ($self) = @_;
328  
329 65 my (@ignore_elements) = grep { not $self->{"_$_"} } qw(script style);
330 65 $self->{_p}->ignore_elements(@ignore_elements); # if @ is empty, we reset ;)
331  
332 65 ▶ return unless $self->{_optimize};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_optimize"} 39 26 65
333  
334 #sub allow
335 # return unless $self->{_optimize}; # till I figure it out (huh)
336  
337 26 ▶ if ( $self->{_rules}{'*'} ) { # default allow
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_rules'}{'*'}) { } 8 18 26
338 8 $self->{_p}->report_tags(); # so clear it
339 }
340 else {
341  
342 my (@reports) =
343 grep { # report only tags we want
344 76 $self->{_rules}{$_}
345 18 } keys %{ $self->{_rules} };
346  
347 $self->{_p}->report_tags( # default deny, so optimize
348 @reports
349 18 ▶ ) if @reports;
Branch: 1 branch
BranchTrueFalseTotal
if @reports 11 7 18
350 }
351  
352 # sub deny
353 # return unless $self->{_optimize}; # till I figure it out (huh)
354 my (@ignores) =
355 26 grep { not $self->{_rules}{$_} } grep { $_ ne '*' } keys %{ $self->{_rules} };
356  
357 $self->{_p}->ignore_tags( # always ignore stuff we don't want
358 @ignores
359 26 ▶ ) if @ignores;
Branch: 1 branch
BranchTrueFalseTotal
if @ignores 2 24 26
360  
361 26 $self->{_optimize} = 0;
362 26 return;
363 }
364  
365 16 1;
366  
367 #print sprintf q[ '%-12s => %s,], "$_'", $h{$_} for sort keys %h;# perl!
368 #perl -ne"chomp;print $_;print qq'\t\t# test ', ++$a if /ok\(/;print $/" test.pl >test2.pl
369 #perl -ne"chomp;print $_;if( /ok\(/ ){s/\#test \d+$//;print qq'\t\t# test ', ++$a }print $/" test.pl >test2.pl
370 #perl -ne"chomp;if(/ok\(/){s/# test .*$//;print$_,qq'\t\t# test ',++$a}else{print$_}print$/" test.pl >test2.pl
371  
372 __END__
↑ Summary