blib/lib/HTTP/Cookies/Chrome.pm

filter: Statementstmt 89.1% 239 / 268 Branchbran 47.7% 42 / 88 Conditioncond 44.2% 27 / 61 MC/DCmcdc 12.9% 4 / 31 Subroutinesub 91.6% 44 / 48 Podpod 100% 6 / 6 totaltotal 72.1% 362 / 502 CC 72 SCAR 21.6
CC
72
Cov
74%
guess_path
30.0
set_cookie
18.7
_insert
16.3
CRAP 8.6
↑ Summary
1 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package HTTP::Cookies::Chrome;
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
13 5 my @names = qw( _VERSION KEY VALUE PATH DOMAIN PORT PATH_SPEC
14 SECURE EXPIRES DISCARD REST );
15 5 my $n = 0;
16 5 foreach my $name ( @names ) {
17 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 55 my $m = $n++;
19 0 ▶ *{$name} = sub () { $m }
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 55 }
21 }
22  
23 =encoding utf8
24  
25 =head1 NAME
26  
27 HTTP::Cookies::Chrome - Cookie storage and management for Google Chrome
28  
29 =head1 SYNOPSIS
30  
31 use HTTP::Cookies::Chrome;
32  
33 my $password = HTTP::Cookies::Chrome->get_from_gnome;
34  
35 my $cookie_jar = HTTP::Cookies::Chrome->new(
36 chrome_safe_storage_password => $password,
37 file => ...,
38 autosave => ...,
39 );
40 $cookie_jar->load( $path_to_cookies );
41  
42 # otherwise same as HTTP::Cookies
43  
44 =head1 DESCRIPTION
45  
46 This package overrides the C<load()> and C<save()> methods of
47 C<HTTP::Cookies> so it can work with Google Chrome cookie files,
48 which are SQLite databases. This also should work from Chrome clones,
49 such as Brave.
50  
51 First, you are allowed to create different profiles within Chrome, and
52 each profile has its own set of files. The default profile is just C<Default>.
53 Along with that, there are various clones with their own product names.
54 The expected paths incorporate the product and profiles:
55  
56 Starting with Chrome 80, cookie values may be (likely are) encrypted
57 with a password that Chrome changes and stores somewhere. Additionally,
58 each cookie record tracks several other fields. If you are
59 using an earlier Chrome, you should use an older version of this module
60 (the 1.x series).
61  
62 =over 4
63  
64 =item macOS - ~/Library/Application Support/PRODUCT/Chrome/PROFILE/Cookies
65  
66 =item Linux - ~/.config/PRODUCT/PROFILE/Cookies
67  
68 =item Windows - C:\Users\USER\AppData\Local\PRODUCT\User Data\$profile\Cookies
69  
70 =back
71  
72 =head2 Notes on modernization
73  
74 HTTP::Cookies::Chrome v3 works with version 24 of the Chrome cookies
75 database. If you need something earlier, use an older version of this
76 module.
77  
78 The Chrome cookie database version 24 changed how it stored encrypted
79 cookies. It takes the SHA256 of the domain and prepends the binary value
80 to the plaintext of the cookie value. It then encrypts the entire thing,
81 and puts C<v10> in front of it as before.
82  
83 So far, this module has been updated to read that format and discard the
84 SHA256 rather than verifying its value if correct, or at least passing
85 that on so the user can verify it themselves. This module should do
86 either or both of those.
87  
88 Also, this module has not yet been updated to write it out in the same
89 way.
90  
91 =cut
92  
93 5 ▶use base qw( HTTP::Cookies );
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 5 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 5 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 5 $VERSION = '4.001';
100  
101 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 8 ▶sub _add_value {
Subroutine _add_value: called CC=1 SCAR=0.0
104 8 my( $self, $key, $value ) = @_;
105 8 $self->_stash->{$key} = $value;
106 }
107  
108 253 ▶sub _cipher { $_[0]->_get_value( 'cipher' ) }
Subroutine _cipher: called CC=1 SCAR=0.0
109  
110 5 ▶sub _connect {
Subroutine _connect: called CC=1 SCAR=0.0
111 5 my( $self, $file ) = @_;
112 5 my $dbh = DBI->connect( "dbi:SQLite:dbname=$file", '', '',
113 {
114 sqlite_see_if_its_a_number => 1,
115 } );
116 5 $_[0]->{dbh} = $dbh;
117 }
118  
119 1 ▶sub _create_table {
Subroutine _create_table: called CC=1 SCAR=0.0
120 1 my( $self ) = @_;
121  
122 1 $self->_dbh->do( 'DROP TABLE IF EXISTS cookies' );
123 1 $self->_dbh->do( 'DROP TABLE IF EXISTS meta' );
124  
125 1 $self->_dbh->do( q(CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)) );
126 1 $self->_dbh->do( q(INSERT INTO meta VALUES('mmap_status','-1')) );
127 1 $self->_dbh->do( q(INSERT INTO meta VALUES('version','24')) );
128 1 $self->_dbh->do( q(INSERT INTO meta VALUES('last_compatible_version','24q')) );
129  
130 1 $self->_dbh->do( <<'SQL' );
131 CREATE TABLE cookies (
132 creation_utc INTEGER NOT NULL,
133 host_key TEXT NOT NULL,
134 top_frame_site_key TEXT NOT NULL,
135 name TEXT NOT NULL,
136 value TEXT NOT NULL,
137 encrypted_value BLOB NOT NULL,
138 path TEXT NOT NULL,
139 expires_utc INTEGER NOT NULL,
140 is_secure INTEGER NOT NULL,
141 is_httponly INTEGER NOT NULL,
142 last_access_utc INTEGER NOT NULL,
143 has_expires INTEGER NOT NULL,
144 is_persistent INTEGER NOT NULL,
145 priority INTEGER NOT NULL,
146 samesite INTEGER NOT NULL,
147 source_scheme INTEGER NOT NULL,
148 source_port INTEGER NOT NULL,
149 last_update_utc INTEGER NOT NULL,
150 source_type INTEGER NOT NULL,
151 has_cross_site_ancestor INTEGER NOT NULL
152 );
153 SQL
154  
155 1 $self->_dbh->do( <<'SQL' );
156 CREATE UNIQUE INDEX cookies_unique_index ON cookies(
157 host_key,
158 top_frame_site_key,
159 has_cross_site_ancestor,
160 name,
161 path,
162 source_scheme,
163 source_port
164 );
165 SQL
166 }
167  
168 15 ▶sub _dbh { $_[0]->{dbh} }
Subroutine _dbh: called CC=1 SCAR=0.0
169  
170 92 ▶sub _db_24_or_later {
Subroutine _db_24_or_later: called CC=3 SCAR=11.0
171 92 my( $self ) = shift;
172  
173 92 state $yes;
174 92 ▶ return $yes if defined $yes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $yes 89 3 92
175  
176 3 my $v = $self->_db_version;
177 3 ▶ $yes = defined $v and $v >= 24;
Branch: 1 branch
BranchTrueFalseTotal
$yes = defined $v and $v >= 24 3 0 3
178 }
179  
180 3 ▶sub _db_version {
Subroutine _db_version: called CC=7 SCAR=20.7
181 3 my( $self ) = @_;
182  
183 3 state $has_meta = eval {
184 3 my $sql = q(SELECT 1 FROM sqlite_master WHERE type='table' AND name='meta');
185 3 my $array = $self->_dbh->selectall_arrayref( $sql );
186 3 @$array > 0;
187 };
188 0 ▶ if( $@ ) { warn $@ }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
189  
190 3 state $version;
191 3 ▶ return $version if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 3 3
192 3 ▶ return unless $has_meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_meta 0 3 3
193  
194 3 ▶ $version = eval {
Condition50%1/2
eval { do { my $key = "version"; my $sql = "SELECT * FROM meta WHERE key = '${key}'"; my $rv = $self->_dbh->selectall_arrayref($sql); @$rv ? $rv->[0][1] : 0 } } // 0
l!l
30
MC/DC0%0/1
eval { do { my $key = "version"; my $sql = "SELECT * FROM meta WHERE key = '${key}'"; my $rv = $self->_dbh->selectall_arrayref($sql); @$rv ? $rv->[0][1] : 0 } } // 0
eval { do { my $key = "version"; my $sql = "SELECT * FROM meta WHERE key = '${key}'"; my $rv = $self->_dbh->selectall_arrayref($sql); @$rv ? $rv->[0][1] : 0 } }
Truth table50%1/2
A
A= eval { do { my $key = "version"; my $sql = "SELECT * FROM meta WHERE key = '${key}'"; my $rv = $self->_dbh->selectall_arrayref($sql); @$rv ? $rv->[0][1] : 0 } }
Aresult
11
00
195 3 my $key = 'version';
196 3 my $sql = qq(SELECT * FROM meta WHERE key = '$key');
197 3 my $rv = $self->_dbh->selectall_arrayref( $sql );
198 3 ▶ @$rv ? $rv->[0][1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$rv ? : 3 0 3
199 } // 0;
200 0 ▶ if( $@ ) { warn $@ }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
201  
202 3 return $version;
203 };
204  
205 92 ▶sub _decrypt {
Subroutine _decrypt: called CC=10 SCAR=26.0
206 92 my( $self, $blob ) = @_;
207  
208 92 ▶ unless( $self->_cipher ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cipher) 0 92 92
209 0 ▶ warnings::warn("Decrypted cookies is not set up") if warnings::enabled();
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
210 0 return;
211 }
212  
213 92 my $type = substr $blob, 0, 3, '';
214  
215 92 ▶ unless( $type eq 'v10' ) { # v11 is a thing, too
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "v10") 0 92 92
216 0 ▶ warnings::warn("Encrypted value is unexpected type <$type>") if warnings::enabled();
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
217 0 return;
218 }
219  
220 92 my $plaintext = $self->_cipher->decrypt( $blob );
221 92 my $sha256;
222  
223 92 ▶ if( $self->_db_24_or_later ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_db_24_or_later) 92 0 92
224 92 $sha256 = substr $plaintext, 0, 32, '';
225 }
226  
227 # padding is always added to get to a multiple of 16. If the value is already
228 # a multiple of 16, it's padded by an additional 16 octets.
229 92 my $padding_count = ord( substr $plaintext, -1 );
230  
231 92 my $padding = substr( $plaintext, -$padding_count );
232  
233 92 ▶ unless( $padding =~ /\A(.){$padding_count}\z/ and ord(substr $padding, 0, 1) == $padding_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($padding =~ /\A(.){$padding_count}\z/ and ord substr($padding, 0, 1) == $padding_count) 69 23 92
Condition100%3/3
$padding =~ /\A(.){$padding_count}\z/ and ord substr($padding, 0, 1) == $padding_count
!ll&&!rl&&r
482123
MC/DC100%2/2
$padding =~ /\A(.){$padding_count}\z/ and ord substr($padding, 0, 1) == $padding_count
$padding =~ /\A(.){$padding_count}\z/ ord substr($padding, 0, 1) == $padding_count
Truth table100%3/3
A and B
A= $padding =~ /\A(.){$padding_count}\z/B= ord substr($padding, 0, 1) == $padding_count
ABresult
0X0
100
111
234 69 ▶ warnings::warn("Unexpected padding in encrypted cookie") if warnings::enabled();
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 69 69
235 }
236  
237 92 ▶ substr( $plaintext, -$padding_count ) = '' if $padding_count <= 16;
Branch: 1 branch
BranchTrueFalseTotal
if $padding_count <= 16 23 69 92
238  
239 92 ( $plaintext, $sha256 );
240 }
241  
242 23 ▶sub _encrypt {
Subroutine _encrypt: called CC=6 SCAR=23.0
243 23 my( $self, $value ) = @_;
244  
245 23 ▶ unless( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 23 23
246 0 ▶ warnings::warn("Value is not defined! Nothing to encrypt!") if warnings::enabled();
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
247 0 return;
248 }
249  
250 23 ▶ unless( $self->_cipher ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cipher) 0 23 23
251 0 ▶ warnings::warn("Encrypted cookies is not set up") if warnings::enabled();
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
252 0 return;
253 }
254  
255 23 my $blocksize = 16;
256  
257 # padding is always added. If the length is already a multiple of 16, the
258 # padding is the same as the blocksize.
259 23 my $padding_length = ($blocksize - length($value) % $blocksize);
260 23 ▶ $padding_length = $blocksize if $padding_length == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $padding_length == 0 0 23 23
261 23 my $padding = chr($padding_length) x $padding_length;
262 23 my $encrypted = 'v10' . $self->_cipher->encrypt( $value . $padding );
263  
264 23 $encrypted;
265 }
266  
267 1 ▶sub _filter_cookies {
Subroutine _filter_cookies: called CC=1 SCAR=0.2
268 1 my( $self ) = @_;
269  
270 $self->scan(
271 23 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=20.8
272 23 my( $version, $key, $val, $path, $domain, $port,
273 $path_spec, $secure, $expires, $discard, $rest ) = @_;
274  
275 23 my @parts = @_;
276  
277 23 ▶ return if $parts[DISCARD] && not $self->{ignore_discard};
Branch: 1 branch
BranchTrueFalseTotal
if $parts[9] and not $self->{"ignore_discard"} 0 23 23
Condition33%1/3
$parts[9] and not $self->{"ignore_discard"}
!ll&&!rl&&r
2300
MC/DC0%0/2
$parts[9] and not $self->{"ignore_discard"}
$parts[9] not $self->{"ignore_discard"}
Truth table33%1/3
A and not(B)
A= $parts[9]B= not $self->{"ignore_discard"}
ABresult
0X0
100
111
278 23 ▶ return if defined $parts[EXPIRES] && time > $parts[EXPIRES];
Branch: 1 branch
BranchTrueFalseTotal
if defined $parts[8] and time > $parts[8] 0 23 23
Condition33%1/3
defined $parts[8] and time > $parts[8]
!ll&&!rl&&r
0230
MC/DC0%0/2
defined $parts[8] and time > $parts[8]
defined $parts[8] time > $parts[8]
Truth table33%1/3
A and B
A= defined $parts[8]B= time > $parts[8]
ABresult
0X0
100
111
279  
280 23 $parts[EXPIRES] = $rest->{expires_utc};
281 23 ▶ $parts[SECURE] = $parts[SECURE] ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$parts[7] ? : 15 8 23
282  
283 23 ▶ my $bool = $domain =~ /^\./ ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$domain =~ /^\./ ? : 17 6 23
284  
285 23 $self->_insert( @parts );
286 }
287 1 );
288 }
289  
290 4 ▶sub _get_rows {
Subroutine _get_rows: called CC=2 SCAR=6.9
291 4 my( $self, $file ) = @_;
292  
293 4 my $dbh = $self->_connect( $file );
294  
295 4 my $sth = $dbh->prepare( <<'SQL' );
296 SELECT
297 creation_utc,
298 host_key,
299 name,
300 value,
301 path,
302 expires_utc,
303 is_secure,
304 is_httponly,
305 last_access_utc,
306 has_expires,
307 is_persistent,
308 priority,
309 encrypted_value,
310 samesite,
311 source_scheme,
312 source_port
313 FROM
314 cookies
315 SQL
316  
317 4 $sth->execute;
318  
319 my @rows =
320 map {
321 92 ▶ if( my $e = $_->encrypted_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $_->encrypted_value) 92 0 92
322 92 my( $p, $sha256 ) = $self->_decrypt( $e );
323 92 $_->decrypted_value( $p );
324 }
325 92 $_;
326 }
327 92 map { HTTP::Cookies::Chrome::Record->new( $_ ) }
328 4 @{ $sth->fetchall_arrayref };
329  
330 4 $dbh->disconnect;
331  
332 4 \@rows;
333 }
334  
335 253 ▶sub _get_value {
Subroutine _get_value: called CC=1 SCAR=0.0
336 253 my( $self, $key ) = @_;
337 253 $self->_stash->{$key}
338 }
339  
340 {
341 5 my $creation_offset = 0;
342  
343 23 ▶sub _insert {
Subroutine _insert: called CC=12 SCAR=27.9
344 23 my( $self, @parts ) = @_;
345  
346 23 my $rest = $parts[REST];
347  
348 23 ▶ $rest->{httponly} //= 0;
Condition50%1/2
$rest->{"httponly"} //= 0
l!l
023
MC/DC0%0/1
$rest->{"httponly"} //= 0
$rest->{"httponly"}
Truth table50%1/2
A
A= $rest->{"httponly"}
Aresult
11
00
349 23 ▶ $rest->{last_update_utc} //= time() * 1000;
Condition50%1/2
$rest->{"last_update_utc"} //= time * 1000
l!l
023
MC/DC0%0/1
$rest->{"last_update_utc"} //= time * 1000
$rest->{"last_update_utc"}
Truth table50%1/2
A
A= $rest->{"last_update_utc"}
Aresult
11
00
350 23 ▶ $rest->{samesite} //= -1; # magic value for unspecified
Condition50%1/2
$rest->{"samesite"} //= -1
l!l
230
MC/DC0%0/1
$rest->{"samesite"} //= -1
$rest->{"samesite"}
Truth table50%1/2
A
A= $rest->{"samesite"}
Aresult
11
00
351 23 ▶ $rest->{source_scheme} //= '';
Condition50%1/2
$rest->{"source_scheme"} //= ""
l!l
230
MC/DC0%0/1
$rest->{"source_scheme"} //= ""
$rest->{"source_scheme"}
Truth table50%1/2
A
A= $rest->{"source_scheme"}
Aresult
11
00
352 23 ▶ $rest->{source_type} //= '';
Condition50%1/2
$rest->{"source_type"} //= ""
l!l
023
MC/DC0%0/1
$rest->{"source_type"} //= ""
$rest->{"source_type"}
Truth table50%1/2
A
A= $rest->{"source_type"}
Aresult
11
00
353 23 ▶ $rest->{top_frame_site_key} //= '';
Condition50%1/2
$rest->{"top_frame_site_key"} //= ""
l!l
023
MC/DC0%0/1
$rest->{"top_frame_site_key"} //= ""
$rest->{"top_frame_site_key"}
Truth table50%1/2
A
A= $rest->{"top_frame_site_key"}
Aresult
11
00
354 23 ▶ $rest->{has_cross_site_ancestor} //= 0;
Condition50%1/2
$rest->{"has_cross_site_ancestor"} //= 0
l!l
023
MC/DC0%0/1
$rest->{"has_cross_site_ancestor"} //= 0
$rest->{"has_cross_site_ancestor"}
Truth table50%1/2
A
A= $rest->{"has_cross_site_ancestor"}
Aresult
11
00
355  
356 # possibly thinking about a feature to remove the encryption,
357 # so we'd need to re-encrypt things. Here we assume that already
358 # exists so we always re-encrypt.
359 23 my $encrypted_value = '';
360  
361 # If we have a value and there was a previous encrypted value,
362 # encrypted the current value and blank out the value. Other
363 23 ▶ if( $parts[VALUE] and $rest->{encrypted_value} and $self->_cipher ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] and $rest->{"encrypted_value"} and $self->_cipher) 23 0 23
Condition33%2/6
$parts[2] and $rest->{"encrypted_value"} and $self->_cipher
!ll&&!rl&&r
0023
$parts[2] and $rest->{"encrypted_value"}
!ll&&!rl&&r
0023
MC/DC0%0/3
$parts[2] and $rest->{"encrypted_value"} and $self->_cipher
$parts[2] $rest->{"encrypted_value"} $self->_cipher
Truth table25%1/4
A and B and C
A= $parts[2]B= $rest->{"encrypted_value"}C= $self->_cipher
ABCresult
0XX0
10X0
1100
1111
364 23 $encrypted_value = $self->_encrypt( $parts[VALUE] );
365 23 $parts[VALUE] = '';
366 }
367  
368 # Some cookies don't have values. WTF?
369 23 ▶ $parts[VALUE] //= '';
Condition50%1/2
$parts[2] //= ""
l!l
230
MC/DC0%0/1
$parts[2] //= ""
$parts[2]
Truth table50%1/2
A
A= $parts[2]
Aresult
11
00
370  
371 my @values = (
372 $rest->{creation_utc},
373 $parts[DOMAIN],
374 $rest->{top_frame_site_key},
375 @parts[KEY, VALUE],
376 $encrypted_value,
377 $parts[PATH],
378 $rest->{expires_utc},
379 $parts[SECURE],
380 23 @{ $rest }{ qw(is_httponly last_access_utc has_expires is_persistent priority samesite source_scheme) },
381 $parts[PORT],
382 23 @{$rest}{qw(last_update_utc source_type has_cross_site_ancestor)},
383 );
384  
385 23 $self->{insert_sth}->execute( @values );
386 }
387 }
388  
389 5 ▶sub _get_utc_microseconds {
Subroutine _get_utc_microseconds: not called CC=2 SCAR=7.5
390 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
391 5 ▶ use bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
392 0 ▶ POSIX::strftime( '%s', gmtime() ) * 1_000_000 + ($_[1]//0);
Condition0%0/2
$_[1] // bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
l!l
00
MC/DC0%0/1
$_[1] // bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
393 }
394  
395 4 ▶sub _make_cipher {
Subroutine _make_cipher: called CC=1 SCAR=0.0
396 4 my( $self, $password ) = @_;
397  
398 4 my $key = do {
399 4 state $rc2 = require PBKDF2::Tiny;
400 4 my $s = $self->_platform_settings;
401 4 my $salt = 'saltysalt';
402 4 my $length = 16;
403 4 PBKDF2::Tiny::derive( 'SHA-1', $password, $salt, $s->{iterations}, $length );
404 };
405  
406 4 state $rc1 = require Crypt::Rijndael;
407 4 my $cipher = Crypt::Rijndael->new( $key, Crypt::Rijndael::MODE_CBC() );
408 4 $cipher->set_iv( ' ' x 16 );
409  
410 4 $self->_add_value( chrome_safe_storage_password => $password );
411 4 $self->_add_value( cipher => $cipher );
412 }
413  
414 4 ▶sub _platform_settings {
Subroutine _platform_settings: called CC=1 SCAR=0.0
415 # https://n8henrie.com/2014/05/decrypt-chrome-cookies-with-python/
416 # https://github.com/n8henrie/pycookiecheat/issues/12
417 4 state $settings = {
418 darwin => {
419 iterations => 1003,
420 },
421 linux => {
422 iterations => 1,
423 },
424 MSWin32 => {
425 },
426 };
427  
428 4 $settings->{$^O};
429 }
430  
431 1 ▶sub _prepare_insert {
Subroutine _prepare_insert: called CC=1 SCAR=0.0
432 1 my( $self ) = @_;
433 1 state $columns = [qw(
434 creation_utc
435 host_key
436 top_frame_site_key
437 name
438 value
439 encrypted_value
440 path
441 expires_utc
442 is_secure
443 is_httponly
444 last_access_utc
445 has_expires
446 is_persistent
447 priority
448 samesite
449 source_scheme
450 source_port
451 last_update_utc
452 source_type
453 has_cross_site_ancestor
454 )];
455 1 state $columns_str = join ', ', @$columns;
456 1 state $placeholders = join ', ', ('?') x @$columns;
457 1 my $sth = $self->{insert_sth} = $self->_dbh->prepare_cached( <<"SQL" );
458 INSERT INTO cookies ($columns_str) VALUES ( $placeholders )
459 SQL
460  
461 }
462  
463 261 ▶sub _stash {
Subroutine _stash: called CC=2 SCAR=6.9
464 261 state $mod_key = 'X-CHROME';
465 261 ▶ $_[0]->{$mod_key} //= {};
Condition100%2/2
$_[0]{$mod_key} //= {}
l!l
2574
MC/DC100%1/1
$_[0]{$mod_key} //= {}
$_[0]{$mod_key}
Truth table100%2/2
A
A= $_[0]{$mod_key}
Aresult
11
00
466 }
467  
468 =head2 Class methods
469  
470 =over 4
471  
472 =item * guess_password
473  
474 Try to retrieve the Chrome Safe Storage password by accessing the
475 system secrets for the logged-in user. This returns nothing if it
476 can't find it.
477  
478 You don't need to use this to get the password.
479  
480 On macOS, this looks in the Keyring using C<security>.
481  
482 On Linux, this uses C<secret-tool>, which you might have to install
483 separately. Also, some early versions used the hard-coded password
484 C<peanut>, and some others may have used C<mock_password>.
485  
486 I don't know how to do this on Windows. If you know, send a pull request.
487 That goes for other systems too.
488  
489 =cut
490  
491 0 ▶sub guess_password {
Subroutine guess_password: not called CC=3 SCAR=24.8
Pod documented
492 0 my $p = do {
493 0 ▶ if( $^O eq 'darwin' ) { `security find-generic-password -a "Chrome" -w` }
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'darwin') { } 0 0 0
elsif ($^O eq 'linux') { } 0 0 0
494 0 elsif( $^O eq 'linux' ) { `secret-tool lookup xdg:schema chrome_libsecret_os_crypt_password application chrome` }
495 };
496 0 chomp $p;
497 0 $p
498 }
499  
500 =item * guess_path( PROFILE )
501  
502 Try to retrieve the directory that contains the Cookies file. If you
503 don't specify C<PROFILE>, it uses C<Default>.
504  
505 macOS: F<~/Library/Application Support/Google/Chrome/PROFILE/Cookies>
506  
507 Linux: F<~/.config/google-chrome/PROFILE/Cookies>
508  
509 =cut
510  
511 0 ▶sub guess_path {
Subroutine guess_path: not called CC=5 SCAR=34.0
Pod documented
512 0 my( $self, $profile ) = @_;
513 0 ▶ $profile //= 'Default';
Condition0%0/2
$profile //= "Default"
l!l
00
MC/DC0%0/1
$profile //= "Default"
$profile
Truth table0%0/2
A
A= $profile
Aresult
11
00
514  
515 0 my $path_to_cookies = do {
516 0 ▶ if( $^O eq 'darwin' ) { "$ENV{HOME}/Library/Application Support/Google/Chrome/$profile/Cookies" }
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'darwin') { } 0 0 0
elsif ($^O eq 'linux') { } 0 0 0
517 0 elsif( $^O eq 'linux' ) { "$ENV{HOME}/.config/google-chrome/$profile/Cookies" }
518 };
519  
520 0 ▶ return unless -e $path_to_cookies;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path_to_cookies 0 0 0
521 0 $path_to_cookies
522 }
523  
524 =item * new
525  
526 The extends the C<new> in L<HTTP::Cookies>, with the additional parameter
527 for the decryption password.
528  
529 chrome_safe_storage_password - the password
530  
531 =cut
532  
533 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
534 4 my( $class, %args ) = @_;
535  
536 4 my $pass = delete $args{chrome_safe_storage_password};
537 4 my $file = delete $args{file};
538  
539 4 my $self = $class->SUPER::new( %args );
540  
541 4 ▶ return $self unless defined $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass 0 4 4
542  
543 4 $self->_make_cipher( $pass );
544  
545 4 ▶ if( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 3 1 4
546 3 $self->{file} = $file;
547 3 $self->load;
548 }
549  
550 4 return $self;
551 }
552  
553 =item * load
554  
555 This overrides the C<load> from L<HTTP::Cookies>. There are a few
556 differences that matter.
557  
558 The Cookies database for Chrome tracks many more things than L<HTTP::Cookies>
559 knows about, so this shoves everything into the "rest" hash. Notably:
560  
561 =over 4
562  
563 =item * Chrome sets the port to -1 if the cookie does not specify the port.
564  
565 =item * The value of the cookie is either the plaintext value or the decrypted value from C<encrypted_value>.
566  
567 =item * If C<ignore_discard> is set, this ignores the C<$maxage> part of L<HTTP::Cookies>, but remembers the value in C<expires_utc>.
568  
569 =back
570  
571 =cut
572  
573 7 ▶sub load {
Subroutine load: called CC=7 SCAR=20.1
Pod documented
574 7 my( $self, $file ) = @_;
575  
576 7 ▶ $file ||= $self->{'file'} || return;
Condition80%4/5
$file ||= $self->{"file"} || (return)
l!l&&r!l&&!r
061
$self->{'file'} || (return)
l!l
34
MC/DC50%1/2
$file ||= $self->{"file"} || (return)
$file $self->{'file'}
Truth table66%2/3
A ||= B
A= $fileB= $self->{'file'}
ABresult
1X1
011
000
577  
578 # $cookie_jar->set_cookie( $version, $key, $val, $path,
579 # $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )
580  
581 3 my $rows = $self->_get_rows( $file );
582  
583 3 foreach my $row ( @$rows ) {
584 69 ▶ my $value = length $row->value ? $row->value : $row->decrypted_value;
Branch: 1 branch
BranchTrueFalseTotal
length $row->value ? : 0 69 69
585  
586 # if $max_page is not defined, HTTP::Cookies will not remove
587 # the cookies. We still track the actual value in the the
588 # hash and we can put the original back in place.
589 69 my $max_age = do {
590 0 ▶ if( $self->{ignore_discard} ) { undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ignore_discard'}) { } 0 69 69
591 69 else { ($row->expires_utc / 1_000_000) - time }
592 };
593  
594 # I've noticed that Chrome sets most ports to -1
595 69 ▶ my $port = $row->source_port > 0 ? $row->source_port : 80;
Branch: 1 branch
BranchTrueFalseTotal
$row->source_port > 0 ? : 69 0 69
596  
597 my $rc = $self->set_cookie(
598 undef, # version
599 $row->name, # key
600 $value, # value
601 $row->path, # path
602 $row->host_key, # domain
603 $row->source_port, # port
604 undef, # path spec
605 $row->is_secure, # secure
606 $max_age, # max_age
607 0, # discard
608 {
609 69 map { $_ => $row->$_() } qw(
610 value
611 creation_utc
612 is_httponly
613 last_access_utc
614 expires_utc
615 has_expires
616 is_persistent
617 priority
618 encrypted_value
619 samesite
620 source_scheme
621 is_same_party
622 source_port
623 )
624 }
625 );
626  
627 }
628  
629 3 1;
630 }
631  
632 =back
633  
634 =head2 Instance Methods
635  
636 =over 4
637  
638 =item * save( [ FILE ] )
639  
640 With no argument, save the cookies to the original filename. With
641 a file name argument, write the cookies to that filename. This will
642 be a SQLite database.
643  
644 =cut
645  
646 1 ▶sub save {
Subroutine save: called CC=3 SCAR=11.8
Pod documented
647 1 my( $self, $new_file ) = @_;
648  
649 1 ▶ $new_file ||= $self->{'file'} || return;
Condition20%1/5
$new_file ||= $self->{"file"} || (return)
l!l&&r!l&&!r
100
$self->{'file'} || (return)
l!l
00
MC/DC0%0/2
$new_file ||= $self->{"file"} || (return)
$new_file $self->{'file'}
Truth table33%1/3
A ||= B
A= $new_fileB= $self->{'file'}
ABresult
1X1
011
000
650  
651 1 my $dbh = $self->_connect( $new_file );
652  
653 1 $self->_create_table;
654 1 $self->_prepare_insert;
655 1 $self->_filter_cookies;
656 1 $dbh->disconnect;
657  
658 1 1;
659 }
660  
661 =item * set_cookie
662  
663 Overrides the C<set_cookie> in L<HTTP::Cookies> so it can ignore
664 the port check. Chrome uses C<-1> as the port if the cookie did not
665 specify a port. This version of C<set_cookie> does no port check.
666  
667 =cut
668  
669 # We have to override this part because Chrome has -1 as a valid
670 # port value (for "unspecified port"). Otherwise this is lifted from
671 # HTTP::Cookies
672 69 ▶sub set_cookie {
Subroutine set_cookie: called CC=11 SCAR=29.3
Pod documented
673 69 my $self = shift;
674 69 my($version,
675 $key, $val, $path, $domain, $port,
676 $path_spec, $secure, $maxage, $discard, $rest) = @_;
677  
678 # path and key can not be empty (key can't start with '$')
679 69 ▶ return $self if !defined($path) || $path !~ m,^/, ||
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path && $path =~ m[^/] && defined $key or $key =~ /^\$/ 0 69 69
Condition33%3/9
not defined $path && $path =~ m[^/] && defined $key or $key =~ /^\$/
l!l&&r!l&&!r
0069
defined $path && $path =~ m[^/] && defined $key
!ll&&!rl&&r
0069
defined $path and $path =~ m[^/]
!ll&&!rl&&r
0069
MC/DC0%0/4
not defined $path && $path =~ m[^/] && defined $key or $key =~ /^\$/
defined $path $path =~ m[^/] defined $key $key =~ /^\$/
Truth table20%1/5
not(A and B && C) or D
A= defined $pathB= $path =~ m[^/]C= defined $keyD= $key =~ /^\$/
ABCDresult
0XXX1
10XX1
110X1
11111
11100
680 !defined($key) || $key =~ m,^\$,;
681  
682 # ensure legal port
683 69 if (0 && defined $port) { # nerf this part
684 return $self unless $port =~ /^_?\d+(?:,\d+)*$/;
685 }
686  
687 69 my $expires;
688 69 ▶ if (defined $maxage) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxage) 69 0 69
689 69 ▶ if ($maxage <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxage <= 0) 0 69 69
690 0 delete $self->{COOKIES}{$domain}{$path}{$key};
691 0 return $self;
692 }
693 69 $expires = time() + $maxage;
694 }
695 69 ▶ $version = 0 unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 69 0 69
696  
697 69 my @array = ($version, $val,$port,
698 $path_spec,
699 $secure, $expires, $discard);
700 69 ▶ push(@array, {%$rest}) if defined($rest) && %$rest;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rest and %$rest 69 0 69
Condition33%1/3
defined $rest and %$rest
!ll&&!rl&&r
0069
MC/DC0%0/2
defined $rest and %$rest
defined $rest %$rest
Truth table33%1/3
A and B
A= defined $restB= %$rest
ABresult
0X0
100
111
701 # trim off undefined values at end
702 69 ▶ pop(@array) while !defined $array[-1];
Branch: 1 branch
BranchTrueFalseTotal
defined $array[-1] or do { pop @array } 69 0 69
703  
704 69 $self->{COOKIES}{$domain}{$path}{$key} = \@array;
705 69 $self;
706 }
707  
708 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
709 package HTTP::Cookies::Chrome::Record;
710 5 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
711  
712 5 my %columns = map { state $n = 0; $_, $n++ } qw(
713 creation_utc
714 host_key
715 name
716 value
717 path
718 expires_utc
719 is_secure
720 is_httponly
721 last_access_utc
722 has_expires
723 is_persistent
724 priority
725 encrypted_value
726 samesite
727 source_scheme
728 source_port
729 is_same_party
730 decrypted_value
731 );
732  
733 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
734 92 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
735 92 my( $class, $array ) = @_;
736 92 bless $array, $class;
737 }
738  
739 161 ▶sub decrypted_value {
Subroutine decrypted_value: called CC=2 SCAR=6.9
740 161 my( $self, $value ) = @_;
741  
742 161 ▶ return $self->[ $columns{decrypted_value} ] unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 69 92 161
743 92 $self->[ $columns{decrypted_value} ] = $value;
744 }
745  
746 1610 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
747 1610 my( $self ) = @_;
748 1610 my $method = $AUTOLOAD;
749 1610 $method =~ s/.*:://;
750  
751 1610 ▶ die "No method <$method>" unless exists $columns{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $columns{$method} 0 1610 1610
752  
753 1610 $self->[ $columns{$method} ];
754 }
755  
756 92 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
757 }
758  
759 =back
760  
761 =head2 Getting the Chrome Safe Storage password
762  
763 You can get the Chrome Safe Storage password, although you may have to
764 respond to other dialogs and features of its storage mechanism:
765  
766 On macOS:
767  
768 % security find-generic-password -a "Chrome" -w
769 % security find-generic-password -a "Brave" -w
770  
771 On Ubuntu using libsecret:
772  
773 % secret-tool lookup xdg:schema chrome_libsecret_os_crypt_password application chrome
774 % secret-tool lookup xdg:schema chrome_libsecret_os_crypt_password application brave
775  
776 If you know of other methods, let me know.
777  
778 Some useful information:
779  
780 =over 4
781  
782 =item * On Linux systems not using a keychain, the password might be C<peanut>
783 or C<mock_password>. Maybe I should use L<Passwd::Keyring::Gnome>
784  
785 =item * L<https://rtfm.co.ua/en/chromium-linux-keyrings-secret-service-passwords-encryption-and-store/>
786  
787 =item * L<https://stackoverflow.com/questions/57646301/decrypt-chrome-cookies-from-sqlite-db-on-mac-os>
788  
789 =item * L<https://superuser.com/a/969488/12972>
790  
791 =back
792  
793 =head2 The Chrome cookies table
794  
795 creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY
796 host_key TEXT NOT NULL
797 name TEXT NOT NULL
798 value TEXT NOT NULL
799 path TEXT NOT NULL
800 expires_utc INTEGER NOT NULL
801 is_secure INTEGER NOT NULL
802 is_httponly INTEGER NOT NULL
803 last_access_utc INTEGER NOT NULL
804 has_expires INTEGER NOT NULL
805 is_persistent INTEGER NOT NULL
806 priority INTEGER NOT NULL
807 encrypted_value BLOB
808 samesite INTEGER NOT NULL
809 source_scheme INTEGER NOT NULL
810 source_port INTEGER NOT NULL
811 is_same_party INTEGER NOT NULL
812  
813 =head1 TO DO
814  
815 There are many ways that this module can approve.
816  
817 1. The L<HTTP::Cookies> module was written a long time ago. We still
818 inherit from it, but it might be time to completely dump it even if
819 we keep the interface.
820  
821 2. Some Windows people can fill in the Windows details for C<guess_password>
822 and C<guess_path>.
823  
824 3. As in (2), systems that aren't Linux or macOS can fill in their details.
825  
826 4. We need a way to specify a new password to output the cookies to a
827 different Chrome-like SQLite database. The easiest thing right now might be
828 to make a completely new object with the new password and load cookies
829 into it.
830  
831 =head1 SOURCE AVAILABILITY
832  
833 This module is in Github:
834  
835 https://github.com/briandfoy/http-cookies-chrome
836  
837 =head1 AUTHOR
838  
839 brian d foy, C<< <bdfoy@cpan.org> >>
840  
841 =head1 CREDITS
842  
843 Jon Orwant pointed out the problem with dates too far in the future
844  
845 =head1 COPYRIGHT AND LICENSE
846  
847 Copyright © 2009-2026, brian d foy <bdfoy@cpan.org>. All rights reserved.
848  
849 This program is free software; you can redistribute it and/or modify
850 it under the terms of the Artistic License 2.0.
851  
852 =cut
853  
854 5 1;
↑ Summary