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/OpenAICompatible.pm ↑ Summary blib/lib/Langertha/Engine/Ollama.pm »
1 package Langertha::HTTP::Redirect;
2 # ABSTRACT: The redirect policy both HTTP backends follow: credentials stay on their origin
3 14 our $VERSION = '0.503';
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶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 14 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 14 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 14 my $MIN_SEARCHED = 8;
36  
37 14 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 14 1;
149  
150 __END__
151  
152 =pod
153  
154 =encoding UTF-8
155  
156 =head1 NAME
157  
158 Langertha::HTTP::Redirect - The redirect policy both HTTP backends follow: credentials stay on their origin
159  
160 =head1 VERSION
161  
162 version 0.503
163  
164 =head1 SYNOPSIS
165  
166 use Langertha::HTTP::Redirect;
167  
168 # Net::Async::HTTP, one hop at a time (max_redirects => 0):
169 my $next = Langertha::HTTP::Redirect::next_request( $request, $response );
170 return $response unless $next; # not followed: the caller sees the 3xx
171  
172 # LWP (Langertha::HTTP::UserAgent::redirect_ok), on the referral LWP built:
173 return 0 unless Langertha::HTTP::Redirect::guard_referral( $referral, $response );
174  
175 =head1 DESCRIPTION
176  
177 B<Internal module.> Its interface may change without notice.
178  
179 An engine puts its credential on each request for B<its own> origin: an
180 C<Authorization> or C<x-api-key> header, a C<?key=> query parameter. Neither
181 HTTP client keeps it there on a redirect. L<LWP::UserAgent> clones the request
182 with every header (it drops only C<Authorization>, and only since 6.83), so an
183 C<x-api-key> reached whatever host the server named; L<Net::Async::HTTP> keeps
184 the Location's query, so a server echoing the request URI (C<return 301
185 https://new.example$request_uri>) sent Gemini's C<?key=> along (karr k374).
186  
187 This module is the one policy both backends follow
188 (L<Langertha::HTTP::UserAgent> for LWP, L<Langertha::Role::AsyncHTTP> for
189 L<Net::Async::HTTP>):
190  
191 =over 4
192  
193 =item * Only C<GET> and C<HEAD> are followed, on 301, 302, 303, 307 and 308
194 with a C<Location> — what both clients follow by default. A C<POST> (chat,
195 embeddings, a key in the body) is never re-sent anywhere, even through an
196 agent whose C<requests_redirectable> lists it.
197  
198 =item * Only to C<http> or C<https>, and never from C<https> down to C<http>.
199  
200 =item * On the B<same origin> (scheme, host and port) the request goes on
201 unchanged, credential included. C<Cookie> and C<Host> are never carried over.
202 C<http://host> to C<https://host> is B<another> origin (the scheme differs):
203 the credential is dropped, so a keyed GET behind an http-to-https redirect
204 gets a 401 from the https side. Configure the engine with the C<https> URL.
205  
206 =item * To B<another origin> only the representation headers go along
207 (C<Accept>, C<Accept-Charset>, C<Accept-Encoding>, C<Accept-Language>,
208 C<Content-Type>, C<Content-Language>, C<User-Agent>) — every other header is
209 dropped, so an auth header of any name stays behind without a list of names to
210 keep up to date. Userinfo is dropped from the new URL. Every query parameter
211 whose value is a credential the chain of requests carried (a query value
212 under a credential-like name such as C<key>, or a credential-like header's
213 value, without its C<Bearer>/C<Basic> scheme) is removed; the target's own
214 query is otherwise kept byte for byte. If such a credential would still be in
215 the new URL (in its path, say), the redirect is not followed.
216  
217 =back
218  
219 A redirect that is not followed leaves the 3xx response to the caller, which
220 then fails with its status line like any other non-success response. When
221 this policy refused it, the response carries a C<Client-Warning> header saying
222 why (C<redirect not followed: Langertha::HTTP::Redirect: ...>); so does the
223 last 3xx when the hop limit ran out on L<Net::Async::HTTP>.
224  
225 =head2 same_origin
226  
227 Langertha::HTTP::Redirect::same_origin( $uri_a, $uri_b ); # 1 or 0
228  
229 True when both URLs have the same scheme, host and port (compared
230 case-insensitively, a default port equal to its explicit number).
231  
232 =head2 next_request
233  
234 my $next = Langertha::HTTP::Redirect::next_request( $request, $response );
235 my $next = Langertha::HTTP::Redirect::next_request( $request, $response, $pinned_host );
236  
237 The request to send for the redirect C<$response> to C<$request>, or C<undef>
238 when it is not followed. C<$request> is not modified. The chain of earlier
239 responses (C<< $response->previous >>) is searched for credentials too.
240 C<$pinned_host> is passed on to L</guard_referral>.
241  
242 =head2 guard_referral
243  
244 my $follow = Langertha::HTTP::Redirect::guard_referral( $referral, $response );
245 my $follow = Langertha::HTTP::Redirect::guard_referral( $referral, $response, $pinned_host );
246  
247 Applies the policy to C<$referral>, the request about to be sent for the
248 redirect C<$response> (whose C<request> is the hop it answers). Returns false
249 when the redirect must not be followed, and then adds a C<Client-Warning>
250 header naming the reason to C<$response> (C<redirect not followed:
251 Langertha::HTTP::Redirect: ...>); otherwise strips C<$referral> in place if it
252 goes to another origin and returns true. The method checked is the one of
253 C<< $response->request >>, so a C<POST> is refused even when an agent's
254 C<requests_redirectable> would allow it.
255  
256 When C<$pinned_host> is given (the host of an engine's
257 L<Langertha::Role::HTTP/connect_address>), a redirect from that host to any
258 other host is refused too (C<connect_address pins ...; not to another host>):
259 the pinned address was checked for that host only. A redirect staying on the
260 host, on any port, is decided as usual and connects to the pinned address
261 again.
262  
263 =head1 SUPPORT
264  
265 =head2 Issues
266  
267 Please report bugs and feature requests on GitHub at
268 L<https://github.com/Getty/langertha/issues>.
269  
270 =head2 IRC
271  
272 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
273  
274 =head1 CONTRIBUTING
275  
276 Contributions are welcome! Please fork the repository and submit a pull request.
277  
278 =head1 AUTHOR
279  
280 Torsten Raudssus <getty@cpan.org>
281  
282 =head1 COPYRIGHT AND LICENSE
283  
284 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
285  
286 This is free software; you can redistribute it and/or modify it under
287 the same terms as the Perl 5 programming language system itself.
288  
289 =cut
« blib/lib/Langertha/Role/OpenAICompatible.pm ↑ Summary blib/lib/Langertha/Engine/Ollama.pm »