| 1 | package Apache::AuthCookie::Util; | ||
| 2 | $Apache::AuthCookie::Util::VERSION = '3.32'; | ||
| 3 | # ABSTRACT: Internal Utility Functions for AuthCookie | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base 'Exporter'; | ||
| 7 | use URI; | ||
| 8 | |||
| 9 | our @EXPORT_OK = qw( | ||
| 10 | is_blank | ||
| 11 | is_local_destination | ||
| 12 | ); | ||
| 13 | |||
| 14 | sub expires { | ||
| 15 | my($time,$format) = @_; | ||
| 16 | $format ||= 'http'; | ||
| 17 | |||
| 18 | my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; | ||
| 19 | my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/; | ||
| 20 | |||
| 21 | # pass through preformatted dates for the sake of expire_calc() | ||
| 22 | $time = expire_calc($time); | ||
| 23 | return $time unless $time =~ /^\d+$/; | ||
| 24 | |||
| 25 | # make HTTP/cookie date string from GMT'ed time | ||
| 26 | # (cookies use '-' as date separator, HTTP uses ' ') | ||
| 27 | my($sc) = ' '; | ||
| 28 | $sc = '-' if $format eq "cookie"; | ||
| 29 | my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time); | ||
| 30 | $year += 1900; | ||
| 31 | return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT", | ||
| 32 | $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec); | ||
| 33 | } | ||
| 34 | |||
| 35 | # -- expire_calc() shamelessly taken from CGI::Util | ||
| 36 | # This internal routine creates an expires time exactly some number of | ||
| 37 | # hours from the current time. It incorporates modifications from | ||
| 38 | # Mark Fisher. | ||
| 39 | sub expire_calc { | ||
| 40 | my($time) = @_; | ||
| 41 | my(%mult) = ('s'=>1, | ||
| 42 | 'm'=>60, | ||
| 43 | 'h'=>60*60, | ||
| 44 | 'd'=>60*60*24, | ||
| 45 | 'M'=>60*60*24*30, | ||
| 46 | 'y'=>60*60*24*365); | ||
| 47 | # format for time can be in any of the forms... | ||
| 48 | # "now" -- expire immediately | ||
| 49 | # "+180s" -- in 180 seconds | ||
| 50 | # "+2m" -- in 2 minutes | ||
| 51 | # "+12h" -- in 12 hours | ||
| 52 | # "+1d" -- in 1 day | ||
| 53 | # "+3M" -- in 3 months | ||
| 54 | # "+2y" -- in 2 years | ||
| 55 | # "-3m" -- 3 minutes ago(!) | ||
| 56 | # If you don't supply one of these forms, we assume you are | ||
| 57 | # specifying the date yourself | ||
| 58 | my($offset); | ||
| 59 | if (!$time || (lc($time) eq 'now')) { | ||
| 60 | $offset = 0; | ||
| 61 | } elsif ($time=~/^\d+/) { | ||
| 62 | return $time; | ||
| 63 | } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) { | ||
| 64 | $offset = ($mult{$2} || 1)*$1; | ||
| 65 | } else { | ||
| 66 | return $time; | ||
| 67 | } | ||
| 68 | return (time+$offset); | ||
| 69 | } | ||
| 70 | |||
| 71 | # escape embedded CR, LF, TAB's to prevent possible XSS attacks. | ||
| 72 | # see http://www.securiteam.com/securityreviews/5WP0E2KFGK.html | ||
| 73 | sub escape_destination { | ||
| 74 | my $text = shift; | ||
| 75 | |||
| 76 | $text =~ s/([\r\n\t\>\<"])/sprintf("%%%02X", ord $1)/ge; | ||
| 77 | |||
| 78 | return $text; | ||
| 79 | } | ||
| 80 | |||
| 81 | # return true if the given user agent understands a HTTP_FORBIDDEN response | ||
| 82 | # with custom content. Some agents (e.g.: Symbian OS browser), use their own | ||
| 83 | # HTML and completely ignore the HTTP content. | ||
| 84 | sub understands_forbidden_response { | ||
| 85 | my $ua = shift; | ||
| 86 | |||
| 87 | return 0 if $ua =~ qr{\AMozilla/5\.0 \(SymbianOS/} # Symbian phones | ||
| 88 | or $ua =~ qr{\bIEMobile/10}; # Nokia Lumia 920, possibly others? | ||
| 89 | |||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | # return true if the given value is blank or not defined. | ||
| 94 | sub is_blank { | ||
| 95 | return defined $_[0] && ($_[0] =~ /\S/) ? 0 : 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | # returns true if the given value looks like a local destination | ||
| 99 | sub is_local_destination { | ||
| 100 | my ($destination, $current_uri) = @_; | ||
| 101 | |||
| 102 | # blank location is not considered "local" | ||
| 103 | return 0 if is_blank($destination); | ||
| 104 | |||
| 105 | # If the location does not start with a scheme or is not protocol relative, | ||
| 106 | # then the location is local. | ||
| 107 | # Scheme is defined in RFC 3986 as: | ||
| 108 | # ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) | ||
| 109 | return 1 if $destination !~ m|^ (?: [a-z] [a-z0-9+-.]* :)? //|ix; | ||
| 110 | |||
| 111 | # Otherwise it is an absolute URL, but it might still be local to the | ||
| 112 | # current request, so we need to account for that. | ||
| 113 | $current_uri = URI->new($current_uri) or return 0; | ||
| 114 | my $destination_uri = URI->new($destination) or return 0; | ||
| 115 | |||
| 116 | # If the current URI and the destination have same scheme, host, and port, | ||
| 117 | # then the URL is local | ||
| 118 | return 1 if lc($current_uri->scheme) eq lc($destination_uri->scheme) | ||
| 119 | and lc($current_uri->host) eq lc($destination_uri->host) | ||
| 120 | and $current_uri->port == $destination_uri->port; | ||
| 121 | |||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | |||
| 127 | __END__ |