Apache2/AuthCookie/Base.pm untested

filter: Statementstmt 0.0% 0 / 283 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 548 CC 65 SCAR 47.0
CC
65
Cov
0%
authenticate
342.0
login
182.0
recognize_user
132.0
CRAP 109.9
1 package Apache2::AuthCookie::Base;
2 $Apache2::AuthCookie::Base::VERSION = '3.32';
3 # ABSTRACT: Common Methods Shared by Apache2 and Apache2_4 AuthCookie Subclasses.
4  
5 use strict;
6 use mod_perl2 1.99022;
7 use Carp;
8  
9 use Apache::AuthCookie::Util qw(is_blank is_local_destination);
10 use Apache2::AuthCookie::Params;
11 use Apache2::RequestRec;
12 use Apache2::RequestUtil;
13 use Apache2::Log;
14 use Apache2::Access;
15 use Apache2::Response;
16 use Apache2::URI;
17 use Apache2::Util;
18 use APR::Table;
19 use Apache2::Const qw(OK DECLINED SERVER_ERROR M_GET HTTP_FORBIDDEN HTTP_MOVED_TEMPORARILY HTTP_OK);
20 use Encode ();
21  
22  
23 sub authenticate {
24 my ($auth_type, $r) = @_;
25  
26 my $debug = $r->dir_config("AuthCookieDebug") || 0;
27  
28 $r->server->log_error("authenticate() entry") if ($debug >= 3);
29 $r->server->log_error("auth_type " . $auth_type) if ($debug >= 3);
30  
31 if (my $prev = ($r->prev || $r->main)) {
32 # we are in a subrequest or internal redirect. Just copy user from the
33 # previous or main request if its is present
34 if (defined $prev->user) {
35 $r->server->log_error('authenticate() is in a subrequest or internal redirect.') if $debug >= 3;
36 # encoding would have been handled in prev req, so do not encode here.
37 $r->user( $prev->user );
38 return OK;
39 }
40 }
41  
42 if ($debug >= 3) {
43 $r->server->log_error("r=$r authtype=". $r->auth_type);
44 }
45  
46 if ($r->auth_type ne $auth_type) {
47 # This location requires authentication because we are being called,
48 # but we don't handle this AuthType.
49 $r->server->log_error("AuthType mismatch: $auth_type =/= ".$r->auth_type) if $debug >= 3;
50 return DECLINED;
51 }
52  
53 # Ok, the AuthType is $auth_type which we handle, what's the authentication
54 # realm's name?
55 my $auth_name = $r->auth_name;
56 $r->server->log_error("auth_name $auth_name") if $debug >= 2;
57 unless ($auth_name) {
58 $r->server->log_error("AuthName not set, AuthType=$auth_type", $r->uri);
59 return SERVER_ERROR;
60 }
61  
62 # Get the Cookie header. If there is a session key for this realm, strip
63 # off everything but the value of the cookie.
64 my $ses_key_cookie = $auth_type->key($r) || '';
65  
66 $r->server->log_error("ses_key_cookie " . $ses_key_cookie) if $debug >= 1;
67 $r->server->log_error("uri " . $r->uri) if $debug >= 2;
68  
69 if ($ses_key_cookie) {
70 my ($auth_user, @args) = $auth_type->authen_ses_key($r, $ses_key_cookie);
71  
72 if (!is_blank($auth_user) and scalar @args == 0) {
73 # We have a valid session key, so we return with an OK value.
74 # Tell the rest of Apache what the authentication method and
75 # user is.
76  
77 $r->ap_auth_type($auth_type);
78 $r->user( $auth_type->_encode($r, $auth_user) );
79 $r->server->log_error("user authenticated as $auth_user")
80 if $debug >= 1;
81  
82 # send new cookie if SessionTimeout is on
83 if (my $expires = $r->dir_config("${auth_name}SessionTimeout")) {
84 $auth_type->send_cookie($r, $ses_key_cookie,
85 {expires => $expires});
86 }
87  
88 return OK;
89 }
90 elsif (scalar @args > 0 and $auth_type->can('custom_errors')) {
91 return $auth_type->custom_errors($r, $auth_user, @args);
92 }
93 else {
94 # There was a session key set, but it's invalid for some reason. So,
95 # remove it from the client now so when the credential data is posted
96 # we act just like it's a new session starting.
97 $auth_type->remove_cookie($r);
98 $r->subprocess_env('AuthCookieReason', 'bad_cookie');
99 }
100 }
101 else {
102 $r->subprocess_env('AuthCookieReason', 'no_cookie');
103 }
104  
105 # This request is not authenticated, but tried to get a protected
106 # document. Send client the authen form.
107 return $auth_type->login_form($r);
108 }
109  
110  
111 sub cookie_name {
112 my ($self, $r) = @_;
113  
114 my $auth_type = $r->auth_type;
115 my $auth_name = $r->auth_name;
116  
117 my $cookie_name = $r->dir_config("${auth_name}CookieName") ||
118 "${auth_type}_${auth_name}";
119  
120 return $cookie_name;
121 }
122  
123  
124 sub cookie_string {
125 my $self = shift;
126 my %p = @_;
127 for (qw/request key/) {
128 croak "missing required parameter $_" unless defined $p{$_};
129 }
130 # its okay if value is undef here.
131  
132 my $r = $p{request};
133  
134 $p{value} = '' unless defined $p{value};
135  
136 my $string = sprintf '%s=%s', @p{'key','value'};
137  
138 my $auth_name = $r->auth_name;
139  
140 if (my $expires = $p{expires} || $r->dir_config("${auth_name}Expires")) {
141 $expires = Apache::AuthCookie::Util::expires($expires);
142 $string .= "; expires=$expires";
143 }
144  
145 $string .= '; path=' . ( $self->get_cookie_path($r) || '/' );
146  
147 if (my $domain = $r->dir_config("${auth_name}Domain")) {
148 $string .= "; domain=$domain";
149 }
150  
151 if ($r->dir_config("${auth_name}Secure")) {
152 $string .= '; secure';
153 }
154  
155 # HttpOnly is an MS extension. See
156 # http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp
157 if ($r->dir_config("${auth_name}HttpOnly")) {
158 $string .= '; HttpOnly';
159 }
160  
161 # SameSite is an anti-CSRF cookie property. See
162 # https://www.owasp.org/index.php/SameSite
163 if (my $samesite = $r->dir_config("${auth_name}SameSite")) {
164 if ($samesite =~ /\A(strict|lax)\z/i) {
165 $samesite = lc($1);
166 $string .= "; SameSite=$samesite";
167 }
168 }
169  
170 return $string;
171 }
172  
173  
174 sub decoded_requires {
175 my ($self, $r) = @_;
176  
177 my $reqs = $r->requires or return;
178 my $encoding = $self->requires_encoding($r);
179  
180 unless (is_blank($encoding)) {
181 for my $req (@$reqs) {
182 $$req{requirement} = Encode::decode($encoding, $$req{requirement});
183 }
184 }
185  
186 return $reqs;
187 }
188  
189  
190 sub decoded_user {
191 my ($self, $r) = @_;
192  
193 my $user = $r->user;
194  
195 if (is_blank($user)) {
196 return $user;
197 }
198  
199 my $encoding = $self->encoding($r);
200  
201 if (!is_blank($encoding)) {
202 $user = Encode::decode($encoding, $user);
203 }
204  
205 return $user;
206 }
207  
208  
209 sub encoding {
210 my ($self, $r) = @_;
211  
212 my $auth_name = $r->auth_name;
213  
214 return $r->dir_config("${auth_name}Encoding");
215 }
216  
217  
218 sub escape_uri {
219 my ($r, $string) = @_;
220 return Apache2::Util::escape_path($string, $r->pool);
221 }
222  
223  
224 sub get_cookie_path {
225 my ($self, $r) = @_;
226  
227 my $auth_name = $r->auth_name;
228  
229 return $r->dir_config("${auth_name}Path");
230 }
231  
232  
233 sub handle_cache {
234 my ($self, $r) = @_;
235  
236 my $auth_name = $r->auth_name;
237  
238 return unless $auth_name;
239  
240 unless ($r->dir_config("${auth_name}Cache")) {
241 $r->no_cache(1);
242 $r->err_headers_out->set(Pragma => 'no-cache');
243 }
244 }
245  
246  
247 sub key {
248 my ($self, $r) = @_;
249  
250 my $cookie_name = $self->cookie_name($r);
251  
252 my $allcook = ($r->headers_in->get("Cookie") || "");
253  
254 return ($allcook =~ /(?:^|\s)$cookie_name=([^;]*)/)[0];
255 }
256  
257  
258 sub login {
259 my ($self, $r) = @_;
260  
261 my $debug = $r->dir_config("AuthCookieDebug") || 0;
262  
263 my $auth_type = $r->auth_type;
264 my $auth_name = $r->auth_name;
265  
266 my $params = $self->params($r);
267  
268 if ($r->method eq 'POST') {
269 $self->_convert_to_get($r);
270 }
271  
272 my $default_destination = $r->dir_config("${auth_name}DefaultDestination");
273 my $destination = $params->param('destination');
274  
275 if (is_blank($destination)) {
276 if (!is_blank($default_destination)) {
277 $destination = $default_destination;
278 $r->server->log_error("destination set to $destination");
279 }
280 else {
281 $r->server->log_error("No key 'destination' found in form data");
282 $r->subprocess_env('AuthCookieReason', 'no_cookie');
283 return $auth_type->login_form($r);
284 }
285 }
286  
287 if ($r->dir_config("${auth_name}EnforceLocalDestination")) {
288 my $current_url = $r->construct_url;
289 unless (is_local_destination($destination, $current_url)) {
290 $r->server->log_error("non-local destination $destination detected for uri ",$r->uri);
291  
292 if (is_local_destination($default_destination, $current_url)) {
293 $destination = $default_destination;
294 $r->server->log_error("destination changed to $destination");
295 }
296 else {
297 $r->server->log_error("Returning login form: non local destination: $destination");
298 $r->subprocess_env('AuthCookieReason', 'no_cookie');
299 return $auth_type->login_form($r);
300 }
301 }
302 }
303  
304 # Get the credentials from the data posted by the client
305 my @credentials;
306 for (my $i = 0; defined $params->param("credential_$i"); $i++) {
307 my $key = "credential_$i";
308 my $val = $params->param($key);
309 $r->server->log_error("$key $val") if $debug >= 2;
310 push @credentials, $val;
311 }
312  
313 # save creds in pnotes so login form script can use them if it wants to
314 $r->pnotes("${auth_name}Creds", \@credentials);
315  
316 # Exchange the credentials for a session key.
317 my $ses_key = $self->authen_cred($r, @credentials);
318 unless ($ses_key) {
319 $r->server->log_error("Bad credentials") if $debug >= 2;
320 $r->subprocess_env('AuthCookieReason', 'bad_credentials');
321 $r->uri($self->untaint_destination($destination));
322 return $auth_type->login_form($r);
323 }
324  
325 if ($debug >= 2) {
326 defined $ses_key ? $r->server->log_error("ses_key $ses_key")
327 : $r->server->log_error("ses_key undefined");
328 }
329  
330 $self->send_cookie($r, $ses_key);
331  
332 $self->handle_cache($r);
333  
334 if ($debug >= 2) {
335 $r->server->log_error("redirect to $destination");
336 }
337  
338 $r->headers_out->set(
339 "Location" => $self->untaint_destination($destination));
340  
341 return HTTP_MOVED_TEMPORARILY;
342 }
343  
344  
345 sub login_form {
346 my ($self, $r) = @_;
347  
348 my $auth_name = $r->auth_name;
349  
350 if ($r->method eq 'POST') {
351 $self->_convert_to_get($r);
352 }
353  
354 # There should be a PerlSetVar directive that gives us the URI of
355 # the script to execute for the login form.
356  
357 my $authen_script;
358 unless ($authen_script = $r->dir_config($auth_name . "LoginScript")) {
359 $r->server->log_error("PerlSetVar '${auth_name}LoginScript' not set", $r->uri);
360 return SERVER_ERROR;
361 }
362  
363 my $status = $self->login_form_status($r);
364 $status = HTTP_FORBIDDEN unless defined $status;
365  
366 $r->custom_response($status, $authen_script);
367  
368 return $status;
369 }
370  
371  
372 sub login_form_status {
373 my ($self, $r) = @_;
374  
375 my $ua = $r->headers_in->get('User-Agent')
376 or return HTTP_FORBIDDEN;
377  
378 if (Apache::AuthCookie::Util::understands_forbidden_response($ua)) {
379 return HTTP_FORBIDDEN;
380 }
381 else {
382 return HTTP_OK;
383 }
384 }
385  
386  
387 sub logout {
388 my ($self,$r) = @_;
389  
390 my $debug = $r->dir_config("AuthCookieDebug") || 0;
391  
392 $self->remove_cookie($r);
393  
394 $self->handle_cache($r);
395 }
396  
397  
398 sub params {
399 my ($self, $r) = @_;
400  
401 return Apache2::AuthCookie::Params->new($r);
402 }
403  
404  
405 sub recognize_user {
406 my ($self, $r) = @_;
407  
408 # only check if user is not already set
409 return DECLINED unless is_blank($r->user);
410  
411 my $debug = $r->dir_config("AuthCookieDebug") || 0;
412  
413 my $auth_type = $r->auth_type;
414 my $auth_name = $r->auth_name;
415  
416 return DECLINED if is_blank($auth_type) or is_blank($auth_name);
417  
418 return DECLINED if is_blank($r->headers_in->get('Cookie'));
419  
420 my $cookie = $self->key($r);
421 my $cookie_name = $self->cookie_name($r);
422  
423 $r->server->log_error("cookie $cookie_name is $cookie")
424 if $debug >= 2;
425  
426 return DECLINED if is_blank($cookie);
427  
428 my ($user,@args) = $auth_type->authen_ses_key($r, $cookie);
429  
430 if (!is_blank($user) and scalar @args == 0) {
431 $r->server->log_error("user is $user") if $debug >= 2;
432  
433 # send cookie with update expires timestamp if session timeout is on
434 if (my $expires = $r->dir_config("${auth_name}SessionTimeout")) {
435 $self->send_cookie($r, $cookie, {expires => $expires});
436 }
437  
438 $r->user( $self->_encode($r, $user) );
439 }
440 elsif (scalar @args > 0 and $auth_type->can('custom_errors')) {
441 return $auth_type->custom_errors($r, $user, @args);
442 }
443  
444 return is_blank($user) ? DECLINED : OK;
445 }
446  
447  
448 sub remove_cookie {
449 my ($self, $r) = @_;
450  
451 my $cookie_name = $self->cookie_name($r);
452  
453 my $debug = $r->dir_config("AuthCookieDebug") || 0;
454  
455 my $str = $self->cookie_string(
456 request => $r,
457 key => $cookie_name,
458 value => '',
459 expires => 'Mon, 21-May-1971 00:00:00 GMT'
460 );
461  
462 $r->err_headers_out->add("Set-Cookie" => "$str");
463 $r->server->log_error("removed cookie $cookie_name") if $debug >= 2;
464 }
465  
466  
467 sub requires_encoding {
468 my ($self, $r) = @_;
469  
470 my $auth_name = $r->auth_name;
471  
472 return $r->dir_config("${auth_name}RequiresEncoding");
473 }
474  
475  
476 sub send_cookie {
477 my ($self, $r, $ses_key, $cookie_args) = @_;
478  
479 $cookie_args = {} unless defined $cookie_args;
480  
481 my $cookie_name = $self->cookie_name($r);
482  
483 my $cookie = $self->cookie_string(
484 request => $r,
485 key => $cookie_name,
486 value => $ses_key,
487 %$cookie_args
488 );
489  
490 $self->send_p3p($r);
491  
492 $r->err_headers_out->add("Set-Cookie" => $cookie);
493 }
494  
495  
496 sub send_p3p {
497 my ($self, $r) = @_;
498  
499 my $auth_name = $r->auth_name;
500  
501 if (my $p3p = $r->dir_config("${auth_name}P3P")) {
502 $r->err_headers_out->set(P3P => $p3p);
503 }
504 }
505  
506  
507 sub untaint_destination {
508 my ($self, $dest) = @_;
509  
510 return Apache::AuthCookie::Util::escape_destination($dest);
511 }
512  
513 # convert current request to GET
514 sub _convert_to_get {
515 my ($self, $r) = @_;
516  
517 return unless $r->method eq 'POST';
518  
519 my $debug = $r->dir_config("AuthCookieDebug") || 0;
520  
521 $r->server->log_error("Converting POST -> GET") if $debug >= 2;
522  
523 my $args = $self->params($r);
524  
525 my @pairs = ();
526  
527 for my $name ($args->param) {
528 # we dont want to copy login data, only extra data
529 next if $name eq 'destination'
530 or $name =~ /^credential_\d+$/;
531  
532 for my $v ($args->param($name)) {
533 push @pairs, escape_uri($r, $name) . '=' . escape_uri($r, $v);
534 }
535 }
536  
537 $r->args(join '&', @pairs) if scalar(@pairs) > 0;
538  
539 $r->method('GET');
540 $r->method_number(M_GET);
541 $r->headers_in->unset('Content-Length');
542 }
543  
544 sub _encode {
545 my ($self, $r, $value) = @_;
546  
547 my $encoding = $self->encoding($r);
548  
549 if (is_blank($encoding)) {
550 return $value;
551 }
552 else {
553 return Encode::encode($encoding, $value);
554 }
555 }
556  
557 1;
558  
559 __END__