| 1 | package CGI::Plus; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use CGI::Safe 'taint'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base 'CGI::Safe'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use String::Util ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use CGI::Cookie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # version | ||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Debug::ShowStuff | ||||||||||||||||||||||||||||
| 13 | # use Debug::ShowStuff ':all'; | ||||||||||||||||||||||||||||
| 14 | # use Debug::ShowStuff::ShowVar; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # enable file uploads | ||||||||||||||||||||||||||||
| 17 | 1 | $CGI::DISABLE_UPLOADS = 0; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # maximum upload: 5 mb | ||||||||||||||||||||||||||||
| 20 | 1 | $CGI::POST_MAX = 5 * 1024 * 1024; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # set path to empty string | ||||||||||||||||||||||||||||
| 23 | 1 | $ENV{'PATH'} = ''; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | CGI::Plus -- Extra utilities for CGI | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 Description | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This module adds a few enhancements to | ||||||||||||||||||||||||||||
| 32 | L<CGI::Safe|http://search.cpan.org/~ovid/CGI-Safe/lib/CGI/Safe.pm>, | ||||||||||||||||||||||||||||
| 33 | which itself adds a few security-based enancements to | ||||||||||||||||||||||||||||
| 34 | L<CGI.pm|http://perldoc.perl.org/CGI.html>. The enhancement are almost | ||||||||||||||||||||||||||||
| 35 | entirely additions - the only method that is overridden is new(), and | ||||||||||||||||||||||||||||
| 36 | the changes there are only addition. The enhancements in this module entirely | ||||||||||||||||||||||||||||
| 37 | use the object-oriented interface. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | use CGI::Plus; | ||||||||||||||||||||||||||||
| 42 | my ($cgi, $cookie, $url, $param); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # new CGI::Plus object | ||||||||||||||||||||||||||||
| 45 | $cgi = CGI::Plus->new(); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # turn on checks for cross-site request forgeries (CSRF) | ||||||||||||||||||||||||||||
| 48 | $cgi->csrf(1); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # get a cookie and look at its values | ||||||||||||||||||||||||||||
| 51 | $cookie = $cgi->incoming_cookies->{'mycookie'}; | ||||||||||||||||||||||||||||
| 52 | print $cookie->{'values'}->{'x'}, "\n"; | ||||||||||||||||||||||||||||
| 53 | print $cookie->{'values'}->{'y'}, "\n"; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # more concise way to get an incoming cookie | ||||||||||||||||||||||||||||
| 56 | $cookie = $cgi->ic->{'mycookie'}; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # resend a cookie, but change one of its values | ||||||||||||||||||||||||||||
| 59 | $cookie = $cgi->resend_cookie('mycookie'); | ||||||||||||||||||||||||||||
| 60 | $cookie->{'values'}->{'x'} = 2; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # add an outgoing cookie, set some values | ||||||||||||||||||||||||||||
| 63 | $cookie = $cgi->new_send_cookie('newcookie'); | ||||||||||||||||||||||||||||
| 64 | $cookie->{'values'}->{'val1'} = '1'; | ||||||||||||||||||||||||||||
| 65 | $cookie->{'values'}->{'val2'} = '2'; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # output HTTP header with outgoing cookies, including CSRF | ||||||||||||||||||||||||||||
| 68 | # check cookie, automatically added | ||||||||||||||||||||||||||||
| 69 | print $cgi->header_plus; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # output header again if it hasn't already been sent, but if it | ||||||||||||||||||||||||||||
| 72 | # has then output an empty string | ||||||||||||||||||||||||||||
| 73 | print $cgi->header_plus; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # output the URL of the current page but set a new value | ||||||||||||||||||||||||||||
| 76 | # for the "t" param and remove the "j" param | ||||||||||||||||||||||||||||
| 77 | $url = $cgi->self_link(params=>{t=>2, j=>undef}); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # check if the submitted form includes the value of the CSRF | ||||||||||||||||||||||||||||
| 80 | # cookie that was sent | ||||||||||||||||||||||||||||
| 81 | if (! $cgi->csrf_check) | ||||||||||||||||||||||||||||
| 82 | { die 'security error' } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # output the randomly generated value of the CSRF cookie, | ||||||||||||||||||||||||||||
| 85 | # output: KTFnGgpkZ4 | ||||||||||||||||||||||||||||
| 86 | print $cgi->csrf_value, "\n"; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # output the hidden input form field that uses the same | ||||||||||||||||||||||||||||
| 89 | # value as the CSRF cookie | ||||||||||||||||||||||||||||
| 90 | # output: <input type="hidden" name="csrf" value="KTFnGgpkZ4"> | ||||||||||||||||||||||||||||
| 91 | print $cgi->csrf_field, "\n"; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # get the CSRF check param for use in a URL | ||||||||||||||||||||||||||||
| 94 | # output: csrf=KTFnGgpkZ4 | ||||||||||||||||||||||||||||
| 95 | print $cgi->csrf_param; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # set a custom header | ||||||||||||||||||||||||||||
| 98 | $cgi->set_header('myheader', 'whatever'); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # change content type | ||||||||||||||||||||||||||||
| 101 | $cgi->set_content_type('text/json'); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # output HTTP headers, including added cookies, the CSRF cookie, | ||||||||||||||||||||||||||||
| 104 | # and the new header | ||||||||||||||||||||||||||||
| 105 | print $cgi->header_plus; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # outputs something like this: | ||||||||||||||||||||||||||||
| 108 | # Set-Cookie: newcookie=val2&2&val1&1; path=/ | ||||||||||||||||||||||||||||
| 109 | # Set-Cookie: mycookie=y&2&x&2; path=/ | ||||||||||||||||||||||||||||
| 110 | # Set-Cookie: csrf=v&KTFnGgpkZ4; path=/ | ||||||||||||||||||||||||||||
| 111 | # Date: Sun, 29 Jul 2012 04:08:06 GMT | ||||||||||||||||||||||||||||
| 112 | # Myheader: whatever | ||||||||||||||||||||||||||||
| 113 | # Content-Type: text/json; charset=ISO-8859-1 | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 INSTALLATION | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | CGI::Plus can be installed with the usual routine: | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | perl Makefile.PL | ||||||||||||||||||||||||||||
| 120 | make | ||||||||||||||||||||||||||||
| 121 | make test | ||||||||||||||||||||||||||||
| 122 | make install | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 METHODS | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 130 | ## new | ||||||||||||||||||||||||||||
| 131 | # | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 CGI::Plus->new() | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | Creates and returns a CGI::Plus object. New calls the super-class' new() | ||||||||||||||||||||||||||||
| 136 | method, so all params sent to this method will be passed through to CGI | ||||||||||||||||||||||||||||
| 137 | and CGI::Safe. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub new { | ||||||||||||||||||||||||||||
| 142 | 3 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 3 | my $cgi = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # set cookies | ||||||||||||||||||||||||||||
| 146 | 3 | $cgi->initialize_cookies(); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # call super method | ||||||||||||||||||||||||||||
| 149 | 3 | return $cgi; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | # new | ||||||||||||||||||||||||||||
| 153 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 157 | # ic, oc | ||||||||||||||||||||||||||||
| 158 | # quick accesors to incoming and outgoing cookies | ||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 $cgi->ic, $cgi->oc | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | ▶ | sub incoming_cookies { return $_[0]->{'cookies'}->{'incoming'} } | ||||||||||||||||||||||||||
|
Subroutine
incoming_cookies: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | 2 | ▶ | sub ic { return shift->incoming_cookies(@_) } | ||||||||||||||||||||||||||
|
Subroutine
ic: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | ▶ | sub outgoing_cookies { return $_[0]->{'cookies'}->{'outgoing'} } | ||||||||||||||||||||||||||
|
Subroutine
outgoing_cookies: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub oc { return shift->outgoing_cookies(@_) } | ||||||||||||||||||||||||||
|
Subroutine
oc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # | ||||||||||||||||||||||||||||
| 173 | # ic, oc | ||||||||||||||||||||||||||||
| 174 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 178 | # initialize_cookies | ||||||||||||||||||||||||||||
| 179 | # private method | ||||||||||||||||||||||||||||
| 180 | # | ||||||||||||||||||||||||||||
| 181 | sub initialize_cookies { | ||||||||||||||||||||||||||||
| 182 | 3 | ▶ | my ($cgi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
initialize_cookies: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | 3 | my ($got, %cookies); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # cookie hashes | ||||||||||||||||||||||||||||
| 186 | 3 | $cgi->{'cookies'} = {}; | |||||||||||||||||||||||||||
| 187 | 3 | $got = $cgi->{'cookies'}->{'incoming'} = {}; | |||||||||||||||||||||||||||
| 188 | 3 | $cgi->{'cookies'}->{'outgoing'} = {}; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # get hash of cookies that were sent | ||||||||||||||||||||||||||||
| 191 | 3 | %cookies = CGI::Cookie->fetch(); | |||||||||||||||||||||||||||
| 192 | # showhash \%cookies, title=>'%cookies'; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # populate cookie values | ||||||||||||||||||||||||||||
| 195 | 3 | foreach my $name (keys %cookies) { | |||||||||||||||||||||||||||
| 196 | 6 | my ($cookie, $element, @value); | |||||||||||||||||||||||||||
| 197 | 6 | $cookie = $cookies{$name}; | |||||||||||||||||||||||||||
| 198 | 6 | $element = {}; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # name of cookie | ||||||||||||||||||||||||||||
| 201 | 6 | $element->{'name'} = $name; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # original cookie object | ||||||||||||||||||||||||||||
| 204 | 6 | $element->{'org'} = $cookie; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # expires | ||||||||||||||||||||||||||||
| 207 | 6 | ▶ | if (defined $cookie->expires()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | { $element->{'expires'} = $cookie->expires() } | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # get parsed values | ||||||||||||||||||||||||||||
| 211 | 6 | @value = $cookie->value(); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # if more than one element in @value, assume it's a hash | ||||||||||||||||||||||||||||
| 214 | 6 | ▶ | if (@value > 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 3 | { $element->{'values'} = {@value} } | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # else it's a single string value | ||||||||||||||||||||||||||||
| 218 | else | ||||||||||||||||||||||||||||
| 219 | 3 | { $element->{'value'} = $cookie->value() } | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # hold on to cookie element | ||||||||||||||||||||||||||||
| 222 | 6 | $got->{$name} = $element; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | # | ||||||||||||||||||||||||||||
| 226 | # initialize_cookies | ||||||||||||||||||||||||||||
| 227 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 231 | # cookie_resend | ||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||
| 233 | sub resend_cookie { | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | my $cgi = shift; | ||||||||||||||||||||||||||
|
Subroutine
resend_cookie: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 235 | 0 | return $cgi->cookie_resend(@_); | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | sub cookie_resend { | ||||||||||||||||||||||||||||
| 239 | 1 | ▶ | my ($cgi, $name, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cookie_resend: called CC=7 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 240 | 1 | my ($got, $send); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # default %opts | ||||||||||||||||||||||||||||
| 243 | 1 | %opts = (ensure=>1, %opts); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # if not ensuring existence of cookie, and cookie doesn't | ||||||||||||||||||||||||||||
| 246 | # exist, return undef | ||||||||||||||||||||||||||||
| 247 | 1 | ▶ | unless ( $cgi->ic->{$name} || $opts{'ensure'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cgi->ic->{$name} or $opts{"ensure"}
MC/DC0%0/2
$cgi->ic->{$name} or $opts{"ensure"}
$cgi->ic->{$name}
$opts{"ensure"}
Truth table33%1/3
A or B
A= $cgi->ic->{$name}B= $opts{"ensure"}
| |||||||||||||||||||||||||||||
| 248 | 0 | return undef; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | # get sent cookie | ||||||||||||||||||||||||||||
| 252 | 1 | ▶ | $got = $cgi->ic->{$name} || {'name'=>$name}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cgi->ic->{$name} || {'name', $name}
MC/DC0%0/1
$cgi->ic->{$name} || {'name', $name}
$cgi->ic->{$name}
Truth table50%1/2
A
A= $cgi->ic->{$name}
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # create cookie that gets sent back out | ||||||||||||||||||||||||||||
| 255 | 1 | $send = {}; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # clone $got cookie | ||||||||||||||||||||||||||||
| 258 | 1 | foreach my $key (keys %$got) { | |||||||||||||||||||||||||||
| 259 | 1 | my $value = $got->{$key}; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # original cookie | ||||||||||||||||||||||||||||
| 262 | 1 | ▶ | if (UNIVERSAL::isa $value, 'CGI::Cookie') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 263 | 0 | $send->{$key} = $value; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # hashref | ||||||||||||||||||||||||||||
| 267 | elsif (UNIVERSAL::isa $value, 'HASH') { | ||||||||||||||||||||||||||||
| 268 | 0 | $send->{$key} = {%$value}; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # else just copy | ||||||||||||||||||||||||||||
| 272 | else { | ||||||||||||||||||||||||||||
| 273 | 1 | $send->{$key} = $value; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # set cookie | ||||||||||||||||||||||||||||
| 278 | 1 | $cgi->oc->{$name} = $send; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # return new cookie | ||||||||||||||||||||||||||||
| 281 | 1 | return $send; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | # | ||||||||||||||||||||||||||||
| 284 | # cookie_resend | ||||||||||||||||||||||||||||
| 285 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 289 | # new_send_cookie | ||||||||||||||||||||||||||||
| 290 | # | ||||||||||||||||||||||||||||
| 291 | sub new_send_cookie { | ||||||||||||||||||||||||||||
| 292 | 0 | ▶ | my ($cgi, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_send_cookie: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 293 | 0 | my ($cookie); | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # create oject | ||||||||||||||||||||||||||||
| 296 | 0 | $cookie = {}; | |||||||||||||||||||||||||||
| 297 | 0 | $cookie->{'name'} = $name; | |||||||||||||||||||||||||||
| 298 | 0 | $cookie->{'values'} = {}; | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # add to hash of outgoing cookies | ||||||||||||||||||||||||||||
| 301 | 0 | $cgi->oc->{$name} = $cookie; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # return new cookie | ||||||||||||||||||||||||||||
| 304 | 0 | return $cookie; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | # | ||||||||||||||||||||||||||||
| 307 | # new_send_cookie | ||||||||||||||||||||||||||||
| 308 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 312 | # self_link | ||||||||||||||||||||||||||||
| 313 | # | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =head2 $cgi->self_link(%options) | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | Returns a url that is a relative link to the current page. The local path of | ||||||||||||||||||||||||||||
| 318 | the URL is sent, but not the protocol or host. So, for example, if the URL | ||||||||||||||||||||||||||||
| 319 | of the current page is | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | http://www.example.com/cgi-plus/?y=1&x=2&t=2&y=2 | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | then $cgi->self_link() would return something like as follows. Note that the | ||||||||||||||||||||||||||||
| 324 | order of the URL params mght be changed. | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | /cgi-plus/?y=1&y=2&x=2&t=2 | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | NOTE: If all you want is to do is get the URL of the current page, then | ||||||||||||||||||||||||||||
| 329 | L<$cgi-E<gt>url()|http://perldoc.perl.org/CGI.html#OBTAINING-THE-SCRIPT%27S-URL> | ||||||||||||||||||||||||||||
| 330 | is a better choice because it preserves the order of URL params. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | B<option:> params | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | The C<params> option allows you to change the values of some of the URL params | ||||||||||||||||||||||||||||
| 335 | while leaving others as-is. C<params> is a hashref of URL params and | ||||||||||||||||||||||||||||
| 336 | their new values. For example, consider this URL: | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | http://www.example.com/cgi-plus/?y=1&x=2&t=2&y=2 | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | Suppose you want to change just that C<t> param from 2 to 3. You would do that | ||||||||||||||||||||||||||||
| 341 | like this: | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | $cgi->self_link(params=>{t=>3}) | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | which gives us this relative URL with the C<x> and C<y> values as they were before, but | ||||||||||||||||||||||||||||
| 346 | with the new C<t> value: | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | /cgi-plus/?y=1&y=2&x=2&t=3 | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | If the value of the param is an array ref, then the param is output once | ||||||||||||||||||||||||||||
| 351 | for each value in the array ref. So, for example, you could set that C<t> | ||||||||||||||||||||||||||||
| 352 | to have the values 4 and 5 like this: | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | $cgi->self_link(params=>{t=>[4,5]}) | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | which gives us | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | /cgi-plus/?y=1&y=2&x=2&t=4&t=5 | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | You can remove params by setting their values to undef: | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | $cgi->self_link(params=>{t=>undef}) | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | which gives us | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | /cgi-plus/?y=1&y=2&x=2 | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | B<option:> clear_params | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | C<clear_params> removes all params from the URL. For example, using our | ||||||||||||||||||||||||||||
| 371 | example URL from above: | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | http://www.example.com/cgi-plus/?y=1&x=2&t=2&y=2 | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | this: | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | $cgi->self_link(clear_params=>1) | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | returns this URL; | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | /cgi-plus/ | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | You can use C<clear_params> in conjunction with C<params> to wipe the slate clean | ||||||||||||||||||||||||||||
| 384 | and send only specific params. So, for example, this call | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | $cgi->self_link(clear_params=>1, params=>{j=>10}) | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | gives us this URL: | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | /cgi-plus/?j=10 | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | B<option:> html | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | The C<html> option returns the URL HTML-escaped. So, for example, this | ||||||||||||||||||||||||||||
| 395 | call: | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | $cgi->self_link(params=>{t=>[4,5]}, html=>1) | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | returns this: | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | /cgi-plus/?y=1&y=2&x=2&t=4&t=5 | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =cut | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | sub self_link { | ||||||||||||||||||||||||||||
| 406 | 0 | ▶ | my ($cgi, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
self_link: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 407 | 0 | my (%params, $rv, $query, $changes, %added); | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | # get params for adding to url | ||||||||||||||||||||||||||||
| 410 | 0 | ▶ | $changes = $opts{'params'} || $opts{'param'} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/5
$opts{'params'} || $opts{'param'} || {}
$opts{"params"} or $opts{"param"}
MC/DC0%0/2
$opts{'params'} || $opts{'param'} || {}
$opts{"params"}
$opts{"param"}
Truth table0%0/3
A or B
A= $opts{"params"}B= $opts{"param"}
| |||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | # start with uri path | ||||||||||||||||||||||||||||
| 413 | 0 | ▶ | unless ($rv = $ENV{'PATH_INFO'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | 0 | $rv = $ENV{'REQUEST_URI'}; | |||||||||||||||||||||||||||
| 415 | 0 | $rv =~ s|\?.*||s; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # get parameter names from cgi | ||||||||||||||||||||||||||||
| 419 | 0 | ▶ | unless ($opts{'clear_params'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 0 | { @params{$cgi->param()} = () } | |||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | # get parameter names from changes | ||||||||||||||||||||||||||||
| 423 | 0 | @params{keys %$changes} = (); | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | # loop through params | ||||||||||||||||||||||||||||
| 426 | 0 | foreach my $key (keys %params) { | |||||||||||||||||||||||||||
| 427 | 0 | my (@vals); | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # get values from adds | ||||||||||||||||||||||||||||
| 430 | 0 | ▶ | if (exists $changes->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | 0 | ▶ | if (ref $changes->{$key}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | 0 | { @vals = @{$changes->{$key}} } | |||||||||||||||||||||||||||
| 433 | else | ||||||||||||||||||||||||||||
| 434 | 0 | { @vals = $changes->{$key} } | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | else { | ||||||||||||||||||||||||||||
| 437 | 0 | @vals = $cgi->param($key); | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | # remove values that are undef | ||||||||||||||||||||||||||||
| 441 | 0 | @vals = grep {defined $_} @vals; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | # output values | ||||||||||||||||||||||||||||
| 444 | 0 | foreach my $val (@vals) { | |||||||||||||||||||||||||||
| 445 | # add delimiter or query marker | ||||||||||||||||||||||||||||
| 446 | 0 | ▶ | if ($query) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | 0 | { $query .= '&' } | |||||||||||||||||||||||||||
| 448 | else | ||||||||||||||||||||||||||||
| 449 | 0 | { $query = '?' } | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | # url escape | ||||||||||||||||||||||||||||
| 452 | 0 | $val = $cgi->escape($val); | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # add value | ||||||||||||||||||||||||||||
| 455 | 0 | $query .= $key . '=' . $val; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | # add query | ||||||||||||||||||||||||||||
| 460 | 0 | ▶ | if ($query) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | 0 | { $rv .= $query } | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | # html escape | ||||||||||||||||||||||||||||
| 464 | 0 | ▶ | if ($opts{'html'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | 0 | { $rv = htmlesc($rv) } | |||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | # return | ||||||||||||||||||||||||||||
| 468 | 0 | return $rv; | |||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | # | ||||||||||||||||||||||||||||
| 471 | # self_link | ||||||||||||||||||||||||||||
| 472 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 477 | # set_header | ||||||||||||||||||||||||||||
| 478 | # | ||||||||||||||||||||||||||||
| 479 | sub set_header { | ||||||||||||||||||||||||||||
| 480 | 0 | ▶ | my ($cgi, $name, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_header: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | # initialize header hash | ||||||||||||||||||||||||||||
| 483 | 0 | ▶ | $cgi->{'headers'} ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$cgi->{"headers"} ||= {}
MC/DC0%0/1
$cgi->{"headers"} ||= {}
$cgi->{"headers"}
Truth table0%0/2
A
A= $cgi->{"headers"}
| |||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | # set header | ||||||||||||||||||||||||||||
| 486 | 0 | $cgi->{'headers'}->{$name} = $value; | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | # return | ||||||||||||||||||||||||||||
| 489 | 0 | return 1; | |||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||
| 491 | # | ||||||||||||||||||||||||||||
| 492 | # set_header | ||||||||||||||||||||||||||||
| 493 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 498 | # set_content_type | ||||||||||||||||||||||||||||
| 499 | # | ||||||||||||||||||||||||||||
| 500 | sub set_content_type { | ||||||||||||||||||||||||||||
| 501 | 0 | ▶ | my ($cgi, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_content_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | # set type | ||||||||||||||||||||||||||||
| 504 | 0 | $cgi->{'content_type'} = $type; | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | # return | ||||||||||||||||||||||||||||
| 507 | 0 | return 1; | |||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||
| 509 | # | ||||||||||||||||||||||||||||
| 510 | # set_content_type | ||||||||||||||||||||||||||||
| 511 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 515 | # header_plus | ||||||||||||||||||||||||||||
| 516 | # | ||||||||||||||||||||||||||||
| 517 | sub header_plus { | ||||||||||||||||||||||||||||
| 518 | 0 | ▶ | my ($cgi, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
header_plus: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 519 | 0 | my (@cookies); | |||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | # if header has already been sent, don't send it again, just | ||||||||||||||||||||||||||||
| 522 | # return empty string | ||||||||||||||||||||||||||||
| 523 | 0 | ▶ | if ($cgi->{'header_sent'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 524 | 0 | { return '' } | |||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | # set content type | ||||||||||||||||||||||||||||
| 527 | 0 | ▶ | if ( (! $opts{'-type'}) && $cgi->{'content_type'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $opts{"-type"} and $cgi->{"content_type"}
MC/DC0%0/2
not $opts{"-type"} and $cgi->{"content_type"}
not $opts{"-type"}
$cgi->{"content_type"}
Truth table0%0/3
not(A) and B
A= not $opts{"-type"}B= $cgi->{"content_type"}
| |||||||||||||||||||||||||||||
| 528 | 0 | $opts{'-type'} = $cgi->{'content_type'}; | |||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | # add headers | ||||||||||||||||||||||||||||
| 532 | 0 | ▶ | if ($cgi->{'headers'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 533 | 0 | ▶ | while ( my($name, $value) = each(%{$cgi->{'headers'}}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 534 | 0 | $name =~ s|^\-*|-|s; | |||||||||||||||||||||||||||
| 535 | 0 | ▶ | $opts{$name} ||= $value; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts{$name} ||= $value
MC/DC0%0/2
$opts{$name} ||= $value
$opts{$name}
$value
Truth table0%0/3
A ||= B
A= $opts{$name}B= $value
| |||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | # add cookies | ||||||||||||||||||||||||||||
| 540 | 0 | foreach my $name ( keys %{$cgi->oc} ) { | |||||||||||||||||||||||||||
| 541 | 0 | my (%element, %params, $cookie); | |||||||||||||||||||||||||||
| 542 | 0 | %element = %{$cgi->oc->{$name}}; | |||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | # 'values' takes precedence over 'value' | ||||||||||||||||||||||||||||
| 545 | 0 | ▶ | if ($element{'values'}) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 546 | 0 | { delete $element{'value'} } | |||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | # else if no 'value' either, set it to empty string | ||||||||||||||||||||||||||||
| 549 | elsif (! defined $element{'value'}) | ||||||||||||||||||||||||||||
| 550 | 0 | { $element{'value'} = '' } | |||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | # loop through values | ||||||||||||||||||||||||||||
| 553 | 0 | foreach my $key (keys %element) { | |||||||||||||||||||||||||||
| 554 | # special case: values | ||||||||||||||||||||||||||||
| 555 | 0 | ▶ | if ($key eq 'values') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 556 | 0 | $params{'-value'} = $element{$key}; | |||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | # else just copy value | ||||||||||||||||||||||||||||
| 560 | else { | ||||||||||||||||||||||||||||
| 561 | 0 | my $send_key = $key; | |||||||||||||||||||||||||||
| 562 | 0 | $send_key =~ s|^\-*|-|; | |||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | 0 | $params{$send_key} = $element{$key}; | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | # set domain | ||||||||||||||||||||||||||||
| 569 | 0 | ▶ | if ($element{'domain'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 570 | 0 | { $params{'-domain'} = $element{'domain'} } | |||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | # set expires: default to one year | ||||||||||||||||||||||||||||
| 573 | 0 | ▶ | if (exists $element{'expires'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | 0 | ▶ | if (defined $element{'expires'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 575 | 0 | { $params{'-expires'} = $element{'expires'} } | |||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | else { | ||||||||||||||||||||||||||||
| 578 | 0 | $params{'-expires'} = '+1y'; | |||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | # create cookie object | ||||||||||||||||||||||||||||
| 582 | 0 | $cookie = CGI::Cookie->new(%params); | |||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 0 | ▶ | if (! defined $cookie) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 585 | # showhash \%params, title=>'error generating cookie'; | ||||||||||||||||||||||||||||
| 586 | 0 | die 'cookie error'; | |||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | # add to cookie array | ||||||||||||||||||||||||||||
| 590 | 0 | push @cookies, $cookie; | |||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | # add cookies to header options | ||||||||||||||||||||||||||||
| 594 | 0 | ▶ | if (@cookies) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 595 | 0 | { $opts{'-cookie'} = \@cookies } | |||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | # note that header has been sent | ||||||||||||||||||||||||||||
| 598 | 0 | $cgi->{'header_sent'} = 1; | |||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | # call super method | ||||||||||||||||||||||||||||
| 601 | 0 | return $cgi->SUPER::header(%opts); | |||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||
| 603 | # | ||||||||||||||||||||||||||||
| 604 | # header_plus | ||||||||||||||||||||||||||||
| 605 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 611 | # CSRF info | ||||||||||||||||||||||||||||
| 612 | # | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | =head1 Cross-site request forgery (CSRF) defenses | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | A L<Cross-site request forgery|http://en.wikipedia.org/wiki/Cross-site_request_forgery> | ||||||||||||||||||||||||||||
| 617 | (CSRF) is a technique for breaching a web site's security. CSRF is one of the | ||||||||||||||||||||||||||||
| 618 | most common web-site vulnerabilities. CGI::Plus provides a technique for | ||||||||||||||||||||||||||||
| 619 | protecting | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | =cut | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | # | ||||||||||||||||||||||||||||
| 624 | # CSRF info | ||||||||||||||||||||||||||||
| 625 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 631 | # csrf | ||||||||||||||||||||||||||||
| 632 | # | ||||||||||||||||||||||||||||
| 633 | sub csrf { | ||||||||||||||||||||||||||||
| 634 | 2 | ▶ | my $cgi = shift; | ||||||||||||||||||||||||||
|
Subroutine
csrf: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | # set csrf value if sent | ||||||||||||||||||||||||||||
| 637 | 2 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 638 | 1 | $cgi->{'csrf'} = $_[0]; | |||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | # set csrf cookie | ||||||||||||||||||||||||||||
| 641 | 1 | ▶ | if ($cgi->{'csrf'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 642 | 1 | my ($cookie); | |||||||||||||||||||||||||||
| 643 | 1 | $cookie = $cgi->cookie_resend($cgi->csrf_name); | |||||||||||||||||||||||||||
| 644 | 1 | ▶ | $cookie->{'values'} ||= {v=>randword(10)}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cookie->{"values"} ||= {"v", randword(10)}
MC/DC0%0/1
$cookie->{"values"} ||= {"v", randword(10)}
$cookie->{"values"}
Truth table50%1/2
A
A= $cookie->{"values"}
| |||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | # return | ||||||||||||||||||||||||||||
| 649 | 1 | return $cgi->{'csrf'}; | |||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||
| 651 | # | ||||||||||||||||||||||||||||
| 652 | # csrf | ||||||||||||||||||||||||||||
| 653 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 657 | # csrf_name | ||||||||||||||||||||||||||||
| 658 | # | ||||||||||||||||||||||||||||
| 659 | sub csrf_name { | ||||||||||||||||||||||||||||
| 660 | 1 | ▶ | return 'csrf'; | ||||||||||||||||||||||||||
|
Subroutine
csrf_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | # | ||||||||||||||||||||||||||||
| 663 | # csrf_name | ||||||||||||||||||||||||||||
| 664 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 668 | # csrf_value | ||||||||||||||||||||||||||||
| 669 | # | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | =head2 $cgi->csrf_value() | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | Returns the string used in CSRF checks. This value must be included in an HTML | ||||||||||||||||||||||||||||
| 674 | form (see L</$cgi-E<gt>csrf_value()>) | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | =cut | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | sub csrf_value { | ||||||||||||||||||||||||||||
| 679 | 0 | ▶ | my ($cgi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
csrf_value: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 680 | 0 | my ($name, $cookie, $rv); | |||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | # must be in csrf mode | ||||||||||||||||||||||||||||
| 683 | 0 | ▶ | if (! $cgi->csrf) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 684 | 0 | { croak 'cannot set CSRF field when not in CSRF mode' } | |||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | # get name of csrf cookie | ||||||||||||||||||||||||||||
| 687 | 0 | $name = $cgi->csrf_name; | |||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | # get csrf cookie | ||||||||||||||||||||||||||||
| 690 | 0 | $cookie = $cgi->oc->{$name}; | |||||||||||||||||||||||||||
| 691 | 0 | ▶ | $cookie or die 'do not have csrf cookie'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | # return | ||||||||||||||||||||||||||||
| 694 | 0 | return $cookie->{'values'}->{'v'}; | |||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||
| 696 | # | ||||||||||||||||||||||||||||
| 697 | # csrf_value | ||||||||||||||||||||||||||||
| 698 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 702 | # csrf_field | ||||||||||||||||||||||||||||
| 703 | # | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | =head2 $cgi->csrf_field() | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | Returns a hidden HTML field with the CSRF check value in it. This field must | ||||||||||||||||||||||||||||
| 708 | be included in HTML forms if you do a CSRF check. The string will look | ||||||||||||||||||||||||||||
| 709 | something like this: | ||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | <input type="hidden" name="csrf" value="8hFnVjSr25"> | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | =cut | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | sub csrf_field { | ||||||||||||||||||||||||||||
| 716 | 0 | ▶ | my ($cgi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
csrf_field: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 717 | 0 | my ($name, $cookie, $rv); | |||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | # must be in csrf mode | ||||||||||||||||||||||||||||
| 720 | 0 | ▶ | if (! $cgi->csrf) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 721 | 0 | { croak 'cannot set CSRF field when not in CSRF mode' } | |||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | # get name of csrf cookie | ||||||||||||||||||||||||||||
| 724 | 0 | $name = $cgi->csrf_name; | |||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | # get csrf cookie | ||||||||||||||||||||||||||||
| 727 | 0 | $cookie = $cgi->oc->{$name}; | |||||||||||||||||||||||||||
| 728 | 0 | ▶ | $cookie or die 'do not have csrf cookie'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | # showhash $cookie; | ||||||||||||||||||||||||||||
| 731 | # showhash $cookie->{'values'}->{'v'}; | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | # build return value | ||||||||||||||||||||||||||||
| 734 | $rv = | ||||||||||||||||||||||||||||
| 735 | '<input type="hidden" ' . | ||||||||||||||||||||||||||||
| 736 | 'name="' . htmlesc($name) . '" ' . | ||||||||||||||||||||||||||||
| 737 | 0 | 'value="' . htmlesc($cookie->{'values'}->{'v'}) . '">'; | |||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||
| 739 | # return | ||||||||||||||||||||||||||||
| 740 | 0 | return $rv; | |||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||
| 742 | # | ||||||||||||||||||||||||||||
| 743 | # csrf_field | ||||||||||||||||||||||||||||
| 744 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 748 | # csrf_param | ||||||||||||||||||||||||||||
| 749 | # | ||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | =head2 $cgi->csrf_param() | ||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||
| 753 | Returns the URL parameter to use in a URL. The return value will look | ||||||||||||||||||||||||||||
| 754 | something like this: | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | csrf=8hFnVjSr25 | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | The string will never contain HTML or URL meta characters, so it does not need | ||||||||||||||||||||||||||||
| 759 | to be HTML or URL escaped. | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | =cut | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | sub csrf_param { | ||||||||||||||||||||||||||||
| 764 | 0 | ▶ | my ($cgi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
csrf_param: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 765 | 0 | my ($name, $cookie, $rv); | |||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | # must be in csrf mode | ||||||||||||||||||||||||||||
| 768 | 0 | ▶ | if (! $cgi->csrf) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 769 | 0 | { croak 'cannot set CSRF field when not in CSRF mode' } | |||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | # get name of csrf cookie | ||||||||||||||||||||||||||||
| 772 | 0 | $name = $cgi->csrf_name; | |||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | # get csrf cookie | ||||||||||||||||||||||||||||
| 775 | 0 | $cookie = $cgi->oc->{$name}; | |||||||||||||||||||||||||||
| 776 | 0 | ▶ | $cookie or die 'do not have csrf cookie'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | # build return value | ||||||||||||||||||||||||||||
| 779 | 0 | $rv = $name . '=' . $cookie->{'values'}->{'v'}; | |||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | # return | ||||||||||||||||||||||||||||
| 782 | 0 | return $rv; | |||||||||||||||||||||||||||
| 783 | } | ||||||||||||||||||||||||||||
| 784 | # | ||||||||||||||||||||||||||||
| 785 | # csrf_param | ||||||||||||||||||||||||||||
| 786 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 790 | # csrf_check | ||||||||||||||||||||||||||||
| 791 | # | ||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||
| 793 | =head2 $cgi->csrf_check() | ||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | Checks if a CSRF check value was sent and that it matches the CSRF check | ||||||||||||||||||||||||||||
| 796 | cookie. CSRF checks must be turned on or this method will croak. The | ||||||||||||||||||||||||||||
| 797 | following code is a typical usage of csrf checking: | ||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | $cgi->csrf(1); | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | if (! $cgi->csrf_check) { | ||||||||||||||||||||||||||||
| 802 | die 'security error'; | ||||||||||||||||||||||||||||
| 803 | } | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | =cut | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | sub csrf_check { | ||||||||||||||||||||||||||||
| 808 | 0 | ▶ | my ($cgi) = @_; | ||||||||||||||||||||||||||
|
Subroutine
csrf_check: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 809 | 0 | my ($name, $cookie, $cookie_value, $form_value); | |||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | # must be in csrf mode | ||||||||||||||||||||||||||||
| 812 | 0 | ▶ | if (! $cgi->csrf) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 813 | 0 | { croak 'cannot check CSRF when not in CSRF mode' } | |||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | # get name of csrf cookie | ||||||||||||||||||||||||||||
| 816 | 0 | $name = $cgi->csrf_name; | |||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||
| 818 | # get csrf cookie | ||||||||||||||||||||||||||||
| 819 | 0 | $cookie = $cgi->oc->{$name}; | |||||||||||||||||||||||||||
| 820 | 0 | ▶ | $cookie or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | # get cookie value | ||||||||||||||||||||||||||||
| 823 | 0 | $cookie_value = $cookie->{'values'}->{'v'}; | |||||||||||||||||||||||||||
| 824 | 0 | ▶ | $cookie_value or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||
| 826 | # get form value | ||||||||||||||||||||||||||||
| 827 | 0 | $form_value = $cgi->param($name); | |||||||||||||||||||||||||||
| 828 | 0 | ▶ | $form_value or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | # return true if same | ||||||||||||||||||||||||||||
| 831 | 0 | ▶ | if ($cookie_value eq $form_value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 832 | 0 | { return 1 } | |||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||
| 834 | # else return false | ||||||||||||||||||||||||||||
| 835 | 0 | return 0; | |||||||||||||||||||||||||||
| 836 | } | ||||||||||||||||||||||||||||
| 837 | # | ||||||||||||||||||||||||||||
| 838 | # csrf_check | ||||||||||||||||||||||||||||
| 839 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | # return true | ||||||||||||||||||||||||||||
| 845 | 1 | 1; | |||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||
| 847 | __END__ | ||||||||||||||||||||||||||||