lib/Template/EmbeddedPerl/SafeString.pm

filter: Statementstmt 68.1% 45 / 66 Branchbran 32.1% 9 / 28 Conditioncond 66.6% 8 / 12 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 73.9% 17 / 23 Podpod 83.3% 10 / 12 totaltotal 62.5% 90 / 144 CC 20 SCAR 18.3
CC
20
Cov
58%
safe
20.0
__ANON__
12.0
is_safe
9.0
CRAP 6.2
« lib/Template/EmbeddedPerl/Arguments.pm ↑ Summary lib/Template/EmbeddedPerl.pm »
1 package Template::EmbeddedPerl::SafeString;
2
3 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use HTML::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 use overload
11 1 ▶ bool => sub { shift->to_bool },
Subroutine BEGIN: called CC=1 SCAR=1.8
Subroutine __ANON__: called CC=1 SCAR=0.0
12 426 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 0 ▶ '+' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
14 0 my ($self, $other, $reverse) = @_;
15 0 ▶ croak "Can only join two safe string objects" unless (ref($other) eq ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other eq ref $self 0 0 0
16
17 0 ▶ return $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
18 ? raw("${other}${self}")
19 : raw("${self}${other}");
20 },
21 24 fallback => 1;
22
23 24 our @EXPORT_OK = qw(raw flattened_raw safe flattened_safe is_safe escape_html safe_concat concat);
24 24 our %EXPORT_TAGS = (all => \@EXPORT_OK, core => ['raw', 'safe', 'escape_html', 'safe_concat']);
25
26 616 ▶sub _make_safe {
Subroutine _make_safe: called CC=1 SCAR=0.0
27 616 my $string_to_make_safe = shift;
28 616 return bless(\$string_to_make_safe, 'Template::EmbeddedPerl::SafeString');
29 }
30
31 221 ▶sub escape_html {
Subroutine escape_html: called CC=1 SCAR=0.0
Pod documented
32 221 my $string = shift;
33 221 return HTML::Escape::escape_html($string);
34 }
35
36 318 ▶sub raw {
Subroutine raw: called CC=4 SCAR=17.9
Pod documented
37 318 ▶ if(scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 318 0 318
38 318 ▶ return is_safe($_[0]) ? $_[0] : _make_safe($_[0])
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_[0]) ? : 50 268 318
39 } else {
40 0 ▶ return map { is_safe($_) ? $_ : _make_safe($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 0 0
41 }
42 }
43
44 0 ▶sub flattened_raw {
Subroutine flattened_raw: not called CC=2 SCAR=17.9
Pod documented
45 0 ▶ my $string = join '', map { is_safe($_) ? $_->to_string : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 0 0
46 0 return _make_safe $string;
47 }
48
49 666 ▶sub is_safe {
Subroutine is_safe: called CC=8 SCAR=22.0
Pod documented
50 666 my $string_to_test = shift;
51 666 ▶ return 0 unless defined($string_to_test);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string_to_test 0 666 666
52 666 ▶ return 0 unless
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($string_to_test) || "") eq "Template::EmbeddedPerl::SafeString" or (builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString" or (builtin::blessed($string_to_test) || "") eq "Mojo::ByteStream" 489 177 666
Condition66%8/12
(builtin::blessed($string_to_test) || "") eq "Template::EmbeddedPerl::SafeString" or (builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString" or (builtin::blessed($string_to_test) || "") eq "Mojo::ByteStream"
l!l&&r!l&&!r
1770489
(builtin::blessed($string_to_test) || "") eq "Template::EmbeddedPerl::SafeString" or (builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString"
l!l&&r!l&&!r
1770489
builtin::blessed($string_to_test) || ''
l!l
177489
builtin::blessed($string_to_test) || ''
l!l
0489
builtin::blessed($string_to_test) || ''
l!l
0489
MC/DC33%1/3
(builtin::blessed($string_to_test) || "") eq "Template::EmbeddedPerl::SafeString" or (builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString" or (builtin::blessed($string_to_test) || "") eq "Mojo::ByteStream"
builtin::blessed($string_to_test) builtin::blessed($string_to_test) builtin::blessed($string_to_test)
Truth table50%2/4
A or B or C
A= builtin::blessed($string_to_test)B= builtin::blessed($string_to_test)C= builtin::blessed($string_to_test)
ABCresult
1XX1
01X1
0011
0000
53 ((Scalar::Util::blessed($string_to_test)||'') eq 'Template::EmbeddedPerl::SafeString') ||
54 ((Scalar::Util::blessed($string_to_test)||'') eq 'Valiant::HTML::SafeString') ||
55 ((Scalar::Util::blessed($string_to_test)||'') eq 'Mojo::ByteStream');
56 177 return 1;
57 }
58
59 0 ▶sub safe {
Subroutine safe: not called CC=4 SCAR=30.0
Pod documented
60 0 ▶ if(scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 0 0
61 0 ▶ return is_safe($_[0]) ? $_[0] : _make_safe(escape_html($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_[0]) ? : 0 0 0
62 } else {
63 0 ▶ return map { is_safe($_) ? $_ : _make_safe(escape_html($_)) } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 0 0
64 }
65 }
66
67 348 ▶sub safe_concat { return flattened_safe(@_) }
Subroutine safe_concat: called CC=1 SCAR=0.0
Pod undocumented
68
69 348 ▶sub flattened_safe {
Subroutine flattened_safe: called CC=2 SCAR=6.9
Pod documented
70 348 ▶ my $string = join '', map { is_safe($_) ? $_->to_string : escape_html($_) } grep { defined($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 118 221 339
71 348 return _make_safe $string;
72 }
73
74 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
75 0 my $class = shift;
76 0 return flattened_safe(@_);
77 }
78
79 0 ▶sub concat { return flattened_safe(@_) }
Subroutine concat: not called CC=1 SCAR=6.9
Pod documented
80
81 544 ▶sub to_string { return ${$_[0]} }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
82
83 1 ▶sub to_bool { return ${$_[0]} ? 1 : 0 }
Subroutine to_bool: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];} ? : 0 1 1
84
85 0 ▶sub append {
Subroutine append: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my ($self, @rest) = @_;
87 0 ▶ my $string = join '', map { is_safe($_) ? $_->to_string : escape_html($_) } grep { defined($_) } @rest;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 0 0
88 0 ${$self} .= $string;
89 }
90
91 24 1;
92  
93 =head1 NAME
94  
95 Template::EmbeddedPerl::SafeString- String rendering safety
96
97 =head1 SYNOPSIS
98
99 use Template::EmbeddedPerl::SafeString'safe', 'escape';
100
101 =head1 DESCRIPTION
102
103 Protecting your templates from the various types of character injection attacks is
104 a prime concern for anyone working with the HTML user interface. This class provides
105 some methods and exports to make this job easier.
106
107 =head1 EXPORTABLE FUNCTIONS
108
109 The following functions can be exported by this library
110
111 =head2 safe
112
113 Given a string or array, returns such marked as 'safe' by using C<html_escape> on the string and
114 then encapsulating it inside an instance of L<Template::EmbeddedPerl::SafeString>. You can safely pass arguments
115 to this since if the string is already marked safe we just return it unaltered.
116
117 =head2 flattened_safe
118
119 Same as C<safe> but always returns a string even if you pass an array of strings (they are all
120 joined together).
121
122 =head2 raw
123
124 Given a string or array of strings, return each marked as safe (by encapsulating it inside an
125 instance of L<Template::EmbeddedPerl::SafeString>. This will just mark strings as safe without doing any
126 escaping first (for that see C<safe>) so be careful with this.
127
128 =head2 flattened_raw
129
130 Same as C<raw> but always returns a string even if you pass an array of strings (they are all
131 joined together).
132
133 =head2 is_safe
134
135 Given a string return a boolean indicating if its marked safe or not. Since C<safe> and C<raw> never
136 double the escapulations / escaping, you probably never need this but saw no reason to not expose it.
137
138 =head2 escape_html
139
140 A wrapper on L<HTML::Escape> just to make your life a bit easier
141
142 =head1 CLASS METHODS
143
144 This package exposes the folllowing class methods
145
146 =head2 new
147
148 my $safe_string = Valiant::HTML::SafeString->new(@strings);
149
150 Given a string, or array of strings, returns a single string that has been C<html_escape>'d as needed
151 and encapulated in an instance. Its safe to pass arguments to this without testing since if a string
152 is already marked safe we don't do any extra escaping (although you will get a new instance).
153
154 =head1 INSTANCE METHODS
155
156 Instances of L<Template::EmbeddedPerl::SafeString> expose the following public methods
157
158 =head2 concat
159
160 Returns a new safe string which appends a list of strings to the old one, making those new strings
161 'safe' as needed. Basically this will escape any strings not marked safe already and then joins them
162 altogether in a single safe string.
163
164 =head2 to_string
165
166 Returns the raw string, suitable for display.
167
168 =head2 to_bool
169
170 Returns a boolean indicating if the string is empty or not.
171
172 =head1 OVERLOADING
173
174 String context calles C<to_string>; Boolean context returns true unless the string is empty.
175
176 =head1 SEE ALSO
177
178 L<Template::EmbeddedPerl>
179  
180 =head1 AUTHOR
181
182 See L<Template::EmbeddedPerl>
183
184 =head1 COPYRIGHT & LICENSE
185
186 See L<Template::EmbeddedPerl>
187
188 =cut
« lib/Template/EmbeddedPerl/Arguments.pm ↑ Summary lib/Template/EmbeddedPerl.pm »