IDS/Whitelist.pm

filter: Statementstmt 14.0% 20 / 142 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 35.2% 6 / 17 Podpod 100% 8 / 8 totaltotal 13.0% 34 / 261 CC 41 SCAR 51.9
CC
41
Cov
9%
is_suspicious
420.0
_load_whitelist_from_xml
132.0
_datastructure_to_string
30.0
CRAP 178.8
↑ Summary IDS.pm »
1 package CGI::IDS::Whitelist;
2  
3 1 our $VERSION = '1.0217';
4  
5 #------------------------- Notes -----------------------------------------------
6 # This source code is documented in both POD and ROBODoc format.
7 # Please find additional POD documentation at the end of this file
8 # (search for "__END__").
9 #-------------------------------------------------------------------------------
10  
11 #****c* IDS::Whitelist
12 # NAME
13 # PerlIDS Whitelist (CGI::IDS::Whitelist)
14 # DESCRIPTION
15 # Whitelist Processor for PerlIDS (CGI::IDS)
16 # AUTHOR
17 # Hinnerk Altenburg <hinnerk@cpan.org>
18 # CREATION DATE
19 # 2010-03-29
20 # COPYRIGHT
21 # Copyright (C) 2010-2014 Hinnerk Altenburg
22 #
23 # This file is part of PerlIDS.
24 #
25 # PerlIDS is free software: you can redistribute it and/or modify
26 # it under the terms of the GNU Lesser General Public License as published by
27 # the Free Software Foundation, either version 3 of the License, or
28 # (at your option) any later version.
29 #
30 # PerlIDS is distributed in the hope that it will be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 # GNU Lesser General Public License for more details.
34 #
35 # You should have received a copy of the GNU Lesser General Public License
36 # along with PerlIDS. If not, see <http://www.gnu.org/licenses/>.
37  
38 #****
39  
40 =head1 NAME
41  
42 CGI::IDS::Whitelist - Whitelist Processor for PerlIDS - Perl Website Intrusion Detection System (XSS, CSRF, SQLI, LFI etc.)
43  
44 =head1 DESCRIPTION
45  
46 Whitelist Processor for PerlIDS (L<CGI::IDS|CGI::IDS>). Performs a basic string check and the whitelist check.
47 See section L<CGI::IDS/Whitelist> for details on setting up a whitelist file. CGI::IDS::Whitelist may also be
48 used standalone without CGI::IDS to check whether a request has suspicious parameters at all before
49 handing it over to CGI::IDS. This may be the case if you let worker servers do the more expensive
50 CGI::IDS job and only want to send over the requests that have suspicious parameters.
51 See L<SYNOPSIS|CGI::IDS::Whitelist/SYNOPSIS> for an example.
52  
53 =head1 SYNOPSIS
54  
55 use CGI;
56 use CGI::IDS::Whitelist;
57  
58 $query = new CGI;
59  
60 my $whitelist = CGI::IDS::Whitelist->new(
61 whitelist_file => '/home/hinnerk/sandbox/ids/cgi-bin/param_whitelist.xml',
62 );
63  
64 my @request_keys = keys %$query->Vars;
65 foreach my $key (@request_keys) {
66 if ( $whitelist->is_suspicious(key => $key, request => $query->Vars ) {
67 send_to_ids_worker_server( $query->Vars );
68 last;
69 }
70 }
71  
72 =head1 METHODS
73  
74 =cut
75  
76 #------------------------- Pragmas ---------------------------------------------
77 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 #------------------------- Libs ------------------------------------------------
81 1 ▶use XML::Simple qw(:strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 #------------------------- Subs ------------------------------------------------
87  
88 #****m* IDS/new
89 # NAME
90 # Constructor
91 # DESCRIPTION
92 # Creates a Whitelist object.
93 # The whitelist will stay loaded during the lifetime of the object.
94 # You may call is_suspicious() multiple times, the collecting debug
95 # arrays suspicious_keys() and non_suspicious_keys() will only be
96 # emptied by an explizit reset() call.
97 # INPUT
98 # HASH
99 # whitelist_file STRING The path to the whitelist XML file
100 # OUTPUT
101 # Whitelist object, dies (croaks) if a whitelist parsing error occurs.
102 # EXAMPLE
103 # # instantiate object
104 # my $whitelist = CGI::IDS::Whitelist->new(
105 # whitelist_file => '/home/hinnerk/sandbox/ids/cgi-bin/param_whitelist.xml',
106 # );
107 # # instantiate object without a whitelist, just performs a basic string check
108 # my $whitelist = CGI::IDS::Whitelist->new();
109  
110 #****
111  
112 =head2 new()
113  
114 Constructor. Can optionally take the path to a whitelist file.
115 If I<whitelist_file> is not given, just a basic string check will be performed.
116  
117 The whitelist will stay loaded during the lifetime of the object.
118 You may call C<is_suspicious()> multiple times, the collecting debug
119 arrays C<suspicious_keys()> and C<non_suspicious_keys()> will only be
120 emptied by an explizit C<reset()> call.
121  
122 For example, the following are valid constructors:
123  
124 my $whitelist = CGI::IDS::Whitelist->new(
125 whitelist_file => '/home/hinnerk/sandbox/ids/cgi-bin/param_whitelist.xml',
126 );
127  
128 my $whitelist = CGI::IDS::Whitelist->new();
129  
130 The Constructor dies (croaks) if a whitelist parsing error occurs.
131  
132 =cut
133  
134 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
135 0 my ($package, %args) = @_;
136  
137 # self member variables
138 my $self = {
139 whitelist_file => $args{whitelist_file},
140 0 suspicious_keys => [],
141 non_suspicious_keys => [],
142 };
143  
144 # create object
145 0 bless $self, $package;
146  
147 # read & parse XML
148 0 $self->_load_whitelist_from_xml($self->{whitelist_file});
149  
150 0 return $self;
151 }
152  
153 #****m* IDS/Whitelist/is_suspicious
154 # NAME
155 # is_suspicious
156 # DESCRIPTION
157 # Performs the whitelist check for a given request parameter.
158 # INPUT
159 # HASHREF
160 # + key The key of the request parameter to be checked
161 # + request HASHREF to the complete request (for whitelist conditions check)
162 # OUTPUT
163 # 1 if you should check it with the complete filter set,
164 # 0 if harmless or sucessfully whitelisted.
165 # SYNOPSIS
166 # $whitelist->is_suspicious( key => 'mykey', request => $request );
167 #****
168  
169 =head2 is_suspicious()
170  
171 DESCRIPTION
172 Performs the whitelist check for a given request parameter.
173 INPUT
174 HASHREF
175 + key The key of the request parameter to be checked
176 + request HASHREF to the complete request (for whitelist conditions check)
177 OUTPUT
178 1 if you should check it with the complete filter set,
179 0 if harmless or sucessfully whitelisted.
180 SYNOPSIS
181 $whitelist->is_suspicious( key => 'mykey', request => $request );
182  
183 =cut
184  
185 0 ▶sub is_suspicious {
Subroutine is_suspicious: not called CC=20 SCAR=60.4
Pod documented
186 0 my ($self, %args) = @_;
187 0 my $key = $args{key};
188 0 my $request = $args{request};
189 0 my $request_value = $args{request}->{$key};
190 0 my $contains_encoding = 0;
191  
192 # skip if value is empty or generally whitelisted
193 0 ▶ if ( $request_value ne '' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($request_value ne '' and not $self->{'whitelist'}{$key} && !defined($self->{'whitelist'}{$key}{'rule'}) && !defined($self->{'whitelist'}{$key}{'conditions'}) && !defined($self->{'whitelist'}{$key}{'encoding'})) { } 0 0 0
Condition0%0/12
$request_value ne "" and not $self->{"whitelist"}{$key} && !defined($self->{"whitelist"}{$key}{"rule"}) && !defined($self->{"whitelist"}{$key}{"conditions"}) && !defined($self->{"whitelist"}{$key}{"encoding"})
!ll&&!rl&&r
000
$self->{'whitelist'}{$key} && !defined($self->{'whitelist'}{$key}{'rule'}) && !defined($self->{'whitelist'}{$key}{'conditions'}) && !defined($self->{'whitelist'}{$key}{'encoding'})
!ll&&!rl&&r
000
$self->{"whitelist"}{$key} and not defined $self->{"whitelist"}{$key}{"rule"} and not defined $self->{"whitelist"}{$key}{"conditions"}
!ll&&!rl&&r
000
$self->{"whitelist"}{$key} and not defined $self->{"whitelist"}{$key}{"rule"}
!ll&&!rl&&r
000
MC/DC0%0/5
$request_value ne "" and not $self->{"whitelist"}{$key} && !defined($self->{"whitelist"}{$key}{"rule"}) && !defined($self->{"whitelist"}{$key}{"conditions"}) && !defined($self->{"whitelist"}{$key}{"encoding"})
$request_value ne "" $self->{"whitelist"}{$key} not defined $self->{"whitelist"}{$key}{"rule"} not defined $self->{"whitelist"}{$key}{"conditions"} !defined($self->{'whitelist'}{$key}{'encoding'})
Truth table0%0/6
A and not(B and not(C) and not(D) && not(E))
A= $request_value ne ""B= $self->{"whitelist"}{$key}C= not defined $self->{"whitelist"}{$key}{"rule"}D= not defined $self->{"whitelist"}{$key}{"conditions"}E= !defined($self->{'whitelist'}{$key}{'encoding'})
ABCDEresult
0XXXX0
111110
10XXX1
110XX1
1110X1
111101
194 !( $self->{whitelist}{$key} &&
195 !defined($self->{whitelist}{$key}->{rule}) &&
196 !defined($self->{whitelist}{$key}->{conditions}) &&
197 !defined($self->{whitelist}{$key}->{encoding})
198 )
199 ) {
200 0 my $request_value_orig = $request_value;
201 0 $request_value = $self->convert_if_marked_encoded(key => $key, value => $request_value);
202 0 ▶ if ($request_value ne $request_value_orig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_value ne $request_value_orig) 0 0 0
203 0 $contains_encoding = 1;
204 }
205  
206 0 $request_value = $self->make_utf_8($request_value);
207  
208 # scan only if value is not harmless
209 0 ▶ if ( !$self->is_harmless_string($request_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_harmless_string($request_value)) { } 0 0 0
210 0 my $attacks = {};
211  
212 0 ▶ if (!$self->{whitelist}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'whitelist'}{$key}) { } 0 0 0
213 # apply filters to value, not in whitelist
214 0 push (@{$self->{suspicious_keys}}, {key => $key, value => $request_value, reason => 'key'}); # key not whitelisted
215 0 return 1;
216 }
217 else {
218 # check if all conditions match
219 0 my $condition_mismatch = 0;
220 0 foreach my $condition (@{$self->{whitelist}{$key}->{conditions}}) {
221 0 ▶ if (! defined($request->{$condition->{key}}) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$request{$condition->{"key"}} or defined $condition->{"rule"} and not $$request{$condition->{"key"}} =~ /$condition->{"rule"}/) 0 0 0
Condition0%0/6
not defined $$request{$condition->{"key"}} or defined $condition->{"rule"} and not $$request{$condition->{"key"}} =~ /$condition->{"rule"}/
l!l&&r!l&&!r
000
defined $condition->{"rule"} and not $$request{$condition->{"key"}} =~ /$condition->{"rule"}/
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $$request{$condition->{"key"}} or defined $condition->{"rule"} and not $$request{$condition->{"key"}} =~ /$condition->{"rule"}/
not defined $$request{$condition->{"key"}} defined $condition->{"rule"} not $$request{$condition->{"key"}} =~ /$condition->{"rule"}/
Truth table0%0/4
not(A) or B and not(C)
A= not defined $$request{$condition->{"key"}}B= defined $condition->{"rule"}C= not $$request{$condition->{"key"}} =~ /$condition->{"rule"}/
ABCresult
1XX1
0111
00X0
0100
222 ( defined ($condition->{rule}) && $request->{$condition->{key}} !~ $condition->{rule} )
223 ) {
224 0 $condition_mismatch = 1;
225 }
226 }
227  
228 # Apply filters if key is not in whitelisted environment conditions
229 # or if the value does not match the whitelist rule if one is set.
230 # Filtering is skipped if no rule is set.
231 0 ▶ if ( $condition_mismatch ||
Branch: 1 branch
BranchTrueFalseTotal
if ($condition_mismatch or defined $self->{'whitelist'}{$key}{'rule'} and not $request_value =~ /$self->{'whitelist'}{$key}{'rule'}/ or $contains_encoding) { } 0 0 0
Condition0%0/9
$condition_mismatch or defined $self->{"whitelist"}{$key}{"rule"} and not $request_value =~ /$self->{"whitelist"}{$key}{"rule"}/ or $contains_encoding
l!l&&r!l&&!r
000
$condition_mismatch or defined $self->{"whitelist"}{$key}{"rule"} and not $request_value =~ /$self->{"whitelist"}{$key}{"rule"}/
l!l&&r!l&&!r
000
defined $self->{"whitelist"}{$key}{"rule"} and not $request_value =~ /$self->{"whitelist"}{$key}{"rule"}/
!ll&&!rl&&r
000
MC/DC0%0/4
$condition_mismatch or defined $self->{"whitelist"}{$key}{"rule"} and not $request_value =~ /$self->{"whitelist"}{$key}{"rule"}/ or $contains_encoding
$condition_mismatch defined $self->{"whitelist"}{$key}{"rule"} not $request_value =~ /$self->{"whitelist"}{$key}{"rule"}/ $contains_encoding
Truth table0%0/6
A or B and not(C) or D
A= $condition_mismatchB= defined $self->{"whitelist"}{$key}{"rule"}C= not $request_value =~ /$self->{"whitelist"}{$key}{"rule"}/D= $contains_encoding
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
232 (defined($self->{whitelist}{$key}->{rule}) &&
233 $request_value !~ $self->{whitelist}{$key}->{rule}) ||
234 $contains_encoding
235 ) {
236 # apply filters to value, whitelist rules mismatched
237 0 my $reason = '';
238 0 ▶ if ($condition_mismatch) {
Branch: 2 branches
BranchTrueFalseTotal
if ($condition_mismatch) { } 0 0 0
elsif (not $contains_encoding) { } 0 0 0
239 0 $reason = 'cond'; # condition mismatch
240 }
241 elsif (!$contains_encoding) {
242 0 $reason = 'rule'; # rule mismatch
243 }
244 else {
245 0 $reason = 'enc'; # contains encoding
246 }
247 0 push (@{$self->{suspicious_keys}}, {key => $key, value => $request_value, reason => $reason});
248 0 return 1;
249 }
250 else {
251 # skipped, whitelist rule matched
252 0 push (@{$self->{non_suspicious_keys}}, {key => $key, value => $request_value, reason => 'r&c'}); # rule & conditions matched
253 }
254 }
255 }
256 else {
257 # skipped, harmless string
258 0 push (@{$self->{non_suspicious_keys}}, {key => $key, value => $request_value, reason => 'harml'}); # harmless
259 }
260 }
261 else {
262 # skipped, empty value or key generally whitelisted
263 0 ▶ my $reason = $request_value ? 'key' : 'empty';
Branch: 1 branch
BranchTrueFalseTotal
$request_value ? : 0 0 0
264 0 push (@{$self->{non_suspicious_keys}}, {key => $key, value => $request_value, reason => $reason});
265 }
266 0 return 0;
267 }
268  
269 #****m* IDS/Whitelist/convert_if_marked_encoded
270 # NAME
271 # convert_if_marked_encoded
272 # DESCRIPTION
273 # Tries to JSON-decode and flatten a value to a plain string if the key has been marked as JSON in the whitelist.
274 # Other encodings may follow in future.
275 # INPUT
276 # HASHREF
277 # + key
278 # + value
279 # OUTPUT
280 # The JSON-decoded and flattened 'value' if key is marked JSON. Plain keys and values, newline separated.
281 # Untouched 'value' otherwise.
282 # SYNOPSIS
283 # $whitelist->convert_if_marked_encoded( key => 'data', value = '{"a":"b","c":["123", 111, "456"]}');
284 #****
285  
286 =head2 convert_if_marked_encoded()
287  
288 DESCRIPTION
289 Tries to JSON-decode and flatten a value to a plain string if the key has been marked as JSON in the whitelist.
290 Other encodings may follow in future.
291 INPUT
292 HASHREF
293 + key
294 + value
295 OUTPUT
296 The JSON-decoded and flattened 'value' if key is marked JSON. Plain keys and values, newline separated.
297 Untouched 'value' otherwise.
298 SYNOPSIS
299 $whitelist->convert_if_marked_encoded( key => 'data', value => '{"a":"b","c":["123", 111, "456"]}');
300  
301 =cut
302  
303 0 ▶sub convert_if_marked_encoded {
Subroutine convert_if_marked_encoded: not called CC=4 SCAR=30.0
Pod documented
304 0 my ($self, %args) = @_;
305 0 my $key = $args{key};
306 0 my $request_value = $args{value};
307  
308 # If marked as JSON, try to convert from JSON to reduce false positives
309 0 ▶ if (defined($self->{whitelist}{$key}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"whitelist"}{$key} and defined $self->{"whitelist"}{$key}{"encoding"} and $self->{"whitelist"}{$key}{"encoding"} eq "json") 0 0 0
Condition0%0/6
defined $self->{"whitelist"}{$key} and defined $self->{"whitelist"}{$key}{"encoding"} and $self->{"whitelist"}{$key}{"encoding"} eq "json"
!ll&&!rl&&r
000
defined $self->{"whitelist"}{$key} and defined $self->{"whitelist"}{$key}{"encoding"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{"whitelist"}{$key} and defined $self->{"whitelist"}{$key}{"encoding"} and $self->{"whitelist"}{$key}{"encoding"} eq "json"
defined $self->{"whitelist"}{$key} defined $self->{"whitelist"}{$key}{"encoding"} $self->{"whitelist"}{$key}{"encoding"} eq "json"
Truth table0%0/4
A and B and C
A= defined $self->{"whitelist"}{$key}B= defined $self->{"whitelist"}{$key}{"encoding"}C= $self->{"whitelist"}{$key}{"encoding"} eq "json"
ABCresult
0XX0
10X0
1100
1111
310 defined($self->{whitelist}{$key}->{encoding}) &&
311 $self->{whitelist}{$key}->{encoding} eq 'json') {
312  
313 0 $request_value = _json_to_string($request_value);
314 }
315 0 return $request_value;
316 }
317  
318 #****m* IDS/Whitelist/suspicious_keys
319 # NAME
320 # suspicious_keys
321 # DESCRIPTION
322 # Returns the set of filters that are suspicious
323 # Keys are listed from the last reset() or Whitelist->new()
324 # INPUT
325 # none
326 # OUTPUT
327 # [ { 'value' => , 'reason' => , 'key' => }, { ... } ]
328 # SYNOPSIS
329 # $whitelist->suspicious_keys();
330 #****
331  
332 =head2 suspicious_keys()
333  
334 DESCRIPTION
335 Returns the set of filters that are suspicious
336 Keys are listed from the last reset() or Whitelist->new()
337 INPUT
338 none
339 OUTPUT
340 [ { 'value' => , 'reason' => , 'key' => }, { ... } ]
341 SYNOPSIS
342 $whitelist->suspicious_keys();
343  
344 =cut
345  
346 0 ▶sub suspicious_keys {
Subroutine suspicious_keys: not called CC=1 SCAR=6.9
Pod documented
347 0 my ($self) = @_;
348 0 return $self->{suspicious_keys};
349 }
350  
351 #****m* IDS/Whitelist/non_suspicious_keys
352 # NAME
353 # non_suspicious_keys
354 # DESCRIPTION
355 # Returns the set of filters that have been checked but are not suspicious
356 # Keys are listed from the last reset() or Whitelist->new()
357 # INPUT
358 # none
359 # OUTPUT
360 # [ { 'value' => , 'reason' => , 'key' => }, { ... } ]
361 # SYNOPSIS
362 # $whitelist->non_suspicious_keys();
363 #****
364  
365 =head2 non_suspicious_keys()
366  
367 DESCRIPTION
368 Returns the set of filters that have been checked but are not suspicious
369 Keys are listed from the last reset() or Whitelist->new()
370 INPUT
371 none
372 OUTPUT
373 [ { 'value' => , 'reason' => , 'key' => }, { ... } ]
374 SYNOPSIS
375 $whitelist->non_suspicious_keys();
376  
377 =cut
378  
379 0 ▶sub non_suspicious_keys {
Subroutine non_suspicious_keys: not called CC=1 SCAR=6.9
Pod documented
380 0 my ($self) = @_;
381 0 return $self->{non_suspicious_keys};
382 }
383  
384 #****m* IDS/Whitelist/reset
385 # NAME
386 # reset
387 # DESCRIPTION
388 # resets the member variables suspicious_keys and non_suspicious_keys to []
389 # INPUT
390 # none
391 # OUTPUT
392 # none
393 # SYNOPSIS
394 # $whitelist->reset();
395 #****
396  
397 =head2 reset()
398  
399 DESCRIPTION
400 resets the member variables suspicious_keys and non_suspicious_keys to []
401 INPUT
402 none
403 OUTPUT
404 none
405 SYNOPSIS
406 $whitelist->reset();
407  
408 =cut
409  
410 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
411 0 my ($self) = @_;
412 0 $self->{suspicious_keys} = [];
413 0 $self->{non_suspicious_keys} = [];
414 }
415  
416 #****f* IDS/Whitelist/is_harmless_string
417 # NAME
418 # is_harmless_string
419 # DESCRIPTION
420 # Performs a basic regexp check for harmless characters
421 # INPUT
422 # + string
423 # OUTPUT
424 # BOOLEAN (pattern match return value)
425 # SYNOPSIS
426 # $whitelist->is_harmless_string( $string );
427 #****
428  
429 =head2 is_harmless_string()
430  
431 DESCRIPTION
432 Performs a basic regexp check for harmless characters
433 INPUT
434 + string
435 OUTPUT
436 BOOLEAN (pattern match return value)
437 SYNOPSIS
438 $whitelist->is_harmless_string( $string );
439  
440 =cut
441  
442 0 ▶sub is_harmless_string {
Subroutine is_harmless_string: not called CC=1 SCAR=6.9
Pod documented
443 0 my ($self, $string) = @_;
444  
445 0 $string = $self->make_utf_8($string);
446  
447 0 return ( $string !~ m/[^\w\s\/@!?\.]+|(?:\.\/)|(?:@@\w+)/ );
448 }
449  
450 #****f* IDS/Whitelist/make_utf_8
451 # NAME
452 # make_utf_8
453 # DESCRIPTION
454 # Encodes string to UTF-8 and strips malformed UTF-8 characters
455 # INPUT
456 # + string
457 # OUTPUT
458 # UTF-8 string
459 # SYNOPSIS
460 # $whitelist->make_utf_8( $string );
461 #****
462  
463 =head2 make_utf_8()
464  
465 DESCRIPTION
466 Encodes string to UTF-8 and strips malformed UTF-8 characters
467 INPUT
468 + string
469 OUTPUT
470 UTF-8 string
471 SYNOPSIS
472 $whitelist->make_utf_8( $string );
473  
474 =cut
475  
476 0 ▶sub make_utf_8 {
Subroutine make_utf_8: not called CC=4 SCAR=30.0
Pod documented
477 0 my ($self, $string) = @_;
478  
479 # make string UTF-8
480 0 my $utf8_encoded = '';
481 0 eval {
482 0 $utf8_encoded = Encode::encode('UTF-8', $string, Encode::FB_CROAK);
483 };
484 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
485 # sanitize malformed UTF-8
486 0 $utf8_encoded = '';
487 0 my @chars = split(//, $string);
488 0 foreach my $char (@chars) {
489 0 ▶ my $utf_8_char = eval { Encode::encode('UTF-8', $char, Encode::FB_CROAK) }
Branch: 1 branch
BranchTrueFalseTotal
my $utf_8_char = eval { do { &Encode::encode("UTF-8", $char, 1) } } or next 0 0 0
490 or next;
491 0 $utf8_encoded .= $utf_8_char;
492 }
493 }
494 0 return $utf8_encoded;
495 }
496  
497 #****im* IDS/Whitelist/_load_whitelist_from_xml
498 # NAME
499 # _load_whitelist_from_xml
500 # DESCRIPTION
501 # loads the parameter whitelist XML file
502 # croaks if a xml or regexp parsing error occors
503 # INPUT
504 # whitelistfile path + name of the XML whitelist file
505 # OUTPUT
506 # int number of loaded rules
507 # SYNOPSIS
508 # $self->_load_whitelist_from_xml('/home/xyz/param_whitelist.xml');
509 #****
510  
511 0 ▶sub _load_whitelist_from_xml {
Subroutine _load_whitelist_from_xml: not called CC=11 SCAR=48.8
512 0 my ($self, $whitelistfile) = @_;
513 0 my $whitelistcnt = 0;
514  
515 0 ▶ if ($whitelistfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whitelistfile) 0 0 0
516 # read & parse whitelist XML
517 0 my $whitelistxml;
518 0 eval {
519 0 $whitelistxml = XMLin($whitelistfile,
520 forcearray => [ qw(whitelist param conditions condition)],
521 keyattr => [],
522 );
523 };
524 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
525 0 croak "Error in _load_whitelist_from_xml while parsing $whitelistfile: $@";
526 }
527  
528 # convert XML structure into handy data structure
529 0 foreach my $whitelistobj (@{$whitelistxml->{param}}) {
530 0 my @conditionslist = ();
531 0 foreach my $condition (@{$whitelistobj->{conditions}[0]{condition}}) {
532 0 ▶ if (defined($condition->{rule})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $condition->{"rule"}) 0 0 0
533 # copy for error message
534 0 my $rule = $condition->{rule};
535  
536 0 eval {
537 0 $condition->{rule} = qr/$condition->{rule}/ms;
538 };
539 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
540 0 croak 'Error in whitelist rule of condition "' . $condition->{key} . '" for param "' . $whitelistobj->{key} . '": ' . $rule . ' Message: ' . $@;
541 }
542 }
543 0 push(@conditionslist, $condition);
544 }
545 0 my %whitelisthash = ();
546 0 ▶ if (defined($whitelistobj->{rule})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $whitelistobj->{"rule"}) 0 0 0
547 0 eval {
548 0 $whitelisthash{rule} = qr/$whitelistobj->{rule}/ms;
549 };
550 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
551 0 croak 'Error in whitelist rule for param "' . $whitelistobj->{key} . '": ' . $whitelistobj->{rule} . ' Message: ' . $@;
552 }
553 }
554 0 ▶ if (@conditionslist) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conditionslist) 0 0 0
555 0 $whitelisthash{conditions} = \@conditionslist;
556 }
557 0 ▶ if ($whitelistobj->{encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whitelistobj->{"encoding"}) 0 0 0
558 0 $whitelisthash{encoding} = $whitelistobj->{encoding};
559 }
560 0 $self->{whitelist}{$whitelistobj->{key}} = \%whitelisthash;
561 0 $whitelistcnt++;
562 }
563 }
564 0 return $whitelistcnt;
565 }
566  
567 #****if* IDS/Whitelist/_json_to_string
568 # NAME
569 # _json_to_string
570 # DESCRIPTION
571 # Tries to decode a string from JSON. Uses _datastructure_to_string().
572 # INPUT
573 # value the string to convert
574 # OUTPUT
575 # value converted string if correct JSON, the unchanged input string otherwise
576 # SYNOPSIS
577 # IDS::Whitelist::_json_to_string($value);
578 #****
579  
580 0 ▶sub _json_to_string {
Subroutine _json_to_string: not called CC=2 SCAR=17.9
581 0 my ($value) = @_;
582 0 my $json_ds;
583 0 eval {
584 0 $json_ds = JSON::XS::decode_json($value);
585 };
586 0 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
587 0 $value = _datastructure_to_string($json_ds)."\n";
588 }
589 0 return $value;
590 }
591  
592 #****if* IDS/Whitelist/_datastructure_to_string
593 # NAME
594 # _datastructure_to_string
595 # DESCRIPTION
596 # Walks recursively through array or hash and concatenates keys and values to one single string (\n separated)
597 # INPUT
598 # ref the array/hash to convert
599 # OUTPUT
600 # string converted string
601 # SYNOPSIS
602 # IDS::Whitelist::_datastructure_to_string($ref);
603 #****
604  
605 0 ▶sub _datastructure_to_string {
Subroutine _datastructure_to_string: not called CC=5 SCAR=34.0
606 0 my $in = shift;
607 0 my $out = '';
608 0 ▶ if (ref $in eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $in eq 'HASH') { } 0 0 0
elsif (ref $in eq 'ARRAY') { } 0 0 0
609 0 foreach (keys %$in) {
610 0 $out .= $_."\n";
611 0 $out .= _datastructure_to_string($in->{$_});
612 }
613 }
614 elsif (ref $in eq 'ARRAY') {
615 0 foreach (@$in) {
616 0 $out = _datastructure_to_string($_) . $out;
617 }
618 }
619 else {
620 0 $out .= $in."\n";
621 }
622 0 return $out;
623 }
624  
625 1 1;
626  
627 __END__
↑ Summary IDS.pm »