blib/lib/Catalyst/Plugin/HTML/Scrubber.pm

filter: Statementstmt 93.8% 92 / 98 Branchbran 75.7% 53 / 70 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 14 / 14 Podpod 33.3% 1 / 3 totaltotal 86.8% 178 / 205 CC 45 SCAR 23.4
CC
45
Cov
87%
_scrub_recurse
20.4
_should_scrub_param
14.3
_req_path_exempt_from_scrubbing
7.0
CRAP 10.3
↑ Summary
1 package Catalyst::Plugin::HTML::Scrubber;
2 5 $Catalyst::Plugin::HTML::Scrubber::VERSION = '0.09';
3 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 with 'Catalyst::ClassData';
7  
8 5 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTML::Scrubber;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 __PACKAGE__->mk_classdata('_scrubber');
12  
13 4 ▶sub setup {
Subroutine setup: called CC=4 SCAR=14.2
Pod documented
14 4 my $c = shift;
15  
16 4 my $conf = $c->config->{scrubber};
17 4 ▶ if (ref $conf eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $conf eq 'ARRAY') { } 0 4 4
elsif (ref $conf eq 'HASH') { } 3 1 4
18 0 $c->_scrubber(HTML::Scrubber->new(@$conf));
19 } elsif (ref $conf eq 'HASH') {
20 $c->config->{scrubber}{auto} = 1
21 3 ▶ unless defined $c->config->{scrubber}{auto};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->config->{"scrubber"}{"auto"} 0 3 3
22 3 $c->_scrubber(HTML::Scrubber->new(@{$conf->{params}}));
23 } else {
24 1 $c->_scrubber(HTML::Scrubber->new());
25 }
26  
27 4 return $c->maybe::next::method(@_);
28 }
29  
30 147 ▶sub execute {
Subroutine execute: called CC=4 SCAR=13.9
Pod undocumented
31 147 my $c = shift;
32  
33 147 $c->maybe::next::method(@_);
34  
35 147 my $conf = $c->config->{scrubber};
36  
37 # There are two ways to configure the plugin, it seems; giving a hashref
38 # of params under `scrubber`, with any params intended for HTML::Scrubber
39 # under the vaguely-named `params` key, or an arrayref of params intended
40 # to be passed straight to HTML::Scrubber - save html_scrub() from knowing
41 # about that by abstracting that nastyness away:
42 147 ▶ if (ref $conf ne 'HASH' || $conf->{auto}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conf ne "HASH" or $conf->{"auto"}) 111 36 147
Condition100%3/3
ref $conf ne "HASH" or $conf->{"auto"}
l!l&&r!l&&!r
248736
MC/DC100%2/2
ref $conf ne "HASH" or $conf->{"auto"}
ref $conf ne "HASH" $conf->{"auto"}
Truth table100%3/3
A or B
A= ref $conf ne "HASH"B= $conf->{"auto"}
ABresult
1X1
011
000
43 111 ▶ $c->html_scrub(ref($conf) eq 'HASH' ? $conf : {});
Branch: 1 branch
BranchTrueFalseTotal
ref $conf eq 'HASH' ? : 87 24 111
44 }
45 }
46  
47 113 ▶sub html_scrub {
Subroutine html_scrub: called CC=6 SCAR=18.0
Pod undocumented
48 113 my ($c, $conf) = @_;
49  
50 # Firstly, if an entry in ignore_urls matches, then we don't want to
51 # scrub anything for this request...
52 113 ▶ return if ($c->_req_path_exempt_from_scrubbing($conf));
Branch: 1 branch
BranchTrueFalseTotal
if $c->_req_path_exempt_from_scrubbing($conf) 12 101 113
53  
54 # If there's body_data - for e.g. a POSTed JSON body that was decoded -
55 # then we need to walk through it, scrubbing as appropriate; don't call
56 # body_data unless the content type is one there's a data handler for
57 # though, otherwise we'll trigger an exception (see GH#4)
58 101 ▶ if (exists $c->req->data_handlers->{ $c->req->content_type }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->req->data_handlers->{$c->req->content_type}) 83 18 101
59 83 ▶ if (my $body_data = $c->request->body_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $body_data = $c->request->body_data) 83 0 83
60 83 $c->_scrub_recurse($conf, $c->request->body_data);
61 }
62 }
63  
64 # And if Catalyst::Controller::REST is in use so we have $req->data,
65 # then scrub that too
66 101 ▶ if ($c->request->can('data')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->request->can("data")) 9 92 101
67 9 my $data = $c->request->data;
68 9 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 9 0 9
69 9 $c->_scrub_recurse($conf, $c->request->data);
70 }
71 }
72  
73 # Normal query/POST body parameters:
74 101 $c->_scrub_recurse($conf, $c->request->parameters);
75  
76 }
77  
78 # Recursively scrub param values...
79 241 ▶sub _scrub_recurse {
Subroutine _scrub_recurse: called CC=13 SCAR=30.2
80 241 my ($c, $conf, $data) = @_;
81  
82 # If the thing we've got is a hashref, walk over its keys, checking
83 # whether we should ignore, otherwise, do the needful
84 241 ▶ if (ref $data eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 217 24 241
elsif (ref $data eq 'ARRAY') { } 24 0 24
elsif (ref $data eq 'CODE') { } 0 0 0
85 217 for my $key (keys %$data) {
86 268 ▶ if (!$c->_should_scrub_param($conf, $key, $data->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->_should_scrub_param($conf, $key, $data->{$key})) 60 208 268
87 60 next;
88 }
89  
90 # OK, it's fine to fettle with this key - if its value is
91 # a ref, recurse, otherwise, scrub
92 208 ▶ if (my $ref = ref $data->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $data->{$key}) { } 48 160 208
93 $c->_scrub_recurse($conf, $data->{$key})
94 48 ▶ if defined $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$key} 48 0 48
95 } else {
96 # Alright, non-ref value, so scrub it
97 # FIXME why did we have to have this ref-ref handling fun?
98 #$_ = $c->_scrubber->scrub($_) for (ref($$value) ? @{$$value} : $$value);
99 $data->{$key} = $c->_scrub_value($conf, $data->{$key})
100 160 ▶ if defined $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$key} 160 0 160
101 }
102 }
103 } elsif (ref $data eq 'ARRAY') {
104 24 for (@$data) {
105 48 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 48 48
106 0 $c->_scrub_recurse($conf, $_);
107 } else {
108 48 ▶ $_ = $c->_scrub_value($conf, $_) if defined $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 48 0 48
109 }
110 }
111 } elsif (ref $data eq 'CODE') {
112 0 $c->log->debug("Can't scrub a coderef!");
113 } else {
114 # Note that at this point we don't know what the param was called
115 # as we'll have called ourself with the value, but that's fine as
116 # name-based checks were already done; we do need to pass the
117 # value ($data) along to allow value-based ignore_values to work.
118 0 ▶ $data = $c->_scrub_value($conf, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $c->_should_scrub_param($conf, "", $data) 0 0 0
119 if $c->_should_scrub_param($conf, '', $data);
120 }
121 }
122  
123  
124 # Wrap HTML::Scrubber's scrub() so we can decode HTML entities if needed
125 208 ▶sub _scrub_value {
Subroutine _scrub_value: called CC=3 SCAR=11.0
126 208 my ($c, $conf, $value) = @_;
127  
128 208 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 208 208
129
130 208 $value = $c->_scrubber->scrub($value);
131  
132 208 ▶ if ($conf->{no_encode_entities}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"no_encode_entities"}) 12 196 208
133 12 $value = HTML::Entities::decode_entities($value);
134 }
135 208 return $value;
136 }
137  
138 268 ▶sub _should_scrub_param {
Subroutine _should_scrub_param: called CC=14 SCAR=26.6
139 268 my ($c, $conf, $param, $value) = @_;
140 # If we only want to operate on certain params, do that checking
141 # now...
142 268 ▶ if ($conf && $conf->{ignore_params}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf and $conf->{"ignore_params"}) 228 40 268
Condition100%3/3
$conf and $conf->{"ignore_params"}
!ll&&!rl&&r
436228
MC/DC100%2/2
$conf and $conf->{"ignore_params"}
$conf $conf->{"ignore_params"}
Truth table100%3/3
A and B
A= $confB= $conf->{"ignore_params"}
ABresult
0X0
100
111
143 228 my $ignore_params = $c->config->{scrubber}{ignore_params};
144 228 ▶ if (ref $ignore_params ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ignore_params ne "ARRAY") 0 228 228
145 0 $ignore_params = [ $ignore_params ];
146 }
147 228 for my $ignore_param (@$ignore_params) {
148 420 ▶ if (ref $ignore_param eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ignore_param eq 'Regexp') { } 228 192 420
149 228 ▶ return if $param =~ $ignore_param;
Branch: 1 branch
BranchTrueFalseTotal
if $param =~ /$ignore_param/ 36 192 228
150 } else {
151 192 ▶ return if $param eq $ignore_param;
Branch: 1 branch
BranchTrueFalseTotal
if $param eq $ignore_param 12 180 192
152 }
153 }
154 }
155  
156 # For cases where there are legitimate values that HTML::Scrubber will
157 # munge... one example was an API where e.g. `<:100' would be eaten.
158 # To allow any param where a `<` is not followed by a `>` in the same
159 # param you could use qr{<[^]+$} or similar.
160 220 ▶ if ($conf && $conf->{ignore_values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf and $conf->{"ignore_values"}) 108 112 220
Condition100%3/3
$conf and $conf->{"ignore_values"}
!ll&&!rl&&r
4108108
MC/DC100%2/2
$conf and $conf->{"ignore_values"}
$conf $conf->{"ignore_values"}
Truth table100%3/3
A and B
A= $confB= $conf->{"ignore_values"}
ABresult
0X0
100
111
161 108 my $ignore_values = $conf->{ignore_values};
162 108 ▶ if (ref $ignore_values ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ignore_values ne "ARRAY") 0 108 108
163 0 $ignore_values = [ $ignore_values ];
164 }
165 108 for my $ignore_value (@$ignore_values) {
166 108 ▶ if (defined $value && $value =~ $ignore_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value =~ /$ignore_value/) 12 96 108
Condition66%2/3
defined $value and $value =~ /$ignore_value/
!ll&&!rl&&r
09612
MC/DC50%1/2
defined $value and $value =~ /$ignore_value/
defined $value $value =~ /$ignore_value/
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /$ignore_value/
ABresult
0X0
100
111
167 12 return;
168 }
169 }
170 }
171  
172 # If we've not bailed above, we didn't match any ignore_params
173 # entries, or didn't have any, so we do want to scrub
174 208 return 1;
175 }
176  
177  
178 113 ▶sub _req_path_exempt_from_scrubbing {
Subroutine _req_path_exempt_from_scrubbing: called CC=7 SCAR=19.5
179 113 my ($c, $conf) = @_;
180 113 ▶ return unless exists $conf->{ignore_paths};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf->{"ignore_paths"} 35 78 113
181  
182 78 my $req_path = $c->req->path;
183 78 ▶ $req_path = "/$req_path" unless $req_path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $req_path =~ m[^/] 78 0 78
184 78 for my $ignore (@{ $conf->{ignore_paths} }) {
185 150 ▶ if (ref $ignore eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ignore eq 'Regexp') { } 72 78 150
186 72 ▶ return 1 if $req_path =~ $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path =~ /$ignore/ 6 66 72
187 } else {
188 78 ▶ return 1 if $req_path eq $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path eq $ignore 6 72 78
189 }
190 }
191 }
192  
193 # Incredibly nasty monkey-patch to rewind filehandle before parsing - see
194 # https://github.com/perl-catalyst/catalyst-runtime/pull/186
195 # First, get the default handlers hashref:
196 5 my $default_data_handlers = Catalyst->default_data_handlers();
197  
198 # Wrap the coderef for application/json in one that rewinds the filehandle
199 # first:
200 5 my $orig_json_handler = $default_data_handlers->{'application/json'};
201 2 ▶$default_data_handlers->{'application/json'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
202 2 $_[0]->seek(0,0); # rewind $fh arg
203 2 $orig_json_handler->(@_);
204 5 };
205  
206  
207 {
208 # and now replace the original default_data_handlers() with a version that
209 # returns our modified handlers
210 5 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
211 4 ▶ *Catalyst::default_data_handlers = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 4 return $default_data_handlers;
213 5 };
214 }
215  
216 5 __PACKAGE__->meta->make_immutable;
217  
218 5 1;
219 __END__
↑ Summary