blib/lib/HTML/EscapeEvil.pm

filter: Statementstmt 69.9% 128 / 183 Branchbran 62.0% 36 / 58 Conditioncond 36.3% 16 / 44 MC/DCmcdc 3.8% 1 / 26 Subroutinesub 82.7% 24 / 29 Podpod 90.0% 18 / 20 totaltotal 61.9% 223 / 360 CC 49 SCAR 21.0
CC
49
Cov
63%
start
31.8
text
14.2
new
8.6
CRAP 8.2
↑ Summary
1 package HTML::EscapeEvil;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use base qw(HTML::Filter Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our ( $ENTITY_REGEXP, %JS_EVENT, $VERSION );
9  
10 5 __PACKAGE__->mk_accessors(
11 qw(allow_comment allow_declaration allow_process allow_entity_reference allow_script allow_style collection_process)
12 );
13 5 __PACKAGE__->mk_ro_accessors(qw(processes));
14  
15 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 $VERSION = 0.05;
18  
19 5 my @allow_entity_references =
20 ( "amp", "lt", "gt", "quot", "apos", "#039", "nbsp", "copy", "reg" );
21 5 $ENTITY_REGEXP = "&(" . ( join "|", @allow_entity_references ) . ")(;)";
22  
23 # ============================================================================= #
24 # allow javascript event handler setting
25 # cite : javascript in href. e.g. <a href="javascript:alert('hello')">hello</a>
26 # ============================================================================= #
27 5 %JS_EVENT = (
28 cite => 0,
29 onblur => 0,
30 onchange => 0,
31 onclick => 0,
32 ondblclick => 0,
33 onerror => 0,
34 onfocus => 0,
35 onkeydown => 0,
36 onkeypress => 0,
37 onkeyup => 0,
38 onload => 0,
39 onmousedown => 0,
40 onmousemove => 0,
41 onmouseout => 0,
42 onmouseover => 0,
43 onmouseup => 0,
44 onreset => 0,
45 onselect => 0,
46 onsubmit => 0,
47 onunload => 0,
48 );
49 }
50  
51 4 ▶sub new {
Subroutine new: called CC=8 SCAR=21.5
Pod documented
52  
53 4 my ( $class, %args ) = @_;
54 4 my $self = $class->SUPER::new;
55  
56 4 foreach (
57 qw(allow_comment allow_declaration allow_process allow_style allow_script collection_process)
58 )
59 {
60  
61 24 ▶ $self->{$_} = ( $args{$_} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{$_} ? : 11 13 24
62 }
63  
64 4 ▶ if ( $args{allow_entity_reference} ne ""
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'allow_entity_reference'} ne '' and $args{'allow_entity_reference'} == 0) { } 0 4 4
Condition66%2/3
$args{"allow_entity_reference"} ne "" and $args{"allow_entity_reference"} == 0
!ll&&!rl&&r
220
MC/DC0%0/2
$args{"allow_entity_reference"} ne "" and $args{"allow_entity_reference"} == 0
$args{"allow_entity_reference"} ne "" $args{"allow_entity_reference"} == 0
Truth table66%2/3
A and B
A= $args{"allow_entity_reference"} ne ""B= $args{"allow_entity_reference"} == 0
ABresult
0X0
100
111
65 && $args{allow_entity_reference} == 0 )
66 {
67  
68 0 $self->{allow_entity_reference} = 0;
69 }
70 else {
71  
72 4 $self->{allow_entity_reference} = 1;
73 }
74  
75 4 $self->{processes} = [];
76  
77 4 $self->{_content} = [];
78 4 $self->{_allow_tags} = {};
79  
80 4 ▶ bless $self, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
81  
82 4 ▶ if ( $args{allow_tags} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"allow_tags"}) 2 2 4
83  
84 $self->add_allow_tags( ( ref( $args{allow_tags} ) eq "ARRAY" )
85 2 ? @{ $args{allow_tags} }
86 2 ▶ : $args{allow_tags} );
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'allow_tags'} eq 'ARRAY' ? : 2 0 2
87 }
88  
89 4 return $self;
90 }
91  
92 0 ▶sub set_allow_tags {
Subroutine set_allow_tags: not called CC=1 SCAR=6.9
Pod documented
93  
94 0 my $self = shift;
95 0 $self->{_allow_tags} = {};
96 0 $self->{_current_tag} = undef;
97  
98 0 $self->{allow_script} = 0;
99 0 $self->{allow_style} = 0;
100 0 $self->{allow_comment} = 0;
101 0 $self->{allow_declaration} = 0;
102 0 $self->{allow_process} = 0;
103 0 $self->{allow_entity_reference} = 1;
104 0 $self->{collection_process} = 0;
105  
106 0 $self->clear_content;
107 0 $self->clear_process;
108 0 $self->add_allow_tags(@_);
109 }
110  
111 3 ▶sub add_allow_tags {
Subroutine add_allow_tags: called CC=4 SCAR=16.4
Pod documented
112  
113 3 my ( $self, @tags ) = @_;
114 3 foreach my $tag (@tags) {
115  
116 10 $tag = lc $tag;
117 10 ▶ if ( $tag eq "script" || $tag eq "style" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "script" or $tag eq "style") 0 10 10
Condition33%1/3
$tag eq "script" or $tag eq "style"
l!l&&r!l&&!r
0010
MC/DC0%0/2
$tag eq "script" or $tag eq "style"
$tag eq "script" $tag eq "style"
Truth table33%1/3
A or B
A= $tag eq "script"B= $tag eq "style"
ABresult
1X1
011
000
118  
119 0 $self->{"allow_$tag"} = 1;
120 0 next;
121 }
122 10 $self->{_allow_tags}->{$tag} = 1;
123 }
124  
125 }
126  
127 2 ▶sub deny_tags {
Subroutine deny_tags: called CC=4 SCAR=16.4
Pod documented
128  
129 2 my ( $self, @tags ) = @_;
130 2 foreach my $tag (@tags) {
131  
132 2 $tag = lc $tag;
133 2 ▶ if ( $tag eq "script" || $tag eq "style" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "script" or $tag eq "style") 0 2 2
Condition33%1/3
$tag eq "script" or $tag eq "style"
l!l&&r!l&&!r
002
MC/DC0%0/2
$tag eq "script" or $tag eq "style"
$tag eq "script" $tag eq "style"
Truth table33%1/3
A or B
A= $tag eq "script"B= $tag eq "style"
ABresult
1X1
011
000
134  
135 0 $self->{"allow_$tag"} = 0;
136 0 next;
137 }
138 2 delete $self->{_allow_tags}->{$tag};
139 }
140 }
141  
142 1 ▶sub get_allow_tags {
Subroutine get_allow_tags: called CC=3 SCAR=11.2
Pod documented
143  
144 1 my $self = shift;
145 1 my @tags = keys %{ $self->{_allow_tags} };
146 1 ▶ push @tags, "script" if $self->{allow_script};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_script"} 1 0 1
147 1 ▶ push @tags, "style" if $self->{allow_style};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_style"} 1 0 1
148 1 return sort { $a cmp $b } @tags;
149 }
150  
151 52 ▶sub is_allow_tags {
Subroutine is_allow_tags: called CC=5 SCAR=16.8
Pod documented
152  
153 52 my ( $self, $tag ) = @_;
154 52 my $flag;
155 52 $tag = lc $tag;
156 52 ▶ if ( $tag eq "script" || $tag eq "style" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'script' or $tag eq 'style') { } 0 52 52
Condition33%1/3
$tag eq "script" or $tag eq "style"
l!l&&r!l&&!r
0052
MC/DC0%0/2
$tag eq "script" or $tag eq "style"
$tag eq "script" $tag eq "style"
Truth table33%1/3
A or B
A= $tag eq "script"B= $tag eq "style"
ABresult
1X1
011
000
157  
158 0 $flag = $self->{"allow_$tag"};
159 }
160 else {
161  
162 52 ▶ $flag = ( exists $self->{_allow_tags}->{$tag} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_allow_tags'}{$tag} ? : 37 15 52
163 }
164 52 ▶ return ($flag) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 37 15 52
165 }
166  
167 0 ▶sub deny_all {
Subroutine deny_all: not called CC=1 SCAR=6.9
Pod documented
168  
169 0 my $self = shift;
170 0 $self->{_allow_tags} = {};
171 0 $self->{_current_tag} = undef;
172 0 $self->{allow_script} = 0;
173 0 $self->{allow_style} = 0;
174 0 $self->{allow_comment} = 0;
175 0 $self->{allow_declaration} = 0;
176 0 $self->{allow_process} = 0;
177 0 $self->{allow_entity_reference} = 0;
178 }
179  
180 4 ▶sub filtered_html {
Subroutine filtered_html: called CC=1 SCAR=0.0
Pod documented
181  
182 4 my $self = shift;
183 4 my $content = join "", @{ $self->{_content} };
184 4 $self->clear_content;
185 4 return $content;
186 }
187  
188 2 ▶sub filtered_file {
Subroutine filtered_file: called CC=4 SCAR=15.2
Pod documented
189  
190 2 my $self = shift;
191 2 my $fh;
192 2 ▶ ( ref( $_[0] ) eq "GLOB" || ref( \$_[0] ) eq "GLOB" )
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'GLOB' || ref \$_[0] eq 'GLOB' ? : 0 2 2
Condition40%2/5
ref $_[0] eq "GLOB" or ref \$_[0] eq "GLOB"
l!l&&r!l&&!r
002
open $fh, "> $_[0]" or croak($!)
l!l
20
MC/DC0%0/2
ref $_[0] eq "GLOB" or ref \$_[0] eq "GLOB"
ref $_[0] eq "GLOB" ref \$_[0] eq "GLOB"
MC/DC0%0/1
open $fh, "> $_[0]" or croak($!)
open $fh, "> $_[0]"
Truth table33%1/3
A or B
A= ref $_[0] eq "GLOB"B= ref \$_[0] eq "GLOB"
ABresult
1X1
011
000
Truth table50%1/2
A
A= open $fh, "> $_[0]"
Aresult
11
00
193 ? ( $fh = $_[0] )
194 : ( open $fh, "> $_[0]" or croak($!) );
195 2 print $fh $self->filtered_html;
196 2 close $fh;
197 }
198  
199 2 ▶sub filtered {
Subroutine filtered: called CC=6 SCAR=18.2
Pod documented
200  
201 2 my $self = shift;
202 2 my $content;
203 2 ▶ if ( -e $_[0] || ref( $_[0] ) eq "GLOB" || ref( \$_[0] ) eq "GLOB" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $_[0] or ref $_[0] eq 'GLOB' or ref \$_[0] eq 'GLOB') { } 1 1 2
elsif ($_[0] ne '') { } 1 0 1
Condition66%4/6
-e $_[0] or ref $_[0] eq "GLOB" or ref \$_[0] eq "GLOB"
l!l&&r!l&&!r
101
-e $_[0] or ref $_[0] eq "GLOB"
l!l&&r!l&&!r
101
MC/DC33%1/3
-e $_[0] or ref $_[0] eq "GLOB" or ref \$_[0] eq "GLOB"
-e $_[0] ref $_[0] eq "GLOB" ref \$_[0] eq "GLOB"
Truth table50%2/4
A or B or C
A= -e $_[0]B= ref $_[0] eq "GLOB"C= ref \$_[0] eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
204  
205 1 $self->parse_file( $_[0] );
206 }
207 elsif ( $_[0] ne "" ) {
208  
209 1 $self->parse( $_[0] );
210 }
211 else {
212  
213 0 croak("content is empty");
214 }
215  
216 2 ▶ if ( $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 1 1 2
217  
218 1 $self->filtered_file( $_[1] );
219 1 $content = 1;
220 }
221 else {
222  
223 1 $content = $self->filtered_html;
224 }
225  
226 2 $self->eof;
227 2 $self->{_current_tag} = undef;
228  
229 2 return $content;
230 }
231  
232 7 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
233  
234 7 my $self = shift;
235 7 $self->eof;
236 7 $self->clear_process;
237 7 $self->clear_content;
238 7 $self->{_current_tag} = undef;
239 }
240  
241 11 ▶sub clear_content {
Subroutine clear_content: called CC=2 SCAR=6.9
Pod undocumented
242  
243 11 my $self = shift;
244 11 ▶ $self->{_content} = [] if scalar @{ $self->{_content} };
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"_content"};} 5 6 11
245 }
246  
247 7 ▶sub clear_process {
Subroutine clear_process: called CC=2 SCAR=6.9
Pod documented
248  
249 7 my $self = shift;
250 7 ▶ $self->{processes} = [] if scalar @{ $self->{processes} };
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"processes"};} 1 6 7
251 }
252  
253 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
254  
255 4 my $self = shift;
256 4 $self->clear;
257 }
258  
259 53 ▶sub _escape {
Subroutine _escape: called CC=1 SCAR=0.0
260  
261 53 my $string = shift;
262 53 $string =~ s/&/&amp;/g;
263 53 $string =~ s/</&lt;/g;
264 53 $string =~ s/>/&gt;/g;
265 53 $string =~ s/\"/&quot;/g;
266 53 $string =~ s/\'/&#039;/g;
267 53 return $string;
268 }
269  
270 0 ▶sub _unescape {
Subroutine _unescape: not called CC=1 SCAR=6.9
271  
272 0 my $string = shift;
273 0 $string =~ s/&amp;/&/g;
274 0 $string =~ s/&lt;/</g;
275 0 $string =~ s/&gt;/>/g;
276 0 $string =~ s/&quot;/\"/g;
277 0 $string =~ s/&#039;/\'/g;
278 0 $string =~ s/&apos;/\'/g;
279 0 return $string;
280 }
281  
282 39 ▶sub _unescape_entities {
Subroutine _unescape_entities: called CC=1 SCAR=0.0
283  
284 39 my $string = shift;
285 39 $string =~ s/$ENTITY_REGEXP/\&$1$2/g;
286 39 return $string;
287 }
288  
289 # ============================== override method start ============================== #
290  
291 0 ▶sub declaration {
Subroutine declaration: not called CC=2 SCAR=17.9
Pod documented
292  
293 0 my ( $self, $declaration ) = @_;
294 0 $declaration = "<!$declaration>";
295 $self->output( ( $self->{allow_declaration} )
296 0 ▶ ? $declaration
Branch: 1 branch
BranchTrueFalseTotal
$self->{'allow_declaration'} ? : 0 0 0
297 : &_escape($declaration) );
298 }
299  
300 3 ▶sub process {
Subroutine process: called CC=3 SCAR=11.0
Pod documented
301  
302 3 my ( $self, $process, $process_text ) = @_;
303 3 ▶ if ( $self->{collection_process} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"collection_process"}) 1 2 3
304  
305 1 my $tmp_process = $process;
306 1 $tmp_process =~ s/\?$//;
307 1 push @{ $self->{processes} }, $tmp_process;
308 }
309 $self->SUPER::process( $process,
310 3 ▶ ( $self->{allow_process} ) ? $process_text : &_escape($process_text) );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'allow_process'} ? : 3 0 3
311 }
312  
313 25 ▶sub start {
Subroutine start: called CC=8 SCAR=34.6
Pod documented
314  
315 25 my ( $self, $tagname, $attr, $attrseq, $text ) = @_;
316 25 $self->{_current_tag} = lc $tagname;
317 25 ▶ if ( $self->is_allow_tags($tagname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_allow_tags($tagname)) { } 18 7 25
318  
319 18 ▶ if ( !$self->allow_script ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->allow_script) 0 18 18
320 ## change javascript event handler(1 : allow) e.g <body onload="alert(1)"> => <body onload="void(0)">
321 0 foreach ( keys %{$attr} ) {
322  
323 0 my $event = lc $_;
324 0 ▶ if ( exists $JS_EVENT{$event} && !$JS_EVENT{$event} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $JS_EVENT{$event} and not $JS_EVENT{$event}) 0 0 0
Condition0%0/3
exists $JS_EVENT{$event} and not $JS_EVENT{$event}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $JS_EVENT{$event} and not $JS_EVENT{$event}
exists $JS_EVENT{$event} not $JS_EVENT{$event}
Truth table0%0/3
A and not(B)
A= exists $JS_EVENT{$event}B= not $JS_EVENT{$event}
ABresult
0X0
100
111
325  
326 #delete $attr->{$event};
327 0 $attr->{$event} = "void(0)";
328 }
329 }
330  
331 ## change javascript <a href="javascript:evil_script('evil')"> => <a href="javascript:void(0)">
332 0 ▶ if ( !$JS_EVENT{cite} && $attr->{href} =~ /^(java|vb)script:/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $JS_EVENT{"cite"} and $attr->{"href"} =~ /^(java|vb)script:/i) 0 0 0
Condition0%0/3
not $JS_EVENT{"cite"} and $attr->{"href"} =~ /^(java|vb)script:/i
!ll&&!rl&&r
000
MC/DC0%0/2
not $JS_EVENT{"cite"} and $attr->{"href"} =~ /^(java|vb)script:/i
not $JS_EVENT{"cite"} $attr->{"href"} =~ /^(java|vb)script:/i
Truth table0%0/3
not(A) and B
A= not $JS_EVENT{"cite"}B= $attr->{"href"} =~ /^(java|vb)script:/i
ABresult
0X0
100
111
333  
334 0 $attr->{href} = "javascript:void(0)";
335 }
336 ## tag is generated again
337 0 my $element = HTML::Element->new( $tagname, %{$attr} );
338 0 $text = $element->starttag;
339 0 $element->delete;
340 0 $element = undef;
341 }
342 }
343 else {
344 7 $text = &_escape($text);
345 }
346 25 $self->SUPER::start( $tagname, $attr, $attrseq, $text );
347 }
348  
349 25 ▶sub end {
Subroutine end: called CC=2 SCAR=6.9
Pod documented
350  
351 25 my ( $self, $tagname, $text ) = @_;
352 25 $self->{_current_tag} = undef;
353 25 ▶ $text = &_escape($text) if !$self->is_allow_tags($tagname);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_allow_tags($tagname) 7 18 25
354 25 $self->SUPER::end( $tagname, $text );
355 }
356  
357 0 ▶sub comment {
Subroutine comment: not called CC=2 SCAR=17.9
Pod documented
358  
359 0 my ( $self, $comment ) = @_;
360 0 $comment = "<!--$comment-->";
361 0 ▶ $self->output( ( $self->{allow_comment} ) ? $comment : &_escape($comment) );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'allow_comment'} ? : 0 0 0
362 }
363  
364 39 ▶sub text {
Subroutine text: called CC=8 SCAR=26.5
Pod documented
365  
366 39 my ( $self, $text, $is_cdata ) = @_;
367 39 $text = &_escape($text);
368 39 ▶ $text = &_unescape_entities($text) if $self->{allow_entity_reference};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_entity_reference"} 39 0 39
369 $text = &_unescape($text)
370 if $is_cdata
371 && $self->{_current_tag} eq "script"
372 39 ▶ && $self->{allow_script};
Branch: 1 branch
BranchTrueFalseTotal
if $is_cdata and $self->{"_current_tag"} eq "script" and $self->{"allow_script"} 0 39 39
Condition33%2/6
$is_cdata and $self->{"_current_tag"} eq "script" and $self->{"allow_script"}
!ll&&!rl&&r
3900
$is_cdata and $self->{"_current_tag"} eq "script"
!ll&&!rl&&r
3900
MC/DC0%0/3
$is_cdata and $self->{"_current_tag"} eq "script" and $self->{"allow_script"}
$is_cdata $self->{"_current_tag"} eq "script" $self->{"allow_script"}
Truth table25%1/4
A and B and C
A= $is_cdataB= $self->{"_current_tag"} eq "script"C= $self->{"allow_script"}
ABCresult
0XX0
10X0
1100
1111
373 $text = &_unescape($text)
374 39 ▶ if $is_cdata && $self->{_current_tag} eq "style" && $self->{allow_style};
Branch: 1 branch
BranchTrueFalseTotal
if $is_cdata and $self->{"_current_tag"} eq "style" and $self->{"allow_style"} 0 39 39
Condition33%2/6
$is_cdata and $self->{"_current_tag"} eq "style" and $self->{"allow_style"}
!ll&&!rl&&r
3900
$is_cdata and $self->{"_current_tag"} eq "style"
!ll&&!rl&&r
3900
MC/DC0%0/3
$is_cdata and $self->{"_current_tag"} eq "style" and $self->{"allow_style"}
$is_cdata $self->{"_current_tag"} eq "style" $self->{"allow_style"}
Truth table25%1/4
A and B and C
A= $is_cdataB= $self->{"_current_tag"} eq "style"C= $self->{"allow_style"}
ABCresult
0XX0
10X0
1100
1111
375 39 $self->SUPER::text( $text, $is_cdata );
376 }
377  
378 92 ▶sub output {
Subroutine output: called CC=1 SCAR=0.0
Pod undocumented
379  
380 92 my ( $self, $content ) = @_;
381 92 push @{ $self->{_content} }, $content;
382 }
383  
384 5 1;
385  
386 __END__
↑ Summary