blib/lib/Langertha/HTTP/Redirect.pm

filter: Statementstmt 100% 87 / 87 Branchbran 89.2% 50 / 56 Conditioncond 64.0% 41 / 64 MC/DCmcdc 29.0% 9 / 31 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 79.6% 200 / 251 CC 58 SCAR 29.8
CC
58
Cov
86%
guard_referral
30.0
same_origin
16.3
_chain_secrets
14.7
CRAP 19.6
« blib/lib/Langertha/Role/AnthropicCompatible.pm ↑ Summary blib/lib/Langertha/ServerTool.pm »
1 package Langertha::HTTP::Redirect;
2 # ABSTRACT: The redirect policy both HTTP backends follow: credentials stay on their origin
3 15 our $VERSION = '0.505';
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use URI::Escape qw( uri_unescape );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # Headers that describe the representation, not the caller: the only ones that
12 # go to another origin.
13 15 my %KEEP = map { $_ => 1 } qw(
14 accept accept-charset accept-encoding accept-language
15 content-type content-language user-agent
16 );
17  
18 # Names under which a request carries a credential. Only used to find the
19 # secret values the chain sent (which must not reach another origin in a URL);
20 # headers to another origin are dropped by the %KEEP allowlist, not by name.
21 # Anchored on the whole name: a pagination cursor (Gemini's pageToken,
22 # page_token, nextPageToken) is not a credential, and treating its value as one
23 # would drop it from a cross-origin redirect and lose the page.
24 15 my $CREDENTIAL_NAME = qr/\A(?:
25 (?:api[-_]?)?key
26 | token | (?:access|auth|bearer|id|refresh|session)[-_]?token
27 | auth | (?:proxy-)?authori[sz]ation
28 | (?:client[-_])?secret | passw(?:or)?d
29 | sig | signature
30 | (?:[a-z0-9]+-)+(?:token|key|secret)
31 )\z/xi;
32  
33 # A secret this short would match too much of an unrelated URL; it is still
34 # dropped as an exact query value, just not searched for in the rest of the URL.
35 15 my $MIN_SEARCHED = 8;
36  
37 15 my %FOLLOWED_CODE = map { $_ => 1 } 301, 302, 303, 307, 308;
38  
39 95 ▶sub same_origin {
Subroutine same_origin: called CC=12 SCAR=27.9
Pod documented
40 95 my ( $left, $right ) = map { URI->new("$_") } @_;
41 95 ▶ return 0 unless lc( $left->scheme // '' ) eq lc( $right->scheme // '' );
Branch: 1 branch
BranchTrueFalseTotal
unless lc($left->scheme // "") eq lc($right->scheme // "") 1 94 95
Condition50%2/4
$left->scheme // ""
l!l
950
$right->scheme // ""
l!l
950
MC/DC0%0/1
$left->scheme // ""
$left->scheme
MC/DC0%0/1
$right->scheme // ""
$right->scheme
Truth table50%1/2
A
A= $left->scheme
Aresult
11
00
Truth table50%1/2
A
A= $right->scheme
Aresult
11
00
42 94 ▶ return 0 unless $left->can('host') && $right->can('host');
Branch: 1 branch
BranchTrueFalseTotal
unless $left->can("host") and $right->can("host") 0 94 94
Condition33%1/3
$left->can("host") and $right->can("host")
!ll&&!rl&&r
0094
MC/DC0%0/2
$left->can("host") and $right->can("host")
$left->can("host") $right->can("host")
Truth table33%1/3
A and B
A= $left->can("host")B= $right->can("host")
ABresult
0X0
100
111
43 94 ▶ return 0 unless lc( $left->host // '' ) eq lc( $right->host // '' );
Branch: 1 branch
BranchTrueFalseTotal
unless lc($left->host // "") eq lc($right->host // "") 32 62 94
Condition50%2/4
$left->host // ""
l!l
940
$right->host // ""
l!l
940
MC/DC0%0/1
$left->host // ""
$left->host
MC/DC0%0/1
$right->host // ""
$right->host
Truth table50%1/2
A
A= $left->host
Aresult
11
00
Truth table50%1/2
A
A= $right->host
Aresult
11
00
44 62 ▶ return ( $left->port // 0 ) == ( $right->port // 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($left->port // 0) == ($right->port // 0) ? : 34 28 62
Condition50%2/4
$left->port // 0
l!l
620
$right->port // 0
l!l
620
MC/DC0%0/1
$left->port // 0
$left->port
MC/DC0%0/1
$right->port // 0
$right->port
Truth table50%1/2
A
A= $left->port
Aresult
11
00
Truth table50%1/2
A
A= $right->port
Aresult
11
00
45 }
46  
47  
48 122 ▶sub next_request {
Subroutine next_request: called CC=7 SCAR=19.5
Pod documented
49 122 my ( $request, $response, $pinned_host ) = @_;
50 122 ▶ return undef unless $FOLLOWED_CODE{ $response->code };
Branch: 1 branch
BranchTrueFalseTotal
unless $FOLLOWED_CODE{$response->code} 61 61 122
51 61 my $location = $response->header('Location');
52 61 ▶ return undef unless defined $location && length $location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location and length $location 1 60 61
Condition66%2/3
defined $location and length $location
!ll&&!rl&&r
1060
MC/DC50%1/2
defined $location and length $location
defined $location length $location
Truth table66%2/3
A and B
A= defined $locationB= length $location
ABresult
0X0
100
111
53 60 ▶ return _refuse( $response, 'only GET and HEAD are redirected, not ' . uc $request->method )
Branch: 1 branch
BranchTrueFalseTotal
unless uc($request->method) =~ /\A(?:GET|HEAD)\z/ 3 57 60
54 unless ( uc $request->method ) =~ /\A(?:GET|HEAD)\z/;
55 57 my $next = HTTP::Request->new( $request->method,
56 URI->new_abs( $location, $request->uri ), $request->headers->clone, $request->content );
57 57 ▶ $next->protocol( $request->protocol ) if defined $request->protocol;
Branch: 1 branch
BranchTrueFalseTotal
if defined $request->protocol 0 57 57
58 57 $next->remove_header( 'Host', 'Cookie' );
59 57 ▶ return guard_referral( $next, $response, $pinned_host ) ? $next : undef;
Branch: 1 branch
BranchTrueFalseTotal
guard_referral($next, $response, $pinned_host) ? : 48 9 57
60 }
61  
62  
63 110 ▶sub guard_referral {
Subroutine guard_referral: called CC=29 SCAR=34.0
Pod documented
64 110 my ( $referral, $response, $pinned_host ) = @_;
65 # The method of the request the redirect answers, not the referral's: LWP
66 # turns a 302/303 POST into a GET, and a requests_redirectable with POST in it
67 # would otherwise re-send a body (a chat, AKI's key) to wherever Location says.
68 110 my $original = $response->request;
69 110 ▶ return _refuse( $response, 'only GET and HEAD are redirected, not '
Branch: 2 branches
BranchTrueFalseTotal
unless $original and uc($original->method) =~ /\A(?:GET|HEAD)\z/ 9 101 110
$original ? : 9 0 9
Condition66%2/3
$original and uc($original->method) =~ /\A(?:GET|HEAD)\z/
!ll&&!rl&&r
09101
MC/DC50%1/2
$original and uc($original->method) =~ /\A(?:GET|HEAD)\z/
$original uc($original->method) =~ /\A(?:GET|HEAD)\z/
Truth table66%2/3
A and B
A= $originalB= uc($original->method) =~ /\A(?:GET|HEAD)\z/
ABresult
0X0
100
111
70 . ( $original ? uc $original->method : 'a request of unknown method' ) )
71 unless $original && ( uc $original->method ) =~ /\A(?:GET|HEAD)\z/;
72 101 my $from = $original->uri;
73 101 my $to = $referral->uri;
74 101 ▶ my $scheme = lc( $to->scheme // '' );
Condition50%1/2
$to->scheme // ""
l!l
1010
MC/DC0%0/1
$to->scheme // ""
$to->scheme
Truth table50%1/2
A
A= $to->scheme
Aresult
11
00
75 101 ▶ return _refuse( $response, "not to a non-HTTP URL ($scheme)" )
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "http" or $scheme eq "https" 1 100 101
Condition100%3/3
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
64361
MC/DC100%2/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table100%3/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
76 unless $scheme eq 'http' || $scheme eq 'https';
77 100 ▶ return _refuse( $response, 'not from https to http' )
Branch: 1 branch
BranchTrueFalseTotal
if lc($from->scheme // "") eq "https" and $scheme eq "http" 2 98 100
Condition80%4/5
lc($from->scheme // "") eq "https" and $scheme eq "http"
!ll&&!rl&&r
62362
$from->scheme // ""
l!l
1000
MC/DC50%1/2
lc($from->scheme // "") eq "https" and $scheme eq "http"
$from->scheme $scheme eq "http"
Truth table66%2/3
A and B
A= $from->schemeB= $scheme eq "http"
ABresult
0X0
100
111
78 if lc( $from->scheme // '' ) eq 'https' && $scheme eq 'http';
79 # A connection pinned to a checked address (connect_address, karr k375) is
80 # pinned for its host only; another host would be resolved afresh, which is
81 # the DNS-rebinding window the pin closes.
82 98 ▶ return _refuse( $response, "connect_address pins $pinned_host; not to another host (" . lc( $to->host // '' ) . ')' )
Branch: 1 branch
BranchTrueFalseTotal
if defined $pinned_host and $from->can("host") and lc($from->host // "") eq lc $pinned_host and not $to->can("host") && lc($to->host // "") eq lc $pinned_host 7 91 98
Condition72%13/18
defined $pinned_host and $from->can("host") and lc($from->host // "") eq lc $pinned_host and not $to->can("host") && lc($to->host // "") eq lc $pinned_host
!ll&&!rl&&r
8477
defined $pinned_host and $from->can("host") and lc($from->host // "") eq lc $pinned_host
!ll&&!rl&&r
83114
defined $pinned_host and $from->can("host")
!ll&&!rl&&r
83015
$from->host // ""
l!l
150
$to->can('host') && lc($to->host // '') eq lc $pinned_host
!ll&&!rl&&r
077
$to->host // ""
l!l
140
$to->host // ""
l!l
70
MC/DC0%0/5
defined $pinned_host and $from->can("host") and lc($from->host // "") eq lc $pinned_host and not $to->can("host") && lc($to->host // "") eq lc $pinned_host
defined $pinned_host $from->can("host") $from->host $to->can('host') $to->host
Truth table33%2/6
A and B and C and not(D && E)
A= defined $pinned_hostB= $from->can("host")C= $from->hostD= $to->can('host')E= $to->host
ABCDEresult
0XXXX0
10XXX0
110XX0
111110
1110X1
111101
83 if defined $pinned_host && $from->can('host') && lc( $from->host // '' ) eq lc $pinned_host
84 && !( $to->can('host') && lc( $to->host // '' ) eq lc $pinned_host );
85 91 ▶ return 1 if same_origin( $from, $to );
Branch: 1 branch
BranchTrueFalseTotal
if same_origin($from, $to) 33 58 91
86  
87 58 $referral->remove_header($_) for grep { !$KEEP{ lc $_ } } $referral->header_field_names;
88  
89 58 $to = $to->clone;
90 58 $to->userinfo(undef);
91 58 my %secret = _chain_secrets($response);
92 58 ▶ if ( %secret && defined $to->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%secret and defined $to->query) 19 39 58
Condition100%3/3
%secret and defined $to->query
!ll&&!rl&&r
73219
MC/DC100%2/2
%secret and defined $to->query
%secret defined $to->query
Truth table100%3/3
A and B
A= %secretB= defined $to->query
ABresult
0X0
100
111
93 19 my @pairs = $to->query_form;
94 19 my ( @kept, $dropped );
95 19 ▶ while ( my ( $name, $value ) = splice @pairs, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 33 19 52
96 33 ▶ if ( defined $value && $secret{$value} ) { $dropped = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $secret{$value}) { } 18 15 33
Condition66%2/3
defined $value and $secret{$value}
!ll&&!rl&&r
01518
MC/DC50%1/2
defined $value and $secret{$value}
defined $value $secret{$value}
Truth table66%2/3
A and B
A= defined $valueB= $secret{$value}
ABresult
0X0
100
111
97 15 else { push @kept, $name, $value }
98 }
99 # Rewritten only when something was dropped, so an untouched query keeps
100 # its bytes (a presigned URL's signature must not be re-encoded).
101 19 ▶ if ($dropped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dropped) 18 1 19
102 18 ▶ if (@kept) { $to->query_form( \@kept ) }
Branch: 1 branch
BranchTrueFalseTotal
if (@kept) { } 13 5 18
103 5 else { $to->query(undef) }
104 }
105 }
106 58 my $url = uri_unescape("$to");
107 58 for my $value ( grep { length >= $MIN_SEARCHED } keys %secret ) {
108 80 ▶ return _refuse( $response, 'a credential of the request would be in the URL of another origin' )
Branch: 1 branch
BranchTrueFalseTotal
if index($url, $value) != -1 7 73 80
109 if index( $url, $value ) >= 0;
110 }
111 51 $referral->uri($to);
112 51 return 1;
113 }
114  
115 # A redirect that is not followed goes back to the caller as the 3xx; say why on
116 # it, the way LWP does for its own refusals (Client-Warning).
117 29 ▶sub _refuse {
Subroutine _refuse: called CC=1 SCAR=0.0
118 29 my ( $response, $reason ) = @_;
119 29 $response->push_header( 'Client-Warning' => "redirect not followed: Langertha::HTTP::Redirect: $reason" );
120 29 return undef;
121 }
122  
123  
124 # Every credential value the chain of requests behind $response carried: query
125 # values under a credential-like name, and credential-like header values (with
126 # and without their auth scheme). The value set, not the names, is what a
127 # redirect URL is checked against, so an echo under any name is caught.
128 58 ▶sub _chain_secrets {
Subroutine _chain_secrets: called CC=13 SCAR=26.9
129 58 my ($response) = @_;
130 58 my %secret;
131 58 ▶ for ( my $hop = $response; $hop; $hop = $hop->previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hop) 59 58 117
132 59 ▶ my $request = $hop->request or next;
Branch: 1 branch
BranchTrueFalseTotal
my $request = $hop->request or next 59 0 59
133 59 ▶ my @pairs = $request->uri->can('query_form') ? $request->uri->query_form : ();
Branch: 1 branch
BranchTrueFalseTotal
$request->uri->can('query_form') ? : 59 0 59
134 59 ▶ while ( my ( $name, $value ) = splice @pairs, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 29 59 88
135 29 ▶ $secret{$value} = 1 if defined $value && length $value && $name =~ $CREDENTIAL_NAME;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value and $name =~ /$CREDENTIAL_NAME/ 20 9 29
Condition50%3/6
defined $value and length $value and $name =~ /$CREDENTIAL_NAME/
!ll&&!rl&&r
0920
defined $value and length $value
!ll&&!rl&&r
0029
MC/DC33%1/3
defined $value and length $value and $name =~ /$CREDENTIAL_NAME/
defined $value length $value $name =~ /$CREDENTIAL_NAME/
Truth table50%2/4
A and B and C
A= defined $valueB= length $valueC= $name =~ /$CREDENTIAL_NAME/
ABCresult
0XX0
10X0
1100
1111
136 }
137 59 for my $name ( grep { $_ =~ $CREDENTIAL_NAME } $request->header_field_names ) {
138 46 for my $value ( $request->header($name) ) {
139 46 ▶ next unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 46 46
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
140 46 $secret{$value} = 1;
141 46 ▶ $secret{$1} = 1 if $value =~ /\A\S+\s+(\S+)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A\S+\s+(\S+)\s*\z/ 17 29 46
142 }
143 }
144 }
145 58 return %secret;
146 }
147  
148 15 1;
149  
150 __END__
« blib/lib/Langertha/Role/AnthropicCompatible.pm ↑ Summary blib/lib/Langertha/ServerTool.pm »