Impersonate/HTTP.pm

filter: Statementstmt 100% 97 / 97 Branchbran 88.8% 48 / 54 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 5 totaltotal 94.2% 181 / 192 CC 40 SCAR 24.1
CC
40
Cov
96%
coherent_headers
20.0
response_head
7.0
parse_request_head
6.0
CRAP 11.1
« Impersonate/Connection.pm ↑ Summary Impersonate/Cert.pm »
1 package Proxy::Impersonate::HTTP;
2 6 ▶use v5.10; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 our @EXPORT_OK = qw(parse_request_head body_length coherent_headers response_head strip_hop_headers);
5  
6 6 my %REASON = (
7 200=>'OK', 201=>'Created', 204=>'No Content', 205=>'Reset Content',
8 301=>'Moved Permanently', 302=>'Found', 303=>'See Other',
9 304=>'Not Modified', 307=>'Temporary Redirect', 308=>'Permanent Redirect',
10 400=>'Bad Request', 401=>'Unauthorized', 403=>'Forbidden', 404=>'Not Found',
11 411=>'Length Required', 417=>'Expectation Failed',
12 500=>'Internal Server Error', 502=>'Bad Gateway', 503=>'Service Unavailable',
13 504=>'Gateway Timeout',
14 );
15  
16 # Forward WebKit's real per-request headers so the origin sees a coherent request
17 # CONTEXT, not curl-impersonate's static navigation defaults. curl's template
18 # hard-codes Sec-Fetch-Mode: navigate / Sec-Fetch-Dest: document / Accept:
19 # text/html on EVERY request, which makes every subresource (image/script/XHR)
20 # look like a top-level document load -- a glaring tell. WebKit computes correct
21 # per-request Sec-Fetch-* + Accept, so forwarding them (they override the template
22 # in place) fixes it. The User-Agent/Sec-CH-UA identity headers are supplied
23 # separately via override_headers.
24 6 my %HOP = map { $_ => 1 } qw(
25 connection keep-alive proxy-connection proxy-authenticate proxy-authorization
26 te trailer transfer-encoding upgrade
27 );
28 6 my %DROP = (%HOP, map { $_ => 1 } qw(
29 host content-length expect user-agent accept accept-language accept-encoding
30 upgrade-insecure-requests priority http2-settings
31 ));
32  
33 41 ▶sub strip_hop_headers {
Subroutine strip_hop_headers: called CC=3 SCAR=11.0
Pod undocumented
34 41 my ($h) = @_;
35 41 ▶ if (my $conn = $h->{connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $h->{"connection"}) 2 39 41
36 2 ▶ delete @{$h}{map { split /\s*,\s*/, lc $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $conn eq 'ARRAY' ? : 0 2 2
37 (ref $conn eq 'ARRAY' ? @$conn : $conn)};
38 }
39 41 delete @{$h}{keys %HOP};
40 41 return $h;
41 }
42 # Accept-Language is NOT forwarded: WebKit/libsoup may render the profile
43 # languages in a non-Chrome format (comma-space, lower-cased region), a tell next
44 # to a Chrome JA4. It is supplied as an identity header (override_headers) in the
45 # exact Chrome format instead -- see EV::WebKit::Fingerprint::identity_headers.
46 # Headers a real browser sends only on a USER-ACTIVATED navigation: if WebKit did
47 # not send one, strip curl's template default (undef => "Header:") so a subresource
48 # (or an automated navigation) does not carry e.g. Sec-Fetch-User: ?1. NB
49 # Upgrade-Insecure-Requests is NOT here -- it is not user-gated (Chrome sends it on
50 # every navigation) and WebKit never emits it, so it is synthesized per target +
51 # destination in coherent_headers, not merely stripped.
52 6 my %CONDITIONAL = map { $_ => 1 } qw(sec-fetch-user);
53 # Chrome's per-destination Accept for the browser-FLAVORED destinations. NOT
54 # forwarded from WebKit for these (whose Accept is Safari-flavored -- a Chrome/JA4
55 # request with a Safari Accept is a tell) and NOT left as curl's static document
56 # default (wrong for subresources). Synthesized from Sec-Fetch-Dest so the value
57 # is Chrome's AND matches the request context. A frame navigation (iframe/frame)
58 # uses the same Accept as a top-level document.
59 6 my $DOC_ACCEPT = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7';
60 6 my %CHROME_ACCEPT = (
61 document => $DOC_ACCEPT,
62 iframe => $DOC_ACCEPT,
63 frame => $DOC_ACCEPT,
64 image => 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
65 style => 'text/css,*/*;q=0.1',
66 );
67  
68 # Parse an HTTP/1.x request head. Returns undef if the head is incomplete
69 # (no CRLFCRLF yet), else a hashref. headers is an ordered arrayref of [k,v]
70 # pairs, preserving case and order as received.
71 62 ▶sub parse_request_head {
Subroutine parse_request_head: called CC=6 SCAR=18.0
Pod undocumented
72 62 my ($buf) = @_;
73 62 my $end = index($buf, "\r\n\r\n");
74 62 ▶ return undef if $end < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 1 61 62
75 61 my $head = substr($buf, 0, $end);
76 61 my ($line, @lines) = split /\r\n/, $head;
77 61 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 61 61
78 61 ▶ my ($method, $target, $version) = $line =~ m{\A([!#\x24%&'*+.^_`|~0-9A-Za-z-]+) +([^\x00-\x20\x7f]+) +(HTTP/1\.[01])\z}
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ m[\A([!#\x24%&'*+.^_`|~0-9A-Za-z-]+) +([^\x00-\x20\x7f]+) +(HTTP/1\.[01])\z] or return undef 61 0 61
79 or return undef;
80 61 my @pairs;
81 61 for my $h (@lines) {
82 118 ▶ my ($k, $v) = $h =~ /\A([!#\x24%&'*+.^_`|~0-9A-Za-z-]+):[ \t]*([^\0\r\n]*[^ \t\0\r\n]|)[ \t]*\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $h =~ /\A([!#\x24%&'*+.^_`|~0-9A-Za-z-]+):[ \t]*([^\0\r\n]*[^ \t\0\r\n]|)[ \t]*\z/ or return undef 111 7 118
83 or return undef;
84 111 push @pairs, [$k, $v];
85 }
86 return {
87 54 method => $method, target => $target, version => $version,
88 headers => \@pairs, header_end => $end + 4,
89 };
90 }
91  
92 57 ▶sub _get {
Subroutine _get: called CC=3 SCAR=11.0
93 57 my ($pairs, $name) = @_;
94 57 $name = lc $name;
95 57 ▶ for (@$pairs) { return $_->[1] if lc($_->[0]) eq $name }
Branch: 1 branch
BranchTrueFalseTotal
if lc $_->[0] eq $name 3 108 111
96 54 return undef;
97 }
98  
99 # How the request/response body is framed. Returns ('chunked') or
100 # ('length', $n) or ('none').
101 57 ▶sub body_length {
Subroutine body_length: called CC=6 SCAR=17.9
Pod undocumented
102 57 my ($pairs) = @_;
103 57 my $te = _get($pairs, 'transfer-encoding');
104 57 ▶ return (length($te) ? 'chunked' : 'invalid') if defined $te;
Branch: 2 branches
BranchTrueFalseTotal
if defined $te 3 54 57
length $te ? : 3 0 3
105 54 my @cl = map { $_->[1] } grep { lc($_->[0]) eq 'content-length' } @$pairs;
106 54 ▶ if (@cl) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cl) 11 43 54
107 # RFC 7230 3.3.3: repeated (or comma-listed) values must all agree, and
108 # the value must be a plain non-negative integer. Anything else leaves
109 # the body unframeable -- and taking a guess would leave the remainder
110 # in the buffer to be re-parsed as a SECOND request, which is exactly
111 # the request-smuggling primitive. Refuse instead.
112 11 my %v = map { $_ => 1 } map { split /\s*,\s*/, $_, -1 } @cl;
113 11 my @vals = keys %v;
114 11 ▶ return ('invalid') if @vals != 1 || $vals[0] !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if @vals != 1 or not $vals[0] =~ /^\d+$/ 5 6 11
Condition100%3/3
@vals != 1 or not $vals[0] =~ /^\d+$/
l!l&&r!l&&!r
326
MC/DC100%2/2
@vals != 1 or not $vals[0] =~ /^\d+$/
@vals != 1 not $vals[0] =~ /^\d+$/
Truth table100%3/3
A or not(B)
A= @vals != 1B= not $vals[0] =~ /^\d+$/
ABresult
1X1
011
000
115 6 return ('length', $vals[0] + 0);
116 }
117 43 return ('none');
118 }
119  
120 # The request headers to forward upstream, lower-cased. A value of undef means
121 # "remove this header from the impersonate template".
122 60 ▶sub coherent_headers {
Subroutine coherent_headers: called CC=20 SCAR=30.0
Pod undocumented
123 60 my ($pairs, $chrome) = @_;
124 60 my (%out, %seen, $dest, $req_accept, @connection);
125 60 for (@$pairs) {
126 127 my $k = lc $_->[0];
127 127 ▶ $dest = $_->[1] if $k eq 'sec-fetch-dest';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "sec-fetch-dest" 16 111 127
128 127 ▶ $req_accept = $_->[1] if $k eq 'accept';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "accept" 15 112 127
129 127 ▶ push @connection, split /\s*,\s*/, lc $_->[1] if $k eq 'connection';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "connection" 5 122 127
130 127 ▶ next if $DROP{$k} || index($k, 'sec-ch-') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $DROP{$k} or index($k, "sec-ch-") == 0 82 45 127
Condition100%3/3
$DROP{$k} or index($k, "sec-ch-") == 0
l!l&&r!l&&!r
81145
MC/DC100%2/2
$DROP{$k} or index($k, "sec-ch-") == 0
$DROP{$k} index($k, "sec-ch-") == 0
Truth table100%3/3
A or B
A= $DROP{$k}B= index($k, "sec-ch-") == 0
ABresult
1X1
011
000
131 45 $out{$k} = $_->[1];
132 45 ▶ $seen{$k} = 1 if $CONDITIONAL{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $CONDITIONAL{$k} 1 44 45
133 }
134 60 $out{$_} = undef for grep { !$seen{$_} } keys %CONDITIONAL;
135 # Upgrade-Insecure-Requests: real Chrome sends "1" on every navigation
136 # (document/iframe/frame) and nothing on a subresource; Safari sends it on
137 # neither; WebKit emits it never. So strip curl's static template UIR by default
138 # and synthesize "1" only for a Chrome-target navigation (below).
139 60 $out{'upgrade-insecure-requests'} = undef;
140 # Accept, target-family-aware. For a CHROME target WebKit's own Accept is
141 # Safari-flavored (a tell next to a Chrome JA4), so synthesize Chrome's per-
142 # destination value (%CHROME_ACCEPT); for an unmapped dest that carried an
143 # app-set Accept (EventSource text/event-stream, fetch application/json --
144 # browser-agnostic, which Chrome also sends) forward it rather than flattening
145 # to */*; else */*. For a SAFARI target WebKit IS WebKit, so its own per-
146 # request Accept is already the right family AND per-destination -> forward it
147 # unchanged (synthesizing Chrome's here would be the same tell, reversed).
148 # Only acts when the request carried Sec-Fetch-Dest (a real WebKit request);
149 # otherwise curl's template Accept stands.
150 60 ▶ if (defined $dest) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest) 16 44 60
151 16 my $d = lc $dest;
152 16 ▶ $out{'upgrade-insecure-requests'} = '1'
Branch: 1 branch
BranchTrueFalseTotal
if $chrome and $d eq "document" || $d eq "iframe" || $d eq "frame" 6 10 16
Condition100%9/9
$chrome and $d eq "document" || $d eq "iframe" || $d eq "frame"
!ll&&!rl&&r
376
$d eq "document" or $d eq "iframe" or $d eq "frame"
l!l&&r!l&&!r
517
$d eq "document" or $d eq "iframe"
l!l&&r!l&&!r
328
MC/DC100%4/4
$chrome and $d eq "document" || $d eq "iframe" || $d eq "frame"
$chrome $d eq "document" $d eq "iframe" $d eq "frame"
Truth table100%5/5
A and B or C or D
A= $chromeB= $d eq "document"C= $d eq "iframe"D= $d eq "frame"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
153 if $chrome && ($d eq 'document' || $d eq 'iframe' || $d eq 'frame');
154 16 ▶ if ($chrome) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chrome) { } 13 3 16
elsif (defined $req_accept) { } 2 1 3
155 13 ▶ $out{accept} = exists $CHROME_ACCEPT{$d} ? $CHROME_ACCEPT{$d}
Branch: 2 branches
BranchTrueFalseTotal
exists $CHROME_ACCEPT{$d} ? : 8 5 13
defined $req_accept ? : 3 2 5
156 : defined $req_accept ? $req_accept
157 : '*/*';
158 }
159 2 elsif (defined $req_accept) { $out{accept} = $req_accept }
160 1 else { $out{accept} = '*/*' } # symmetry: never leak curl's template Accept
161 }
162 60 ▶ delete @out{@connection} if @connection;
Branch: 1 branch
BranchTrueFalseTotal
if @connection 3 57 60
163 60 ▶ $out{'accept-encoding'} = 'identity' if exists $out{range};
Branch: 1 branch
BranchTrueFalseTotal
if exists $out{"range"} 1 59 60
164 60 return \%out;
165 }
166  
167 # Serialise a response status line + headers + terminating blank line. A header
168 # whose value is an arrayref (a repeated header, e.g. Set-Cookie) emits one line
169 # per value. Control chars in names/values are stripped (response-splitting guard).
170 40 ▶sub response_head {
Subroutine response_head: called CC=7 SCAR=19.5
Pod undocumented
171 40 my ($status, $headers) = @_;
172 40 ▶ my $reason = $REASON{$status} // 'Status';
Condition100%2/2
$REASON{$status} // "Status"
l!l
391
MC/DC100%1/1
$REASON{$status} // "Status"
$REASON{$status}
Truth table100%2/2
A
A= $REASON{$status}
Aresult
11
00
173 40 my $out = "HTTP/1.1 $status $reason\r\n";
174 40 for my $k (sort keys %$headers) {
175 66 my $v = $headers->{$k};
176 66 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 66 66
177 66 (my $ck = $k) =~ tr/\r\n\0//d;
178 66 ▶ for my $val (ref $v eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 2 64 66
179 68 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 68 68
180 68 (my $cv = $val) =~ tr/\r\n\0//d;
181 68 $out .= "$ck: $cv\r\n";
182 }
183 }
184 40 $out .= "\r\n";
185 40 return $out;
186 }
187  
188 6 1;
189  
190 __END__
« Impersonate/Connection.pm ↑ Summary Impersonate/Cert.pm »